]> git.basschouten.com Git - openhab-addons.git/blob
4d79c6661cffe86045760a729336f415f41d1667
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.volvooncall.internal;
14
15 import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.*;
16
17 import java.time.ZonedDateTime;
18
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;
40
41 import com.google.gson.Gson;
42 import com.google.gson.GsonBuilder;
43 import com.google.gson.JsonDeserializer;
44
45 /**
46  * The {@link VolvoOnCallHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author GaĆ«l L'hopital - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(configurationPid = "binding.volvooncall", service = ThingHandlerFactory.class)
53 public class VolvoOnCallHandlerFactory extends BaseThingHandlerFactory {
54
55     private final Logger logger = LoggerFactory.getLogger(VolvoOnCallHandlerFactory.class);
56     private final VehicleStateDescriptionProvider stateDescriptionProvider;
57     private final Gson gson;
58     private final HttpClient httpClient;
59
60     @Activate
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()))
78                 .create();
79     }
80
81     @Override
82     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
83         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
84     }
85
86     @Override
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);
93         }
94         logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());
95         return null;
96     }
97 }