Skip to content

Commit

Permalink
Add "schedule" channel
Browse files Browse the repository at this point in the history
Allows to control the whole schedule as a bitmask. Useful in scripts.
  • Loading branch information
Sonic-Amiga committed Oct 9, 2020
1 parent aabc396 commit dae5a1e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ There's no global configuration for this binding.
| sched_fri | Switch | Scheduled clean enabled for Friday | N |
| sched_sat | Switch | Scheduled clean enabled for Saturday | N |
| sched_sun | Switch | Scheduled clean enabled for Sunday | N |
| schedule | Number | Schedule bitmask for use in scripts. 7 bits, bit #0 corresponds to Sunday | N |
| edge_clean | Switch | Seek out and clean along walls and furniture legs | N |
| always_finish | Switch | Whether to keep cleaning if the bin becomes full | N |
| power_boost | String | Power boost mode: "auto", "performance", "eco" | N |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class IRobotBindingConstants {
public final static String CHANNEL_SCHED_SWITCH_PREFIX = "sched_";
public final static String[] CHANNEL_SCHED_SWITCH = { "sched_sun", "sched_mon", "sched_tue", "sched_wed",
"sched_thu", "sched_fri", "sched_sat" };
public final static String CHANNEL_SCHEDULE = "schedule";
public final static String CHANNEL_EDGE_CLEAN = "edge_clean";
public final static String CHANNEL_ALWAYS_FINISH = "always_finish";
public final static String CHANNEL_POWER_BOOST = "power_boost";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,26 @@ public void handleCommand(ChannelUID channelUID, Command command) {
for (int i = 0; i < CHANNEL_SCHED_SWITCH.length; i++) {
if (ch.equals(CHANNEL_SCHED_SWITCH[i])) {
JSONArray cycle = schedule.getJSONArray("cycle");
cycle.put(i, command.equals(OnOffType.ON) ? "start" : "none");

JSONObject state = new JSONObject();
state.put("cleanSchedule", schedule);
sendDelta(state);
enableCycle(cycle, i, command.equals(OnOffType.ON));
sendSchedule(schedule);
break;
}
}
}
} else if (ch.equals(CHANNEL_SCHEDULE)) {
if (command instanceof DecimalType) {
int bitmask = ((DecimalType) command).intValue();
JSONArray cycle = new JSONArray();

for (int i = 0; i < CHANNEL_SCHED_SWITCH.length; i++) {
enableCycle(cycle, i, (bitmask & (1 << i)) != 0);
}

JSONObject schedule = new JSONObject();
schedule.put("cycle", cycle);
sendSchedule(schedule);
}
} else if (ch.equals(CHANNEL_EDGE_CLEAN)) {
if (command instanceof OnOffType) {
JSONObject state = new JSONObject();
Expand Down Expand Up @@ -174,6 +185,16 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}

private void enableCycle(JSONArray cycle, int i, boolean enable) {
cycle.put(i, enable ? "start" : "none");
}

private void sendSchedule(JSONObject schedule) {
JSONObject state = new JSONObject();
state.put("cleanSchedule", schedule);
sendDelta(state);
}

private void sendDelta(JSONObject state) {
// Huge thanks to Dorita980 author(s) for an insight on this
JSONObject request = new JSONObject();
Expand Down Expand Up @@ -388,10 +409,18 @@ public void processMessage(String topic, byte[] payload) {

if (schedule.has("cycle")) {
JSONArray cycle = schedule.getJSONArray("cycle");
int binary = 0;

for (int i = 0; i < cycle.length(); i++) {
reportSwitch(CHANNEL_SCHED_SWITCH[i], cycle.getString(i).equals("start"));
boolean on = cycle.getString(i).equals("start");

reportSwitch(CHANNEL_SCHED_SWITCH[i], on);
if (on) {
binary |= (1 << i);
}
}

reportInt(CHANNEL_SCHEDULE, binary);
}

lastSchedule = schedule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<label>Schedule Sun</label>
<description>Sunday schedule active</description>
</channel>
<channel id="schedule" typeId="schedule" />
<channel id="edge_clean" typeId="edge_clean" />
<channel id="always_finish" typeId="always_finish" />
<channel id="power_boost" typeId="power_boost" />
Expand Down Expand Up @@ -203,6 +204,12 @@
<item-type>Switch</item-type>
<label>Schedule</label>
</channel-type>
<channel-type id="schedule" advanced="true">
<item-type>Number</item-type>
<label>Schedule</label>
<description>Schedule bitmask for use in scripts: Sun Mon Tue Wed Thu Fri Sat</description>
<state min="0" max="127" />
</channel-type>
<channel-type id="rssi" advanced="true">
<item-type>Number</item-type>
<label>RSSI</label>
Expand Down

0 comments on commit dae5a1e

Please sign in to comment.