-
Notifications
You must be signed in to change notification settings - Fork 16
/
input.js
executable file
·74 lines (70 loc) · 3.09 KB
/
input.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
module.exports = function(RED) {
function nibeInput(config) {
RED.nodes.createNode(this,config);
const server = RED.nodes.getNode(config.server);
const nibe = server.nibe;
var savedError = {};
if(config.add===true && config.name.toLowerCase()!="config" && config.name.toLowerCase()!="error") {
nibe.addRegister(config.name);
}
let register = config.name.toLowerCase();
if(server.hP()[config.name]!==undefined) {
register = server.hP()[config.name]
}
server.nibeData.on('ready', data => {
if(server.hP()[config.name]!==undefined) {
register = server.hP()[config.name]
}
})
var node = this;
if(config.name=="") {
server.nibeData.on('data', data => {
let saved = node.context().get(data.register);
if(data.error!==undefined) {
} else {
if(saved!=data.data) {
node.send([{topic:data.register,payload:data.data},{topic:data.register,payload:data.data,raw:data}]);
node.context().set(data.register, data.data);
node.status({ fill: 'green', shape: 'dot', text: `${data.register}: ${data.data} ${data.unit}` });
} else {
node.send([null,{topic:data.register,payload:data.data,raw:data}]);
}
}
})
} else if(config.name.toLowerCase()=="error") {
server.nibeData.on('fault', data => {
if(savedError.from!==data.from || savedError.message!==data.message) {
node.send({topic:data.from,payload:data.message});
savedError = data;
}
node.send([null,{topic:data.from,payload:data.message}]);
})
} else {
server.nibeData.on(register, data => {
if(register===data.register) {
let saved = node.context().get(data.register);
if(data.error!==undefined) {
} else {
if(saved!=data.data) {
node.send([{topic:data.register,payload:data.data,raw:data},{topic:data.register,payload:data.data,raw:data}]);
node.context().set(data.register, data.data);
node.status({ fill: 'green', shape: 'dot', text: `${data.data}${data.unit}` });
} else {
node.send([null,{topic:data.register,payload:data.data,raw:data}]);
}
}
}
})
}
/*nibe.data.on(register, data => {
node.status({ fill: 'red', shape: 'dot', text: data });
})*/
if(config.name.toLowerCase()=="config") {
server.nibeData.on('config', data => {
node.send([{topic:"config",payload:data},null]);
})
server.nibe.getConfig();
}
}
RED.nodes.registerType("nibe-input",nibeInput);
}