-
Notifications
You must be signed in to change notification settings - Fork 16
/
price.js
executable file
·110 lines (103 loc) · 4.76 KB
/
price.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
105
106
107
108
109
110
module.exports = function(RED) {
function nibePrice(config) {
RED.nodes.createNode(this,config);
const server = RED.nodes.getNode(config.server);
const startUp = () => {
let system = config.system.replace('s','S');
let conf = server.nibe.getConfig();
this.status({ fill: 'yellow', shape: 'dot', text: `System ${system}` });
let arr = [
{topic:"inside_set_"+config.system,source:"nibe"},
{topic:"outside",source:"nibe"}
];
if(conf.system.pump!=="F370" && conf.system.pump!=="F470") {
arr.push({topic:"dM",source:"nibe"});
arr.push({topic:"dMstart",source:"nibe"})
}
if(conf.price===undefined) {
conf.price = {};
server.nibe.setConfig(conf);
}
if(conf.home.inside_sensors===undefined) {
conf.home.inside_sensors = [];
server.nibe.setConfig(conf);
}
if(conf.price['sensor_'+config.system]===undefined || conf.price['sensor_'+config.system]=="Ingen") {
arr.push({topic:"inside_"+config.system,source:"nibe"});
} else {
let index = conf.home.inside_sensors.findIndex(i => i.name == conf.price['sensor_'+config.system]);
if(index!==-1) {
var insideSensor = Object.assign({}, conf.home.inside_sensors[index]);
//let insideSensor = conf.home.inside_sensors[index];
arr.push(insideSensor);
}
}
if(conf.price.enable!==true) arr = [];
server.initiatePlugin(arr,'price',config.system).then(data => {
this.status({ fill: 'green', shape: 'dot', text: `System ${system}` });
this.send({enabled:true});
},(reject => {
this.status({ fill: 'red', shape: 'dot', text: `System ${system}` });
this.send({enabled:false});
}));
}
this.on('input', function(msg) {
let conf = server.nibe.getConfig();
if(msg.topic=="update") {
let data = {system:config.system}
server.updateData(data);
return;
} else if(msg.topic=="price/enable") {
let req = msg.topic.split('/');
if(conf[req[0]]===undefined) conf[req[0]] = {};
if(conf[req[0]][req[1]]!==msg.payload) {
conf[req[0]][req[1]] = msg.payload;
server.nibe.setConfig(conf);
}
startUp();
} else if(msg.payload!==undefined && msg.topic!==undefined && msg.topic!=="") {
let req = msg.topic.split('/');
if(conf[req[0]]===undefined) conf[req[0]] = {};
if(conf[req[0]][req[1]+'_'+config.system]!==msg.payload) {
conf[req[0]][req[1]+'_'+config.system] = msg.payload;
server.nibe.setConfig(conf);
}
startUp();
}
});
if(server.nibe.core!==undefined && server.nibe.core.connected!==undefined && server.nibe.core.connected===true) {
startUp();
} else {
server.nibeData.on('ready', (data) => {
startUp();
})
}
server.nibeData.on(this.id, (data) => {
if(data.changed===true) {
config.system = data.system;
if(server.nibe.core!==undefined && server.nibe.core.connected!==undefined && server.nibe.core.connected===true) {
startUp();
}
}
})
server.nibeData.on('pluginPriceGraph', (data) => {
if(data.system===config.system) {
this.send({topic:"Graf",payload:[]});
this.send({topic:"Graf",payload:data.values});
}
});
server.nibeData.on('pluginPrice', (data) => {
if(data.system===config.system) {
this.send({topic:"Nuvarande Elprisnivå",payload:data.price_level.data});
this.send({topic:"Nuvarande Elpris",payload:data.price_current.data});
this.send([null,{topic:"test",payload:data}]);
}
})
this.on('close', function() {
let system = config.system.replace('s','S');
server.nibeData.removeAllListeners();
this.status({ fill: 'yellow', shape: 'dot', text: `System ${system}` });
});
}
RED.nodes.registerType("nibe-price",nibePrice);
}