]> git.basschouten.com Git - openhab-addons.git/blob
f39ff2a91ea98edbe92db8b479710efc123258ad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.jetty.client.HttpClient;
20 import org.openhab.binding.tellstick.internal.core.TelldusCoreBridgeHandler;
21 import org.openhab.binding.tellstick.internal.discovery.TellstickDiscoveryService;
22 import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
23 import org.openhab.binding.tellstick.internal.handler.TelldusDevicesHandler;
24 import org.openhab.binding.tellstick.internal.live.TelldusLiveBridgeHandler;
25 import org.openhab.binding.tellstick.internal.local.TelldusLocalBridgeHandler;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.net.http.HttpClientFactory;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link TellstickHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Jarle Hjortland - Initial contribution
45  * @author Jan Gustafsson - Adding support for local API
46  */
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.tellstick")
48 public class TellstickHandlerFactory extends BaseThingHandlerFactory {
49     private final Logger logger = LoggerFactory.getLogger(TellstickHandlerFactory.class);
50     private TellstickDiscoveryService discoveryService = null;
51     private final HttpClient httpClient;
52
53     @Activate
54     public TellstickHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
55         this.httpClient = httpClientFactory.getCommonHttpClient();
56     }
57
58     @Override
59     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
61     }
62
63     private void registerDeviceDiscoveryService(TelldusBridgeHandler tellstickBridgeHandler) {
64         if (discoveryService == null) {
65             discoveryService = new TellstickDiscoveryService(tellstickBridgeHandler);
66             discoveryService.activate();
67             bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
68         } else {
69             discoveryService.addBridgeHandler(tellstickBridgeHandler);
70         }
71     }
72
73     @Override
74     protected ThingHandler createHandler(Thing thing) {
75         if (thing.getThingTypeUID().equals(TELLDUSCOREBRIDGE_THING_TYPE)) {
76             TelldusCoreBridgeHandler handler = new TelldusCoreBridgeHandler((Bridge) thing);
77             registerDeviceDiscoveryService(handler);
78             return handler;
79         } else if (thing.getThingTypeUID().equals(TELLDUSLIVEBRIDGE_THING_TYPE)) {
80             TelldusLiveBridgeHandler handler = new TelldusLiveBridgeHandler((Bridge) thing);
81             registerDeviceDiscoveryService(handler);
82             return handler;
83         } else if (thing.getThingTypeUID().equals(TELLDUSLOCALBRIDGE_THING_TYPE)) {
84             TelldusLocalBridgeHandler handler = new TelldusLocalBridgeHandler((Bridge) thing, httpClient);
85             registerDeviceDiscoveryService(handler);
86             return handler;
87         } else if (supportsThingType(thing.getThingTypeUID())) {
88             return new TelldusDevicesHandler(thing);
89         } else {
90             logger.debug("ThingHandler not found for {}", thing.getThingTypeUID());
91             return null;
92         }
93     }
94 }