Skip to content

Commit

Permalink
Fallback to miIO-call if developer API does not respond
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 25, 2018
1 parent 2639ab7 commit 1ff4a41
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/devices/gateway/subdevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,32 @@ module.exports = Thing.type(Parent => class SubDevice extends Parent {
resolve(this.getProperties(props));
};

// Reject this promise in 5 seconds
/*
* Fallback behavior, use a regular call if properties have not
* been resolved in a second.
*/
setTimeout(() => {
this._currentRead = null;
reject(new Error('Timeout while waiting for sub-device properties'));
}, 5000);
this.debug('Read via DEV timed out, using fallback API');
this._parent.call('get_device_prop_exp', [ [ 'lumi.' + this.internalId, ...this._propertiesToMonitor ]])
.then(result => {
for(let i=0; i<result[0].length; i++) {
let name = this._propertiesToMonitor[i];
const def = this._propertyDefinitions[name];
let value = result[0][i];

name = def.name || name;
value = def.mapper(value);

this.setProperty(name, value);
}

this._currentRead.resolve();
})
.catch(err => {
this._currentRead = null;
reject(err);
});
}, 1000);
});

return this._currentRead.promise;
Expand Down

0 comments on commit 1ff4a41

Please sign in to comment.