]> git.basschouten.com Git - openhab-addons.git/blob
a19e431341fd7fb8aff42f0a971db92a0ce7e3ff
[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.lametrictime.internal;
14
15 import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.ws.rs.client.ClientBuilder;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.lametrictime.internal.discovery.LaMetricTimeAppDiscoveryService;
28 import org.openhab.binding.lametrictime.internal.handler.ClockAppHandler;
29 import org.openhab.binding.lametrictime.internal.handler.CountdownAppHandler;
30 import org.openhab.binding.lametrictime.internal.handler.LaMetricTimeHandler;
31 import org.openhab.binding.lametrictime.internal.handler.RadioAppHandler;
32 import org.openhab.binding.lametrictime.internal.handler.StopwatchAppHandler;
33 import org.openhab.binding.lametrictime.internal.handler.WeatherAppHandler;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.osgi.framework.ServiceRegistration;
43 import org.osgi.service.component.annotations.Activate;
44 import org.osgi.service.component.annotations.Component;
45 import org.osgi.service.component.annotations.Reference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * The {@link LaMetricTimeHandlerFactory} is responsible for creating things and thing
51  * handlers.
52  *
53  * @author Gregory Moyer - Initial contribution
54  */
55 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lametrictime")
56 @NonNullByDefault
57 public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
58
59     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Set.of(THING_TYPE_DEVICE, THING_TYPE_CLOCK_APP,
60             THING_TYPE_COUNTDOWN_APP, THING_TYPE_RADIO_APP, THING_TYPE_STOPWATCH_APP, THING_TYPE_WEATHER_APP);
61
62     private static final int EVENT_STREAM_CONNECT_TIMEOUT = 10;
63     private static final int EVENT_STREAM_READ_TIMEOUT = 10;
64
65     private final Logger logger = LoggerFactory.getLogger(LaMetricTimeHandlerFactory.class);
66
67     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
68
69     private final ClientBuilder clientBuilder;
70
71     private final StateDescriptionOptionsProvider stateDescriptionProvider;
72
73     @Activate
74     public LaMetricTimeHandlerFactory(@Reference ClientBuilder clientBuilder,
75             @Reference StateDescriptionOptionsProvider stateDescriptionProvider) {
76         this.clientBuilder = clientBuilder //
77                 .connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
78                 .readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
79         this.stateDescriptionProvider = stateDescriptionProvider;
80     }
81
82     @Override
83     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84         return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
85     }
86
87     @Override
88     protected @Nullable ThingHandler createHandler(Thing thing) {
89         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
90
91         if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
92             logger.debug("Creating handler for LaMetric Time device {}", thing);
93
94             LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider,
95                     clientBuilder);
96             registerAppDiscoveryService(deviceHandler);
97
98             return deviceHandler;
99         } else if (THING_TYPE_CLOCK_APP.equals(thingTypeUID)) {
100             logger.debug("Creating handler for LaMetric Time clock app {}", thing);
101             return new ClockAppHandler(thing);
102         } else if (THING_TYPE_COUNTDOWN_APP.equals(thingTypeUID)) {
103             logger.debug("Creating handler for LaMetric Time countdown app {}", thing);
104             return new CountdownAppHandler(thing);
105         } else if (THING_TYPE_RADIO_APP.equals(thingTypeUID)) {
106             logger.debug("Creating handler for LaMetric Time radio app {}", thing);
107             return new RadioAppHandler(thing);
108         } else if (THING_TYPE_STOPWATCH_APP.equals(thingTypeUID)) {
109             logger.debug("Creating handler for LaMetric Time stopwatch app {}", thing);
110             return new StopwatchAppHandler(thing);
111         } else if (THING_TYPE_WEATHER_APP.equals(thingTypeUID)) {
112             logger.debug("Creating handler for LaMetric Time weather app {}", thing);
113             return new WeatherAppHandler(thing);
114         }
115
116         return null;
117     }
118
119     @Override
120     protected void removeHandler(final ThingHandler thingHandler) {
121         if (!(thingHandler instanceof LaMetricTimeHandler)) {
122             return;
123         }
124
125         unregisterAppDiscoveryService((LaMetricTimeHandler) thingHandler);
126     }
127
128     /**
129      * Register the given device handler to participate in discovery of new apps.
130      *
131      * @param deviceHandler the device handler to register (must not be <code>null</code>)
132      */
133     private synchronized void registerAppDiscoveryService(final LaMetricTimeHandler deviceHandler) {
134         logger.debug("Registering app discovery service");
135         LaMetricTimeAppDiscoveryService discoveryService = new LaMetricTimeAppDiscoveryService(deviceHandler);
136         discoveryServiceReg.put(deviceHandler.getThing().getUID(),
137                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
138     }
139
140     /**
141      * Unregister the given device handler from participating in discovery of new apps.
142      *
143      * @param deviceHandler the device handler to unregister (must not be <code>null</code>)
144      */
145     private synchronized void unregisterAppDiscoveryService(final LaMetricTimeHandler deviceHandler) {
146         ThingUID thingUID = deviceHandler.getThing().getUID();
147         ServiceRegistration<?> serviceReg = discoveryServiceReg.remove(thingUID);
148         if (serviceReg != null) {
149             logger.debug("Unregistering app discovery service");
150             serviceReg.unregister();
151         }
152     }
153 }