forked from pfiaux/node-red-contrib-neeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactive-now.js
38 lines (30 loc) · 919 Bytes
/
active-now.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const brainAPI = require('./lib/brainAPI');
module.exports = function(RED) {
function ActiveNowNode(config) {
RED.nodes.createNode(this,config);
const node = this;
node.brain = RED.nodes.getNode(config.brain);
if (!node.brain) {
node.warn('No NEEO Brain configured!');
}
node.on('input', () => {
const noBrainIp = !node.brain || !node.brain.ip;
if (noBrainIp) {
node.error('No NEEO Brain configured!');
node.send({ payload: [] });
return;
}
brainAPI.getActiveNow(node.brain.ip)
.then((activeNow) => {
const msg = { payload: activeNow };
node.send(msg);
})
.catch((error) => {
node.error(`Failed to fetch active now list: ${error.message}`);
node.send({ payload: [] });
});
});
}
RED.nodes.registerType('active-now', ActiveNowNode);
};