Skip to content

Commit

Permalink
fix: catch rejected promises from Yeelight lib in State node
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmattmatt committed Apr 8, 2018
1 parent 01cdc56 commit 890763b
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/YeeLightNodeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@ export default function YeeLightNodeState(RED) {

// on, hex, bri, hue, sat, duration
const onInput = msg => {
node.serverConfig.yeelight.sync().then(state => {
msg.payload = sanitizeState(state);
// only send message if new information or if requested by input
if (
JSON.stringify(msg.payload) !== JSON.stringify(lastSentState) ||
Object.keys(msg).length > 1
) {
node.send(msg);
lastSentState = msg.payload;
}
});
node.serverConfig.yeelight
.sync()
.then(state => {
msg.payload = sanitizeState(state);
// only send message if new information or if requested by input
if (
JSON.stringify(msg.payload) !== JSON.stringify(lastSentState) ||
Object.keys(msg).length > 1
) {
node.send(msg);
lastSentState = msg.payload;
}
})
.catch(e => {
if (e.message === 'timeout') {
e.code = 'timeout';
e.message = 'Local timeout in "Yeelight.command" execution';
}
node.log('An error occured while syncing or setting a new value');
console.error(e);
node.status({
fill: 'red',
shape: 'ring',
text: `Command send error: ${e.code}`,
});
});
};

(function init() {
Expand Down

0 comments on commit 890763b

Please sign in to comment.