2 * Copyright (c) 2010-2023 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.jeelink.internal;
15 import static org.openhab.binding.jeelink.internal.JeeLinkBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
39 * The {@link JeeLinkHandlerFactory} is responsible for creating things and thing handlers.
41 * @author Volker Bier - Initial contribution
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.jeelink")
44 public class JeeLinkHandlerFactory extends BaseThingHandlerFactory {
45 private final Logger logger = LoggerFactory.getLogger(JeeLinkHandlerFactory.class);
47 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
48 private final SerialPortManager serialPortManager;
51 public JeeLinkHandlerFactory(final @Reference SerialPortManager serialPortManager) {
52 this.serialPortManager = serialPortManager;
56 public boolean supportsThingType(ThingTypeUID thingTypeUid) {
57 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUid);
61 protected ThingHandler createHandler(Thing thing) {
62 ThingTypeUID thingTypeUid = thing.getThingTypeUID();
63 ThingHandler handler = null;
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());
69 handler = new JeeLinkHandler((Bridge) thing, serialPortManager);
70 registerSensorDiscoveryService((JeeLinkHandler) handler);
72 handler = SensorDefinition.createHandler(thingTypeUid, thing);
74 if (handler == null) {
75 logger.debug("skipping creation of unknown handler for thing {} with type {}...",
76 thing.getUID().getId(), thing.getThingTypeUID().getId());
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();
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<>()));