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.satel.internal;
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
17 import java.util.Hashtable;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.satel.internal.config.SatelThingConfig;
27 import org.openhab.binding.satel.internal.discovery.SatelDeviceDiscoveryService;
28 import org.openhab.binding.satel.internal.handler.Atd100Handler;
29 import org.openhab.binding.satel.internal.handler.Ethm1BridgeHandler;
30 import org.openhab.binding.satel.internal.handler.IntRSBridgeHandler;
31 import org.openhab.binding.satel.internal.handler.SatelBridgeHandler;
32 import org.openhab.binding.satel.internal.handler.SatelEventLogHandler;
33 import org.openhab.binding.satel.internal.handler.SatelOutputHandler;
34 import org.openhab.binding.satel.internal.handler.SatelPartitionHandler;
35 import org.openhab.binding.satel.internal.handler.SatelShutterHandler;
36 import org.openhab.binding.satel.internal.handler.SatelSystemHandler;
37 import org.openhab.binding.satel.internal.handler.SatelZoneHandler;
38 import org.openhab.core.config.core.Configuration;
39 import org.openhab.core.config.discovery.DiscoveryService;
40 import org.openhab.core.io.transport.serial.SerialPortManager;
41 import org.openhab.core.thing.Bridge;
42 import org.openhab.core.thing.Thing;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.ThingUID;
45 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
46 import org.openhab.core.thing.binding.ThingHandler;
47 import org.openhab.core.thing.binding.ThingHandlerFactory;
48 import org.openhab.core.thing.type.ThingType;
49 import org.osgi.framework.ServiceRegistration;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
54 * The {@link SatelHandlerFactory} is responsible for creating things and thing
57 * @author Krzysztof Goworek - Initial contribution
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.satel")
61 public class SatelHandlerFactory extends BaseThingHandlerFactory {
63 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
64 .of(BRIDGE_THING_TYPES_UIDS, DEVICE_THING_TYPES_UIDS, VIRTUAL_THING_TYPES_UIDS)
65 .flatMap(uids -> uids.stream()).collect(Collectors.toSet());
67 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new ConcurrentHashMap<>();
69 private @Nullable SerialPortManager serialPortManager;
72 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
77 public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
78 @Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
79 ThingUID effectiveUID = thingUID;
80 if (effectiveUID == null) {
81 if (DEVICE_THING_TYPES_UIDS.contains(thingTypeUID)) {
82 effectiveUID = getDeviceUID(thingTypeUID, thingUID, configuration, bridgeUID);
85 return super.createThing(thingTypeUID, configuration, effectiveUID, bridgeUID);
89 protected @Nullable ThingHandler createHandler(Thing thing) {
90 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
92 if (Ethm1BridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
93 SatelBridgeHandler bridgeHandler = new Ethm1BridgeHandler((Bridge) thing);
94 registerDiscoveryService(bridgeHandler);
96 } else if (IntRSBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
97 final SerialPortManager serialPortManager = this.serialPortManager;
98 if (serialPortManager != null) {
99 SatelBridgeHandler bridgeHandler = new IntRSBridgeHandler((Bridge) thing, serialPortManager);
100 registerDiscoveryService(bridgeHandler);
101 return bridgeHandler;
103 throw new IllegalStateException(
104 "Unable to create INT-RS bridge thing. The serial port manager is missing.");
106 } else if (SatelZoneHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
107 return new SatelZoneHandler(thing);
108 } else if (SatelOutputHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
109 return new SatelOutputHandler(thing);
110 } else if (SatelPartitionHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
111 return new SatelPartitionHandler(thing);
112 } else if (SatelShutterHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
113 return new SatelShutterHandler(thing);
114 } else if (SatelSystemHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
115 return new SatelSystemHandler(thing);
116 } else if (SatelEventLogHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
117 return new SatelEventLogHandler(thing);
118 } else if (Atd100Handler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
119 return new Atd100Handler(thing);
125 @SuppressWarnings("null")
127 protected void removeHandler(ThingHandler thingHandler) {
128 super.removeHandler(thingHandler);
130 ServiceRegistration<?> discoveryServiceRegistration = discoveryServiceRegistrations
131 .remove(thingHandler.getThing().getUID());
132 if (discoveryServiceRegistration != null) {
133 discoveryServiceRegistration.unregister();
138 protected void setSerialPortManager(final SerialPortManager serialPortManager) {
139 this.serialPortManager = serialPortManager;
142 protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
143 this.serialPortManager = null;
146 private void registerDiscoveryService(SatelBridgeHandler bridgeHandler) {
147 SatelDeviceDiscoveryService discoveryService = new SatelDeviceDiscoveryService(bridgeHandler,
148 this::resolveThingType);
149 ServiceRegistration<?> discoveryServiceRegistration = bundleContext
150 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
151 discoveryServiceRegistrations.put(bridgeHandler.getThing().getUID(), discoveryServiceRegistration);
154 private ThingUID getDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
155 @Nullable ThingUID bridgeUID) {
157 if (THING_TYPE_SHUTTER.equals(thingTypeUID)) {
158 deviceId = String.format("%s-%s", configuration.get(SatelThingConfig.UP_ID),
159 configuration.get(SatelThingConfig.DOWN_ID));
161 deviceId = String.valueOf(configuration.get(SatelThingConfig.ID));
163 return bridgeUID != null ? new ThingUID(thingTypeUID, deviceId, bridgeUID.getId())
164 : new ThingUID(thingTypeUID, deviceId);
167 private ThingType resolveThingType(ThingTypeUID thingTypeUID) {
168 final ThingType result = super.getThingTypeByUID(thingTypeUID);
169 if (result != null) {
172 throw new IllegalArgumentException("Invalid thing type provided: " + thingTypeUID);