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