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.tellstick.internal;
15 import static org.openhab.binding.tellstick.internal.TellstickBindingConstants.*;
17 import java.util.Hashtable;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.tellstick.internal.core.TelldusCoreBridgeHandler;
23 import org.openhab.binding.tellstick.internal.discovery.TellstickDiscoveryService;
24 import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
25 import org.openhab.binding.tellstick.internal.handler.TelldusDevicesHandler;
26 import org.openhab.binding.tellstick.internal.live.TelldusLiveBridgeHandler;
27 import org.openhab.binding.tellstick.internal.local.TelldusLocalBridgeHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.net.http.HttpClientFactory;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link TellstickHandlerFactory} is responsible for creating things and thing
47 * @author Jarle Hjortland - Initial contribution
48 * @author Jan Gustafsson - Adding support for local API
49 * @author Laurent Garnier - fix discovery service registering/unregistering
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tellstick")
53 public class TellstickHandlerFactory extends BaseThingHandlerFactory {
54 private final Logger logger = LoggerFactory.getLogger(TellstickHandlerFactory.class);
55 private final HttpClient httpClient;
56 private @Nullable TellstickDiscoveryService discoveryService = null;
57 private @Nullable ServiceRegistration<DiscoveryService> discoveryServiceRegistration = null;
60 public TellstickHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
61 this.httpClient = httpClientFactory.getCommonHttpClient();
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 private synchronized void registerDeviceDiscoveryService(TelldusBridgeHandler tellstickBridgeHandler) {
70 TellstickDiscoveryService service = discoveryService;
71 if (service == null) {
72 service = new TellstickDiscoveryService(tellstickBridgeHandler);
74 discoveryService = service;
75 discoveryServiceRegistration = (ServiceRegistration<DiscoveryService>) bundleContext
76 .registerService(DiscoveryService.class.getName(), service, new Hashtable<>());
78 service.addBridgeHandler(tellstickBridgeHandler);
82 private synchronized void unregisterDeviceDiscoveryService(TelldusBridgeHandler tellstickBridgeHandler) {
83 TellstickDiscoveryService service = discoveryService;
84 if (service != null) {
85 if (service.isOnlyForOneBridgeHandler()) {
87 discoveryService = null;
88 ServiceRegistration<DiscoveryService> serviceReg = discoveryServiceRegistration;
89 if (serviceReg != null) {
90 serviceReg.unregister();
91 discoveryServiceRegistration = null;
94 service.removeBridgeHandler(tellstickBridgeHandler);
100 protected @Nullable ThingHandler createHandler(Thing thing) {
101 if (thing.getThingTypeUID().equals(TELLDUSCOREBRIDGE_THING_TYPE)) {
102 TelldusCoreBridgeHandler handler = new TelldusCoreBridgeHandler((Bridge) thing);
103 registerDeviceDiscoveryService(handler);
105 } else if (thing.getThingTypeUID().equals(TELLDUSLIVEBRIDGE_THING_TYPE)) {
106 TelldusLiveBridgeHandler handler = new TelldusLiveBridgeHandler((Bridge) thing);
107 registerDeviceDiscoveryService(handler);
109 } else if (thing.getThingTypeUID().equals(TELLDUSLOCALBRIDGE_THING_TYPE)) {
110 TelldusLocalBridgeHandler handler = new TelldusLocalBridgeHandler((Bridge) thing, httpClient);
111 registerDeviceDiscoveryService(handler);
113 } else if (supportsThingType(thing.getThingTypeUID())) {
114 return new TelldusDevicesHandler(thing);
116 logger.debug("ThingHandler not found for {}", thing.getThingTypeUID());
122 protected void removeHandler(ThingHandler thingHandler) {
123 if (thingHandler instanceof TelldusBridgeHandler telldusBridgeHandler) {
124 unregisterDeviceDiscoveryService(telldusBridgeHandler);