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.digitalstrom.internal.discovery;
15 import java.util.HashMap;
16 import java.util.Hashtable;
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;
46 * The {@link DiscoveryServiceManager} manages the different scene and device discovery services and informs them about
47 * new added or removed scenes and devices.
49 * @author Michael Ochel - Initial contribution
50 * @author Matthias Siegele - Initial contribution
52 public class DiscoveryServiceManager
53 implements SceneStatusListener, DeviceStatusListener, TemperatureControlStatusListener {
55 private final Logger logger = LoggerFactory.getLogger(DiscoveryServiceManager.class);
57 private final Map<String, AbstractDiscoveryService> discoveryServices;
58 private final Map<String, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59 private final String bridgeUID;
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}.
66 * @param bridgeHandler (must not be null)
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));
76 for (ThingTypeUID type : DeviceHandler.SUPPORTED_THING_TYPES) {
77 discoveryServices.put(type.getId(), new DeviceDiscoveryService(bridgeHandler, type));
79 for (ThingTypeUID type : CircuitHandler.SUPPORTED_THING_TYPES) {
80 discoveryServices.put(type.getId(), new DeviceDiscoveryService(bridgeHandler, type));
82 for (ThingTypeUID type : ZoneTemperatureControlHandler.SUPPORTED_THING_TYPES) {
83 discoveryServices.put(type.getId(), new ZoneTemperatureControlDiscoveryService(bridgeHandler, type));
85 bridgeHandler.registerSceneStatusListener(this);
86 bridgeHandler.registerDeviceStatusListener(this);
87 bridgeHandler.registerTemperatureControlStatusListener(this);
91 * Deactivates all {@link SceneDiscoveryService}s and {@link DeviceDiscoveryService}s of this
92 * {@link DiscoveryServiceManager} and unregisters them from the given {@link BundleContext}.
94 * @param bundleContext (must not be null)
96 public void unregisterDiscoveryServices(BundleContext bundleContext) {
97 if (discoveryServices != null) {
98 for (AbstractDiscoveryService service : discoveryServices.values()) {
99 if (service instanceof SceneDiscoveryService sceneDisServ) {
100 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + sceneDisServ.getID());
101 sceneDisServ.deactivate();
102 serviceReg.unregister();
103 discoveryServiceRegs.remove(bridgeUID + sceneDisServ.getID());
105 if (service instanceof DeviceDiscoveryService devDisServ) {
106 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + devDisServ.getID());
107 devDisServ.deactivate();
108 serviceReg.unregister();
109 discoveryServiceRegs.remove(bridgeUID + devDisServ.getID());
111 if (service instanceof ZoneTemperatureControlDiscoveryService devDisServ) {
112 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(bridgeUID + devDisServ.getID());
113 devDisServ.deactivate();
114 serviceReg.unregister();
115 discoveryServiceRegs.remove(bridgeUID + devDisServ.getID());
122 * Registers all {@link SceneDiscoveryService}s and {@link DeviceDiscoveryService}s of this
123 * {@link DiscoveryServiceManager} to the given {@link BundleContext}.
125 * @param bundleContext (must not be null)
127 public void registerDiscoveryServices(BundleContext bundleContext) {
128 if (discoveryServices != null) {
129 for (AbstractDiscoveryService service : discoveryServices.values()) {
130 if (service instanceof SceneDiscoveryService discoveryService) {
131 this.discoveryServiceRegs.put(bridgeUID + discoveryService.getID(), bundleContext
132 .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
134 if (service instanceof DeviceDiscoveryService discoveryService) {
135 this.discoveryServiceRegs.put(bridgeUID + discoveryService.getID(), bundleContext
136 .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
138 if (service instanceof ZoneTemperatureControlDiscoveryService discoveryService) {
139 this.discoveryServiceRegs.put(bridgeUID + discoveryService.getID(), bundleContext
140 .registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
147 public String getSceneStatusListenerID() {
148 return SceneStatusListener.SCENE_DISCOVERY;
152 public void onSceneStateChanged(boolean flag) {
157 public void onSceneRemoved(InternalScene scene) {
158 if (discoveryServices.get(scene.getSceneType()) != null) {
159 ((SceneDiscoveryService) discoveryServices.get(scene.getSceneType())).onSceneRemoved(scene);
164 public void onSceneAdded(InternalScene scene) {
165 if (discoveryServices.get(scene.getSceneType()) != null) {
166 ((SceneDiscoveryService) discoveryServices.get(scene.getSceneType())).onSceneAdded(scene);
171 public void onDeviceStateChanged(DeviceStateUpdate deviceStateUpdate) {
176 public void onDeviceRemoved(GeneralDeviceInformation device) {
177 if (device instanceof Device dev) {
178 String id = dev.getHWinfo().substring(0, 2);
179 if (dev.isSensorDevice()) {
180 id = dev.getHWinfo().replace("-", "");
182 if (discoveryServices.get(id) != null) {
183 ((DeviceDiscoveryService) discoveryServices.get(id)).onDeviceRemoved(device);
186 if (device instanceof Circuit) {
187 if (discoveryServices.get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()) != null) {
188 ((DeviceDiscoveryService) discoveryServices
189 .get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString())).onDeviceRemoved(device);
195 public void onDeviceAdded(GeneralDeviceInformation device) {
197 if (device instanceof Device dev) {
198 String id = dev.getHWinfo().substring(0, 2);
199 if (dev.isSensorDevice()) {
200 id = dev.getHWinfo();
202 if (discoveryServices.get(id) != null) {
203 ((DeviceDiscoveryService) discoveryServices.get(id)).onDeviceAdded(device);
206 if (device instanceof Circuit) {
207 if (discoveryServices.get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()) != null) {
208 ((DeviceDiscoveryService) discoveryServices
209 .get(DsDeviceThingTypeProvider.SupportedThingTypes.circuit.toString()))
210 .onDeviceAdded(device);
213 } catch (RuntimeException ex) {
214 logger.warn("Unable to add devices {}", device, ex);
219 public void onDeviceConfigChanged(ChangeableDeviceConfigEnum whatConfig) {
224 public void onSceneConfigAdded(short sceneId) {
229 public String getDeviceStatusListenerID() {
230 return DeviceStatusListener.DEVICE_DISCOVERY;
234 public void configChanged(TemperatureControlStatus tempControlStatus) {
235 // currently only this thing-type exists
236 if (discoveryServices.get(DigitalSTROMBindingConstants.THING_TYPE_ZONE_TEMERATURE_CONTROL.toString()) != null) {
237 ((ZoneTemperatureControlDiscoveryService) discoveryServices
238 .get(DigitalSTROMBindingConstants.THING_TYPE_ZONE_TEMERATURE_CONTROL.toString()))
239 .configChanged(tempControlStatus);
244 public void registerTemperatureSensorTransmitter(
245 TemperatureControlSensorTransmitter temperatureSensorTransreciver) {
250 public Integer getTemperationControlStatusListenrID() {
251 return TemperatureControlStatusListener.DISCOVERY;
255 public void onTargetTemperatureChanged(Float newValue) {
260 public void onControlValueChanged(Integer newValue) {