]> git.basschouten.com Git - openhab-addons.git/blob
aa4ce7c1f8a2720cfc47959596e65c7aa97d88bb
[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.tellstick.internal;
14
15 import static org.openhab.binding.tellstick.internal.TellstickBindingConstants.*;
16
17 import java.util.Hashtable;
18
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;
42
43 /**
44  * The {@link TellstickHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Jarle Hjortland - Initial contribution
48  * @author Jan Gustafsson - Adding support for local API
49  * @author Laurent Garnier - fix discovery service registering/unregistering
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tellstick")
52 @NonNullByDefault
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;
58
59     @Activate
60     public TellstickHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
61         this.httpClient = httpClientFactory.getCommonHttpClient();
62     }
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     private synchronized void registerDeviceDiscoveryService(TelldusBridgeHandler tellstickBridgeHandler) {
70         TellstickDiscoveryService service = discoveryService;
71         if (service == null) {
72             service = new TellstickDiscoveryService(tellstickBridgeHandler);
73             service.activate();
74             discoveryService = service;
75             discoveryServiceRegistration = (ServiceRegistration<DiscoveryService>) bundleContext
76                     .registerService(DiscoveryService.class.getName(), service, new Hashtable<>());
77         } else {
78             service.addBridgeHandler(tellstickBridgeHandler);
79         }
80     }
81
82     private synchronized void unregisterDeviceDiscoveryService(TelldusBridgeHandler tellstickBridgeHandler) {
83         TellstickDiscoveryService service = discoveryService;
84         if (service != null) {
85             if (service.isOnlyForOneBridgeHandler()) {
86                 service.deactivate();
87                 discoveryService = null;
88                 ServiceRegistration<DiscoveryService> serviceReg = discoveryServiceRegistration;
89                 if (serviceReg != null) {
90                     serviceReg.unregister();
91                     discoveryServiceRegistration = null;
92                 }
93             } else {
94                 service.removeBridgeHandler(tellstickBridgeHandler);
95             }
96         }
97     }
98
99     @Override
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);
104             return handler;
105         } else if (thing.getThingTypeUID().equals(TELLDUSLIVEBRIDGE_THING_TYPE)) {
106             TelldusLiveBridgeHandler handler = new TelldusLiveBridgeHandler((Bridge) thing);
107             registerDeviceDiscoveryService(handler);
108             return handler;
109         } else if (thing.getThingTypeUID().equals(TELLDUSLOCALBRIDGE_THING_TYPE)) {
110             TelldusLocalBridgeHandler handler = new TelldusLocalBridgeHandler((Bridge) thing, httpClient);
111             registerDeviceDiscoveryService(handler);
112             return handler;
113         } else if (supportsThingType(thing.getThingTypeUID())) {
114             return new TelldusDevicesHandler(thing);
115         } else {
116             logger.debug("ThingHandler not found for {}", thing.getThingTypeUID());
117             return null;
118         }
119     }
120
121     @Override
122     protected void removeHandler(ThingHandler thingHandler) {
123         if (thingHandler instanceof TelldusBridgeHandler telldusBridgeHandler) {
124             unregisterDeviceDiscoveryService(telldusBridgeHandler);
125         }
126     }
127 }