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.handler;
15 import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants.*;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
21 import java.util.Objects;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
30 import org.openhab.binding.openwebnet.internal.discovery.OpenWebNetDeviceDiscoveryService;
31 import org.openhab.binding.openwebnet.internal.handler.config.OpenWebNetBusBridgeConfig;
32 import org.openhab.binding.openwebnet.internal.handler.config.OpenWebNetZigBeeBridgeConfig;
33 import org.openhab.core.config.core.status.ConfigStatusMessage;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.ChannelUID;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingStatusDetail;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.binding.ConfigStatusBridgeHandler;
41 import org.openhab.core.thing.binding.ThingHandlerService;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.RefreshType;
44 import org.openwebnet4j.BUSGateway;
45 import org.openwebnet4j.GatewayListener;
46 import org.openwebnet4j.OpenDeviceType;
47 import org.openwebnet4j.OpenGateway;
48 import org.openwebnet4j.USBGateway;
49 import org.openwebnet4j.communication.OWNAuthException;
50 import org.openwebnet4j.communication.OWNException;
51 import org.openwebnet4j.message.Automation;
52 import org.openwebnet4j.message.Auxiliary;
53 import org.openwebnet4j.message.BaseOpenMessage;
54 import org.openwebnet4j.message.CEN;
55 import org.openwebnet4j.message.EnergyManagement;
56 import org.openwebnet4j.message.FrameException;
57 import org.openwebnet4j.message.GatewayMgmt;
58 import org.openwebnet4j.message.Lighting;
59 import org.openwebnet4j.message.OpenMessage;
60 import org.openwebnet4j.message.Scenario;
61 import org.openwebnet4j.message.Thermoregulation;
62 import org.openwebnet4j.message.What;
63 import org.openwebnet4j.message.Where;
64 import org.openwebnet4j.message.WhereZigBee;
65 import org.openwebnet4j.message.Who;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
70 * The {@link OpenWebNetBridgeHandler} is responsible for handling communication with gateways and handling events.
72 * @author Massimo Valla - Initial contribution, Lighting, Automation, Scenario
73 * @author Andrea Conte - Energy management, Thermoregulation
74 * @author Gilberto Cocchi - Thermoregulation
75 * @author Giovanni Fabiani - Aux
78 public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implements GatewayListener {
80 private final Logger logger = LoggerFactory.getLogger(OpenWebNetBridgeHandler.class);
82 private static final int GATEWAY_ONLINE_TIMEOUT_SEC = 20; // Time to wait for the gateway to become connected
84 private static final int REFRESH_ALL_DEVICES_DELAY_MSEC = 500; // Delay to wait before trying again another all
85 // devices refresh request after a connect/reconnect
86 private static final int REFRESH_ALL_DEVICES_DELAY_MAX_MSEC = 15000; // Maximum delay to wait for all devices
87 // refresh after a connect/reconnect
89 private static final int REFRESH_ALL_CHECK_DELAY_SEC = 20; // Delay to wait to check which devices are
92 private long lastRegisteredDeviceTS = -1; // timestamp when the last device has been associated to the bridge
93 private long refreshAllDevicesDelay = 0; // delay waited before starting all devices refresh
95 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.BRIDGE_SUPPORTED_THING_TYPES;
97 // ConcurrentHashMap of devices registered to this BridgeHandler
98 // association is: ownId (String) -> OpenWebNetThingHandler, with ownId = WHO.WHERE
99 private Map<String, @Nullable OpenWebNetThingHandler> registeredDevices = new ConcurrentHashMap<>();
100 private Map<String, Long> discoveringDevices = new ConcurrentHashMap<>();
102 protected @Nullable OpenGateway gateway;
103 private boolean isBusGateway = false;
105 private boolean isGatewayConnected = false;
107 public @Nullable OpenWebNetDeviceDiscoveryService deviceDiscoveryService;
108 private boolean reconnecting = false; // we are trying to reconnect to gateway
109 private @Nullable ScheduledFuture<?> refreshAllSchedule;
110 private @Nullable ScheduledFuture<?> connectSchedule;
112 private boolean scanIsActive = false; // a device scan has been activated by OpenWebNetDeviceDiscoveryService;
113 private boolean discoveryByActivation;
115 public OpenWebNetBridgeHandler(Bridge bridge) {
119 public boolean isBusGateway() {
124 public void initialize() {
125 ThingTypeUID thingType = getThing().getThingTypeUID();
127 if (thingType.equals(THING_TYPE_ZB_GATEWAY)) {
128 gw = initZigBeeGateway();
130 gw = initBusGateway();
136 if (gw.isConnected()) { // gateway is already connected, device can go ONLINE
137 isGatewayConnected = true;
138 updateStatus(ThingStatus.ONLINE);
140 updateStatus(ThingStatus.UNKNOWN);
141 logger.debug("Trying to connect gateway {}... ", gw);
144 connectSchedule = scheduler.schedule(() -> {
145 // if status is still UNKNOWN after timer ends, set the device OFFLINE
146 if (thing.getStatus().equals(ThingStatus.UNKNOWN)) {
147 logger.info("status still UNKNOWN. Setting device={} to OFFLINE", thing.getUID());
148 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
149 "@text/offline.comm-error-timeout");
151 }, GATEWAY_ONLINE_TIMEOUT_SEC, TimeUnit.SECONDS);
152 logger.debug("bridge {} initialization completed", thing.getUID());
153 } catch (OWNException e) {
154 logger.debug("gw.connect() returned OWNException: {}", e.getMessage());
155 // status is updated by callback onConnectionError()
162 * Init a ZigBee gateway based on config
164 private @Nullable OpenGateway initZigBeeGateway() {
165 logger.debug("Initializing ZigBee USB Gateway");
166 OpenWebNetZigBeeBridgeConfig zbBridgeConfig = getConfigAs(OpenWebNetZigBeeBridgeConfig.class);
167 String serialPort = zbBridgeConfig.getSerialPort();
168 if (serialPort == null || serialPort.isEmpty()) {
169 logger.warn("Cannot connect ZigBee USB Gateway. No serial port has been provided in Bridge configuration.");
170 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
171 "@text/offline.conf-error-no-serial-port");
174 return new USBGateway(serialPort);
179 * Init a BUS gateway based on config
181 private @Nullable OpenGateway initBusGateway() {
182 logger.debug("Initializing BUS gateway");
184 OpenWebNetBusBridgeConfig busBridgeConfig = getConfigAs(OpenWebNetBusBridgeConfig.class);
185 String host = busBridgeConfig.getHost();
186 if (host == null || host.isEmpty()) {
187 logger.warn("Cannot connect to BUS Gateway. No host/IP has been provided in Bridge configuration.");
188 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
189 "@text/offline.conf-error-no-ip-address");
192 int port = busBridgeConfig.getPort().intValue();
193 String passwd = busBridgeConfig.getPasswd();
195 if (passwd.length() >= 4) {
196 passwdMasked = "******" + passwd.substring(passwd.length() - 3, passwd.length());
198 passwdMasked = "******";
200 discoveryByActivation = busBridgeConfig.getDiscoveryByActivation();
201 logger.debug("Creating new BUS gateway with config properties: {}:{}, pwd={}, discoveryByActivation={}",
202 host, port, passwdMasked, discoveryByActivation);
203 return new BUSGateway(host, port, passwd);
208 public void handleCommand(ChannelUID channelUID, Command command) {
209 logger.debug("handleCommand (command={} - channel={})", command, channelUID);
210 OpenGateway gw = gateway;
211 if (gw == null || !gw.isConnected()) {
212 logger.warn("Gateway is NOT connected, skipping command");
215 if (command instanceof RefreshType) {
216 refreshAllBridgeDevices();
218 logger.warn("Command or channel not supported: channel={} command={}", channelUID, command);
224 public Collection<ConfigStatusMessage> getConfigStatus() {
225 return Collections.emptyList();
229 public void handleRemoval() {
231 super.handleRemoval();
235 public void dispose() {
236 ScheduledFuture<?> rSc = refreshAllSchedule;
240 ScheduledFuture<?> cs = connectSchedule;
248 private void disconnectGateway() {
249 OpenGateway gw = gateway;
251 gw.closeConnection();
252 gw.unsubscribe(this);
253 logger.debug("Gateway {} connection closed and unsubscribed", gw.toString());
256 reconnecting = false;
260 public Collection<Class<? extends ThingHandlerService>> getServices() {
261 return Collections.singleton(OpenWebNetDeviceDiscoveryService.class);
265 * Search for devices connected to this bridge handler's gateway
267 * @param listener to receive device found notifications
269 public synchronized void searchDevices() {
271 logger.debug("------$$ scanIsActive={}", scanIsActive);
272 OpenGateway gw = gateway;
274 if (!gw.isDiscovering()) {
275 if (!gw.isConnected()) {
276 logger.debug("------$$ Gateway '{}' is NOT connected, cannot search for devices", gw);
279 logger.info("------$$ STARTED active SEARCH for devices on bridge '{}'", thing.getUID());
281 gw.discoverDevices();
282 } catch (OWNException e) {
283 logger.warn("------$$ OWNException while discovering devices on bridge '{}': {}", thing.getUID(),
287 logger.debug("------$$ Searching devices on bridge '{}' already activated", thing.getUID());
291 logger.warn("------$$ Cannot search devices: no gateway associated to this handler");
296 public void onNewDevice(@Nullable Where w, @Nullable OpenDeviceType deviceType, @Nullable BaseOpenMessage message) {
297 OpenWebNetDeviceDiscoveryService discService = deviceDiscoveryService;
298 if (discService != null) {
299 if (w != null && deviceType != null) {
300 discService.newDiscoveryResult(w, deviceType, message);
302 logger.warn("onNewDevice with null where/deviceType, msg={}", message);
305 logger.warn("onNewDevice but null deviceDiscoveryService");
310 public void onDiscoveryCompleted() {
311 logger.info("------$$ FINISHED active SEARCH for devices on bridge '{}'", thing.getUID());
315 * Notifies that the scan has been stopped/aborted by OpenWebNetDeviceDiscoveryService
317 public void scanStopped() {
318 scanIsActive = false;
319 logger.debug("------$$ scanIsActive={}", scanIsActive);
322 private void discoverByActivation(BaseOpenMessage baseMsg) {
323 logger.debug("discoverByActivation: msg={}", baseMsg);
324 OpenWebNetDeviceDiscoveryService discService = deviceDiscoveryService;
325 if (discService == null) {
326 logger.warn("discoverByActivation: null OpenWebNetDeviceDiscoveryService, ignoring msg={}", baseMsg);
329 // we support these types only
330 if (baseMsg instanceof Lighting || baseMsg instanceof Automation || baseMsg instanceof EnergyManagement
331 || baseMsg instanceof Thermoregulation || baseMsg instanceof CEN || baseMsg instanceof Scenario) {
332 BaseOpenMessage bmsg = baseMsg;
333 if (baseMsg instanceof Lighting) {
334 What what = baseMsg.getWhat();
335 if (Lighting.WhatLighting.OFF.equals(what)) { // skipping OFF msg: cannot distinguish dimmer/switch
336 logger.debug("discoverByActivation: skipping OFF msg: cannot distinguish dimmer/switch");
339 if (Lighting.WhatLighting.ON.equals(what)) { // if not already done just now, request light status to
340 // distinguish dimmer from switch
341 if (discoveringDevices.containsKey(ownIdFromMessage(baseMsg))) {
343 "discoverByActivation: we just requested status for this device and it's ON -> it's a switch");
345 OpenGateway gw = gateway;
348 discoveringDevices.put(ownIdFromMessage(baseMsg),
349 Long.valueOf(System.currentTimeMillis()));
350 gw.send(Lighting.requestStatus(baseMsg.getWhere().value()));
352 } catch (OWNException e) {
353 logger.warn("discoverByActivation: Exception while requesting light state: {}",
360 discoveringDevices.remove(ownIdFromMessage(baseMsg));
362 OpenDeviceType type = null;
364 type = bmsg.detectDeviceType();
365 } catch (FrameException e) {
366 logger.warn("Exception while detecting device type: {}", e.getMessage());
369 discService.newDiscoveryResult(bmsg.getWhere(), type, bmsg);
371 logger.debug("discoverByActivation: no device type detected from msg: {}", bmsg);
377 * Register a device ThingHandler to this BridgHandler
379 * @param ownId the device OpenWebNet id
380 * @param thingHandler the thing handler to be registered
382 protected void registerDevice(String ownId, OpenWebNetThingHandler thingHandler) {
383 if (registeredDevices.containsKey(ownId)) {
384 logger.warn("registering device with an existing ownId={}", ownId);
386 registeredDevices.put(ownId, thingHandler);
387 lastRegisteredDeviceTS = System.currentTimeMillis();
388 logger.debug("registered device ownId={}, thing={}", ownId, thingHandler.getThing().getUID());
392 * Un-register a device from this bridge handler
394 * @param ownId the device OpenWebNet id
396 protected void unregisterDevice(String ownId) {
397 if (registeredDevices.remove(ownId) != null) {
398 logger.debug("un-registered device ownId={}", ownId);
400 logger.warn("could not un-register ownId={} (not found)", ownId);
405 * Get an already registered device on this bridge handler
407 * @param ownId the device OpenWebNet id
408 * @return the registered device Thing handler or null if the id cannot be found
410 public @Nullable OpenWebNetThingHandler getRegisteredDevice(String ownId) {
411 return registeredDevices.get(ownId);
414 private void refreshAllBridgeDevices() {
415 logger.debug("--- --- ABOUT TO REFRESH ALL devices for bridge {}", thing.getUID());
417 final List<Thing> things = getThing().getThings();
418 int total = things.size();
419 logger.debug("--- FOUND {} things by getThings()", total);
421 if (registeredDevices.isEmpty()) { // no registered device yet
422 if (refreshAllDevicesDelay < REFRESH_ALL_DEVICES_DELAY_MAX_MSEC) {
423 logger.debug("--- REGISTER device not started yet... re-scheduling refreshAllBridgeDevices()");
424 refreshAllDevicesDelay += REFRESH_ALL_DEVICES_DELAY_MSEC * 3;
425 refreshAllSchedule = scheduler.schedule(this::refreshAllBridgeDevices,
426 REFRESH_ALL_DEVICES_DELAY_MSEC * 3, TimeUnit.MILLISECONDS);
430 "--- --- NONE OF {} CHILD DEVICE(S) has REGISTERED with bridge {}: check Things configuration (stopping refreshAllBridgeDevices)",
431 total, thing.getUID());
432 refreshAllDevicesDelay = 0;
435 } else if (System.currentTimeMillis() - lastRegisteredDeviceTS < REFRESH_ALL_DEVICES_DELAY_MSEC) {
436 // a device has been registered with the bridge just now, let's wait for other devices: re-schedule
438 logger.debug("--- REGISTER device just called... re-scheduling refreshAllBridgeDevices()");
439 refreshAllSchedule = scheduler.schedule(this::refreshAllBridgeDevices, REFRESH_ALL_DEVICES_DELAY_MSEC,
440 TimeUnit.MILLISECONDS);
443 for (Thing ownThing : things) {
444 OpenWebNetThingHandler hndlr = (OpenWebNetThingHandler) ownThing.getHandler();
447 logger.debug("--- REFRESHING ALL DEVICES FOR thing #{}/{}: {}", howMany, total, ownThing.getUID());
448 hndlr.refreshAllDevices();
450 logger.warn("--- No handler for thing {}", ownThing.getUID());
453 logger.debug("--- --- COMPLETED REFRESH all devices for bridge {}", thing.getUID());
454 refreshAllDevicesDelay = 0;
455 // set a check that all things are Online
456 refreshAllSchedule = scheduler.schedule(() -> checkAllRefreshed(things), REFRESH_ALL_CHECK_DELAY_SEC,
459 logger.debug("--- --- NO CHILD DEVICE to REFRESH for bridge {}", thing.getUID());
463 private void checkAllRefreshed(List<Thing> things) {
465 int total = things.size();
466 boolean allOnline = true;
467 for (Thing ownThing : things) {
469 ThingStatus ts = ownThing.getStatus();
470 if (ThingStatus.ONLINE == ts) {
471 logger.debug("--- CHECKED ONLINE thing #{}/{}: {}", howMany, total, ownThing.getUID());
473 logger.debug("--- CHECKED ^^^OFFLINE^^^ thing #{}/{}: {}", howMany, total, ownThing.getUID());
478 logger.debug("--- --- REFRESH CHECK COMPLETED: all things ONLINE for bridge {}", thing.getUID());
480 logger.debug("--- --- REFRESH CHECK COMPLETED: NOT all things ONLINE for bridge {}", thing.getUID());
485 public void onEventMessage(@Nullable OpenMessage msg) {
486 logger.trace("RECEIVED <<<<< {}", msg);
488 logger.warn("received event msg is null");
491 if (msg.isACK() || msg.isNACK()) {
492 return; // we ignore ACKS/NACKS
494 // GATEWAY MANAGEMENT
495 if (msg instanceof GatewayMgmt) {
500 BaseOpenMessage baseMsg = (BaseOpenMessage) msg;
501 // let's try to get the Thing associated with this message...
502 if (baseMsg instanceof Lighting || baseMsg instanceof Automation || baseMsg instanceof EnergyManagement
503 || baseMsg instanceof Thermoregulation || baseMsg instanceof CEN || baseMsg instanceof Auxiliary
504 || baseMsg instanceof Scenario) {
505 String ownId = ownIdFromMessage(baseMsg);
506 logger.debug("ownIdFromMessage({}) --> {}", baseMsg, ownId);
507 OpenWebNetThingHandler deviceHandler = registeredDevices.get(ownId);
508 if (deviceHandler == null) {
509 OpenGateway gw = gateway;
510 if (isBusGateway && ((gw != null && !gw.isDiscovering() && scanIsActive)
511 || (discoveryByActivation && !scanIsActive))) {
512 discoverByActivation(baseMsg);
514 logger.debug("ownId={} has NO DEVICE associated, ignoring it", ownId);
517 deviceHandler.handleMessage(baseMsg);
520 logger.debug("BridgeHandler ignoring frame {}. WHO={} is not supported by this binding", baseMsg,
526 public void onConnected() {
527 isGatewayConnected = true;
528 Map<String, String> properties = editProperties();
529 boolean propertiesChanged = false;
530 OpenGateway gw = gateway;
532 logger.warn("received onConnected() but gateway is null");
535 if (gw instanceof USBGateway) {
536 logger.info("---- CONNECTED to ZigBee USB gateway bridge '{}' (serialPort: {})", thing.getUID(),
537 ((USBGateway) gw).getSerialPortName());
539 logger.info("---- CONNECTED to BUS gateway bridge '{}' ({}:{})", thing.getUID(),
540 ((BUSGateway) gw).getHost(), ((BUSGateway) gw).getPort());
541 // update serial number property (with MAC address)
542 if (!Objects.equals(properties.get(PROPERTY_SERIAL_NO), gw.getMACAddr().toUpperCase())) {
543 properties.put(PROPERTY_SERIAL_NO, gw.getMACAddr().toUpperCase());
544 propertiesChanged = true;
545 logger.debug("updated property gw serialNumber: {}", properties.get(PROPERTY_SERIAL_NO));
548 if (!Objects.equals(properties.get(PROPERTY_FIRMWARE_VERSION), gw.getFirmwareVersion())) {
549 properties.put(PROPERTY_FIRMWARE_VERSION, gw.getFirmwareVersion());
550 propertiesChanged = true;
551 logger.debug("updated property gw firmware version: {}", properties.get(PROPERTY_FIRMWARE_VERSION));
553 if (propertiesChanged) {
554 updateProperties(properties);
555 logger.info("properties updated for bridge '{}'", thing.getUID());
557 updateStatus(ThingStatus.ONLINE);
558 // schedule a refresh for all devices
559 refreshAllSchedule = scheduler.schedule(this::refreshAllBridgeDevices, REFRESH_ALL_DEVICES_DELAY_MSEC,
560 TimeUnit.MILLISECONDS);
564 public void onConnectionError(@Nullable OWNException error) {
567 errMsg = "unknown error";
569 errMsg = error.getMessage();
571 logger.info("---- ON CONNECTION ERROR for gateway {}: {}", gateway, errMsg);
572 isGatewayConnected = false;
573 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
574 "@text/offline.comm-error-connection" + " (onConnectionError - " + errMsg + ")");
575 tryReconnectGateway();
579 public void onConnectionClosed() {
580 isGatewayConnected = false;
581 logger.debug("onConnectionClosed() - isGatewayConnected={}", isGatewayConnected);
582 // NOTE: cannot change to OFFLINE here because we are already in REMOVING state
586 public void onDisconnected(@Nullable OWNException e) {
587 isGatewayConnected = false;
590 errMsg = "unknown error";
592 errMsg = e.getMessage();
594 logger.info("---- DISCONNECTED from gateway {}. OWNException: {}", gateway, errMsg);
595 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
596 "@text/offline.comm-error-disconnected" + " (onDisconnected - " + errMsg + ")");
597 tryReconnectGateway();
600 private void tryReconnectGateway() {
601 OpenGateway gw = gateway;
605 logger.info("---- Starting RECONNECT cycle to gateway {}", gw);
608 } catch (OWNAuthException e) {
609 logger.info("---- AUTH error from gateway. Stopping re-connect");
610 reconnecting = false;
611 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR,
612 "@text/offline.conf-error-auth" + " (" + e + ")");
615 logger.debug("---- already reconnecting");
618 logger.warn("---- cannot start RECONNECT, gateway is null");
623 public void onReconnected() {
624 reconnecting = false;
625 OpenGateway gw = gateway;
626 logger.info("---- RE-CONNECTED to bridge {}", thing.getUID());
628 updateStatus(ThingStatus.ONLINE);
629 if (gw.getFirmwareVersion() != null) {
630 this.updateProperty(PROPERTY_FIRMWARE_VERSION, gw.getFirmwareVersion());
631 logger.debug("gw firmware version: {}", gw.getFirmwareVersion());
633 // schedule a refresh for all devices
634 refreshAllSchedule = scheduler.schedule(this::refreshAllBridgeDevices, REFRESH_ALL_DEVICES_DELAY_MSEC,
635 TimeUnit.MILLISECONDS);
640 * Return a ownId string (=WHO.WHERE) from the device Where address and handler
642 * @param where the Where address (to be normalized)
643 * @param handler the device handler
644 * @return the ownId String
646 protected String ownIdFromDeviceWhere(Where where, OpenWebNetThingHandler handler) {
647 return handler.ownIdPrefix() + "." + normalizeWhere(where);
651 * Returns a ownId string (=WHO.WHERE) from a Who and Where address
654 * @param where the Where address (to be normalized)
655 * @return the ownId String
657 public String ownIdFromWhoWhere(Who who, Where where) {
658 return who.value() + "." + normalizeWhere(where);
662 * Return a ownId string (=WHO.WHERE) from a BaseOpenMessage
664 * @param baseMsg the BaseOpenMessage
665 * @return the ownId String
667 public String ownIdFromMessage(BaseOpenMessage baseMsg) {
668 return baseMsg.getWho().value() + "." + normalizeWhere(baseMsg.getWhere());
672 * Transform a Where address into a Thing id string
674 * @param where the Where address
675 * @return the thing Id string
677 public String thingIdFromWhere(Where where) {
678 return normalizeWhere(where); // '#' cannot be used in ThingUID;
682 * Normalize a Where address to generate ownId and Thing id
684 * @param where the Where address
685 * @return the normalized address as String
687 public String normalizeWhere(Where where) {
688 String str = where.value();
689 if (where instanceof WhereZigBee) {
690 str = ((WhereZigBee) where).valueWithUnit(WhereZigBee.UNIT_ALL); // 76543210X#9 --> 765432100#9
692 if (str.indexOf("#4#") == -1) { // skip APL#4#bus case
693 if (str.indexOf('#') == 0) { // Thermo central unit (#0) or zone via central unit (#Z, Z=[1-99]) --> Z
694 str = str.substring(1);
695 } else if (str.indexOf('#') > 0) { // Thermo zone Z and actuator N (Z#N, Z=[1-99], N=[1-9]) --> Z
696 str = str.substring(0, str.indexOf('#'));
700 return str.replace('#', 'h');