forked from anerdins/node-red-contrib-nibepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.js
executable file
·67 lines (63 loc) · 3.16 KB
/
request.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module.exports = function(RED) {
function nibeRequest(config) {
RED.nodes.createNode(this,config);
var timer;
var reqOn = false;
this.server = RED.nodes.getNode(config.server);
let nibe = this.server.nibe;
const suncalc = this.server.suncalc;
if (this.server) {
this.on('input', function(msg) {
let register = config.name;
if(this.server.hP()[config.name]!==undefined) {
register = this.server.hP()[config.name]
}
if(register.toLowerCase()=="astro") {
timer = setTimeout(() => {
this.status({ fill: 'yellow', shape: 'dot', text: `` });
}, 5000);
this.status({ fill: 'green', shape: 'dot', text: `Requesting astro data` });
msg.astro = suncalc(msg)
this.status({ fill: 'green', shape: 'dot', text: `` });
this.send(msg);
} else if(register.toLowerCase()=="config") {
this.status({ fill: 'yellow', shape: 'dot', text: `Requesting configuration` });
let config = nibe.getConfig();
if(config!==undefined) {
this.status({ fill: 'green', shape: 'dot', text: `Received configuration` });
msg.config = config;
this.send([msg,{topic:config.name,payload:msg.config}]);
} else {
this.send(msg);
this.status({ fill: 'red', shape: 'dot', text: `Timeout requesting configuration` });
}
} else {
if(reqOn===false) {
reqOn = true;
this.status({ fill: 'yellow', shape: 'dot', text: `Requesting data` });
nibe.reqData(register).then(result => {
this.status({ fill: 'green', shape: 'dot', text: `Value: ${result.data}${result.unit}` });
clearTimeout(timer);
msg[config.name+"_raw"] = result;
msg[config.name] = result.data;
this.send([msg,{topic:config.name,payload:result.data}])
reqOn = false;
setTimeout(() => {
this.status({ fill: 'yellow', shape: 'dot', text: `` });
}, 10000);
},(reject => {
}));
timer = setTimeout(() => {
reqOn = false;
this.send(msg);
this.status({ fill: 'red', shape: 'dot', text: `Timeout requesting data` });
}, 30000,msg);
}
}
});
} else {
// No config node configured
}
}
RED.nodes.registerType("nibe-request",nibeRequest);
}