Skip to content

Commit 73b02d3

Browse files
committed
Add a timeout when waiting for the full status from Roomba
1 parent 1e900ac commit 73b02d3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

.changeset/short-hounds-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"homebridge-roomba2": patch
3+
---
4+
5+
Add a timeout when waiting for the full status from Roomba so we release our connection to Roomba

src/accessory.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ const WATCH_IDLE_TIMEOUT_MILLIS = 600_000;
2323
*/
2424
const MAX_CACHED_STATUS_AGE_MILLIS = 60_000;
2525

26+
/**
27+
* How long will we wait for the Roomba to send status before giving up?
28+
*/
29+
const MAX_WAIT_FOR_STATUS_MILLIS = 60_000;
30+
2631
/**
2732
* Coalesce refreshState requests into one when they're less than this many millis apart.
2833
*/
@@ -236,10 +241,27 @@ export default class RoombaAccessory implements AccessoryPlugin {
236241
return;
237242
}
238243

244+
const startedWaitingForStatus = Date.now();
245+
239246
/* Wait until we've received a state with all of the information we desire */
240247
return new Promise((resolve) => {
248+
let receivedState: RobotState | undefined = undefined;
249+
250+
const timeout = setTimeout(() => {
251+
this.log.debug(
252+
"Timeout waiting for full state from Roomba ({}ms). Last state received was: %s",
253+
Date.now() - startedWaitingForStatus,
254+
receivedState ? JSON.stringify(receivedState) : "<none>",
255+
);
256+
resolve();
257+
}, MAX_WAIT_FOR_STATUS_MILLIS);
258+
241259
const updateState = (state: RobotState) => {
260+
receivedState = state;
261+
242262
if (this.receivedRobotStateIsComplete(state)) {
263+
clearTimeout(timeout);
264+
243265
/* NB: the actual state is received and updated in the listener in connect() */
244266
this.log.debug(
245267
"Refreshed Roomba's state in %ims: %s",

0 commit comments

Comments
 (0)