]> git.basschouten.com Git - openhab-addons.git/blob
5893131873c88980350093e84042d5bea6f69671
[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.digitalstrom.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18
19 import org.openhab.binding.digitalstrom.internal.DigitalSTROMBindingConstants;
20 import org.openhab.binding.digitalstrom.internal.handler.BridgeHandler;
21 import org.openhab.binding.digitalstrom.internal.handler.CircuitHandler;
22 import org.openhab.binding.digitalstrom.internal.handler.DeviceHandler;
23 import org.openhab.binding.digitalstrom.internal.handler.SceneHandler;
24 import org.openhab.binding.digitalstrom.internal.handler.ZoneTemperatureControlHandler;
25 import org.openhab.binding.digitalstrom.internal.lib.climate.TemperatureControlSensorTransmitter;
26 import org.openhab.binding.digitalstrom.internal.lib.climate.jsonresponsecontainer.impl.TemperatureControlStatus;
27 import org.openhab.binding.digitalstrom.internal.lib.listener.DeviceStatusListener;
28 import org.openhab.binding.digitalstrom.internal.lib.listener.SceneStatusListener;
29 import org.openhab.binding.digitalstrom.internal.lib.listener.TemperatureControlStatusListener;
30 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Circuit;
31 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.Device;
32 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.GeneralDeviceInformation;
33 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.DeviceStateUpdate;
34 import org.openhab.binding.digitalstrom.internal.lib.structure.devices.deviceparameters.constants.ChangeableDeviceConfigEnum;
35 import org.openhab.binding.digitalstrom.internal.lib.structure.scene.InternalScene;
36 import org.openhab.binding.digitalstrom.internal.providers.DsDeviceThingTypeProvider;
37 import org.openhab.core.config.discovery.AbstractDiscoveryService;
38 import org.openhab.core.config.discovery.DiscoveryService;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceRegistration;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link DiscoveryServiceManager} manages the different scene and device discovery services and informs them about
47  * new added or removed scenes and devices.
48  *
49  * @author Michael Ochel - Initial contribution
50  * @author Matthias Siegele - Initial contribution
51  */
52 public class DiscoveryServiceManager
53         implements SceneStatusListener, DeviceStatusListener, TemperatureControlStatusListener {
54
55     private final Logger logger = LoggerFactory.getLogger(DiscoveryServiceManager.class);
56
57     private final Map<String, AbstractDiscoveryService> discoveryServices;
58     private final Map<String, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59     private final String bridgeUID;
60
61     /**
62      * Creates a new {@link DiscoveryServiceManager} and generates automatically all {@link SceneDiscoveryService}s and
63      * {@link DeviceDiscoveryService}s for all supported {@link ThingType}s of the {@link DeviceHandler} and
64      * {@link SceneHandler}.
65      *
66      * @param bridgeHandler (must not be null)
67      */
68     public DiscoveryServiceManager(BridgeHandler bridgeHandler) {
69         bridgeUID = bridgeHandler.getThing().getUID().getAsString();
70         discoveryServices = new HashMap<>(SceneHandler.SUPPORTED_THING_TYPES.size()
71                 + DeviceHandler.SUPPORTED_THING_TYPES.size() + CircuitHandler.SUPPORTED_THING_TYPES.size()
72                 + ZoneTemperatureControlHandler.SUPPORTED_THING_TYPES.size());
73         for (ThingTypeUID type : SceneHandler.SUPPORTED_THING_TYPES) {
74             discoveryServices.put(type.getId(), new SceneDiscoveryService(bridgeHandler, type));
75         }
76         for (ThingTypeUID type : DeviceHandler.SUPPORTED_THING_TYPES) {
77             discoveryServices.put(type.getId(), new DeviceDiscoveryService(bridgeHandler, type));
78         }
79         for (ThingTypeUID type : CircuitHandler.SUPPORTED_THING_TYPES) {
80             discoveryServices.put(type.getId(), new DeviceDiscoveryService(bridgeHandler, type));
81         }
82         for (ThingTypeUID type : ZoneTemperatureControlHandler.SUPPORTED_THING_TYPES) {
83             discoveryServices.put(type.getId(), new ZoneTemperatureControlDiscoveryService(bridgeHandler, type));
84         }
85         bridgeHandler.registerSceneStatusListener(this);
86         bridgeHandler.registerDeviceStatusListener(this);
87         bridgeHandler.registerTemperatureControlStatusListener(this);
88     }
89
90     /**
91      * Deactivates all {@link SceneDiscoveryService}s and {@link DeviceDiscoveryService}s of this
92      * {@link DiscoveryServiceManager} and unregisters them from the given {@link BundleContext}.
93      *
94      * @param bundleContext (must not be null)
95      */
96     public void unregisterDiscoveryServices(BundleContext bundleContext) {
97         if (discoveryServices != null) {
98             for (AbstractDiscoveryService service : discoveryServices.values()) {
99                 if (service instanceof SceneDiscoveryService) {
100                     SceneDiscoveryService sceneDisServ = (SceneDiscoveryService) service;
101                     ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + sceneDisServ.getID());
102                     sceneDisServ.deactivate();
103                     serviceReg.unregister();
104                     discoveryServiceRegs.remove(bridgeUID + sceneDisServ.getID());
105                 }
106                 if (service instanceof DeviceDiscoveryService) {
107                     DeviceDiscoveryService devDisServ = (DeviceDiscoveryService) service;
108                     ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + devDisServ.getID());
109                     devDisServ.deactivate();
110                     serviceReg.unregister();
111                     discoveryServiceRegs.remove(bridgeUID + devDisServ.getID());
112                 }
113                 if (service instanceof ZoneTemperatureControlDiscoveryService) {
114                     ZoneTemperatureControlDiscoveryService devDisServ = (ZoneTemperatureControlDiscoveryService) service;
115                     ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + devDisServ.getID());
116                     devDisServ.deactivate();
117                     serviceReg.unregister();
118                     discoveryServiceRegs.remove(bridgeUID + devDisServ.getID());
119                 }
120             }
121         }
122     }
123
124     /**
125      * Registers all {@link SceneDiscoveryService}s and {@link DeviceDiscoveryService}s of this
126      * {@link DiscoveryServiceManager} to the given {@link BundleContext}.
127      *
128      * @param bundleContext (must not be null)
129      */
130     public void registerDiscoveryServices(BundleContext bundleContext) {
131         if (discoveryServices != null) {
132             for (AbstractDiscoveryService service : discoveryServices.values()) {
133                 if (service instanceof SceneDiscoveryService) {
134                     this.discoveryServiceRegs.put(bridgeUID + ((SceneDiscoveryService) service).getID(), bundleContext
135                             .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
136                 }
137                 if (service instanceof DeviceDiscoveryService) {
138                     this.discoveryServiceRegs.put(bridgeUID + ((DeviceDiscoveryService) service).getID(), bundleContext
139                             .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
140                 }
141                 if (service instanceof ZoneTemperatureControlDiscoveryService) {
142                     this.discoveryServiceRegs
143                             .put(bridgeUID + ((ZoneTemperatureControlDiscoveryService) service).getID(), bundleContext
144                                     .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
145                 }
146             }
147         }
148     }
149
150     @Override
151     public String getSceneStatusListenerID() {
152         return SceneStatusListener.SCENE_DISCOVERY;
153     }
154
155     @Override
156     public void onSceneStateChanged(boolean flag) {
157         // nothing to do
158     }
159
160     @Override
161     public void onSceneRemoved(InternalScene scene) {
162         if (discoveryServices.get(scene.getSceneType()) != null) {
163             ((SceneDiscoveryService) discoveryServices.get(scene.getSceneType())).onSceneRemoved(scene);
164         }
165     }
166
167     @Override
168     public void onSceneAdded(InternalScene scene) {
169         if (discoveryServices.get(scene.getSceneType()) != null) {
170             ((SceneDiscoveryService) discoveryServices.get(scene.getSceneType())).onSceneAdded(scene);
171         }
172     }
173
174     @Override
175     public void onDeviceStateChanged(DeviceStateUpdate deviceStateUpdate) {
176         // nothing to do
177     }
178
179     @Override
180     public void onDeviceRemoved(GeneralDeviceInformation device) {
181         if (device instanceof Device) {
182             String id = ((Device) device).getHWinfo().substring(0, 2);
183             if (((Device) device).isSensorDevice()) {
184                 id = ((Device) device).getHWinfo().replace("-", "");
185             }
186             if (discoveryServices.get(id) != null) {
187                 ((DeviceDiscoveryService) discoveryServices.get(id)).onDeviceRemoved(device);
188             }
189         }
190         if (device instanceof Circuit) {
191             if (discoveryServices.get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()) != null) {
192                 ((DeviceDiscoveryService) discoveryServices
193                         .get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString())).onDeviceRemoved(device);
194             }
195         }
196     }
197
198     @Override
199     public void onDeviceAdded(GeneralDeviceInformation device) {
200         try {
201             if (device instanceof Device) {
202                 String id = ((Device) device).getHWinfo().substring(0, 2);
203                 if (((Device) device).isSensorDevice()) {
204                     id = ((Device) device).getHWinfo();
205                 }
206                 if (discoveryServices.get(id) != null) {
207                     ((DeviceDiscoveryService) discoveryServices.get(id)).onDeviceAdded(device);
208                 }
209             }
210             if (device instanceof Circuit) {
211                 if (discoveryServices.get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()) != null) {
212                     ((DeviceDiscoveryService) discoveryServices
213                             .get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()))
214                             .onDeviceAdded(device);
215                 }
216             }
217         } catch (RuntimeException ex) {
218             logger.warn("Unable to add devices {}", device, ex);
219         }
220     }
221
222     @Override
223     public void onDeviceConfigChanged(ChangeableDeviceConfigEnum whatConfig) {
224         // nothing to do
225     }
226
227     @Override
228     public void onSceneConfigAdded(short sceneId) {
229         // nothing to do
230     }
231
232     @Override
233     public String getDeviceStatusListenerID() {
234         return DeviceStatusListener.DEVICE_DISCOVERY;
235     }
236
237     @Override
238     public void configChanged(TemperatureControlStatus tempControlStatus) {
239         // currently only this thing-type exists
240         if (discoveryServices.get(DigitalSTROMBindingConstants.THING_TYPE_ZONE_TEMERATURE_CONTROL.toString()) != null) {
241             ((ZoneTemperatureControlDiscoveryService) discoveryServices
242                     .get(DigitalSTROMBindingConstants.THING_TYPE_ZONE_TEMERATURE_CONTROL.toString()))
243                     .configChanged(tempControlStatus);
244         }
245     }
246
247     @Override
248     public void registerTemperatureSensorTransmitter(
249             TemperatureControlSensorTransmitter temperatureSensorTransreciver) {
250         // nothing to do
251     }
252
253     @Override
254     public Integer getTemperationControlStatusListenrID() {
255         return TemperatureControlStatusListener.DISCOVERY;
256     }
257
258     @Override
259     public void onTargetTemperatureChanged(Float newValue) {
260         // nothing to do
261     }
262
263     @Override
264     public void onControlValueChanged(Integer newValue) {
265         // nothing to do
266     }
267 }