]> git.basschouten.com Git - openhab-addons.git/blob
d4a2e6016806c8ca92192ef89c755679e54f0309
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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             } else if (VIRTUAL_THING_TYPES_UIDS.contains(thingTypeUID) && bridgeUID != null) {
84                 effectiveUID = new ThingUID(thingTypeUID, bridgeUID.getId());
85             }
86         }
87         return super.createThing(thingTypeUID, configuration, effectiveUID, bridgeUID);
88     }
89
90     @Override
91     protected @Nullable ThingHandler createHandler(Thing thing) {
92         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
93
94         if (Ethm1BridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
95             SatelBridgeHandler bridgeHandler = new Ethm1BridgeHandler((Bridge) thing);
96             registerDiscoveryService(bridgeHandler);
97             return bridgeHandler;
98         } else if (IntRSBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
99             final SerialPortManager serialPortManager = this.serialPortManager;
100             if (serialPortManager != null) {
101                 SatelBridgeHandler bridgeHandler = new IntRSBridgeHandler((Bridge) thing, serialPortManager);
102                 registerDiscoveryService(bridgeHandler);
103                 return bridgeHandler;
104             } else {
105                 throw new IllegalStateException(
106                         "Unable to create INT-RS bridge thing. The serial port manager is missing.");
107             }
108         } else if (SatelZoneHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
109             return new SatelZoneHandler(thing);
110         } else if (SatelOutputHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
111             return new SatelOutputHandler(thing);
112         } else if (SatelPartitionHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
113             return new SatelPartitionHandler(thing);
114         } else if (SatelShutterHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
115             return new SatelShutterHandler(thing);
116         } else if (SatelSystemHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
117             return new SatelSystemHandler(thing);
118         } else if (SatelEventLogHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
119             return new SatelEventLogHandler(thing);
120         } else if (Atd100Handler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
121             return new Atd100Handler(thing);
122         }
123
124         return null;
125     }
126
127     @SuppressWarnings("null")
128     @Override
129     protected void removeHandler(ThingHandler thingHandler) {
130         super.removeHandler(thingHandler);
131
132         ServiceRegistration<?> discoveryServiceRegistration = discoveryServiceRegistrations
133                 .remove(thingHandler.getThing().getUID());
134         if (discoveryServiceRegistration != null) {
135             discoveryServiceRegistration.unregister();
136         }
137     }
138
139     @Reference
140     protected void setSerialPortManager(final SerialPortManager serialPortManager) {
141         this.serialPortManager = serialPortManager;
142     }
143
144     protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
145         this.serialPortManager = null;
146     }
147
148     private void registerDiscoveryService(SatelBridgeHandler bridgeHandler) {
149         SatelDeviceDiscoveryService discoveryService = new SatelDeviceDiscoveryService(bridgeHandler,
150                 this::resolveThingType);
151         ServiceRegistration<?> discoveryServiceRegistration = bundleContext
152                 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>());
153         discoveryServiceRegistrations.put(bridgeHandler.getThing().getUID(), discoveryServiceRegistration);
154     }
155
156     private ThingUID getDeviceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID, Configuration configuration,
157             @Nullable ThingUID bridgeUID) {
158         String deviceId;
159         if (THING_TYPE_SHUTTER.equals(thingTypeUID)) {
160             deviceId = String.format("%s-%s", configuration.get(SatelThingConfig.UP_ID),
161                     configuration.get(SatelThingConfig.DOWN_ID));
162         } else {
163             deviceId = String.valueOf(configuration.get(SatelThingConfig.ID));
164         }
165         return bridgeUID != null ? new ThingUID(thingTypeUID, deviceId, bridgeUID.getId())
166                 : new ThingUID(thingTypeUID, deviceId);
167     }
168
169     private ThingType resolveThingType(ThingTypeUID thingTypeUID) {
170         final ThingType result = super.getThingTypeByUID(thingTypeUID);
171         if (result != null) {
172             return result;
173         }
174         throw new IllegalArgumentException("Invalid thing type provided: " + thingTypeUID);
175     }
176 }