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