forked from Bouni/node-red-contrib-luxtronik2-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluxtronik2-ws.js
130 lines (110 loc) · 4.59 KB
/
luxtronik2-ws.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
*
* This node red node reads the data from a Luxtronik2 heat pump controller and parses the data.
* It uses the new (as of version 3.81 afaik) websocket interface.
*
* Copyright 2017 Bouni
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
module.exports = function(RED) {
"use strict";
const WebSocket = require('ws');
function query(node, msg, callback) {
const uri = 'ws://' + node.config.host + ':' + node.config.port;
const login = "LOGIN;" + node.config.password;
node.ws = new WebSocket(uri, 'Lux_WS');
msg.payload = {};
node.count = 0;
node.ws.on('open', function open() {
node.status({fill:"green",shape:"dot",text:"connected"});
try {
node.ws.send(login);
node.ws.send("REFRESH");
} catch (e) {
ws.close();
}
});
node.ws.on('message', function (data, flags) {
processResponse(data, node, msg, callback);
});
node.ws.on('close', function(code, reason) {
node.status({fill:"grey",shape:"dot",text:"disconnected"});
});
node.ws.on('error', function(error) {
node.status({fill:"red",shape:"dot",text:"Error " + error});
});
}
function processResponse(data, node, msg, callback) {
var parseString = require('xml2js').parseString;
parseString(data, function(error, json) {
if("Navigation" in json) {
node.status({fill:"green",shape:"dot",text:"process Navigation"});
// Reply to the REFRESH command, gives us the structure but no actual data
for(var i in json.Navigation.item) {
var name = json.Navigation.item[i].name[0];
if (name == 'Access: User') {
node.status({fill:"green",shape:"dot",text:"Skipping: "+name});
} else {
var id = json.Navigation.item[i].$.id;
msg.payload[name] = {};
node.ws.send('GET;'+id);
node.count++;
}
}
} else {
// Replys to the GET;<id> commands that get us the actual data
var rootname = json.Content.name[0];
if(rootname) {
for(var j in json.Content.item) {
var group = json.Content.item[j].name[0];
node.status({fill:"green",shape:"dot",text:"process "+group});
msg.payload[rootname][group] = {};
if ('raw' in json.Content.item[j]) {
var name = json.Content.item[j].name;
var value = json.Content.item[j].value;
msg.payload[rootname][group] = value[0];
} else if('item' in json.Content.item[j]) {
for(var k in json.Content.item[j].item) {
var name = json.Content.item[j].item[k].name;
var value = json.Content.item[j].item[k].value;
msg.payload[rootname][group][name] = value[0];
}
}
}
}
node.count--;
if(node.count == 0) {
node.ws.terminate();
callback();
}
}
});
}
function Luxtronik2wsNode(config) {
RED.nodes.createNode(this,config);
this.ws = null;
this.config = config;
this.status({fill:"green",shape:"dot",text:"OK"});
var node = this;
this.on('input', function(msg) {
query(node, msg, function() {
//msg.payload = node.msg;
node.send(msg);
node.status({fill:"green",shape:"dot",text:"OK"});
});
});
}
RED.nodes.registerType("Luxtronik2",Luxtronik2wsNode);
}