]> git.basschouten.com Git - openhab-addons.git/commitdiff
[miio] handle invalid rssi response (#10601)
authorMarcel <marcel@verpaalen.com>
Tue, 27 Apr 2021 20:27:24 +0000 (22:27 +0200)
committerGitHub <noreply@github.com>
Tue, 27 Apr 2021 20:27:24 +0000 (22:27 +0200)
Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoApDTO.java [new file with mode: 0644]
bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoDTO.java
bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java

diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoApDTO.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoApDTO.java
new file mode 100644 (file)
index 0000000..4343be5
--- /dev/null
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2010-2021 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+package org.openhab.binding.miio.internal;
+
+import com.google.gson.JsonPrimitive;
+import com.google.gson.annotations.Expose;
+import com.google.gson.annotations.SerializedName;
+
+/**
+ * Mapping network properties from json for miio info response
+ *
+ * @author Marcel Verpaalen - Initial contribution
+ */
+public class MiIoInfoApDTO {
+    @SerializedName("ssid")
+    @Expose
+    private String ssid;
+    @SerializedName("bssid")
+    @Expose
+    private String bssid;
+    @SerializedName("rssi")
+    @Expose
+    private JsonPrimitive rssi;
+    @SerializedName("wifi_rssi")
+    @Expose
+    private Long wifiRssi;
+    @SerializedName("freq")
+    @Expose
+    private Long freq;
+
+    public String getSsid() {
+        return ssid;
+    }
+
+    public void setSsid(String ssid) {
+        this.ssid = ssid;
+    }
+
+    public String getBssid() {
+        return bssid;
+    }
+
+    public void setBssid(String bssid) {
+        this.bssid = bssid;
+    }
+
+    public Long getRssi() {
+        if (rssi.isNumber()) {
+            return rssi.getAsLong();
+        }
+        return null;
+    }
+
+    public void setRssi(Long rssi) {
+        this.rssi = new JsonPrimitive(rssi);
+    }
+
+    public Long getWifiRssi() {
+        return wifiRssi;
+    }
+
+    public void setWifiRssi(Long wifiRssi) {
+        this.wifiRssi = wifiRssi;
+    }
+
+    public Long getFreq() {
+        return freq;
+    }
+
+    public void setFreq(Long freq) {
+        this.freq = freq;
+    }
+}
index 7696d62acadb72646dfb2350833edddb4c795656..43966106ac32bbfb4fd091ce8f7e0d0a11fd1979 100644 (file)
@@ -45,6 +45,9 @@ public class MiIoInfoDTO {
     @SerializedName("model")
     @Expose
     public String model;
+    @SerializedName("ap")
+    @Expose
+    public MiIoInfoApDTO ap;
     @SerializedName("wifi_fw_ver")
     @Expose
     public String wifiFwVer;
index a19a6d85334c5509023bbb0a85b6020739f7268a..21df3ea3625ef4a13667286117e0c0fc0ca14430 100644 (file)
@@ -32,6 +32,7 @@ import org.openhab.binding.miio.internal.MiIoCommand;
 import org.openhab.binding.miio.internal.MiIoCrypto;
 import org.openhab.binding.miio.internal.MiIoCryptoException;
 import org.openhab.binding.miio.internal.MiIoDevices;
+import org.openhab.binding.miio.internal.MiIoInfoApDTO;
 import org.openhab.binding.miio.internal.MiIoInfoDTO;
 import org.openhab.binding.miio.internal.MiIoMessageListener;
 import org.openhab.binding.miio.internal.MiIoSendCommand;
@@ -58,6 +59,7 @@ import org.slf4j.LoggerFactory;
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
+import com.google.gson.JsonSyntaxException;
 
 /**
  * The {@link MiIoAbstractHandler} is responsible for handling commands, which are
@@ -292,19 +294,30 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
 
     protected boolean updateNetwork(JsonObject networkData) {
         try {
-            updateState(CHANNEL_SSID, new StringType(networkData.getAsJsonObject("ap").get("ssid").getAsString()));
-            updateState(CHANNEL_BSSID, new StringType(networkData.getAsJsonObject("ap").get("bssid").getAsString()));
-            if (networkData.getAsJsonObject("ap").get("rssi") != null) {
-                updateState(CHANNEL_RSSI, new DecimalType(networkData.getAsJsonObject("ap").get("rssi").getAsLong()));
-            } else if (networkData.getAsJsonObject("ap").get("wifi_rssi") != null) {
-                updateState(CHANNEL_RSSI,
-                        new DecimalType(networkData.getAsJsonObject("ap").get("wifi_rssi").getAsLong()));
-            } else {
-                logger.debug("No RSSI info in response");
+            final MiIoInfoDTO miioInfo = GSON.fromJson(networkData, MiIoInfoDTO.class);
+            final MiIoInfoApDTO ap = miioInfo != null ? miioInfo.ap : null;
+            if (miioInfo != null && ap != null) {
+                if (ap.getSsid() != null) {
+                    updateState(CHANNEL_SSID, new StringType(ap.getSsid()));
+                }
+                if (ap.getBssid() != null) {
+                    updateState(CHANNEL_BSSID, new StringType(ap.getBssid()));
+                }
+                if (ap.getRssi() != null) {
+                    updateState(CHANNEL_RSSI, new DecimalType(ap.getRssi()));
+                } else if (ap.getWifiRssi() != null) {
+                    updateState(CHANNEL_RSSI, new DecimalType(ap.getWifiRssi()));
+                } else {
+                    logger.debug("No RSSI info in response");
+                }
+                if (miioInfo.life != null) {
+                    updateState(CHANNEL_LIFE, new DecimalType(miioInfo.life));
+                }
             }
-            updateState(CHANNEL_LIFE, new DecimalType(networkData.get("life").getAsLong()));
             return true;
-        } catch (Exception e) {
+        } catch (NumberFormatException e) {
+            logger.debug("Could not parse number in network response: {}", networkData);
+        } catch (JsonSyntaxException e) {
             logger.debug("Could not parse network response: {}", networkData, e);
         }
         return false;
@@ -420,6 +433,9 @@ public abstract class MiIoAbstractHandler extends BaseThingHandler implements Mi
 
     private void updateProperties(JsonObject miioInfo) {
         final MiIoInfoDTO info = GSON.fromJson(miioInfo, MiIoInfoDTO.class);
+        if (info == null) {
+            return;
+        }
         Map<String, String> properties = editProperties();
         if (info.model != null) {
             properties.put(Thing.PROPERTY_MODEL_ID, info.model);