-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
629 lines (600 loc) · 18.5 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const net = require('net');
const idcCore = require('./lib/idc-core.js');
//
class Deyeidc extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'deyeidc',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
//
this.idc = new idcCore(this.log);
this.client = null;
// ----------------- Timeout variables -----------------
this.executionInterval = 60;
// ----------------- Global variables -----------------
this.connectionActive = false;
this.internDataReady = true;
this.numberRegisterSets = 0;
this.numberCoils = 0;
this.req = 0;
this.memoryValues = [];
this.setWatchPoints = false;
this.resetCounter = 0;
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset the connection indicator during startup
this.setState('info.connection', { val: false, ack: true });
// The adapters config (in the instance object everything under the attribute "native")
// is accessible via this.config:
// Initialize your adapter Here
this.idc = new idcCore(this.log);
// About User changes
await this.checkUserData();
// Loading the Register
try {
const RegisterSets = this.config.registers;
if (RegisterSets && Array.isArray(RegisterSets)) {
this.numberRegisterSets = RegisterSets.length;
this.idc.setRegisters(RegisterSets);
}
} catch (err) {
this.internDataReady = false;
this.log.error(`[readRegisterset] ${err}`);
}
//
// Loading the Coils
try {
const Coils = this.config.coils;
if (Coils && Array.isArray(Coils)) {
this.numberCoils = Coils.length;
this.idc.setCoils(Coils);
}
} catch (err) {
this.internDataReady = false;
this.log.error(`[readCoilset] ${err}`);
}
// already
if (this.internDataReady) {
// first request
this.req = 1;
await this.requestData(this.req);
// timed request
this.updateInterval = this.setInterval(async () => {
this.req = 1;
await this.requestData(this.req);
}, this.executionInterval * 1000);
} else {
this.setState('info.connection', { val: false, ack: true });
this.log.error('Adapter cannot be started without correct settings!');
}
}
/**
* connection to inverter
* @returns boolean
*/
async checkConnected() {
if (this.connectionActive) return true;
this.log.debug(`try to connect . . .`);
try {
this.client = await this.connectToServer();
return true;
} catch (error) {
this.client = null;
return false;
}
}
/**
* connectToServer
* @returns client
*/
connectToServer() {
return new Promise((resolve, reject) => {
const client = new net.Socket();
client.connect({ host: this.config.ipaddress, port: this.config.port }, () => {
this.log.debug('Connected to server');
this.connectionActive = true;
this.setState('info.connection', { val: this.connectionActive, ack: true });
//client.set_Time_out(10000);
resolve(client); // Successful connection, return the socket
});
/*
client.on('timeout', () => {
this.log.debug('Connection timeout');
client.destroy();
if (client.destroyed) this.log.debug('Connection closed/destroyed');
this.connectionActive = false;
this.setState('info.connection', { val: this.connectionActive, ack: true });
});
*/
client.on('error', (error) => {
this.connectionActive = false;
this.setState('info.connection', { val: this.connectionActive, ack: true });
if (error.message.indexOf('EHOSTUNREACH') > 1 || error.message.indexOf('ECONNRESET') > 1) {
this.log.debug(`No connection to inverter: '${error.message}'`);
this.offlineReset();
} else {
this.log.error(`Error during connection: ${error}`);
}
reject(error);
});
client.on('close', () => {
this.log.debug('Connection closed');
this.connectionActive = false;
this.setState('info.connection', { val: this.connectionActive, ack: true });
});
client.on('data', (data) => {
this.onData(data);
});
});
}
/**
* onData
* @param {*} data#
*/
async onData(data) {
try {
const mb = this.idc.checkDataFrame(data);
if (mb) {
if (mb.register == 0) { //checkOnlineDate
this.log.debug(`Response: (checkOnlineDate) ${JSON.stringify(mb)}`);
if (mb.modbus[3] == 0) await this.setOfflineDate(); //modbus[3] = Date/Day
}
else if (mb.register > 0) { //payload
this.log.debug(`Response: (payload) ${JSON.stringify(mb)}`);
await this.updateData(this.idc.readCoils(mb));
this.req++;
if (this.req <= this.numberRegisterSets) {
this.requestData(this.req);
} else if (this.req == this.numberRegisterSets + 1) {
this.log.debug(`Data reception for ${this.req - 1} registersets completed`);
await this.updateData(await this.computeData());
if (this.config.onlinecheck) await this.checkOnlineDate();
this.subscribeWatchpoint();
this.setState('info.lastUpdate', { val: Date.now(), ack: true });
this.setState('info.status', { val: 'idle', ack: true });
}
} else { //other messages
this.log.silly(`RESPONSE ${JSON.stringify(mb)}`); // human readable ALL Responses
}
}
} catch (err) {
if (err.status == 'ECNTRLCODE') {
this.log.silly(`${err.message}: Data may be corrupt, therefore discarded`);
} else if (err.status == 'EFRAMECHK') {
this.log.silly(`${err.message}: Frame CheckSum faulty!`);
} else {
this.log.error(`${err} | ${err.stack}`);
}
}
}
/**
* requestData (do the next request for registerset)
* @param {number} req
*/
async requestData(req) {
if (await this.checkConnected()) {
try {
this.setState('info.status', { val: 'automatic request', ack: true });
const request = this.idc.requestFrame(req, this.idc.modbusFrame(req));
this.log.silly(`Request to register set ${(req)} > ${this.idc.toHexString(request)}`); // human readable
this.client.write(request);
} catch (error) {
this.log.error(`[requestData] error: ${error} stack: ${error.stack}`);
}
}
}
/**
* Calculate data that cannot be read,
* the necessary values must be calulated by formula
* @returns jsonResult
*/
async computeData() {
const jsonResult = [];
if (this.config.computes.length < 1 || this.memoryValues.length < 1) return jsonResult;
let computeValue1 = 0;
let computeValue2 = 0;
let computeResult = 0;
for (const obj of this.config.computes) {
this.log.debug(`[computeData] ${JSON.stringify(obj)}`);
const response = mathOperation(obj.values);
if (response) {
const key1Index = this.memoryValues.findIndex((element => element.key == response.key1));
if (key1Index == -1) {
computeValue1 = parseFloat(response.key1);
if (isNaN(computeValue1)) {
this.log.warn(`Compute Key1 '${response.key1}' not found!`);
continue;
}
} else {
computeValue1 = parseFloat(this.memoryValues[key1Index].value);
}
//
const key2Index = this.memoryValues.findIndex((element => element.key == response.key2));
if (key2Index == -1) {
computeValue2 = parseFloat(response.key2);
if (isNaN(computeValue2)) {
this.log.warn(`Compute Key2 '${response.key2}' not found!`);
continue;
}
} else {
computeValue2 = parseFloat(this.memoryValues[key2Index].value);
}
//
const operation = response.operation;
switch (operation) {
case '+':
computeResult = computeValue1 + computeValue2;
break;
case '-':
computeResult = computeValue1 - computeValue2;
break;
case '*':
computeResult = computeValue1 * computeValue2;
break;
case '/':
if (computeValue2 == 0) {
this.log.warn(`Compute Division '${response.key2}' by zero!`);
continue;
} else {
computeResult = computeValue1 / computeValue2;
}
break;
default:
computeResult = -999;
}
//
const key0 = this.removeInvalidCharacters(obj.key.trim());
const result_hr = (computeResult * 10 ** -obj.factor).toFixed(2);
const jsonObj = { key: key0, value: result_hr, unit: obj.unit, name: obj.name };
jsonResult.push(jsonObj);
}
}
this.log.debug(`[computeData] ResultJson: ${JSON.stringify(jsonResult)}`);
return (jsonResult);
// -- Helper --
function mathOperation(computeString) {
const defMathOperators = ['*', '/', '+', '-'];
for (let i = 0; i < defMathOperators.length; i++) {
const zeichen = defMathOperators[i];
const position = computeString.indexOf(zeichen);
if (position > 0) {
i = defMathOperators.length;
const key1 = computeString.slice(0, position).trim();
const key2 = computeString.slice(position + 1).trim();
const jsonString = { operation: computeString[position], key1: key1, key2: key2 };
return jsonString;
}
}
}
}
/**
* OfflineReset, if 'EHOSTUNREACH' arrived
*/
async offlineReset() {
// Counter for OfflineReset
this.resetCounter++;
const startReset = Math.floor(540 / this.config.pollInterval);
this.log.debug(`[offlineReset] ${this.resetCounter} / ${startReset}`);
if (this.resetCounter == startReset) {
this.log.debug(`[offlineReset] Values will be nullable.`);
for (const obj of this.config.coils) {
if (obj['nullable']) {
this.log.debug(`[offlineReset] ${obj.key}`);
await this.persistData(obj.key, obj.name, 0, 'value', obj.unit, true);
}
}
}
}
/**
* set actual date to inverter (for daily data-reset)
*/
async setOfflineDate() {
if (await this.checkConnected()) {
const data = [];
const req = 0;
const dateControlRegister = 0x16;
const d = new Date();
data[0] = parseInt(decimalToHex(parseInt(d.getFullYear().toString().substring(2))) + decimalToHex(d.getMonth() + 1), 16);
data[1] = parseInt(decimalToHex(d.getDate()) + decimalToHex(d.getHours()), 16);
data[2] = parseInt(decimalToHex(d.getMinutes()) + decimalToHex(d.getSeconds()), 16);
const request = this.idc.requestFrame(req, this.idc.modbusWriteFrame(dateControlRegister, data));
this.log.debug(`[setOfflineDate] write: ${(req)} > ${this.idc.toHexString(request)}`); // human readable
this.client.write(request);
}
function decimalToHex(dec) {
let hex = Number(dec).toString(16);
while (hex.length < 2) {
hex = '0' + hex;
}
return hex;
}
}
/**
* checkOnlineDate
*/
async checkOnlineDate() {
if (await this.checkConnected()) {
const dateControlRegister = 0x17; // Day&Hour
const request = this.idc.requestFrame(0, this.idc.modbusReadFrame(dateControlRegister));
this.log.silly(`Request to date ( ddhh ) > ${this.idc.toHexString(request)}`); // human readable
this.client.write(request);
}
}
/**
* Set a subscriber to watchState 'Power_Set'
* Subscribe first when object has been created
*/
async subscribeWatchpoint() {
if (this.setWatchPoints) return;
const watch = this.config.logger + '.Power_Set';
this.subscribeStates(watch);
this.log.debug(`[subscribeWatchpoint] set to ${watch}`);
this.setWatchPoints = true;
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
async onStateChange(id, state) {
if (state) {
// The state was changed
if ((id.indexOf('Power_Set') > 1) && !state.ack) {
await this.setPower(id, state);
}
} else {
// The state was deleted
this.log.debug(`state ${id} deleted`);
}
}
/**
* set Power
* @param {*} id
* @param {*} state
*/
async setPower(id, state) {
if (await this.checkConnected()) {
const req = 0;
const powerControlRegister = 40;
const data = [];
if (state.val < 1 || state.val > 100) {
data[0] = 100;
} else {
data[0] = state.val;
}
this.log.debug(`[setPower] Power set to ${data[0]}%`);
const request = this.idc.requestFrame(req, this.idc.modbusWriteFrame(powerControlRegister, data));
this.client.write(request);
this.setState(id, { val: data[0], ack: true });
}
}
/**
* save data in ioBroker datapoints
* @param {*} key
* @param {*} name
* @param {*} value
* @param {*} role
* @param {*} unit
*/
async persistData(key, name, value, role, unit, nullable) {
const dp_Device = this.removeInvalidCharacters(String(this.config.logger));
const dp_Value = dp_Device + '.' + this.removeInvalidCharacters(key);
//
await this.setObjectNotExists(dp_Device, {
type: 'channel',
common: {
name: 'Values from device',
desc: 'generated by deyeidc',
role: 'info'
},
native: {}
});
//
// Type recognition <number>
if (isNumber(value)) {
value = parseFloat(value);
//
await this.setObjectNotExistsAsync(dp_Value, {
type: 'state',
common: {
name: name,
type: 'number',
role: role,
unit: unit,
read: true,
write: true,
},
native: {},
});
//this.log.debug(`[persistData] Device "${dp_Device}" Key "${key}" with value: "${value}" and unit "${unit}" with role "${role}" as type "number"`);
} else { // or <string>
await this.setObjectNotExistsAsync(dp_Value, {
type: 'state',
common: {
name: name,
type: 'string',
role: role,
unit: unit,
read: true,
write: true,
},
native: {},
});
//this.log.debug(`[persistData] Device "${dp_Device}" Key "${key}" with value: "${value}" and unit "${unit}" with role "${role}" as type "string"`);
}
// Differentiated writing of data
if (nullable) {
await this.setState(dp_Value, { val: 0, ack: true, q: 0x42 }); // Nullable values while device is not present
} else {
await this.setState(dp_Value, { val: value, ack: true, q: 0x00 });
}
//
function isNumber(n) {
return !isNaN(parseFloat(n)) && !isNaN(n - 0);
}
}
/**
* prepare data vor ioBroker
* @param {*} data
*/
async updateData(data) {
if (data) {
for (const obj of data) {
if (obj.value != 'none') {
const elementIndex = this.memoryValues.findIndex((element => element.key == obj.key));
if (elementIndex == -1) { // new memory object
const jsonString = { key: obj.key, value: obj.value };
this.memoryValues.push(jsonString);
} else { // update memory object
this.memoryValues[elementIndex].value = obj.value;
}
await this.persistData(obj.key, obj.name, obj.value, 'value', obj.unit, false);
}
}
}
}
/**
* check data from UI
* @returns
*/
async checkUserData() {
// The adapters config (in the instance object everything under the attribute "native")
// is accessible via this.config:
// __________________
// check if the IP-Address available
if (!this.config.ipaddress) {
this.log.warn(`No inverter IP specified [${this.config.ipaddress}] .`);
this.internDataReady = false;
return;
}
// check if the IP-Address seems korrect
if (validateIP(this.config.ipaddress)) {
this.log.debug(`IP address [${this.config.ipaddress}] seems to be valid.`);
} else {
this.log.warn(`IP address [${this.config.ipaddress}] is not valid !`);
this.internDataReady = false;
return;
}
// __________________
// check if portnumber is setted
if (this.config.port < 1024) {
this.log.warn(`No port no specified [${this.config.port}].`);
this.config.port = 8899;
this.log.info(`Standard port is used [${this.config.port}].`);
}
// __________________
// InverterNr is plausible
if (!this.config.logger) {
this.log.warn(`No logger number specified [${this.config.logger}].`);
this.internDataReady = false;
return;
}
if (this.config.logger < 2 * 10 ** 9) {
this.log.warn(`Logger number seems to be wrong [${this.config.logger}].`);
this.internDataReady = false;
return;
}
this.idc.setLoggerSn(this.config.logger);
// __________________
// check if the sync time is a number, if not, the string is parsed to a number
if (isNaN(this.config.pollInterval) || this.config.pollInterval < 1) {
this.executionInterval = 60;
this.log.warn(`Sync time was too short (${this.config.pollInterval} sec). New sync time is ${this.executionInterval} sec.`);
} else {
this.executionInterval = this.config.pollInterval;
}
this.log.info(`Retrieving data from the inverter will be done every ${this.executionInterval} seconds`);
//
this.log.debug(`checkUserData is ready`);
return;
//
function validateIP(ip) {
const ipPattern = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const domainPattern = /^(?!:\/\/)([a-zA-Z0-9-_]+\.)+[a-zA-Z]{2,}$/;
return ipPattern.test(ip) || domainPattern.test(ip);
}
}
/**
* createObjectForDevices (loggerSN)
*/
async createObjectForDevices() {
const loggerSn = String(this.config.logger);
await this.setObjectNotExistsAsync(loggerSn, {
type: 'channel',
common: {
name: {
'en': 'Values from device',
'de': 'Werte vom Gerät',
'ru': 'Значения от устройства',
'pt': 'Valores do dispositivo',
'nl': 'aarden van het apparaat',
'fr': "Valeurs de l'appareil",
'it': 'Valori dal dispositivo',
'es': 'Valores desde el dispositivo',
'pl': 'Wartości z urządzenia',
'uk': 'Ціни від пристрою',
'zh-cn': '来自设备的值'
}, desc: 'generated by Deyeidc'
},
native: {},
});
}
/**
*
* @param {string} inputString
* @returns {string}
*/
removeInvalidCharacters(inputString) {
//return inputString;
const regexPattern = '[^a-zA-Z0-9]+';
const regex = new RegExp(regexPattern, 'gu');
return inputString.replace(regex, '_');
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.debug('[onUnload] cleaned everything up...');
// Here you must clear all timeouts or intervals that may still be active
//
this.updateInterval && clearInterval(this.updateInterval);
//
this.client.destroy();
this.setState('info.connection', { val: false, ack: true });
this.setState(`info.status`, { val: 'offline', ack: true });
callback();
} catch (e) {
callback();
}
}
}
// ########## END CLASS ############
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Deyeidc(options);
} else {
// otherwise start the instance directly
new Deyeidc();
}