Skip to content

Commit e32d078

Browse files
committed
Add a long-term slow watching of Roomba's status so we always have a status
Resolves #75
1 parent 2bf85e7 commit e32d078

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

.changeset/shy-deers-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"homebridge-roomba2": minor
3+
---
4+
5+
Add a long-term slow watching of Roomba's status so we always have a status

src/accessory.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ const WATCH_INTERVAL_MILLIS = 30_000;
1818
*/
1919
const WATCH_IDLE_TIMEOUT_MILLIS = 600_000;
2020

21+
/**
22+
* How often to query Roomba and update HomeKit when not actively watching Roomba's status.
23+
*/
24+
const LONG_WATCH_INTERVAL_MILLIS = 3600 * 1000; // 24 * 60 * 60 * 1000;
25+
2126
/**
2227
* How old a cached status can be before we ignore it.
2328
*/
24-
const MAX_CACHED_STATUS_AGE_MILLIS = 60_000;
29+
const MAX_CACHED_STATUS_AGE_MILLIS = LONG_WATCH_INTERVAL_MILLIS + 600_000;
2530

2631
/**
2732
* How long will we wait for the Roomba to send status before giving up?
@@ -202,6 +207,8 @@ export default class RoombaAccessory implements AccessoryPlugin {
202207
.getCharacteristic(Characteristic.ContactSensorState)
203208
.on("get", this.createCharacteristicGetter("Docking status", this.dockingStatus));
204209
}
210+
211+
this.startLongWatch();
205212
}
206213

207214
public identify(): void {
@@ -715,7 +722,7 @@ export default class RoombaAccessory implements AccessoryPlugin {
715722
}
716723

717724
this.log.debug(
718-
"Refreshing Roomba's status (repeating in %is, idle timeout in %is)",
725+
"Watching Roomba's status (repeating in %is, idle timeout in %is)",
719726
WATCH_INTERVAL_MILLIS / 1000,
720727
(WATCH_IDLE_TIMEOUT_MILLIS - timeSinceLastWatchingRequest) / 1000
721728
);
@@ -734,6 +741,18 @@ export default class RoombaAccessory implements AccessoryPlugin {
734741
}
735742
}
736743

744+
private startLongWatch() {
745+
const checkStatus = () => {
746+
this.log.info("Refreshing Roomba's status (repeating in %im)", LONG_WATCH_INTERVAL_MILLIS / 60_000);
747+
748+
this.refreshState();
749+
750+
setTimeout(checkStatus, LONG_WATCH_INTERVAL_MILLIS);
751+
};
752+
753+
checkStatus();
754+
}
755+
737756
private runningStatus = (status: Status) => status.running === undefined
738757
? undefined
739758
: status.running

0 commit comments

Comments
 (0)