2 * Copyright (c) 2010-2022 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.openwebnet.internal.discovery;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
22 import org.openhab.binding.openwebnet.internal.handler.OpenWebNetBridgeHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.openwebnet4j.OpenDeviceType;
32 import org.openwebnet4j.message.BaseOpenMessage;
33 import org.openwebnet4j.message.Where;
34 import org.openwebnet4j.message.WhereZigBee;
35 import org.openwebnet4j.message.Who;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link OpenWebNetDeviceDiscoveryService} is responsible for discovering OpenWebNet devices connected to a
43 * @author Massimo Valla - Initial contribution
44 * @author Andrea Conte - Energy management, Thermoregulation
45 * @author Gilberto Cocchi - Thermoregulation
48 public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService
49 implements DiscoveryService, ThingHandlerService {
51 private final Logger logger = LoggerFactory.getLogger(OpenWebNetDeviceDiscoveryService.class);
53 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.DEVICE_SUPPORTED_THING_TYPES;
54 private static final int SEARCH_TIME_SEC = 60;
56 private @NonNullByDefault({}) OpenWebNetBridgeHandler bridgeHandler;
57 private @NonNullByDefault({}) ThingUID bridgeUID;
59 public OpenWebNetDeviceDiscoveryService() {
60 super(SUPPORTED_THING_TYPES, SEARCH_TIME_SEC);
64 public Set<ThingTypeUID> getSupportedThingTypes() {
65 return SUPPORTED_THING_TYPES;
69 protected void startScan() {
70 logger.info("------ SEARCHING for DEVICES on bridge '{}' ({}) ...", bridgeHandler.getThing().getLabel(),
72 bridgeHandler.searchDevices();
76 protected void stopScan() {
77 logger.debug("------ stopScan() on bridge '{}'", bridgeUID);
78 bridgeHandler.scanStopped();
82 public void abortScan() {
83 logger.debug("------ abortScan() on bridge '{}'", bridgeUID);
84 bridgeHandler.scanStopped();
88 * Create and notify to Inbox a new DiscoveryResult based on WHERE, OpenDeviceType and BaseOpenMessage
90 * @param where the discovered device's address (WHERE)
91 * @param deviceType {@link OpenDeviceType} of the discovered device
92 * @param message the OWN message received that identified the device (optional)
94 public void newDiscoveryResult(Where where, OpenDeviceType deviceType, @Nullable BaseOpenMessage baseMsg) {
95 logger.info("newDiscoveryResult() WHERE={}, deviceType={}", where, deviceType);
96 ThingTypeUID thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_GENERIC_DEVICE; // generic device
97 String thingLabel = OpenWebNetBindingConstants.THING_LABEL_GENERIC_DEVICE;
98 Who deviceWho = Who.UNKNOWN;
100 case ZIGBEE_ON_OFF_SWITCH:
101 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH;
102 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_ON_OFF_SWITCH;
103 deviceWho = Who.LIGHTING;
105 case ZIGBEE_DIMMER_SWITCH:
106 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_DIMMER;
107 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_DIMMER;
108 deviceWho = Who.LIGHTING;
110 case SCS_ON_OFF_SWITCH:
111 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_ON_OFF_SWITCH;
112 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_ON_OFF_SWITCH;
113 deviceWho = Who.LIGHTING;
115 case SCS_DIMMER_SWITCH:
116 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_DIMMER;
117 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_DIMMER;
118 deviceWho = Who.LIGHTING;
120 case SCS_SHUTTER_SWITCH:
121 case SCS_SHUTTER_CONTROL: {
122 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_AUTOMATION;
123 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_AUTOMATION;
124 deviceWho = Who.AUTOMATION;
127 case ZIGBEE_SHUTTER_SWITCH:
128 case ZIGBEE_SHUTTER_CONTROL: {
129 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_AUTOMATION;
130 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_AUTOMATION;
131 deviceWho = Who.AUTOMATION;
134 case SCS_THERMO_SENSOR: {
135 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_SENSOR;
136 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_SENSOR;
137 deviceWho = Who.THERMOREGULATION;
140 case SCS_THERMO_ZONE: {
141 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_ZONE;
142 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_ZONE;
143 deviceWho = Who.THERMOREGULATION;
146 case SCS_THERMO_CENTRAL_UNIT: {
147 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_CU;
148 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_CU;
149 deviceWho = Who.THERMOREGULATION;
152 case SCS_ENERGY_METER: {
153 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_ENERGY_METER;
154 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_ENERGY_METER;
155 deviceWho = Who.ENERGY_MANAGEMENT;
158 case SCENARIO_CONTROL: {
159 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_CEN_SCENARIO_CONTROL;
160 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_CEN_SCENARIO_CONTROL;
161 deviceWho = Who.CEN_SCENARIO_SCHEDULER;
164 case SCS_DRY_CONTACT_IR: {
165 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_DRY_CONTACT_IR;
166 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_DRY_CONTACT_IR;
167 deviceWho = Who.CEN_PLUS_SCENARIO_SCHEDULER;
170 case MULTIFUNCTION_SCENARIO_CONTROL: {
171 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_CENPLUS_SCENARIO_CONTROL;
172 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_CENPLUS_SCENARIO_CONTROL;
173 deviceWho = Who.CEN_PLUS_SCENARIO_SCHEDULER;
177 logger.warn("Device type {} is not supported, default to GENERIC device (WHERE={})", deviceType, where);
178 if (where instanceof WhereZigBee) {
179 thingLabel = "ZigBee " + thingLabel;
181 if (baseMsg != null) {
182 deviceWho = baseMsg.getWho();
186 String ownId = bridgeHandler.ownIdFromWhoWhere(deviceWho, where);
187 if (OpenWebNetBindingConstants.THING_TYPE_BUS_ON_OFF_SWITCH.equals(thingTypeUID)) {
188 if (bridgeHandler.getRegisteredDevice(ownId) != null) {
189 logger.debug("dimmer/switch with WHERE={} already registered, skipping this discovery result", where);
194 String tId = bridgeHandler.thingIdFromWhere(where);
195 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, tId);
197 DiscoveryResult discoveryResult = null;
199 String whereConfig = where.value();
200 if (where instanceof WhereZigBee && WhereZigBee.UNIT_02.equals(((WhereZigBee) where).getUnit())) {
201 logger.debug("UNIT=02 found (WHERE={}) -> will remove previous result if exists", where);
202 thingRemoved(thingUID); // remove previously discovered thing
203 // re-create thingUID with new type
204 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS;
205 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_ON_OFF_SWITCH_2UNITS;
206 thingUID = new ThingUID(thingTypeUID, bridgeUID, tId);
207 whereConfig = ((WhereZigBee) where).valueWithUnit(WhereZigBee.UNIT_ALL); // replace unit '02' with '00'
208 logger.debug("UNIT=02, switching type from {} to {}",
209 OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH,
210 OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS);
212 Map<String, Object> properties = new HashMap<>(2);
213 properties.put(OpenWebNetBindingConstants.CONFIG_PROPERTY_WHERE, whereConfig);
214 properties.put(OpenWebNetBindingConstants.PROPERTY_OWNID, ownId);
215 if (OpenWebNetBindingConstants.THING_TYPE_GENERIC_DEVICE.equals(thingTypeUID)) {
216 thingLabel = thingLabel + " (WHO=" + deviceWho + ", WHERE=" + whereConfig + ")";
218 thingLabel = thingLabel + " (WHERE=" + whereConfig + ")";
220 discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withProperties(properties)
221 .withRepresentationProperty(OpenWebNetBindingConstants.PROPERTY_OWNID).withBridge(bridgeUID)
222 .withLabel(thingLabel).build();
223 thingDiscovered(discoveryResult);
227 public void deactivate() {
232 public void setThingHandler(@Nullable ThingHandler handler) {
233 if (handler instanceof OpenWebNetBridgeHandler) {
234 logger.debug("attaching {} to handler {} ", this, handler);
235 bridgeHandler = (OpenWebNetBridgeHandler) handler;
236 bridgeHandler.deviceDiscoveryService = this;
237 bridgeUID = bridgeHandler.getThing().getUID();
242 public @Nullable ThingHandler getThingHandler() {
243 return bridgeHandler;