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.enocean.internal;
15 import java.util.HashMap;
16 import java.util.Hashtable;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.openhab.binding.enocean.internal.discovery.EnOceanDeviceDiscoveryService;
23 import org.openhab.binding.enocean.internal.handler.EnOceanBaseActuatorHandler;
24 import org.openhab.binding.enocean.internal.handler.EnOceanBaseSensorHandler;
25 import org.openhab.binding.enocean.internal.handler.EnOceanBridgeHandler;
26 import org.openhab.binding.enocean.internal.handler.EnOceanClassicDeviceHandler;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingManager;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
43 * The {@link EnOceanHandlerFactory} is responsible for creating things and thing
46 * @author Daniel Weber - Initial contribution
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.enocean")
49 public class EnOceanHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
52 .concat(EnOceanBridgeHandler.SUPPORTED_THING_TYPES.stream(),
53 EnOceanBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS.stream())
54 .collect(Collectors.toSet());
56 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59 SerialPortManager serialPortManager;
62 ItemChannelLinkRegistry itemChannelLinkRegistry;
65 ThingManager thingManager;
68 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73 protected ThingHandler createHandler(Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (EnOceanBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77 EnOceanBridgeHandler bridgeHandler = new EnOceanBridgeHandler((Bridge) thing, serialPortManager);
78 registerDeviceDiscoveryService(bridgeHandler);
80 } else if (EnOceanBaseActuatorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
81 return new EnOceanBaseActuatorHandler(thing, itemChannelLinkRegistry);
82 } else if (EnOceanBaseSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
83 return new EnOceanBaseSensorHandler(thing, itemChannelLinkRegistry);
84 } else if (EnOceanClassicDeviceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
85 return new EnOceanClassicDeviceHandler(thing, itemChannelLinkRegistry);
92 protected void removeHandler(ThingHandler thingHandler) {
93 if (this.discoveryServiceRegs != null) {
94 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
95 if (serviceReg != null) {
96 serviceReg.unregister();
97 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
102 private void registerDeviceDiscoveryService(EnOceanBridgeHandler handler) {
103 EnOceanDeviceDiscoveryService discoveryService = new EnOceanDeviceDiscoveryService(handler, thingManager);
104 discoveryService.activate();
105 this.discoveryServiceRegs.put(handler.getThing().getUID(),
106 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));