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 index 000000000..4343be5cc --- /dev/null +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoApDTO.java @@ -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; + } +} diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoDTO.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoDTO.java index 7696d62ac..43966106a 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoDTO.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/MiIoInfoDTO.java @@ -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; diff --git a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java index a19a6d853..21df3ea36 100644 --- a/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java +++ b/bundles/org.openhab.binding.miio/src/main/java/org/openhab/binding/miio/internal/handler/MiIoAbstractHandler.java @@ -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 properties = editProperties(); if (info.model != null) { properties.put(Thing.PROPERTY_MODEL_ID, info.model);