2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lametrictime.internal;
15 import static org.openhab.binding.lametrictime.internal.LaMetricTimeBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import java.util.concurrent.TimeUnit;
23 import javax.ws.rs.client.ClientBuilder;
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;
50 * The {@link LaMetricTimeHandlerFactory} is responsible for creating things and thing
53 * @author Gregory Moyer - Initial contribution
55 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lametrictime")
57 public class LaMetricTimeHandlerFactory extends BaseThingHandlerFactory {
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);
62 private static final int EVENT_STREAM_CONNECT_TIMEOUT = 10;
63 private static final int EVENT_STREAM_READ_TIMEOUT = 10;
65 private final Logger logger = LoggerFactory.getLogger(LaMetricTimeHandlerFactory.class);
67 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceReg = new HashMap<>();
69 private final ClientBuilder clientBuilder;
71 private final StateDescriptionOptionsProvider stateDescriptionProvider;
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;
83 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84 return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
88 protected @Nullable ThingHandler createHandler(Thing thing) {
89 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91 if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
92 logger.debug("Creating handler for LaMetric Time device {}", thing);
94 LaMetricTimeHandler deviceHandler = new LaMetricTimeHandler((Bridge) thing, stateDescriptionProvider,
96 registerAppDiscoveryService(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);
120 protected void removeHandler(final ThingHandler thingHandler) {
121 if (!(thingHandler instanceof LaMetricTimeHandler)) {
125 unregisterAppDiscoveryService((LaMetricTimeHandler) thingHandler);
129 * Register the given device handler to participate in discovery of new apps.
131 * @param deviceHandler the device handler to register (must not be <code>null</code>)
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<>()));
141 * Unregister the given device handler from participating in discovery of new apps.
143 * @param deviceHandler the device handler to unregister (must not be <code>null</code>)
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();