Skip to content

Commit

Permalink
Resolve endless loop that blocks homebridge
Browse files Browse the repository at this point in the history
this.disableWait doesn't exist anymore so getStatus kept on recursing
on itself.
This commit removes this.disableWait and makes getStatus a bit more
readable.

Also added a comment to `enableAutoRefresh` because it was a bit
unclear how it actually handles the auto-refresh

Fixes #10
  • Loading branch information
dvcrn committed Nov 30, 2020
1 parent 60b2644 commit 6426d83
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const roombaAccessory = function (log, config) {
this.showRunningAsContactSensor = config.runningContactSensor;
this.showBinStatusAsContactSensor = config.binContactSensor;
this.cacheTTL = config.cacheTTL || 5;
this.disableWait = config.disableWait;
this.roomba = null;

this.accessoryInfo = new Service.AccessoryInformation();
Expand Down Expand Up @@ -172,6 +171,7 @@ roombaAccessory.prototype = {
this.log("Running status requested");

this.getStatus((error, status) => {
this.log.debug(`Received status: ${JSON.stringify(status)} -- error: ${JSON.stringify(error)}`);
if (error) {
callback(error);
} else {
Expand Down Expand Up @@ -252,22 +252,26 @@ roombaAccessory.prototype = {
getStatus(callback, silent) {
let status = this.cache.get(STATUS);

// cache hit, shortcircuit
if (status) {
callback(status.error, status);
} else if (!this.autoRefreshEnabled) {
this.getStatusFromRoomba(callback, silent);
} else {
if (!this.disableWait) {
setTimeout(() => this.getStatus(callback, silent), 10);
} else if (this.cache.get(OLD_STATUS)) {
this.log.warn('Using expired status');
return callback(status.error, status);
}

status = this.cache.get(OLD_STATUS);
callback(status.error, status);
} else {
callback('Failed getting status');
}
// no cache hit, query status from roomba if autorefresh isn't enabled
// if autorefresh is enabled, this step isn't needed because it'll get handled automatically in this.enableAutoRefresh()
if (!this.autoRefreshEnabled) {
return this.getStatusFromRoomba(callback, silent);
}

if (this.cache.get(OLD_STATUS)) {
this.log.warn('Using expired status');

status = this.cache.get(OLD_STATUS);
callback(status.error, status);
}

// roomba is dead
return callback('Failed getting status');
},

getStatusFromRoomba(callback, silent) {
Expand Down Expand Up @@ -453,6 +457,11 @@ roombaAccessory.prototype = {
}
},

/**
* Enables automatic refresh
* This works by listening on the cache 'expired' event - when the cache expires (set by user TTL),
* the event triggers and automatically pulls fresh state from the robot
*/
enableAutoRefresh() {
this.log("Enabling autoRefresh every %s seconds", this.cache.options.stdTTL);

Expand Down

0 comments on commit 6426d83

Please sign in to comment.