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.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.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
42 * The {@link EnOceanHandlerFactory} is responsible for creating things and thing
45 * @author Daniel Weber - Initial contribution
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.enocean")
48 public class EnOceanHandlerFactory extends BaseThingHandlerFactory {
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
51 .concat(EnOceanBridgeHandler.SUPPORTED_THING_TYPES.stream(),
52 EnOceanBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS.stream())
53 .collect(Collectors.toSet());
55 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
58 SerialPortManager serialPortManager;
61 ItemChannelLinkRegistry itemChannelLinkRegistry;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 protected ThingHandler createHandler(Thing thing) {
70 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72 if (EnOceanBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
73 EnOceanBridgeHandler bridgeHandler = new EnOceanBridgeHandler((Bridge) thing, serialPortManager);
74 registerDeviceDiscoveryService(bridgeHandler);
76 } else if (EnOceanBaseActuatorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77 return new EnOceanBaseActuatorHandler(thing, itemChannelLinkRegistry);
78 } else if (EnOceanBaseSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
79 return new EnOceanBaseSensorHandler(thing, itemChannelLinkRegistry);
80 } else if (EnOceanClassicDeviceHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
81 return new EnOceanClassicDeviceHandler(thing, itemChannelLinkRegistry);
88 protected void removeHandler(ThingHandler thingHandler) {
89 if (this.discoveryServiceRegs != null) {
90 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
91 if (serviceReg != null) {
92 serviceReg.unregister();
93 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
98 private void registerDeviceDiscoveryService(EnOceanBridgeHandler handler) {
99 EnOceanDeviceDiscoveryService discoveryService = new EnOceanDeviceDiscoveryService(handler);
100 discoveryService.activate();
101 this.discoveryServiceRegs.put(handler.getThing().getUID(),
102 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));