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.openhab.binding.volvooncall.internal.handler.VehicleHandler;
22 import org.openhab.binding.volvooncall.internal.handler.VehicleStateDescriptionProvider;
23 import org.openhab.binding.volvooncall.internal.handler.VolvoOnCallBridgeHandler;
24 import org.openhab.core.io.net.http.HttpClientFactory;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.gson.Gson;
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.JsonDeserializer;
45 * The {@link VolvoOnCallHandlerFactory} is responsible for creating things and thing
48 * @author Gaƫl L'hopital - Initial contribution
51 @Component(configurationPid = "binding.volvooncall", service = ThingHandlerFactory.class)
52 public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
54 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallHandlerFactory.class);
55 private final VehicleStateDescriptionProvider stateDescriptionProvider;
56 private final Gson gson;
57 private final HttpClientFactory httpClientFactory;
60 public VolvoOnCallHandlerFactory(@Reference VehicleStateDescriptionProvider provider,
61 @Reference HttpClientFactory httpClientFactory) {
62 this.stateDescriptionProvider = provider;
63 this.httpClientFactory = httpClientFactory;
64 this.gson = new GsonBuilder()
65 .registerTypeAdapter(ZonedDateTime.class,
66 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
67 .parse(json.getAsJsonPrimitive().getAsString().replaceAll("\\+0000", "Z")))
68 .registerTypeAdapter(OpenClosedType.class,
69 (JsonDeserializer<OpenClosedType>) (json, type,
70 jsonDeserializationContext) -> json.getAsBoolean() ? OpenClosedType.OPEN
71 : OpenClosedType.CLOSED)
72 .registerTypeAdapter(OnOffType.class,
73 (JsonDeserializer<OnOffType>) (json, type, jsonDeserializationContext) -> OnOffType
74 .from(json.getAsBoolean()))
75 .registerTypeAdapter(StringType.class, (JsonDeserializer<StringType>) (json, type,
76 jsonDeserializationContext) -> StringType.valueOf(json.getAsString()))
81 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
82 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
86 protected @Nullable ThingHandler createHandler(Thing thing) {
87 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88 if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
89 return new VolvoOnCallBridgeHandler((Bridge) thing, gson, httpClientFactory);
90 } else if (VEHICLE_THING_TYPE.equals(thingTypeUID)) {
91 return new VehicleHandler(thing, stateDescriptionProvider);
93 logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());