2 * Copyright (c) 2010-2021 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.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;
41 * The {@link TellstickHandlerFactory} is responsible for creating things and thing
44 * @author Jarle Hjortland - Initial contribution
45 * @author Jan Gustafsson - Adding support for local API
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;
54 public TellstickHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
55 this.httpClient = httpClientFactory.getCommonHttpClient();
59 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
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<>());
69 discoveryService.addBridgeHandler(tellstickBridgeHandler);
74 protected ThingHandler createHandler(Thing thing) {
75 if (thing.getThingTypeUID().equals(TELLDUSCOREBRIDGE_THING_TYPE)) {
76 TelldusCoreBridgeHandler handler = new TelldusCoreBridgeHandler((Bridge) thing);
77 registerDeviceDiscoveryService(handler);
79 } else if (thing.getThingTypeUID().equals(TELLDUSLIVEBRIDGE_THING_TYPE)) {
80 TelldusLiveBridgeHandler handler = new TelldusLiveBridgeHandler((Bridge) thing);
81 registerDeviceDiscoveryService(handler);
83 } else if (thing.getThingTypeUID().equals(TELLDUSLOCALBRIDGE_THING_TYPE)) {
84 TelldusLocalBridgeHandler handler = new TelldusLocalBridgeHandler((Bridge) thing, httpClient);
85 registerDeviceDiscoveryService(handler);
87 } else if (supportsThingType(thing.getThingTypeUID())) {
88 return new TelldusDevicesHandler(thing);
90 logger.debug("ThingHandler not found for {}", thing.getThingTypeUID());