-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
840 lines (789 loc) · 30.7 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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
'use strict';
const { SerialPort } = require('serialport');
const net = require('net');
const utils = require('@iobroker/adapter-core');
class SeplosV3Sniffer extends utils.Adapter {
constructor(options = {}) {
super({
...options,
name: 'seplos-v3-sniffer',
});
this.on('ready', this.onReady.bind(this));
this.on('unload', this.onUnload.bind(this));
this.knownIds = []; // Optimierung für setObjectNotExists
this.serialPort = null;
this.socket = null;
this.buffer = [];
this.lastUpdate = {};
this.updateInterval = 5000; // Standardwert 5 Sekunden
this.reconnectTimeout = null; // Timeout für TCP-Reconnect
this.lastDataReceived = Date.now(); // Letzte empfangene Daten
this.dataTimeout = 10000; // Timeout für Datenprüfung (10 Sekunden)
this.dataCheckInterval = null; // Intervall für Datenprüfung
this.isShuttingDown = false;
}
async onReady() {
const serialAdapter = this.config['serial adapter'] || '/dev/ttyS0';
this.updateInterval = (Number(this.config['update_interval']) || 5) * 1000;
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
if (!this.validateSerialAdapter(serialAdapter)) {
this.log.error(
`Invalid input for the serial adapter: "${serialAdapter}". Please enter a valid address (tcp://ip:port, tcp://name.de:port, /dev/tty*, COM*).`,
);
return; // Prevents the adapter from crashing
}
this.log.info(`Using serial adapter: ${serialAdapter}`);
this.log.info(`Update interval set to: ${this.updateInterval / 1000} seconds`);
if (serialAdapter.startsWith('tcp://')) {
this.log.info('Using TCP connection for serial data');
await this.connectTcp(serialAdapter);
} else {
await this.connectSerial(serialAdapter);
}
// Intervall zur Überprüfung der Daten
this.dataCheckInterval = this.setInterval(() => {
if (Date.now() - this.lastDataReceived > this.dataTimeout) {
this.setState('info.connection', false, true);
}
}, 5000);
}
validateSerialAdapter(serialAdapter) {
const tcpRegex = /^tcp:\/\/([a-zA-Z0-9.-]+):(\d+)$/; // tcp://ip:port oder tcp://name.de:port
const devTtyRegex = /^\/dev\/tty[A-Za-z0-9]+$/; // /dev/tty*
const comRegex = /^COM\d+$/; // COM*
return tcpRegex.test(serialAdapter) || devTtyRegex.test(serialAdapter) || comRegex.test(serialAdapter);
}
async connectTcp(serialAdapter) {
try {
const [, host, port] = serialAdapter.match(/tcp:\/\/(.*):(\d+)/);
this.log.info(`Connecting to TCP serial: ${host}:${port}`);
this.socket = new net.Socket();
this.socket.connect(parseInt(port), host, () => {
this.log.info(`Connected to ${host}:${port}`);
});
this.socket.on('data', data => {
this.processStream(data);
});
this.socket.on('error', err => {
this.log.error(`TCP connection error: ${err.message}`);
});
this.socket.on('close', () => {
this.log.warn('TCP connection closed, retrying...');
//setTimeout(() => this.connectTcp(serialAdapter), 5000);
if (this.reconnectTimeout) {
this.clearTimeout(this.reconnectTimeout);
}
this.reconnectTimeout = this.setTimeout(() => this.connectTcp(serialAdapter), 5000);
});
} catch (error) {
this.log.error(`TCP connection failed: ${error.message}`);
}
}
async connectSerial(serialAdapter) {
try {
this.serialPort = new SerialPort({
path: serialAdapter,
baudRate: 19200,
});
this.serialPort.on('data', data => {
this.processStream(data);
});
this.serialPort.on('error', err => {
this.log.error(`Serial port error: ${err.message}`);
});
} catch (error) {
this.log.error(`Failed to open serial port: ${error.message}`);
}
}
processStream(data) {
for (const byte of data) {
this.buffer.push(byte);
if (this.buffer.length > 100) {
this.buffer.shift();
}
if (this.buffer.length >= 5) {
if (!this.isValidHeader(this.buffer)) {
this.buffer.shift();
continue;
}
const expectedLength = this.getExpectedLength(this.buffer);
if (this.buffer.length >= expectedLength) {
if (this.validateCRC(this.buffer, expectedLength)) {
this.processPacket(Buffer.from(this.buffer.slice(0, expectedLength)));
}
//this.buffer = [];
this.buffer.shift();
}
}
}
}
async onUnload(callback) {
try {
this.isShuttingDown = true; // Set shutdown flag
this.log.info('Cleaning up before shutdown...');
this.buffer = [];
this.lastUpdate = {};
if (this.serialPort) {
this.log.info('Closing serial connection...');
this.serialPort.close();
this.serialPort = null;
}
if (this.socket) {
this.log.info('Closing TCP connection...');
this.socket.destroy();
this.socket = null;
}
if (this.reconnectTimeout) {
this.clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = null;
}
if (this.dataCheckInterval) {
this.clearInterval(this.dataCheckInterval);
this.dataCheckInterval = null;
}
this.knownIds = []; // Leeren der bekannten IDs
this.setState('info.connection', false, true);
this.log.info('Shutdown complete.');
callback();
} catch (error) {
this.log.error(`Error during unload: ${error.message}`);
callback();
}
}
isValidHeader(buffer) {
return (
(buffer[0] >= 0x01 &&
buffer[0] <= 0x10 &&
buffer[1] === 0x04 &&
(buffer[2] === 0x24 || buffer[2] === 0x34)) ||
(buffer[0] >= 0x01 && buffer[0] <= 0x10 && buffer[1] === 0x01 && buffer[2] === 0x12)
);
}
getExpectedLength(buffer) {
// +3 Header, +2 CRC, =+5
if (buffer[2] === 0x24) {
return 41;
} // (0x24) 36+5=41
if (buffer[2] === 0x34) {
return 57;
} // (0x34) 52+5=57
if (buffer[1] === 0x01 && buffer[2] === 0x12) {
return 23;
} // (0x12) 18+5=23
return 0; // If an invalid packet arrives
}
validateCRC(buffer, length) {
const receivedCRC = (buffer[length - 1] << 8) | buffer[length - 2];
const calculatedCRC = this.calculateModbusCRC(buffer.slice(0, length - 2));
return receivedCRC === calculatedCRC;
}
calculateModbusCRC(data) {
let crc = 0xffff;
for (let i = 0; i < data.length; i++) {
crc ^= data[i];
for (let j = 0; j < 8; j++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0xa001;
} else {
crc >>= 1;
}
}
}
return crc;
}
async ensureObjectExists(id, { type, common, native = {} }) {
if (this.isShuttingDown || this.knownIds.includes(id)) {
return; // Nichts tun, wenn das Objekt bereits existiert oder das System herunterfährt
}
try {
await this.setObjectNotExistsAsync(id, {
type,
common,
native,
});
this.knownIds.push(id);
} catch (err) {
this.log.error(`Error creating state ${id}: ${err.message}`);
}
}
async processPacket(buffer) {
const bmsIndex = buffer[0] - 0x01;
const bmsFolder = `bms_${bmsIndex}`;
if (bmsIndex === 0) {
this.lastDataReceived = Date.now();
this.setState('info.connection', true, true);
}
// Stelle sicher, dass der BMS-Ordner existiert
await this.ensureObjectExists(bmsFolder, {
type: 'channel',
common: { name: `bms ${bmsIndex}` },
native: {},
});
const now = Date.now();
let updates = {};
if (buffer[2] === 0x24) {
updates = {
[`${bmsFolder}.pack_voltage`]: {
value: buffer.readUInt16BE(3) / 100.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.current`]: {
value: buffer.readInt16BE(5) / 100.0,
unit: 'A',
role: 'value.current',
ctype: 'number',
},
[`${bmsFolder}.remaining_capacity`]: {
value: buffer.readUInt16BE(7) / 100.0,
unit: 'Ah',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.total_capacity`]: {
value: buffer.readUInt16BE(9) / 100.0,
unit: 'AH',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.total_discharge_capacity`]: {
value: buffer.readUInt16BE(11) / 0.1,
unit: 'AH',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.soc`]: {
value: buffer.readUInt16BE(13) / 10.0,
unit: '%',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.soh`]: {
value: buffer.readUInt16BE(15) / 10.0,
unit: '%',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.cycle_count`]: {
value: buffer.readUInt16BE(17),
unit: 'cycles',
role: 'value',
ctype: 'number',
},
[`${bmsFolder}.average_cell_voltage`]: {
value: buffer.readUInt16BE(19) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.average_cell_temp`]: {
value: buffer.readInt16BE(21) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.max_cell_voltage`]: {
value: buffer.readUInt16BE(23) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.min_cell_voltage`]: {
value: buffer.readUInt16BE(25) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.max_cell_temp`]: {
value: buffer.readUInt16BE(27) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.min_cell_temp`]: {
value: buffer.readUInt16BE(29) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.maxdiscurt`]: {
value: buffer.readUInt16BE(33) / 1.0,
unit: 'A',
role: 'value.current',
ctype: 'number',
},
[`${bmsFolder}.maxchgcurt`]: {
value: buffer.readUInt16BE(35) / 1.0,
unit: 'A',
role: 'value.current',
ctype: 'number',
},
};
} else if (buffer[2] === 0x34) {
updates = {
[`${bmsFolder}.cell_1_voltage`]: {
value: buffer.readUInt16BE(3) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_2_voltage`]: {
value: buffer.readUInt16BE(5) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_3_voltage`]: {
value: buffer.readUInt16BE(7) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_4_voltage`]: {
value: buffer.readUInt16BE(9) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_5_voltage`]: {
value: buffer.readUInt16BE(11) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_6_voltage`]: {
value: buffer.readUInt16BE(13) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_7_voltage`]: {
value: buffer.readUInt16BE(15) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_8_voltage`]: {
value: buffer.readUInt16BE(17) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_9_voltage`]: {
value: buffer.readUInt16BE(19) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_10_voltage`]: {
value: buffer.readUInt16BE(21) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_11_voltage`]: {
value: buffer.readUInt16BE(23) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_12_voltage`]: {
value: buffer.readUInt16BE(25) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_13_voltage`]: {
value: buffer.readUInt16BE(27) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_14_voltage`]: {
value: buffer.readUInt16BE(29) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_15_voltage`]: {
value: buffer.readUInt16BE(31) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_16_voltage`]: {
value: buffer.readUInt16BE(33) / 1000.0,
unit: 'V',
role: 'value.voltage',
ctype: 'number',
},
[`${bmsFolder}.cell_temp_1`]: {
value: buffer.readUInt16BE(35) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.cell_temp_2`]: {
value: buffer.readUInt16BE(37) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.cell_temp_3`]: {
value: buffer.readUInt16BE(39) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.cell_temp_4`]: {
value: buffer.readUInt16BE(41) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.case_temp`]: {
value: buffer.readUInt16BE(51) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
[`${bmsFolder}.power_temp`]: {
value: buffer.readUInt16BE(53) / 10.0 - 273.15,
unit: '°C',
role: 'value.temperature',
ctype: 'number',
},
};
} else if (buffer[2] === 0x12) {
let activeAlarms = [];
let activeProtections = [];
const parseBits = byte => [...Array(8)].map((_, i) => (byte >> i) & 1);
updates = {};
// Spannungsalarme (Low/High)
const lowVoltageCells = [];
const highVoltageCells = [];
// Low Voltage Alarme für Zellen 1–8 (buffer[3])
parseBits(buffer[3]).forEach((bit, i) => {
if (bit) {
lowVoltageCells.push(i + 1);
}
});
// Low Voltage Alarme für Zellen 9–16 (buffer[4])
parseBits(buffer[4]).forEach((bit, i) => {
if (bit) {
lowVoltageCells.push(i + 9);
}
});
// High Voltage Alarme für Zellen 1–8 (buffer[5])
parseBits(buffer[5]).forEach((bit, i) => {
if (bit) {
highVoltageCells.push(i + 1);
}
});
// High Voltage Alarme für Zellen 9–16 (buffer[6])
parseBits(buffer[6]).forEach((bit, i) => {
if (bit) {
highVoltageCells.push(i + 9);
}
});
// Formatierung des Strings
const lowvoltStr = lowVoltageCells.length ? `Low: ${lowVoltageCells.join(', ')}` : '';
const highvoltStr = highVoltageCells.length ? `High: ${highVoltageCells.join(', ')}` : '';
const voltString = [lowvoltStr, highvoltStr].filter(Boolean).join(' | ') || '';
// Temperature Alarms (Low/High)
const lowTempCells = [];
const highTempCells = [];
// Low Temperature Alarme für Zellen 1–8 (buffer[7])
parseBits(buffer[7]).forEach((bit, i) => {
if (bit) {
lowTempCells.push(i + 1);
}
});
// High Temperature Alarme für Zellen 1–8 (buffer[8])
parseBits(buffer[8]).forEach((bit, i) => {
if (bit) {
highTempCells.push(i + 1);
}
});
// Formatierung des Strings
const lowtempStr = lowTempCells.length ? `Low: ${lowTempCells.join(', ')}` : '';
const hightempStr = highTempCells.length ? `High: ${highTempCells.join(', ')}` : '';
const tempString = [lowtempStr, hightempStr].filter(Boolean).join(' | ') || '';
// Balancing-Status
const balancingCells = [];
// Balancing für Zellen 1–8 (buffer[9])
parseBits(buffer[9]).forEach((bit, i) => {
if (bit) {
balancingCells.push(i + 1);
}
});
// Balancing für Zellen 9–16 (buffer[10])
parseBits(buffer[10]).forEach((bit, i) => {
if (bit) {
balancingCells.push(i + 9);
}
});
// Systemstatus (TB09)
const systemStatus = [];
if (buffer[11] & 0x01) {
systemStatus.push('Discharge');
}
if (buffer[11] & 0x02) {
systemStatus.push('Charge');
}
if (buffer[11] & 0x04) {
systemStatus.push('Floating Charge');
}
if (buffer[11] & 0x08) {
systemStatus.push('Full Charge');
}
if (buffer[11] & 0x10) {
systemStatus.push('Standy Mode');
}
if (buffer[11] & 0x20) {
systemStatus.push('Turn Off');
}
// Voltage Event Code nach TB02 dekodieren
if (buffer[12] & 0x01) {
activeAlarms.push('Cell High Voltage Alarm');
}
if (buffer[12] & 0x02) {
activeProtections.push('Cell Over Voltage Protection');
}
if (buffer[12] & 0x04) {
activeAlarms.push('Cell Low Voltage Alarm');
}
if (buffer[12] & 0x08) {
activeProtections.push('Cell Under Voltage Protection');
}
if (buffer[12] & 0x10) {
activeAlarms.push('Pack High Voltage Alarm');
}
if (buffer[12] & 0x20) {
activeProtections.push('Pack Over Voltage Protection');
}
if (buffer[12] & 0x40) {
activeAlarms.push('Pack Low Voltage Alarm');
}
if (buffer[12] & 0x80) {
activeProtections.push('Pack Under Voltage Protection');
}
// Temperature Event Code nach TB03 dekodieren
if (buffer[13] & 0x01) {
activeAlarms.push('Charge High Temperature Alarm');
}
if (buffer[13] & 0x02) {
activeProtections.push('Charge High Temperature Protection');
}
if (buffer[13] & 0x04) {
activeAlarms.push('Charge Low Temperature Alarm');
}
if (buffer[13] & 0x08) {
activeProtections.push('Charge Under Temperature Protection');
}
if (buffer[13] & 0x10) {
activeAlarms.push('Discharge High Temperature Alarm');
}
if (buffer[13] & 0x20) {
activeProtections.push('Discharge Over Temperature Protection');
}
if (buffer[13] & 0x40) {
activeAlarms.push('Discharge Low Temperature Alarm');
}
if (buffer[13] & 0x80) {
activeProtections.push('Discharge Under Temperature Protection');
}
// Environment Temperature Event Code nach TB04 dekodieren
if (buffer[14] & 0x01) {
activeAlarms.push('High Environment Temperature Alarm');
}
if (buffer[14] & 0x02) {
activeProtections.push('Over Environment Temperature Protection');
}
if (buffer[14] & 0x04) {
activeAlarms.push('Low Environment Temperature Alarm');
}
if (buffer[14] & 0x08) {
activeProtections.push('Under Environment Temperature Protection');
}
if (buffer[14] & 0x10) {
activeAlarms.push('High Power Temperature Alarm');
}
if (buffer[14] & 0x20) {
activeProtections.push('Over Power Temperature Protection');
}
if (buffer[14] & 0x40) {
activeAlarms.push('Cell Temperature Low Heating');
}
// Current Event Code nach TB05 dekodieren
if (buffer[15] & 0x01) {
activeAlarms.push('Charge Current Alarm');
}
if (buffer[15] & 0x02) {
activeProtections.push('Charge Over Current Protection');
}
if (buffer[15] & 0x04) {
activeProtections.push('Charge Second Level Current Protection');
}
if (buffer[15] & 0x08) {
activeAlarms.push('Discharge Current Alarm');
}
if (buffer[15] & 0x10) {
activeProtections.push('Discharge Over Current Protection');
}
if (buffer[15] & 0x20) {
activeProtections.push('Discharge Second Level Over Current Protection');
}
if (buffer[15] & 0x40) {
activeProtections.push('Output Short Circuit Protection');
}
// Second Current Event Code nach TB16 dekodieren
if (buffer[16] & 0x01) {
activeAlarms.push('Output Short Latch Up');
}
if (buffer[16] & 0x04) {
activeAlarms.push('Second Charge Latch Up');
}
if (buffer[16] & 0x08) {
activeAlarms.push('Second Discharge Latch Up');
}
// Residual Capacity Event Code nach TB06 dekodieren
if (buffer[17] & 0x04) {
activeAlarms.push('SOC Alarm');
}
if (buffer[17] & 0x08) {
activeProtections.push('SOC Protection');
}
if (buffer[17] & 0x10) {
activeAlarms.push('Cell Difference Alarm');
}
// FET Event Code nach TB07 dekodieren
const FETEvent = [];
if (buffer[18] & 0x01) {
FETEvent.push('Discharge FET On');
}
if (buffer[18] & 0x02) {
FETEvent.push('Charge FET On');
}
if (buffer[18] & 0x04) {
FETEvent.push('Current Limiting FET On');
}
if (buffer[18] & 0x08) {
FETEvent.push('Heating On');
}
// Battery Equalization State Code nach TB08 dekodieren
if (buffer[19] & 0x01) {
activeAlarms.push('Low SOC Alarm');
}
if (buffer[19] & 0x02) {
activeAlarms.push('Intermittent Charge');
}
if (buffer[19] & 0x04) {
activeAlarms.push('External Switch Conrol');
}
if (buffer[19] & 0x08) {
activeAlarms.push('Static Standy Sleep Mode');
}
if (buffer[19] & 0x10) {
activeAlarms.push('History Data Recording');
}
if (buffer[19] & 0x20) {
activeProtections.push('Under SOC Protections');
}
if (buffer[19] & 0x40) {
activeAlarms.push('Active Limited Current');
}
if (buffer[19] & 0x80) {
activeAlarms.push('Passive Limited Current');
}
// Hard Fault Event Code nach TB15 dekodieren
if (buffer[20] & 0x01) {
activeProtections.push('NTC Fault');
}
if (buffer[20] & 0x02) {
activeProtections.push('AFE Fault');
}
if (buffer[20] & 0x04) {
activeProtections.push('Charge Mosfet Fault');
}
if (buffer[20] & 0x08) {
activeProtections.push('Discharge Mosfet Fault');
}
if (buffer[20] & 0x10) {
activeProtections.push('Cell Fault');
}
if (buffer[20] & 0x20) {
activeProtections.push('Break Line Fault');
}
if (buffer[20] & 0x40) {
activeProtections.push('Key Fault');
}
if (buffer[20] & 0x80) {
activeProtections.push('Aerosol Alarm');
}
// Create string data points for active infos, alarms, protections, usw.
updates[`${bmsFolder}.system_status`] = {
value: systemStatus.join(', '),
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.active_balancing_cells`] = {
value: balancingCells.length ? balancingCells.join(', ') : '',
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.cell_temperature_alarms`] = {
value: tempString,
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.cell_voltage_alarms`] = {
value: voltString,
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.FET_status`] = {
value: FETEvent.join(', '),
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.active_alarms`] = {
value: activeAlarms.length ? activeAlarms.join(', ') : '',
role: 'text',
ctype: 'string',
};
updates[`${bmsFolder}.active_protections`] = {
value: activeProtections.length ? activeProtections.join(', ') : '',
role: 'text',
ctype: 'string',
};
}
for (const [key, { value, unit, role, ctype }] of Object.entries(updates)) {
if (!this.lastUpdate[key] || now - this.lastUpdate[key] >= this.updateInterval) {
this.lastUpdate[key] = now;
await this.ensureObjectExists(key, {
type: 'state',
common: {
name: key,
type: ctype,
role,
unit,
read: true,
write: false,
},
native: {},
});
if (!this.isShuttingDown) {
this.setState(key, { val: value, ack: true });
}
}
}
}
}
if (require.main !== module) {
module.exports = options => new SeplosV3Sniffer(options);
} else {
new SeplosV3Sniffer();
}