]> git.basschouten.com Git - openhab-addons.git/blob
22d95b421b453e4b97798135eb5412b45a435726
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.satel.internal;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
16
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
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;
52
53 /**
54  * The {@link SatelHandlerFactory} is responsible for creating things and thing
55  * handlers.
56  *
57  * @author Krzysztof Goworek - Initial contribution
58  */
59 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.satel")
60 @NonNullByDefault
61 public class SatelHandlerFactory extends BaseThingHandlerFactory {
62
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());
66
67     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new ConcurrentHashMap<>();
68
69     private @Nullable SerialPortManager serialPortManager;
70
71     @Override
72     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74     }
75
76     @Override
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);
83             }
84         }
85         return super.createThing(thingTypeUID, configuration, effectiveUID, bridgeUID);
86     }
87
88     @Override
89     protected @Nullable ThingHandler createHandler(Thing thing) {
90         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91
92         if (Ethm1BridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
93             SatelBridgeHandler bridgeHandler = new Ethm1BridgeHandler((Bridge) thing);
94             registerDiscoveryService(bridgeHandler);
95             return 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;
102             } else {
103                 throw new IllegalStateException(
104                         "Unable to create INT-RS bridge thing. The serial port manager is missing.");
105             }
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);
120         }
121
122         return null;
123     }
124
125     @SuppressWarnings("null")
126     @Override
127     protected void removeHandler(ThingHandler thingHandler) {
128         super.removeHandler(thingHandler);
129
130         ServiceRegistration<?> discoveryServiceRegistration = discoveryServiceRegistrations
131                 .remove(thingHandler.getThing().getUID());
132         if (discoveryServiceRegistration != null) {
133             discoveryServiceRegistration.unregister();
134         }
135     }
136
137     @Reference
138     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
139         this.serialPortManager = serialPortManager;
140     }
141
142     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
143         this.serialPortManager = null;
144     }
145
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);
152     }
153
154     private ThingUID getDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
155             @Nullable ThingUID bridgeUID) {
156         String deviceId;
157         if (THING_TYPE_SHUTTER.equals(thingTypeUID)) {
158             deviceId = String.format("%s-%s", configuration.get(SatelThingConfig.UP_ID),
159                     configuration.get(SatelThingConfig.DOWN_ID));
160         } else {
161             deviceId = String.valueOf(configuration.get(SatelThingConfig.ID));
162         }
163         return bridgeUID != null ? new ThingUID(thingTypeUID, deviceId, bridgeUID.getId())
164                 : new ThingUID(thingTypeUID, deviceId);
165     }
166
167     private ThingType resolveThingType(ThingTypeUID thingTypeUID) {
168         final ThingType result = super.getThingTypeByUID(thingTypeUID);
169         if (result != null) {
170             return result;
171         }
172         throw new IllegalArgumentException("Invalid thing type provided: " + thingTypeUID);
173     }
174 }