forked from anerdins/node-red-contrib-nibepi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_req.js
executable file
·62 lines (61 loc) · 2.85 KB
/
config_req.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
module.exports = function(RED) {
function nibeRequest(config) {
RED.nodes.createNode(this,config);
this.server = RED.nodes.getNode(config.server);
let nibe = this.server.nibe;
let translate = this.server.translate;
function getInfo(data,node,msg={}) {
let category = data.category;
let parameter = data.parameter;
let label = data.label;
if(category!==undefined && category!=="" && parameter!==undefined && parameter!=="") {
if(label===undefined || label=="") label = parameter;
let config = nibe.getConfig();
if(config[category]!==undefined && config[category][parameter]!==undefined) {
node.status({ fill: 'green', shape: 'dot', text: `Received parameter ${parameter}` });
// Check if parameter should be visible
if(parameter!=="enable") {
if(config[category].enable===true) {
msg.enabled = true;
} else {
msg.enabled = false;
}
}
// Labeling
let language = "SE"; // default language
if(config.system!==undefined && config.system.language!==undefined) {
language = config.system.language;
}
if(translate!==undefined && translate.dash!==undefined && translate.dash[label]!==undefined && translate.dash[label][language]!==undefined) {
label = translate.dash[label][language]
}
msg.label = label;
msg.payload = config[category][parameter];
node.send(msg);
} else {
node.status({ fill: 'red', shape: 'dot', text: `Could not found parameter` });
node.send(msg);
}
}
}
if (this.server) {
getInfo(config,this);
this.on('input', function(msg) {
if(msg.topic=="setConfig") {
this.server.updateConfig(config.category,config.parameter,msg.payload);
} else {
getInfo(config,this,msg)
}
});
if(config.category!==undefined && config.parameter!==undefined) {
this.server.nibeData.on(`config_${config.category}`, (data) => {
getInfo(config,this)
})
}
} else {
this.status({ fill: 'red', shape: 'dot', text: `No configuration node configured` });
// No config node configured
}
}
RED.nodes.registerType("nibe-config-req",nibeRequest);
}