2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.volvooncall.internal;
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
17 import java.time.ZonedDateTime;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.volvooncall.internal.handler.VehicleHandler;
23 import org.openhab.binding.volvooncall.internal.handler.VehicleStateDescriptionProvider;
24 import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
25 import org.openhab.core.io.net.http.HttpClientFactory;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.gson.Gson;
42 import com.google.gson.GsonBuilder;
43 import com.google.gson.JsonDeserializer;
46 * The {@link VolvoOnCallHandlerFactory} is responsible for creating things and thing
49 * @author Gaƫl L'hopital - Initial contribution
52 @Component(configurationPid = "binding.volvooncall", service = ThingHandlerFactory.class)
53 public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
55 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallHandlerFactory.class);
56 private final VehicleStateDescriptionProvider stateDescriptionProvider;
57 private final Gson gson;
58 private final HttpClient httpClient;
61 public VolvoOnCallHandlerFactory(@Reference VehicleStateDescriptionProvider provider,
62 @Reference HttpClientFactory httpClientFactory) {
63 this.stateDescriptionProvider = provider;
64 this.httpClient = httpClientFactory.createHttpClient(BINDING_ID);
65 this.gson = new GsonBuilder()
66 .registerTypeAdapter(ZonedDateTime.class,
67 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
68 .parse(json.getAsJsonPrimitive().getAsString().replaceAll("\\+0000", "Z")))
69 .registerTypeAdapter(OpenClosedType.class,
70 (JsonDeserializer<OpenClosedType>) (json, type,
71 jsonDeserializationContext) -> json.getAsBoolean() ? OpenClosedType.OPEN
72 : OpenClosedType.CLOSED)
73 .registerTypeAdapter(OnOffType.class,
74 (JsonDeserializer<OnOffType>) (json, type,
75 jsonDeserializationContext) -> json.getAsBoolean() ? OnOffType.ON : OnOffType.OFF)
76 .registerTypeAdapter(StringType.class, (JsonDeserializer<StringType>) (json, type,
77 jsonDeserializationContext) -> StringType.valueOf(json.getAsString()))
82 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
87 protected @Nullable ThingHandler createHandler(Thing thing) {
88 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
89 if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
90 return new VolvoOnCallBridgeHandler((Bridge) thing, gson, httpClient);
91 } else if (VEHICLE_THING_TYPE.equals(thingTypeUID)) {
92 return new VehicleHandler(thing, stateDescriptionProvider);
94 logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());