-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
527 lines (447 loc) · 16.9 KB
/
main.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/**
* solarlog_test adapter
*/
/* jshint -W097 */// jshint strict:false
/*jslint node: true */
'use strict';
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
const adapter = new utils.Adapter('solarlog_test');
var DeviceIpAdress;
var https = require('http');
const cmd = "/getjp"; // Kommandos in der URL nach der Host-Adresse
var statusuz ="on";
var numinv = 0;
var names =[];
var uzimp = "false";
var testend;
var testj= 0;
var testi= 0;
let polling;
// when adapter shuts down
adapter.on('unload', function (callback) {
try {
clearInterval(polling);
adapter.log.info('[END] Stopping solarlog adapter...');
adapter.setState('info.connection', false, true);
callback();
} catch (e) {
callback();
}
});
// is called if a subscribed object changes
adapter.on('objectChange', function (id, obj) {
// Warning, obj can be null if it was deleted
adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj));
});
// is called if a subscribed state changes
adapter.on('stateChange', function (id, state) {
// Warning, state can be null if it was deleted
adapter.log.debug('stateChange ' + id + ' ' + JSON.stringify(state));
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && !state.ack) {
adapter.log.info('ack is not set!');
}
});
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', function (obj) {
if (typeof obj === 'object' && obj.message) {
if (obj.command === 'send') {
// e.g. send email or pushover or whatever
adapter.log('send command');
// Send response in callback if required
if (obj.callback) adapter.sendTo(obj.from, obj.command, 'Message received', obj.callback);
}
}
});
// is called when databases are connected and adapter received configuration.
adapter.on('ready', function() {
if (adapter.config.host) {
adapter.log.info('[START] Starting solarlog adapter');
main();
} else adapter.log.warn('[START] No IP-address set');
});
function main() {
// Vars
DeviceIpAdress = adapter.config.host;
const cmd = "/getjp"; // Kommandos in der URL nach der Host-Adresse
var statusuz ="on";
var numinv = 0;
var uzimp = (adapter.config.invimp).toString();
adapter.log.debug("InvImp: " + adapter.config.invimp);
adapter.log.debug("uzimp: " + uzimp);
var data='{"608":null}';
var options = {
host: DeviceIpAdress,
path: cmd,
method: 'POST',
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'applciation/json',
'Content-Length': data.length
}
};
const pollingTime = adapter.config.pollInterval || 300000;
adapter.log.debug('[INFO] Configured polling interval: ' + pollingTime);
adapter.log.debug('[START] Started Adapter with: ' + adapter.config.host);
adapter.log.debug("Options: " + JSON.stringify(options));
adapter.log.debug("Data: " + JSON.stringify(data));
if (uzimp == "true"){
adapter.log.debug("uzimp: " + uzimp);
adapter.log.debug("WR Importieren");
httpsReqNumInv(data, options, numinv, uzimp, defobjUZ()); //Anlegen eines Channels pro Unterzähler mit den Objekten Wert und Status
testend = setInterval(test, 2000); //überprüfen ob alle Channels angelegt sind.
setTimeout(function(){httpsReqDataStandard(cmd, uzimp);},300000); //abfragen der Standard-Werte
if (!polling) {
polling = setTimeout(function repeat() { // poll states every [30] seconds
httpsReqDataStandard(cmd, uzimp, httpsReqDataUZ(cmd, names));
setTimeout(repeat, pollingTime);
}, pollingTime);
} // endIf
}
else{
adapter.log.debug("uzimp: " + uzimp);
adapter.log.debug("WR nicht Importieren");
httpsReqDataStandard(cmd, uzimp);
if (!polling) {
polling = setTimeout(function repeat() { // poll states every [30] seconds
httpsReqDataStandard(cmd, uzimp);
setTimeout(repeat, pollingTime);
}, pollingTime);
} // endIf
}
// all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
} // endMain
function test() {
adapter.getState("info.numinv", function (err, obj) {
if (obj){
numinv = obj.val;
adapter.log.debug("Inverters to test: " + names);
adapter.log.debug("numinv: " + numinv);
names.forEach(check);
adapter.log.debug("Anzahl positiv: " + testi);
if (testi==numinv){
adapter.log.info("Alle WR/Meter gefunden");
adapter.log.debug("Names: " + names);
httpsReqDataUZ(cmd, names);
clearInterval(testend);
}
else {
testi=0;
adapter.log.warn("Nicht alle WR/Meter gefunden");
testj++;
if (testj>3){
adapter.log.warn("Fehler, noch nicht alle Unterzähler angelegt");
clearInterval(testend);
}
}
}
});
} // END test()
function check(uz) {
adapter.getObject('INV.' + uz, function(err, obj) {
if (obj) {
adapter.log.debug("Adapter " + uz + " vorhanden");
testi++;
}
else{adapter.log.warn("Adapter " + uz + " nicht vorhanden");
}
});
} // END check()
function httpsReqNumInv(data, options, numinv) { //Ermittelt die Anzahl Unterzähler und löst das Anlegen der Channels/Objekte aus.
var req = https.request(options, function(res) {
adapter.log.debug("http Status: " + res.statusCode);
adapter.log.debug('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
var bodyChunks = [];
var chunkLine = 0;
res.on('data', function(chunk) {
chunkLine = chunkLine + 1;
// Hier können die einzelnen Zeilen verarbeitet werden...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
// ...und/oder das Gesamtergebnis (body).
adapter.log.debug("body: " + body);
try{
var dataJ=JSON.parse(body);
while (statusuz != "OFFLINE" && numinv < 100) {
statusuz = (dataJ[608][numinv.toString()]);
if (statusuz != "OFFLINE") {
adapter.log.debug(dataJ[608][numinv.toString()]);
}
numinv++;
}
} catch(e) {
adapter.log.warn("JSON-parse-Fehler NumInv: " + e.message);
}
adapter.setState('info.numinv'/*numinv*/, numinv-1, true);
adapter.log.debug("Numer of Inverters/Meters :" + numinv);
adapter.log.debug("END Request: " + JSON.stringify(data));
adapter.setObjectNotExists('INV', {
type: 'device',
role: '',
common: {
name: "Inverter"
},
native: {}
});
defobjUZ(numinv);
});
});
req.on('error', function(e) { // Fehler abfangen
adapter.log.warn('ERROR ReqNumInv: ' + e.message,"warn");
});
adapter.log.debug("Data to request body: " + data);
// write data to request body
(data ? req.write(data) : adapter.log.warn("Daten: keine Daten im Body angegeben angegeben"));
req.end();
} //end httpsReqNumInv
function defobjUZ(numinv){ //Schlaufe mit Abfrage der Information pro Unterzähler und auslösen der Objekterstellung
for (var i=0; i<numinv-1;i++) {
var data1 = '{"141":{"';
var data2 = '":{"119":null}}}';
var datauz = data1 + i.toString() + data2;
var options = {
host: DeviceIpAdress,
path: cmd,
method: 'POST',
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'applciation/json',
'Content-Length': datauz.length
}
};
adapter.log.debug("Options: " + JSON.stringify(options));
adapter.log.debug("Data: " + JSON.stringify(datauz));
httpsReqSetUZ(datauz, options, i);
}
} //end defobjUZ
function httpsReqSetUZ(data, options, i) { //erstellt die Channels und Objekte pro Unterzähler
var req = https.request(options, function(res) {
adapter.log.debug("http Status: " + res.statusCode);
adapter.log.debug('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
var bodyChunks = [];
var chunkLine = 0;
res.on('data', function(chunk) {
chunkLine = chunkLine + 1;
// Hier können die einzelnen Zeilen verarbeitet werden...
bodyChunks.push(chunk);
}).on('end', function() {
var bodyuz = Buffer.concat(bodyChunks);
// ...und/oder das Gesamtergebnis (body).
adapter.log.debug("body: " + bodyuz);
try{
var dataJuz = (JSON.parse(bodyuz));
// create Channel Inverter(i)
adapter.setObjectNotExists('INV.' + (dataJuz[141][i.toString()][119]).toString(), {
type: 'channel',
role: '',
common: {
name: "" + (dataJuz[141][i.toString()][119]).toString()
},
native: {}
});
// create States PAC/Status Inverter(i)
adapter.setObjectNotExists('INV.' + (dataJuz[141][i.toString()][119]).toString() + ".PAC",{
type: 'state',
common: {
name: 'PAC',
desc: 'Power AC',
type: 'number',
role: "value.pac",
read: true,
write: false,
unit: "W"
},
native: {}
});
adapter.setObjectNotExists('INV.' + (dataJuz[141][i.toString()][119]) + ".status",{
type: 'state',
common: {
name: 'status',
desc: 'Staus of Inverter',
type: 'string',
role: "info.status",
read: true,
write: false
},
native: {}
});
names.push(dataJuz[141][i.toString()][119]);
adapter.log.debug("Inverters: " + names);
} catch(e) {
adapter.log.warn("JSON-parse-Fehler SetUZ: " + e.message);
}
});
});
req.on('error', function(e) { // Fehler abfangen
adapter.log.warn('ERROR ReqSetUZ: ' + e.message,"warn");
});
adapter.log.debug("Data to request body: " + data);
// write data to request body
(data ? req.write(data) : adapter.log.warn("Daten: keine Daten im Body angegeben angegeben"));
req.end();
} //End httpsReqSetUZ
function httpsReqDataStandard(cmd) { //Abfrage der Standardwerte
var data = '{"801":{"170":null}}';
var options = {
host: DeviceIpAdress,
path: cmd,
method: 'POST',
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'applciation/json',
'Content-Length': data.length
}
};
var req = https.request(options, function(res) {
adapter.log.debug("http Status: " + res.statusCode);
adapter.log.debug('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
var bodyChunks = [];
var chunkLine = 0;
res.on('data', function(chunk) {
chunkLine = chunkLine + 1;
// Hier können die einzelnen Zeilen verarbeitet werden...
bodyChunks.push(chunk);
}).on('end', function() {
adapter.log.debug("no more date in response");
var body = Buffer.concat(bodyChunks);
// ...und/oder das Gesamtergebnis (body).
try{
var json = (JSON.parse(body));
adapter.log.debug("Body: " + body);
adapter.setState('info.lastSync', json[801][170][100] , true);
adapter.setState('info.totalPower', json[801][170][116], true);
adapter.setState('status.pac', json[801][170][101], true);
adapter.setState('status.pdc', json[801][170][102], true);
adapter.setState('status.uac', json[801][170][103], true);
adapter.setState('status.udc', json[801][170][104], true);
adapter.setState('status.conspac', json[801][170][110], true);
adapter.setState('status.yieldday', json[801][170][105], true);
adapter.setState('status.yieldyesterday', json[801][170][106], true);
adapter.setState('status.yieldmonth', json[801][170][107], true);
adapter.setState('status.yieldyear', json[801][170][108], true);
adapter.setState('status.yieldtotal', json[801][170][109], true);
adapter.setState('status.consyieldday', json[801][170][111], true);
adapter.setState('status.consyieldyesterday', json[801][170][112], true);
adapter.setState('status.consyieldmonth', json[801][170][113], true);
adapter.setState('status.consyieldyear', json[801][170][114], true);
adapter.setState('status.consyieldtotal', json[801][170][115], true);
} catch(e) {
adapter.log.warn("JSON-parse-Fehler DataStandard: " + e.message);
}
if (uzimp=="true"){
httpsReqDataUZ(cmd, names, httpsReqStatUZ(cmd, names));
}
});
});
req.on('error', function(e) { // Fehler abfangen
adapter.log.warn('ERROR ReqDataStandard: ' + e.message,"warn");
});
adapter.log.debug("Data to request body: " + data);
// write data to request body
(data ? req.write(data) : adapter.log.warn("Daten: keine Daten im Body angegeben angegeben"));
req.end();
} //end httpsReqDataStandard()
function httpsReqDataUZ(cmd, names){ //Abfrage der Unterzählerwerte
var data = '{"782":null}';
var options = {
host: DeviceIpAdress,
path: cmd,
method: 'POST',
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'applciation/json',
'Content-Length': data.length
}
};
var req = https.request(options, function(res) {
adapter.log.debug("http Status: " + res.statusCode);
adapter.log.debug('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
var bodyChunks = [];
var chunkLine = 0;
res.on('data', function(chunk) {
chunkLine = chunkLine + 1;
// Hier können die einzelnen Zeilen verarbeitet werden...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
// ...und/oder das Gesamtergebnis (body).
adapter.log.debug("body: " + body);
try{
var dataJUZ = (JSON.parse(body));
adapter.log.debug("Inv. to treat: " + names);
var namLeng = names.length;
adapter.log.debug("Anzahl Elemente: " + namLeng);
for (var uzi=0; uzi<namLeng; uzi++){
adapter.log.debug("INV." + names[uzi] + ": " + dataJUZ[782][uzi]);
adapter.setState("INV." + names[uzi] + ".PAC", dataJUZ[782][uzi], true);
}
adapter.log.debug("END");
} catch(e) {
adapter.log.warn("JSON-parse-Fehler DataUZ: " + e.message);
}
httpsReqStatUZ(cmd, names);
});
});
req.on('error', function(e) { // Fehler abfangen
adapter.log.warn('ERROR ReqDataUZ: ' + e.message,"warn");
});
adapter.log.debug("Data to request body: " + data);
// write data to request body
(data ? req.write(data) : adapter.log.warn("Daten: keine Daten im Body angegeben angegeben"));
req.end();
} //End httpsReqDataUZ
function httpsReqStatUZ(cmd, names){ //Abfrage der Unterzählerwerte
var data = '{"608":null}';
var options = {
host: DeviceIpAdress,
path: cmd,
method: 'POST',
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Content-Type': 'application/json',
'Accept': 'applciation/json',
'Content-Length': data.length
}
};
var req = https.request(options, function(res) {
adapter.log.debug("http Status: " + res.statusCode);
adapter.log.debug('HEADERS: ' + JSON.stringify(res.headers), (res.statusCode != 200 ? "warn" : "info")); // Header (Rückmeldung vom Webserver)
var bodyChunks = [];
var chunkLine = 0;
res.on('data', function(chunk) {
chunkLine = chunkLine + 1;
// Hier können die einzelnen Zeilen verarbeitet werden...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
// ...und/oder das Gesamtergebnis (body).
adapter.log.debug("body: " + body);
try{
var dataJSUZ = (JSON.parse(body));
adapter.log.debug("Inv. to treat: " + names);
var namLeng = names.length;
adapter.log.debug("Anzahl Elemente: " + namLeng);
for (var uzj=0; uzj<namLeng; uzj++){
adapter.log.debug("INV." + names[uzj] + ": " + dataJSUZ[608][uzj]);
adapter.setState("INV." + names[uzj] + ".status", dataJSUZ[608][uzj], true);
}
adapter.log.debug("END");
} catch(e) {
adapter.log.warn("JSON-parse-Fehler StatUZ: " + e.message);
}
});
});
req.on('error', function(e) { // Fehler abfangen
adapter.log.debug('ERROR ReqStatUZ: ' + e.message,"warn");
});
adapter.log.debug("Data to request body: " + data);
// write data to request body
(data ? req.write(data) : adapter.log.debug("Daten: keine Daten im Body angegeben angegeben"));
req.end();
} //End httpsReqStatUZ