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