forked from anerdins/node-red-contrib-nibepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.js
executable file
·104 lines (97 loc) · 5.44 KB
/
output.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module.exports = function(RED) {
function nibeOutput(config) {
RED.nodes.createNode(this,config);
var timer = {};
this.server = RED.nodes.getNode(config.server);
const nibe = this.server.nibe;
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(config.name===undefined || config.name=="") {
register = msg.topic
if(this.server.hP()[msg.topic]!==undefined) {
register = this.server.hP()[msg.topic]
}
}
if(msg.topic=="getConfig" || config.name=="getConfig") {
nibe.getConfig();
} else if(msg.topic=="setConfig" || config.name=="setConfig") {
nibe.setConfig(msg.payload);
} else if(msg.topic=="refreshConfig" || config.name=="refreshConfig") {
nibe.refreshConfig();
this.status({ fill: 'green', shape: 'dot', text: `Refreshing configuration` });
timer[register] = setTimeout(() => {
this.status({ fill: 'yellow', shape: 'dot', text: `` });
}, 10000);
} else if(msg.topic=="addSensor" || config.name=="addSensor") {
nibe.addSensor(msg.payload);
} else if(msg.topic=="removeSensor" || config.name=="removeSensor") {
nibe.removeSensor(msg.payload);
} else if(msg.topic=="addRegister" || config.name=="addRegister") {
nibe.addRegister(msg.payload)
} else if(msg.topic=="getRegister" || config.name=="getRegister") {
nibe.getRegister();
} else if(msg.topic=="removeRegister" || config.name=="removeRegister") {
nibe.removeRegister(msg.payload);
} else if(msg.topic=="saveGraph" || config.name=="saveGraph") {
this.status({ fill: 'yellow', shape: 'dot', text: `Sparar grafer...` });
this.server.saveGraph().then(result => {
this.send({topic:msg.topic,payload:result});
this.status({ fill: 'green', shape: 'dot', text: `${result}` });
},(err => {
this.send({topic:msg.topic,payload:err});
this.status({ fill: 'red', shape: 'dot', text: `${err}` });
}));
} else if(config.name===undefined || config.name=="") {
msg = {topic:msg.topic,payload:msg.payload};
this.status({ fill: 'yellow', shape: 'dot', text: `${msg.payload}` });
timer[register] = setTimeout(() => {
this.status({ fill: 'red', shape: 'dot', text: `Timeout setting data` });
}, 10000);
nibe.setData(register,msg.payload,(err,result) => {
if(err) return console.log(err);
if(result===true) {
if(timer[register]._called!==true) {
this.send({topic:msg.topic,payload:msg.payload});
this.status({ fill: 'green', shape: 'dot', text: `${msg.payload}` });
setTimeout(() => {
this.status({ fill: 'yellow', shape: 'dot', text: `` });
}, 10000);
clearTimeout(timer[register]);
}
} else {
this.status({ fill: 'red', shape: 'dot', text: `Timeout setting data` });
}
});
} else {
msg = {topic:register,payload:msg.payload};
this.status({ fill: 'yellow', shape: 'dot', text: `${msg.payload}` });
timer[register] = setTimeout(() => {
this.status({ fill: 'red', shape: 'dot', text: `Timeout setting data` });
}, 10000);
nibe.setData(register,msg.payload,(err,result) => {
if(err) return console.log(err);
if(result===true) {
if(timer[register]._called!==true) {
this.send({topic:register,payload:msg.payload});
this.status({ fill: 'green', shape: 'dot', text: `${msg.payload}` });
setTimeout(() => {
this.status({ fill: 'yellow', shape: 'dot', text: `` });
}, 10000);
clearTimeout(timer[register]);
}
} else {
this.status({ fill: 'red', shape: 'dot', text: `Timeout setting data` });
}
});
}
});
} else {
// No config node configured
}
}
RED.nodes.registerType("nibe-output",nibeOutput);
}