[irobot] Add command "cleanRegions" to clean specific regions only (#9775)
Signed-off-by: Florian Binder <fb@java4.info>
This commit is contained in:
@@ -48,8 +48,10 @@ public class IRobotBindingConstants {
|
||||
public static final String CHANNEL_ALWAYS_FINISH = "always_finish";
|
||||
public static final String CHANNEL_POWER_BOOST = "power_boost";
|
||||
public static final String CHANNEL_CLEAN_PASSES = "clean_passes";
|
||||
public static final String CHANNEL_LAST_COMMAND = "last_command";
|
||||
|
||||
public static final String CMD_CLEAN = "clean";
|
||||
public static final String CMD_CLEAN_REGIONS = "cleanRegions";
|
||||
public static final String CMD_SPOT = "spot";
|
||||
public static final String CMD_DOCK = "dock";
|
||||
public static final String CMD_PAUSE = "pause";
|
||||
|
||||
@@ -12,10 +12,17 @@
|
||||
*/
|
||||
package org.openhab.binding.irobot.internal.dto;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
/**
|
||||
* iRobot MQTT protocol messages
|
||||
*
|
||||
* @author Pavel Fedin - Initial contribution
|
||||
* @author Florian Binder - Added CleanRoomsRequest
|
||||
*
|
||||
*/
|
||||
public class MQTTProtocol {
|
||||
@@ -23,6 +30,29 @@ public class MQTTProtocol {
|
||||
public String getTopic();
|
||||
}
|
||||
|
||||
public static class CleanRoomsRequest extends CommandRequest {
|
||||
public int ordered;
|
||||
public String pmap_id;
|
||||
public List<Region> regions;
|
||||
|
||||
public CleanRoomsRequest(String cmd, String mapId, String[] regions) {
|
||||
super(cmd);
|
||||
ordered = 1;
|
||||
pmap_id = mapId;
|
||||
this.regions = Arrays.stream(regions).map(i -> new Region(i)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static class Region {
|
||||
public String region_id;
|
||||
public String type;
|
||||
|
||||
public Region(String id) {
|
||||
this.region_id = id;
|
||||
this.type = "rid";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class CommandRequest implements Request {
|
||||
public String command;
|
||||
public long time;
|
||||
@@ -31,7 +61,7 @@ public class MQTTProtocol {
|
||||
public CommandRequest(String cmd) {
|
||||
command = cmd;
|
||||
time = System.currentTimeMillis() / 1000;
|
||||
initiator = "localApp";
|
||||
initiator = "openhab";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -56,6 +86,7 @@ public class MQTTProtocol {
|
||||
public static class CleanMissionStatus {
|
||||
public String cycle;
|
||||
public String phase;
|
||||
public String initiator;
|
||||
public int error;
|
||||
}
|
||||
|
||||
@@ -183,6 +214,9 @@ public class MQTTProtocol {
|
||||
public String bootloaderVer;
|
||||
// "umiVer":"6",
|
||||
public String umiVer;
|
||||
// "lastCommand":
|
||||
// {"command":"start","initiator":"localApp","time":1610283995,"ordered":1,"pmap_id":"AAABBBCCCSDDDEEEFFF","regions":[{"region_id":"6","type":"rid"}]}
|
||||
public JsonElement lastCommand;
|
||||
}
|
||||
|
||||
// Data comes as JSON string: {"state":{"reported":<Actual content here>}}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Hashtable;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
@@ -65,6 +66,7 @@ import com.google.gson.stream.JsonReader;
|
||||
*
|
||||
* @author hkuhn42 - Initial contribution
|
||||
* @author Pavel Fedin - Rewrite for 900 series
|
||||
* @author Florian Binder - added cleanRegions command and lastCommand channel
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class RoombaHandler extends BaseThingHandler implements MqttConnectionObserver, MqttMessageSubscriber {
|
||||
@@ -128,7 +130,24 @@ public class RoombaHandler extends BaseThingHandler implements MqttConnectionObs
|
||||
cmd = isPaused ? "resume" : "start";
|
||||
}
|
||||
|
||||
sendRequest(new MQTTProtocol.CommandRequest(cmd));
|
||||
if (cmd.startsWith(CMD_CLEAN_REGIONS)) {
|
||||
// format: cleanRegions:<pmid>;<region_id1>,<region_id2>,...
|
||||
if (Pattern.matches("cleanRegions:[^:;,]+;.+(,[^:;,]+)*", cmd)) {
|
||||
String[] cmds = cmd.split(":");
|
||||
String[] params = cmds[1].split(";");
|
||||
|
||||
String mapId = params[0];
|
||||
String[] regionIds = params[1].split(",");
|
||||
|
||||
sendRequest(new MQTTProtocol.CleanRoomsRequest("start", mapId, regionIds));
|
||||
} else {
|
||||
logger.warn("Invalid request: {}", cmd);
|
||||
logger.warn("Correct format: cleanRegions:<pmid>;<region_id1>,<region_id2>,...>");
|
||||
}
|
||||
} else {
|
||||
sendRequest(new MQTTProtocol.CommandRequest(cmd));
|
||||
}
|
||||
|
||||
}
|
||||
} else if (ch.startsWith(CHANNEL_SCHED_SWITCH_PREFIX)) {
|
||||
MQTTProtocol.Schedule schedule = lastSchedule;
|
||||
@@ -489,6 +508,10 @@ public class RoombaHandler extends BaseThingHandler implements MqttConnectionObs
|
||||
}
|
||||
}
|
||||
|
||||
if (reported.lastCommand != null) {
|
||||
reportString(CHANNEL_LAST_COMMAND, reported.lastCommand.toString());
|
||||
}
|
||||
|
||||
reportProperty(Thing.PROPERTY_FIRMWARE_VERSION, reported.softwareVer);
|
||||
reportProperty("navSwVer", reported.navSwVer);
|
||||
reportProperty("wifiSwVer", reported.wifiSwVer);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
<channels>
|
||||
<channel id="command" typeId="command"/>
|
||||
<channel id="last_command" typeId="lastCommand"/>
|
||||
<channel id="cycle" typeId="cycle"/>
|
||||
<channel id="phase" typeId="phase"/>
|
||||
<channel id="battery" typeId="battery"/>
|
||||
@@ -255,4 +256,12 @@
|
||||
</state>
|
||||
</channel-type>
|
||||
|
||||
<channel-type id="lastCommand" advanced="true">
|
||||
<item-type>String</item-type>
|
||||
<label>Last Command</label>
|
||||
<description>The last command which has been received by the iRobot</description>
|
||||
<state readOnly="true">
|
||||
</state>
|
||||
</channel-type>
|
||||
|
||||
</thing:thing-descriptions>
|
||||
|
||||
Reference in New Issue
Block a user