[miio] handle invalid rssi response (#10601)

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
This commit is contained in:
Marcel 2021-04-27 22:27:24 +02:00 committed by GitHub
parent 7d13795380
commit 52b05085aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 11 deletions

View File

@ -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;
}
}

View 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;

View 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);