-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
310 lines (264 loc) · 9.05 KB
/
node_helper.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
const NodeHelper = require('node_helper');
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const DOMParser = require('xmldom').DOMParser;
var config = {
rooms: [{
name: "", id: "",
temperature: { actualTemperature: "", setTemperature: "" },
sensors: []
}],
heaters: [],
windows: [],
url:""
};
module.exports = NodeHelper.create({
start: function () {
const self = this;
console.log('Starting node_helper for: ' + this.name);
},
// Listens to notifications from client (from MMM-AllsvenskanStandings.js).
// Client sends a notification when it wants download new standings.
socketNotificationReceived: function (notification, payload) {
const self = this;
if (notification === 'SET_CONNECTION')
{
config.url= payload;
console.log("["+ this.name+"]"+" connecting to "+ config.url);
}
if (notification === 'GET_DEVICES')
{
config.heaters=[];
self.loadDevices(payload);
}
else if (notification === 'GET_ROOMS')
{
self.loadRooms(payload);
}
else if (notification === 'GET_TEMPERATURE')
{
self.initRealHeaters(payload);
}
else if (notification === 'GET_DATA')
{
self.sendSocketNotification('SET_DATA',config.rooms);
}
},
loadDevices: function () {
console.log("Searching devices.......")
var self = this;
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200)
{
xml = new DOMParser().parseFromString(request.responseText, "text/xml");
let devices = xml.getElementsByTagName("device");
for (let i = 0; i < devices.length; i++) {
let device = devices[i];
let devicename = device.getAttribute("name");
let deviceid = device.getAttribute("ise_id");
let devicetype = device.getAttribute("device_type");
if (devicetype.includes("HM-CC-")) // Heizung
{
if (device.getAttribute("interface").includes("VirtualDevices")) {
}
else {
console.log("[Heater:" + deviceid + "] " + devicename);
config.heaters.push(deviceid);
}
}
else if (devicetype.includes("HM-Sec-SD")) //Rauchmelder
{
console.log("[SmokeDetector:" + deviceid + "] " + devicename);
}
else if (devicetype.includes("HM-Sec-SCo")) //Fenster
{
console.log("[Window:" + deviceid + "] " + devicename);
config.windows.push(deviceid);
}
else if (devicetype.includes("HM-LC-Sw1")) //Schalter steckdose
{
console.log("[Switch:" + deviceid + "] " + devicename);
}
else {
console.log("[Unknown:" + deviceid + "] " + devicename);
}
}
self.sendSocketNotification('SET_DEVICES',config.rooms);
}
}
request.open("GET", config.url+"/config/xmlapi/devicelist.cgi", true);
request.send(null);;
},
initRealHeaters: function ()
{
var self = this;
console.log("Searching sensor ids for previously found heaters.......")
for (i = 0; i < config.heaters.length; i++)
{
let heater = config.heaters[i];
self.updateRoomTemperatureFromHeater(heater);
}
/*
for (i = 0; i < config.windows.length; i++)
{
let window = config.windows[i];
self.updateRoomTemperatureFromHeater(winde);
}
*/
},
updateRoomTemperatureFromHeater: function (deviceID) {
var self = this;
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
xml = new DOMParser().parseFromString(request.responseText, "text/xml");
devices = xml.getElementsByTagName("device");
device = devices[0];
channels = device.getElementsByTagName("channel");
if (channels[4].getAttribute("name").includes(":4")) //Assuming Temperature information to be found below the channel ending with :4
{
let tempSensorID = channels[4].getAttribute("ise_id");
for (let i = 0; i < config.rooms.length; i++) {
room = config.rooms[i];
if (room.sensors.includes(tempSensorID)) {
dataPoints = channels[4].getElementsByTagName("datapoint");
for (let j = 0; j < dataPoints.length; j++) //Scanning device datapoints to get actual tempurature and set temperature
{
dataPoint = dataPoints[j];
var actutalTemp;
var setTemp;
if (dataPoint.getAttribute("name").includes("ACTUAL_TEMPERATURE")) {
let tempvalue =dataPoint.getAttribute("value");
let tempunit=dataPoint.getAttribute("valueunit");
if(tempunit.includes("C"))
{
tempunit="°C"
}
else if(tempunit.includes("F"))
{
tempunit="°F"
}
actutalTemp = Number(tempvalue).toFixed(1) + " " + tempunit;
}
else if (dataPoint.getAttribute("name").includes("SET_TEMPERATURE")) {
let tempvalue =dataPoint.getAttribute("value");
let tempunit=dataPoint.getAttribute("valueunit");
if(tempunit.includes("C"))
{
tempunit="°C"
}
else if(tempunit.includes("F"))
{
tempunit="°F"
}
setTemp = Number(tempvalue).toFixed(1) + " " + tempunit;
}
}
console.log(device.getAttribute("name") + " is providing temperature data for " + room.name + "[" + actutalTemp + "/" + setTemp + "]");
let temp = {
actualTemperature: actutalTemp, setTemperature: setTemp
};
config.rooms[i].temperature.push(temp);
}
}
}
self.sendSocketNotification('SET_TEMPERATURE',config.rooms);
}
}
request.open("GET", config.url+"/config/xmlapi/state.cgi?device_id=" + deviceID, true);
request.send(null);;
},
updateRoomFromWIndow: function (deviceID) {
var self = this;
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
xml = new DOMParser().parseFromString(request.responseText, "text/xml");
devices = xml.getElementsByTagName("device");
device = devices[0];
channels = device.getElementsByTagName("channel");
if (channels[4].getAttribute("name").includes(":4")) //Assuming Temperature information to be found below the channel ending with :4
{
let tempSensorID = channels[4].getAttribute("ise_id");
for (let i = 0; i < config.rooms.length; i++) {
room = config.rooms[i];
if (room.sensors.includes(tempSensorID)) {
dataPoints = channels[4].getElementsByTagName("datapoint");
for (let j = 0; j < dataPoints.length; j++) //Scanning device datapoints to get actual tempurature and set temperature
{
dataPoint = dataPoints[j];
var actutalTemp;
var setTemp;
if (dataPoint.getAttribute("name").includes("ACTUAL_TEMPERATURE")) {
let tempvalue =dataPoint.getAttribute("value");
let tempunit=dataPoint.getAttribute("valueunit");
if(tempunit.includes("C"))
{
tempunit="°C"
}
else if(tempunit.includes("F"))
{
tempunit="°F"
}
actutalTemp = Number(tempvalue).toFixed(1) + " " + tempunit;
}
else if (dataPoint.getAttribute("name").includes("SET_TEMPERATURE")) {
let tempvalue =dataPoint.getAttribute("value");
let tempunit=dataPoint.getAttribute("valueunit");
if(tempunit.includes("C"))
{
tempunit="°C"
}
else if(tempunit.includes("F"))
{
tempunit="°F"
}
setTemp = Number(tempvalue).toFixed(1) + " " + tempunit;
}
}
console.log(device.getAttribute("name") + " is providing temperature data for " + room.name + "[" + actutalTemp + "/" + setTemp + "]");
let temp = {
actualTemperature: actutalTemp, setTemperature: setTemp
};
config.rooms[i].temperature=[];
config.rooms[i].temperature.push(temp);
}
}
}
self.sendSocketNotification('SET_TEMPERATURE',config.rooms);
}
}
request.open("GET", config.url+"/config/xmlapi/state.cgi?device_id=" + deviceID, true);
request.send(null);;
},
loadRooms: function () {
console.log("Searching rooms.......")
var self = this;
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
xml = new DOMParser().parseFromString(request.responseText, "text/xml");
config.rooms=[];
rooms = xml.getElementsByTagName("room");
for (let i = 0; i < rooms.length; i++) {
let roomNode = rooms[i];
let room = {
name: "", id: "", sensors: []
};
room.name = roomNode.getAttribute("name");
room.id = roomNode.getAttribute("ise_id");
room.temperature = [];
roomDevicesNodes = roomNode.getElementsByTagName("channel");
for (let i = 0; i < roomDevicesNodes.length; i++) {
room.sensors.push(roomDevicesNodes[i].getAttribute("ise_id"));
}
config.rooms.push(room);
console.log(room);
}
self.sendSocketNotification('SET_ROOMS',config.rooms);
}
}
request.open("GET", config.url+"/config/xmlapi/roomlist.cgi", true);
request.send(null);;
}
});