]> git.basschouten.com Git - openhab-addons.git/blob
4ff997b5fe5dcd64ba59849361e3a12e681a39e4
[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.jeelink.internal;
14
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.openhab.binding.jeelink.internal.discovery.SensorDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryService;
23 import org.openhab.core.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
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.framework.ServiceRegistration;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link JeeLinkHandlerFactory} is responsible for creating things and thing handlers.
40  *
41  * @author Volker Bier - Initial contribution
42  */
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.jeelink")
44 public class JeeLinkHandlerFactory extends BaseThingHandlerFactory {
45     private final Logger logger = LoggerFactory.getLogger(JeeLinkHandlerFactory.class);
46
47     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
48     private final SerialPortManager serialPortManager;
49
50     @Activate
51     public JeeLinkHandlerFactory(final @Reference SerialPortManager serialPortManager) {
52         this.serialPortManager = serialPortManager;
53     }
54
55     @Override
56     public boolean supportsThingType(ThingTypeUID thingTypeUid) {
57         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUid);
58     }
59
60     @Override
61     protected ThingHandler createHandler(Thing thing) {
62         ThingTypeUID thingTypeUid = thing.getThingTypeUID();
63         ThingHandler handler = null;
64
65         if (thingTypeUid.equals(JEELINK_USB_STICK_THING_TYPE) || thingTypeUid.equals(JEELINK_TCP_STICK_THING_TYPE)
66                 || thingTypeUid.equals(LGW_TCP_STICK_THING_TYPE) || thingTypeUid.equals(LGW_USB_STICK_THING_TYPE)) {
67             logger.debug("creating JeeLinkHandler for thing {}...", thing.getUID().getId());
68
69             handler = new JeeLinkHandler((Bridge) thing, serialPortManager);
70             registerSensorDiscoveryService((JeeLinkHandler) handler);
71         } else {
72             handler = SensorDefinition.createHandler(thingTypeUid, thing);
73
74             if (handler == null) {
75                 logger.debug("skipping creation of unknown handler for thing {} with type {}...",
76                         thing.getUID().getId(), thing.getThingTypeUID().getId());
77             }
78         }
79
80         return handler;
81     }
82
83     @Override
84     protected synchronized void removeHandler(ThingHandler thingHandler) {
85         if (thingHandler instanceof JeeLinkHandler) {
86             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
87             if (serviceReg != null) {
88                 serviceReg.unregister();
89             }
90         }
91     }
92
93     private synchronized void registerSensorDiscoveryService(JeeLinkHandler bridgeHandler) {
94         logger.debug("registering sensor discovery service...");
95         SensorDiscoveryService discoveryService = new SensorDiscoveryService(bridgeHandler);
96         discoveryServiceRegs.put(bridgeHandler.getThing().getUID(),
97                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
98     }
99 }