2 * Copyright (c) 2010-2020 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.rfxcom.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.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.rfxcom.internal.discovery.RFXComDeviceDiscoveryService;
25 import org.openhab.binding.rfxcom.internal.handler.RFXComBridgeHandler;
26 import org.openhab.binding.rfxcom.internal.handler.RFXComHandler;
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.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
42 * The {@link RFXComHandlerFactory} is responsible for creating things and thing
45 * @author Pauli Anttila - Initial contribution
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.rfxcom")
49 public class RFXComHandlerFactory extends BaseThingHandlerFactory {
51 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
52 .concat(RFXComBindingConstants.SUPPORTED_DEVICE_THING_TYPES_UIDS.stream(),
53 RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.stream())
54 .collect(Collectors.toSet());
57 * Service registration map
59 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
61 private final SerialPortManager serialPortManager;
64 public RFXComHandlerFactory(final @Reference SerialPortManager serialPortManager) {
65 this.serialPortManager = serialPortManager;
69 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
70 return SUPPORTED_THING_TYPES.contains(thingTypeUID);
74 protected @Nullable ThingHandler createHandler(Thing thing) {
75 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
77 if (RFXComBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID)) {
78 RFXComBridgeHandler handler = new RFXComBridgeHandler((Bridge) thing, serialPortManager);
79 registerDeviceDiscoveryService(handler);
81 } else if (supportsThingType(thingTypeUID)) {
82 return new RFXComHandler(thing);
89 protected void removeHandler(ThingHandler thingHandler) {
90 if (thingHandler instanceof RFXComBridgeHandler) {
91 unregisterDeviceDiscoveryService(thingHandler.getThing());
95 private synchronized void registerDeviceDiscoveryService(RFXComBridgeHandler handler) {
96 RFXComDeviceDiscoveryService discoveryService = new RFXComDeviceDiscoveryService(handler);
97 discoveryService.activate();
98 this.discoveryServiceRegs.put(handler.getThing().getUID(),
99 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
102 private synchronized void unregisterDeviceDiscoveryService(Thing thing) {
103 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thing.getUID());
104 if (serviceReg != null) {
105 RFXComDeviceDiscoveryService service = (RFXComDeviceDiscoveryService) bundleContext
106 .getService(serviceReg.getReference());
107 serviceReg.unregister();
108 if (service != null) {
109 service.deactivate();