From 8258d9d7226ab5dc3fe75f284bfb130c07c45310 Mon Sep 17 00:00:00 2001 From: Roie Geron Date: Sat, 17 Apr 2021 16:42:37 +0300 Subject: [PATCH] [touchwand] - better handle unit update when currStatus key is null (#10520) * notify listeners on status change using discovery Signed-off-by: Roie Geron * adapt bridge discovery name to new string broadcast Signed-off-by: Roie Geron * check correctly if current status key is not null Signed-off-by: Roie Geron * change log from warn to debug Signed-off-by: Roie Geron * better handle when currStatus is null also move logs from warn to debug Signed-off-by: Roie Geron * remove nonNullByDefault annotation as dto can be null Signed-off-by: Roie Geron --- .../touchwand/internal/TouchWandDimmerHandler.java | 2 +- .../touchwand/internal/TouchWandShutterHandler.java | 2 +- .../touchwand/internal/TouchWandSwitchHandler.java | 2 +- .../internal/TouchWandWallControllerHandler.java | 2 ++ .../TouchWandControllerDiscoveryService.java | 12 +++++++++--- .../discovery/TouchWandUnitDiscoveryService.java | 5 ++--- .../internal/dto/TouchWandShutterSwitchUnitData.java | 2 +- .../dto/TouchWandUnitDataWallController.java | 10 +++++++--- .../internal/dto/TouchWandUnitFromJson.java | 4 ---- 9 files changed, 24 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandDimmerHandler.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandDimmerHandler.java index 4c20541b7..3dfa9a6e7 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandDimmerHandler.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandDimmerHandler.java @@ -56,7 +56,7 @@ public class TouchWandDimmerHandler extends TouchWandBaseUnitHandler { state = new PercentType(convertStatus); updateState(CHANNEL_DIMMER, state); } else { - logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance"); + logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance"); } } } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandShutterHandler.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandShutterHandler.java index 85105d472..c37da2100 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandShutterHandler.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandShutterHandler.java @@ -66,7 +66,7 @@ public class TouchWandShutterHandler extends TouchWandBaseUnitHandler { state = new PercentType(convertStatus); updateState(CHANNEL_SHUTTER, state); } else { - logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance"); + logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance"); } } } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandSwitchHandler.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandSwitchHandler.java index 67c3e76d0..4d8fc5233 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandSwitchHandler.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandSwitchHandler.java @@ -52,7 +52,7 @@ public class TouchWandSwitchHandler extends TouchWandBaseUnitHandler { } updateState(CHANNEL_SWITCH, state); } else { - logger.warn("updateTouchWandUnitState incompatible TouchWandUnitData instance"); + logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance"); } } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandWallControllerHandler.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandWallControllerHandler.java index 3d2da4820..4c0ba772e 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandWallControllerHandler.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandWallControllerHandler.java @@ -58,6 +58,8 @@ public class TouchWandWallControllerHandler extends TouchWandBaseUnitHandler { triggerChannel(CHANNEL_WALLCONTROLLER_ACTION, action); } timeLastEventMs = status.getTs(); + } else { + logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance"); } } } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandControllerDiscoveryService.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandControllerDiscoveryService.java index e5acc5cf8..b74d13e58 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandControllerDiscoveryService.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandControllerDiscoveryService.java @@ -35,6 +35,10 @@ import org.osgi.service.component.annotations.Component; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; + /** * The {@link TouchWandControllerDiscoveryService} Discovery service for Touchwand Controllers. * @@ -132,13 +136,15 @@ public class TouchWandControllerDiscoveryService extends AbstractDiscoveryServic mySocket.receive(datagram); InetAddress address = datagram.getAddress(); String sentence = new String(dgram.getData(), 0, dgram.getLength(), StandardCharsets.US_ASCII); - addDeviceDiscoveryResult(sentence, address.getHostAddress().toString()); + JsonObject bridge = JsonParser.parseString(sentence).getAsJsonObject();// + String name = bridge.get("name").getAsString(); + addDeviceDiscoveryResult(name, address.getHostAddress().toString()); logger.debug("Received Datagram from {}:{} on Port {} message {}", address.getHostAddress(), dgram.getPort(), mySocket.getLocalPort(), sentence); } - } catch (IOException e) { + } catch (IOException | JsonSyntaxException e) { if (!isInterrupted()) { - logger.warn("Error while receiving {}", e.getMessage()); + logger.debug("Error while receiving {}", e.getMessage()); } else { logger.debug("Receiver thread was interrupted {}", e.getMessage()); } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandUnitDiscoveryService.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandUnitDiscoveryService.java index 1d7f1d80a..e2eff422d 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandUnitDiscoveryService.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandUnitDiscoveryService.java @@ -71,7 +71,7 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService @Override protected void startScan() { if (touchWandBridgeHandler.getThing().getStatus() != ThingStatus.ONLINE) { - logger.warn("Could not scan units while bridge offline"); + logger.debug("Could not scan units while bridge offline"); return; } @@ -105,11 +105,9 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService break; case TYPE_SWITCH: addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SWITCH); - notifyListeners(touchWandUnit); break; case TYPE_DIMMER: addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_DIMMER); - notifyListeners(touchWandUnit); break; case TYPE_SHUTTER: addDeviceDiscoveryResult(touchWandUnit, THING_TYPE_SHUTTER); @@ -120,6 +118,7 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService default: continue; } + notifyListeners(touchWandUnit); } } catch (JsonSyntaxException e) { logger.warn("Could not parse unit {}", e.getMessage()); diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandShutterSwitchUnitData.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandShutterSwitchUnitData.java index cea6d5b56..e4678bb53 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandShutterSwitchUnitData.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandShutterSwitchUnitData.java @@ -22,7 +22,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; @NonNullByDefault public class TouchWandShutterSwitchUnitData extends TouchWandUnitData { - private Integer currStatus = 0; + private int currStatus = 0; @Override public Integer getCurrStatus() { diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitDataWallController.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitDataWallController.java index a60451e9e..288aeef3a 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitDataWallController.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitDataWallController.java @@ -12,21 +12,25 @@ */ package org.openhab.binding.touchwand.internal.dto; -import org.eclipse.jdt.annotation.NonNullByDefault; - /** * The {@link TouchWandUnitDataWallController} implements WallController unit * property. * * @author Roie Geron - Initial contribution */ -@NonNullByDefault public class TouchWandUnitDataWallController extends TouchWandUnitData { private CurrStatus currStatus = new CurrStatus(); + // currStatus can be null since the object is created by gson fromJson + // in case the key is null or not exist , the variable will be null. + // if this is the case , default status is created + @Override public Csc getCurrStatus() { + if (currStatus == null) { + currStatus = new CurrStatus(); + } return currStatus.getCsc(); } diff --git a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitFromJson.java b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitFromJson.java index b0563c5e1..a904d5138 100644 --- a/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitFromJson.java +++ b/bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitFromJson.java @@ -47,10 +47,6 @@ public class TouchWandUnitFromJson { type = TYPE_UNKNOWN; } - if (!jsonUnit.has("currStatus") || (jsonUnit.get("currStatus") == null)) { - type = TYPE_UNKNOWN; - } - switch (type) { case TYPE_WALLCONTROLLER: touchWandUnit = gson.fromJson(jsonUnit, TouchWandUnitDataWallController.class);