]> git.basschouten.com Git - openhab-addons.git/commitdiff
[touchwand] - better handle unit update when currStatus key is null (#10520)
authorRoie Geron <roie.geron@gmail.com>
Sat, 17 Apr 2021 13:42:37 +0000 (16:42 +0300)
committerGitHub <noreply@github.com>
Sat, 17 Apr 2021 13:42:37 +0000 (15:42 +0200)
* notify listeners on status change using discovery

Signed-off-by: Roie Geron <roie.geron@gmail.com>
* adapt bridge discovery name to new string broadcast

Signed-off-by: Roie Geron <roie.geron@gmail.com>
* check correctly if current status key is not null

Signed-off-by: Roie Geron <roie.geron@gmail.com>
* change log from warn to debug

Signed-off-by: Roie Geron <roie.geron@gmail.com>
* better handle when currStatus is null

also move logs from warn to debug

Signed-off-by: Roie Geron <roie.geron@gmail.com>
* remove nonNullByDefault annotation

as dto can be null

Signed-off-by: Roie Geron <roie.geron@gmail.com>
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandDimmerHandler.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandShutterHandler.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandSwitchHandler.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/TouchWandWallControllerHandler.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandControllerDiscoveryService.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/discovery/TouchWandUnitDiscoveryService.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandShutterSwitchUnitData.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitDataWallController.java
bundles/org.openhab.binding.touchwand/src/main/java/org/openhab/binding/touchwand/internal/dto/TouchWandUnitFromJson.java

index 4c20541b7e777443b6a5d4e972a9a0ace75e243f..3dfa9a6e7081b3274aedbb0b4dd64342fdc908e4 100644 (file)
@@ -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");
         }
     }
 }
index 85105d472b02ec2b6f442b21bcd17e4043751383..c37da2100eb1a839399f9eec153201651b732916 100644 (file)
@@ -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");
         }
     }
 }
index 67c3e76d05bd010a0ea8635f51ee1b86cb16da2c..4d8fc52333fc3ceaa69a1bc6e7cbd35e5a08b348 100644 (file)
@@ -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");
         }
     }
 
index 3d2da4820b9f2c690464e74446056d54ba84a2d3..4c0ba772ec1884c2e32f5878a9187d8e044a168c 100644 (file)
@@ -58,6 +58,8 @@ public class TouchWandWallControllerHandler extends TouchWandBaseUnitHandler {
                 triggerChannel(CHANNEL_WALLCONTROLLER_ACTION, action);
             }
             timeLastEventMs = status.getTs();
+        } else {
+            logger.debug("updateTouchWandUnitState incompatible TouchWandUnitData instance");
         }
     }
 }
index e5acc5cf8f086f8947479b67a746d0ac75675f46..b74d13e58e802f70eec77ea9263dc2a6d60b86d6 100644 (file)
@@ -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());
                 }
index 1d7f1d80a9a8b7acc36685e3f8c853fbbec8e330..e2eff422da6b56332eb3009d4e5241fce18267bc 100644 (file)
@@ -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());
index cea6d5b56263e2367b5ea549a21b60d0f977867d..e4678bb53a92f4f2b18bccbad78d399a92b9843a 100644 (file)
@@ -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() {
index a60451e9e206764f6b4014e51c51f46123c8dcf8..288aeef3af0f6e5692376f0548aefb6dd8fc5c05 100644 (file)
  */
 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();
     }
 
index b0563c5e1c09bcba4c5d7dcc1ad2bbc7967a6fd3..a904d513827bd20b8688a16ac24ba972dbd6cb8c 100644 (file)
@@ -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);