Skip to content

Commit

Permalink
[miio] Save last msgId and misc minor updates (openhab#11464)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
  • Loading branch information
marcelrv authored Oct 30, 2021
1 parent fdd55a1 commit 2b53a00
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum MiIoCommand {
// Basic device commands
GET_PROPERTY("get_prop"),
GET_PROPERTIES("get_properties"),
GET_DEVICE_PROPERTY_EXP("get_device_prop_exp"),
GET_DEVICE_PROPERTY("get_device_prop"),
GET_VALUE("get_value"),
SET_PROPERTIES("set_properties"),
SET_MODE_BASIC("set_mode"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.openhab.binding.miio.internal;

import static org.openhab.core.library.unit.MetricPrefix.MILLI;
import static org.openhab.core.library.unit.MetricPrefix.*;

import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -39,7 +39,8 @@ public enum MiIoQuantiyTypes {
CELSIUS(SIUnits.CELSIUS, "C", "celcius"),
FAHRENHEIT(ImperialUnits.FAHRENHEIT),
KELVIN(Units.KELVIN, "K"),
PASCAL(SIUnits.PASCAL),
PASCAL(SIUnits.PASCAL, "pa"),
HPA(HECTO(SIUnits.PASCAL)),
SECOND(Units.SECOND, "seconds"),
MINUTE(Units.MINUTE, "minutes"),
HOUR(Units.HOUR, "hours"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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 org.eclipse.jdt.annotation.NonNullByDefault;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
* Storing last id to to be able to reconnect better after binding restart
*
* @author Marcel Verpaalen - Initial contribution
*/
@NonNullByDefault
public class SavedDeviceInfoDTO {

@SerializedName("lastId")
@Expose
private int lastId = 0;
@SerializedName("deviceId")
@Expose
private String deviceId;

public SavedDeviceInfoDTO(int lastId, String deviceId) {
super();
this.lastId = lastId;
this.deviceId = deviceId;
}

public int getLastId() {
return lastId;
}

public void setLastId(int lastId) {
this.lastId = lastId;
}

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
package org.openhab.binding.miio.internal;

import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_USERDATA_PATH;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -21,6 +25,7 @@
import java.nio.file.NoSuchFileException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.slf4j.Logger;

import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
Expand Down Expand Up @@ -106,6 +111,27 @@ public static JsonObject convertFileToJSON(URL fileName) throws JsonIOException,
}
}

/**
* Saves string to file in userdata folder
*
* @param filename
* @param string with content
* @param logger
*/
public static void saveToFile(String filename, String data, Logger logger) {
File folder = new File(BINDING_USERDATA_PATH);
if (!folder.exists()) {
folder.mkdirs();
}
File dataFile = new File(folder, filename);
try (FileWriter writer = new FileWriter(dataFile)) {
writer.write(data);
logger.debug("Saved to {}", dataFile.getAbsolutePath());
} catch (IOException e) {
logger.debug("Failed to write file '{}': {}", dataFile.getName(), e.getMessage());
}
}

public static String minLengthString(String string, int length) {
return String.format("%-" + length + "s", string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public enum CommandParameterType {
ONOFFBOOL("onoffbool"),
ONOFFBOOLSTRING("onoffboolstring"),
ONOFFNUMBER("onoffnumber"),
OPENCLOSENUMBER("openclosenumber"),
OPENCLOSE("openclose"),
STRING("string"),
CUSTOMSTRING("customstring"),
NUMBER("number"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public String sendCloudCommand(String urlPart, String country, String parameters
if (cl == null || !isConnected()) {
throw new MiCloudException("Cannot execute request. Cloud service not available");
}
return cl.request(urlPart, country, parameters);
return cl.request(urlPart.startsWith("/") ? urlPart : "/" + urlPart, country, parameters);
}

public @Nullable RawType getMap(String mapId, String country) throws MiCloudException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public List<CloudDeviceDTO> getDevices(String country) {
public String getDeviceString(String country) {
String resp;
try {
resp = request("/home/device_list_page", country, "{\"getVirtualModel\":false,\"getHuamiDevices\":1}");
resp = request("/home/device_list_page", country, "{\"getVirtualModel\":true,\"getHuamiDevices\":1}");
logger.trace("Get devices response: {}", resp);
if (resp.length() > 2) {
CloudUtil.saveDeviceInfoFile(resp, country, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.HashMap;
Expand All @@ -39,6 +42,7 @@
import org.openhab.binding.miio.internal.MiIoInfoDTO;
import org.openhab.binding.miio.internal.MiIoMessageListener;
import org.openhab.binding.miio.internal.MiIoSendCommand;
import org.openhab.binding.miio.internal.SavedDeviceInfoDTO;
import org.openhab.binding.miio.internal.Utils;
import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
import org.openhab.binding.miio.internal.cloud.CloudConnector;
Expand All @@ -62,6 +66,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;

Expand Down Expand Up @@ -158,6 +163,17 @@ public void initialize() {
isIdentified = false;
deviceVariables.put(TIMESTAMP, Instant.now().getEpochSecond());
deviceVariables.put(PROPERTY_DID, configuration.deviceId);
try {
URL url = new File(BINDING_USERDATA_PATH, "info_" + getThing().getUID().getId() + ".json").toURI().toURL();
SavedDeviceInfoDTO lastIdInfo = GSON.fromJson(Utils.convertFileToJSON(url), SavedDeviceInfoDTO.class);
if (lastIdInfo != null) {
lastId = lastIdInfo.getLastId();
logger.debug("Last Id set to {} for {}", lastId, getThing().getUID());
}
} catch (JsonParseException | IOException | URISyntaxException e) {
logger.debug("Could not read last connection id for {} : {}", getThing().getUID(), e.getMessage());
}

miIoScheduler.schedule(this::initializeData, 1, TimeUnit.SECONDS);
int pollingPeriod = configuration.refreshInterval;
if (pollingPeriod > 0) {
Expand Down Expand Up @@ -221,6 +237,11 @@ public void dispose() {
this.miioCom = null;
}
miIoScheduler.shutdownNow();
logger.debug("Last Id saved for {}: {} -> {} ", getThing().getUID(), lastId,
GSON.toJson(new SavedDeviceInfoDTO(lastId,
(String) deviceVariables.getOrDefault(PROPERTY_DID, getThing().getUID().getId()))));
Utils.saveToFile("info_" + getThing().getUID().getId() + ".json", GSON.toJson(new SavedDeviceInfoDTO(lastId,
(String) deviceVariables.getOrDefault(PROPERTY_DID, getThing().getUID().getId()))), logger);
}

protected int sendCommand(MiIoCommand command) {
Expand All @@ -237,13 +258,11 @@ protected int sendCommand(String commandString) {

/**
* This is used to execute arbitrary commands by sending to the commands channel. Command parameters to be added
* between
* [] brackets. This to allow for unimplemented commands to be executed (e.g. get detailed historical cleaning
* records)
* between [] brackets. This to allow for unimplemented commands to be executed
*
* @param commandString command to be executed
* @param cloud server to be used or empty string for direct sending to the device
* @return vacuum response
* @return message id
*/
protected int sendCommand(String commandString, String cloudServer) {
String command = commandString.trim();
Expand Down Expand Up @@ -284,7 +303,7 @@ protected int sendCommand(String command, String params, String cloudServer, Str
return 0;
}

String getCloudServer() {
public String getCloudServer() {
// This can be improved in the future with additional / more advanced options like e.g. directFirst which would
// use direct communications and in case of failures fall back to cloud communication. For now we keep it
// simple and only have the option for cloud or direct.
Expand Down

0 comments on commit 2b53a00

Please sign in to comment.