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