]> git.basschouten.com Git - openhab-addons.git/blob
fe483a09ea1d266bdd6be0668aea67994259ea7c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openuv.internal;
14
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
16
17 import java.time.ZonedDateTime;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
22 import org.openhab.binding.openuv.internal.handler.OpenUVReportHandler;
23 import org.openhab.core.i18n.LocationProvider;
24 import org.openhab.core.i18n.TimeZoneProvider;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34
35 import com.google.gson.FieldNamingPolicy;
36 import com.google.gson.Gson;
37 import com.google.gson.GsonBuilder;
38 import com.google.gson.JsonDeserializer;
39
40 /**
41  * The {@link OpenUVHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author GaĆ«l L'hopital - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.openuv")
48 public class OpenUVHandlerFactory extends BaseThingHandlerFactory {
49
50     private final LocationProvider locationProvider;
51     private final Gson gson;
52
53     @Activate
54     public OpenUVHandlerFactory(@Reference TimeZoneProvider timeZoneProvider,
55             @Reference LocationProvider locationProvider) {
56         this.locationProvider = locationProvider;
57         this.gson = new GsonBuilder()
58                 .registerTypeAdapter(ZonedDateTime.class,
59                         (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
60                                 .parse(json.getAsJsonPrimitive().getAsString())
61                                 .withZoneSameInstant(timeZoneProvider.getTimeZone()))
62                 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
63     }
64
65     @Override
66     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
67         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID);
68     }
69
70     @Override
71     protected @Nullable ThingHandler createHandler(Thing thing) {
72         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
73
74         return APIBRIDGE_THING_TYPE.equals(thingTypeUID)
75                 ? new OpenUVBridgeHandler((Bridge) thing, locationProvider, gson)
76                 : LOCATION_REPORT_THING_TYPE.equals(thingTypeUID) ? new OpenUVReportHandler(thing) : null;
77     }
78 }