netkitty 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,25 @@ const StringContentEncodingEnum_1 = require("../lib/StringContentEncodingEnum");
13
13
  const NumberToBERBuffer_1 = require("../../helper/NumberToBERBuffer");
14
14
  const BufferToNumber_1 = require("../../helper/BufferToNumber");
15
15
  const NumberToBuffer_1 = require("../../helper/NumberToBuffer");
16
+ const GetBERIntegerLengthFromBuffer_1 = require("../lib/GetBERIntegerLengthFromBuffer");
17
+ var ItemDataType;
18
+ (function (ItemDataType) {
19
+ ItemDataType["Boolean"] = "Boolean";
20
+ ItemDataType["INT8"] = "INT8";
21
+ ItemDataType["INT16"] = "INT16";
22
+ ItemDataType["INT32"] = "INT32";
23
+ ItemDataType["INT64"] = "INT64";
24
+ ItemDataType["INT8U"] = "INT8U";
25
+ ItemDataType["INT16U"] = "INT16U";
26
+ ItemDataType["INT32U"] = "INT32U";
27
+ ItemDataType["FLOAT32"] = "FLOAT32";
28
+ ItemDataType["CODEDENUM"] = "CODED-ENUM";
29
+ ItemDataType["OCTETSTRING"] = "OCTET-STRING";
30
+ ItemDataType["VISIBLESTRING"] = "VISIBLE-STRING";
31
+ ItemDataType["TimeStamp"] = "TimeStamp";
32
+ ItemDataType["Quality"] = "Quality";
33
+ ItemDataType["Structure"] = "Structure";
34
+ })(ItemDataType || (ItemDataType = {}));
16
35
  class Goose extends BaseHeader_1.BaseHeader {
17
36
  constructor() {
18
37
  super(...arguments);
@@ -109,16 +128,19 @@ class Goose extends BaseHeader_1.BaseHeader {
109
128
  type: 'object',
110
129
  label: 'GOOSE PDU',
111
130
  decode: () => {
112
- let redTLVBufferLength = this.instance.length.getValue() - 8;
131
+ let readTLVBufferLength = this.packet.length - this.startPos - 8;
113
132
  while (!this.TLVInstance) {
114
133
  try {
115
- const buffer = this.readBytes(8, redTLVBufferLength, true);
134
+ const buffer = this.readBytes(8, readTLVBufferLength, true);
116
135
  this.TLVInstance = node_tlv_1.default.parse(buffer);
117
- this.readBytes(8, this.TLVInstance.length, false); //Update header length
136
+ const tagWithLength = buffer.toString('hex').replace(this.TLVInstance.bValue.toString('hex'), '#').split('#');
137
+ const tagWithLengthBytes = Math.max(...tagWithLength.map((value) => Math.ceil(value.length / 2)));
138
+ const TLVBufferLength = this.TLVInstance.bValue.length + tagWithLengthBytes;
139
+ this.TLVInstance = node_tlv_1.default.parse(this.readBytes(8, TLVBufferLength, false));
118
140
  }
119
141
  catch (e) {
120
- redTLVBufferLength -= 1;
121
- if (!redTLVBufferLength)
142
+ readTLVBufferLength -= 1;
143
+ if (!readTLVBufferLength)
122
144
  break;
123
145
  }
124
146
  }
@@ -164,7 +186,7 @@ class Goose extends BaseHeader_1.BaseHeader {
164
186
  },
165
187
  timeAllowedtoLive: {
166
188
  type: 'number',
167
- minimum: 1,
189
+ minimum: 0,
168
190
  maximum: 4294967295,
169
191
  label: 'TimeAllowedtoLive',
170
192
  decode: () => {
@@ -252,7 +274,7 @@ class Goose extends BaseHeader_1.BaseHeader {
252
274
  },
253
275
  stNum: {
254
276
  type: 'integer',
255
- minimum: 1,
277
+ minimum: 0,
256
278
  maximum: 4294967295,
257
279
  label: 'StNum',
258
280
  decode: () => {
@@ -267,7 +289,7 @@ class Goose extends BaseHeader_1.BaseHeader {
267
289
  let stNumValue = this.instance.goosePdu.stNum.getValue(-1, (nodePath) => this.recordError(nodePath, 'Not Found'));
268
290
  if (stNumValue < 0)
269
291
  return;
270
- if (stNumValue < 1 || stNumValue > 4294967295) {
292
+ if (stNumValue > 4294967295) {
271
293
  stNumValue = 4294967295;
272
294
  this.recordError(this.instance.goosePdu.stNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295');
273
295
  }
@@ -292,7 +314,7 @@ class Goose extends BaseHeader_1.BaseHeader {
292
314
  let sqNumValue = this.instance.goosePdu.sqNum.getValue(-1, (nodePath) => this.recordError(nodePath, 'Not Found'));
293
315
  if (sqNumValue < 0)
294
316
  return;
295
- if (sqNumValue < 0 || sqNumValue > 4294967295) {
317
+ if (sqNumValue > 4294967295) {
296
318
  sqNumValue = 4294967295;
297
319
  this.recordError(this.instance.goosePdu.sqNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295');
298
320
  }
@@ -386,36 +408,115 @@ class Goose extends BaseHeader_1.BaseHeader {
386
408
  type: 'array',
387
409
  label: 'AllData',
388
410
  items: {
389
- type: 'object',
390
- label: 'Data',
391
- properties: {
392
- dataType: {
393
- type: 'string',
394
- label: 'DataType',
395
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
396
- enum: [
397
- 'Boolean',
398
- 'INT8',
399
- 'INT16',
400
- 'INT32',
401
- 'INT64',
402
- 'INT8U',
403
- 'INT16U',
404
- 'INT32U',
405
- 'FLOAT32',
406
- 'CODED-ENUM',
407
- 'OCTET-STRING',
408
- 'VISIBLE-STRING',
409
- 'TimeStamp',
410
- 'Quality'
411
- ]
411
+ anyOf: [
412
+ {
413
+ type: 'object',
414
+ properties: {
415
+ dataType: { type: 'string', enum: [ItemDataType.Boolean] },
416
+ value: { type: 'boolean' }
417
+ }
418
+ },
419
+ {
420
+ type: 'object',
421
+ properties: {
422
+ dataType: { type: 'string', enum: [ItemDataType.INT8] },
423
+ value: { type: 'integer' }
424
+ }
425
+ },
426
+ {
427
+ type: 'object',
428
+ properties: {
429
+ dataType: { type: 'string', enum: [ItemDataType.INT16] },
430
+ value: { type: 'integer' }
431
+ }
432
+ },
433
+ {
434
+ type: 'object',
435
+ properties: {
436
+ dataType: { type: 'string', enum: [ItemDataType.INT32] },
437
+ value: { type: 'integer' }
438
+ }
439
+ },
440
+ {
441
+ type: 'object',
442
+ properties: {
443
+ dataType: { type: 'string', enum: [ItemDataType.INT64] },
444
+ value: { type: 'string', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.BIGINT }
445
+ }
446
+ },
447
+ {
448
+ type: 'object',
449
+ properties: {
450
+ dataType: { type: 'string', enum: [ItemDataType.INT8U] },
451
+ value: { type: 'integer' }
452
+ }
453
+ },
454
+ {
455
+ type: 'object',
456
+ properties: {
457
+ dataType: { type: 'string', enum: [ItemDataType.INT16U] },
458
+ value: { type: 'integer' }
459
+ }
460
+ },
461
+ {
462
+ type: 'object',
463
+ properties: {
464
+ dataType: { type: 'string', enum: [ItemDataType.INT32U] },
465
+ value: { type: 'integer' }
466
+ }
467
+ },
468
+ {
469
+ type: 'object',
470
+ properties: {
471
+ dataType: { type: 'string', enum: [ItemDataType.FLOAT32] },
472
+ value: { type: 'number' }
473
+ }
474
+ },
475
+ {
476
+ type: 'object',
477
+ properties: {
478
+ dataType: { type: 'string', enum: [ItemDataType.CODEDENUM] },
479
+ value: { type: 'integer' }
480
+ }
481
+ },
482
+ {
483
+ type: 'object',
484
+ properties: {
485
+ dataType: { type: 'string', enum: [ItemDataType.OCTETSTRING] },
486
+ value: { type: 'string', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.HEX }
487
+ }
488
+ },
489
+ {
490
+ type: 'object',
491
+ properties: {
492
+ dataType: { type: 'string', enum: [ItemDataType.VISIBLESTRING] },
493
+ value: { type: 'string', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.ASCII }
494
+ }
495
+ },
496
+ {
497
+ type: 'object',
498
+ properties: {
499
+ dataType: { type: 'string', enum: [ItemDataType.TimeStamp] },
500
+ value: { type: 'string' }
501
+ }
412
502
  },
413
- value: {
414
- type: 'string',
415
- contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.UTF8,
416
- label: 'Value'
503
+ {
504
+ type: 'object',
505
+ properties: {
506
+ dataType: { type: 'string', enum: [ItemDataType.Quality] },
507
+ value: { type: 'string', contentEncoding: StringContentEncodingEnum_1.StringContentEncodingEnum.BINARY }
508
+ }
509
+ },
510
+ {
511
+ type: 'object',
512
+ properties: {
513
+ dataType: { type: 'string', enum: [ItemDataType.Structure] },
514
+ value: {
515
+ $ref: '#/properties/goosePdu/properties/allData'
516
+ }
517
+ }
417
518
  }
418
- }
519
+ ]
419
520
  },
420
521
  /**
421
522
  * +-----------------------------------+-------------------+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -441,264 +542,15 @@ class Goose extends BaseHeader_1.BaseHeader {
441
542
  decode: () => {
442
543
  const allDataTLV = this.TLVChild.find(tlv => tlv.getTag('number') === 0xAB);
443
544
  const dataTLVs = allDataTLV ? allDataTLV.getChild() : [];
444
- const allData = [];
445
- dataTLVs.forEach((dataTLV) => {
446
- const length = dataTLV.getLength('number');
447
- const value = dataTLV.getValue('buffer');
448
- const dataItem = {
449
- dataType: '',
450
- value: ''
451
- };
452
- switch (dataTLV.getTag('number')) {
453
- case 0x83:
454
- {
455
- dataItem.dataType = 'Boolean';
456
- dataItem.value = String(!!parseInt(value.toString('hex'), 16));
457
- }
458
- break;
459
- case 0x84:
460
- {
461
- switch (length) {
462
- case 2:
463
- {
464
- dataItem.dataType = 'CODED-ENUM';
465
- dataItem.value = parseInt(value.toString('hex'), 16).toString();
466
- }
467
- break;
468
- case 3:
469
- {
470
- dataItem.dataType = 'Quality';
471
- dataItem.value = parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0');
472
- }
473
- break;
474
- }
475
- }
476
- break;
477
- case 0x85:
478
- {
479
- if (length === 2) {
480
- {
481
- dataItem.dataType = 'INT8';
482
- dataItem.value = (0, HexToNumber_1.HexToInt8)(value.toString('hex')).toString();
483
- }
484
- }
485
- else if (length === 3) {
486
- {
487
- dataItem.dataType = 'INT16';
488
- dataItem.value = (0, HexToNumber_1.HexToInt16)(value.toString('hex')).toString();
489
- }
490
- }
491
- else if (length === 5) {
492
- {
493
- dataItem.dataType = 'INT32';
494
- dataItem.value = (0, HexToNumber_1.HexToInt32)(value.toString('hex')).toString();
495
- }
496
- }
497
- else if (length === 9) {
498
- {
499
- dataItem.dataType = 'INT64';
500
- dataItem.value = (0, HexToNumber_1.HexToInt64)(value.toString('hex')).toString();
501
- }
502
- }
503
- }
504
- break;
505
- case 0x86:
506
- {
507
- switch (length) {
508
- case 2:
509
- {
510
- dataItem.dataType = 'INT8U';
511
- dataItem.value = (0, HexToNumber_1.HexToUInt8)(value.toString('hex')).toString();
512
- }
513
- break;
514
- case 3:
515
- {
516
- dataItem.dataType = 'INT16U';
517
- dataItem.value = (0, HexToNumber_1.HexToUInt16)(value.toString('hex')).toString();
518
- }
519
- break;
520
- case 5:
521
- {
522
- dataItem.dataType = 'INT32U';
523
- dataItem.value = (0, HexToNumber_1.HexToUInt32)(value.toString('hex')).toString();
524
- }
525
- break;
526
- }
527
- }
528
- break;
529
- case 0x87:
530
- {
531
- dataItem.dataType = 'FLOAT32';
532
- dataItem.value = (0, HexToNumber_1.HexToFloat32)(value.toString('hex')).toString();
533
- }
534
- break;
535
- case 0x89:
536
- {
537
- dataItem.dataType = 'OCTET-STRING';
538
- dataItem.value = value.toString('ascii');
539
- }
540
- break;
541
- case 0x8a:
542
- {
543
- dataItem.dataType = 'VISIBLE-STRING';
544
- dataItem.value = value.toString('ascii');
545
- }
546
- break;
547
- case 0x91:
548
- {
549
- dataItem.dataType = 'TimeStamp';
550
- dataItem.value = parseInt(value.toString('hex'), 16).toString();
551
- }
552
- break;
553
- }
554
- if (!dataItem.dataType)
555
- return;
556
- allData.push(dataItem);
557
- });
545
+ const allData = this.decodeDataTLVItem(dataTLVs);
558
546
  if (allData.length)
559
547
  this.instance.goosePdu.allData.setValue(allData);
560
548
  },
561
549
  encode: () => {
562
550
  const allData = this.instance.goosePdu.allData.getValue([], (nodePath) => this.recordError(nodePath, 'Not Found'));
563
- const dataItemTLVs = allData
564
- .map((dataItem, index) => {
565
- const errorNodePath = this.instance.goosePdu.allData.getPath(index);
566
- dataItem.value = dataItem.value.trim();
567
- switch (dataItem.dataType) {
568
- case 'Boolean': {
569
- const availableStringValues = ['FALSE', 'TRUE'];
570
- const stringValue = dataItem.value.toUpperCase();
571
- let booleanIntValue = 0;
572
- if (availableStringValues.includes(stringValue)) {
573
- booleanIntValue = availableStringValues.indexOf(stringValue);
574
- }
575
- else {
576
- this.recordError(errorNodePath, 'Invalid Boolean value');
577
- }
578
- return new node_tlv_1.default(0x83, Buffer.from(booleanIntValue.toString(16).padStart(2, '0'), 'hex'));
579
- }
580
- case 'INT8': {
581
- let intValue = parseInt(dataItem.value);
582
- if (isNaN(intValue))
583
- this.recordError(errorNodePath, 'Invalid INT8 value');
584
- intValue = intValue ? intValue : 0;
585
- if (intValue < -128 || intValue > 127)
586
- this.recordError(errorNodePath, 'Invalid INT8 value');
587
- return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int8ToBERHex)(intValue).padStart(2 * 2, '0'), 'hex'));
588
- }
589
- case 'INT16': {
590
- let intValue = parseInt(dataItem.value);
591
- if (isNaN(intValue))
592
- this.recordError(errorNodePath, 'Invalid INT16 value');
593
- intValue = intValue ? intValue : 0;
594
- if (intValue < -32768 || intValue > 32767)
595
- this.recordError(errorNodePath, 'Invalid INT16 value');
596
- return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int16ToBERHex)(intValue).padStart(3 * 2, '0'), 'hex'));
597
- }
598
- case 'INT32': {
599
- let intValue = parseInt(dataItem.value);
600
- if (isNaN(intValue))
601
- this.recordError(errorNodePath, 'Invalid INT32 value');
602
- intValue = intValue ? intValue : 0;
603
- if (intValue < -2147483648 || intValue > 2147483647)
604
- this.recordError(errorNodePath, 'Invalid INT32 value');
605
- return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int32ToBERHex)(intValue).padStart(5 * 2, '0'), 'hex'));
606
- }
607
- case 'INT64': {
608
- const intValue = BigInt(dataItem.value);
609
- if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807'))
610
- this.recordError(errorNodePath, 'Invalid INT64 value');
611
- return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int64ToBERHex)(intValue).padStart(9 * 2, '0'), 'hex'));
612
- }
613
- case 'INT8U': {
614
- let uintValue = parseInt(dataItem.value);
615
- if (isNaN(uintValue))
616
- this.recordError(errorNodePath, 'Invalid INT8U value');
617
- uintValue = uintValue ? uintValue : 0;
618
- if (uintValue < 0 || uintValue > 255)
619
- this.recordError(errorNodePath, 'Invalid INT8U value');
620
- return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt8ToBERHex)(uintValue).padStart(2 * 2, '0'), 'hex'));
621
- }
622
- case 'INT16U': {
623
- let uintValue = parseInt(dataItem.value);
624
- if (isNaN(uintValue))
625
- this.recordError(errorNodePath, 'Invalid INT16U value');
626
- uintValue = uintValue ? uintValue : 0;
627
- if (uintValue < 0 || uintValue > 65535)
628
- this.recordError(errorNodePath, 'Invalid INT16U value');
629
- return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(uintValue).padStart(3 * 2, '0'), 'hex'));
630
- }
631
- case 'INT32U': {
632
- let uintValue = parseInt(dataItem.value);
633
- if (isNaN(uintValue))
634
- this.recordError(errorNodePath, 'Invalid INT32U value');
635
- uintValue = uintValue ? uintValue : 0;
636
- if (uintValue < 0 || uintValue > 4294967295)
637
- this.recordError(errorNodePath, 'Invalid INT32U value');
638
- return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt32ToBERHex)(uintValue).padStart(5 * 2, '0'), 'hex'));
639
- }
640
- case 'FLOAT32': {
641
- let float32Value = parseFloat(dataItem.value);
642
- if (isNaN(float32Value))
643
- this.recordError(errorNodePath, 'Invalid FLOAT32 value');
644
- float32Value = float32Value ? float32Value : 0;
645
- return new node_tlv_1.default(0x86, Buffer.from((0, NumberToHex_1.Float32ToHex)(float32Value).padStart(4 * 2, '0'), 'hex'));
646
- }
647
- case 'CODED-ENUM': {
648
- let codedEnumValue = parseInt(dataItem.value);
649
- if (isNaN(codedEnumValue))
650
- this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
651
- codedEnumValue = codedEnumValue ? codedEnumValue : 0;
652
- return new node_tlv_1.default(0x84, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(codedEnumValue).padStart(2 * 2, '0'), 'hex'));
653
- }
654
- case 'OCTET-STRING': {
655
- const asciiText = dataItem.value;
656
- if (!asciiText) {
657
- this.recordError(errorNodePath, 'Empty OCTET-STRING, ignored');
658
- return null;
659
- }
660
- if (asciiText.length > 20)
661
- this.recordError(errorNodePath, 'OCTET-STRING too long');
662
- const hex = Buffer.from(asciiText, 'ascii').toString('hex').padStart(20 * 2);
663
- return new node_tlv_1.default(0x89, Buffer.from(hex, 'hex').subarray(0, 20));
664
- }
665
- case 'VISIBLE-STRING': {
666
- const asciiText = dataItem.value;
667
- if (!asciiText) {
668
- this.recordError(errorNodePath, 'Empty VISIBLE-STRING, ignored');
669
- return null;
670
- }
671
- if (asciiText.length > 35)
672
- this.recordError(errorNodePath, 'VISIBLE-STRING too long');
673
- const hex = Buffer.from(asciiText, 'ascii').toString('hex').padStart(35 * 2);
674
- return new node_tlv_1.default(0x8a, Buffer.from(hex, 'hex').subarray(0, 35));
675
- }
676
- case 'TimeStamp': {
677
- let timestamp = parseInt(dataItem.value);
678
- if (isNaN(timestamp))
679
- this.recordError(errorNodePath, 'Invalid TimeStamp value');
680
- timestamp = timestamp ? timestamp : 0;
681
- return new node_tlv_1.default(0x91, Buffer.from(timestamp.toString(16).padStart(8 * 2, '0'), 'hex'));
682
- }
683
- case 'Quality': {
684
- const bitString = dataItem.value;
685
- let intValue = parseInt(bitString, 2);
686
- if (isNaN(intValue))
687
- this.recordError(errorNodePath, 'Invalid Quality value');
688
- intValue = intValue ? intValue : 0;
689
- return new node_tlv_1.default(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'));
690
- }
691
- default: {
692
- this.recordError(errorNodePath, 'Invalid dataType, ignored');
693
- return null;
694
- }
695
- }
696
- })
697
- .filter((dataItemTLV) => !!dataItemTLV);
551
+ const dataItemTLVs = this.encodeDataTLVItem(allData);
698
552
  if (dataItemTLVs.length) {
699
- let dataItemsHex = '';
700
- dataItemTLVs.forEach(dataItemTLV => dataItemsHex = `${dataItemsHex}${dataItemTLV.toString()}`);
701
- const allDataTLV = new node_tlv_1.default(0xAB, dataItemsHex);
553
+ const allDataTLV = new node_tlv_1.default(0xAB, Buffer.concat(dataItemTLVs.map((dataItemTLV) => Buffer.concat([dataItemTLV.bTag, dataItemTLV.bLength, dataItemTLV.bValue]))));
702
554
  this.TLVChild.push(allDataTLV);
703
555
  }
704
556
  }
@@ -711,6 +563,287 @@ class Goose extends BaseHeader_1.BaseHeader {
711
563
  this.name = 'IEC61850 GOOSE';
712
564
  this.nickname = 'GOOSE';
713
565
  }
566
+ /**
567
+ * Decode data TLV items
568
+ * @param dataTLVs
569
+ * @protected
570
+ */
571
+ decodeDataTLVItem(dataTLVs) {
572
+ const dataItems = [];
573
+ dataTLVs.forEach((dataTLV) => {
574
+ const length = dataTLV.getLength('number');
575
+ const value = dataTLV.getValue('buffer');
576
+ switch (dataTLV.getTag('number')) {
577
+ case 0x83:
578
+ {
579
+ dataItems.push({
580
+ dataType: ItemDataType.Boolean,
581
+ value: !!parseInt(value.toString('hex'), 16)
582
+ });
583
+ }
584
+ break;
585
+ case 0x84:
586
+ {
587
+ switch (length) {
588
+ case 2:
589
+ {
590
+ dataItems.push({
591
+ dataType: ItemDataType.CODEDENUM,
592
+ value: parseInt(value.toString('hex'), 16)
593
+ });
594
+ }
595
+ break;
596
+ case 3:
597
+ {
598
+ dataItems.push({
599
+ dataType: ItemDataType.Quality,
600
+ value: parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0')
601
+ });
602
+ }
603
+ break;
604
+ }
605
+ }
606
+ break;
607
+ case 0x85:
608
+ {
609
+ const integerRealLength = (0, GetBERIntegerLengthFromBuffer_1.GetBERIntegerLengthFromBuffer)(value);
610
+ if (integerRealLength === 1) {
611
+ dataItems.push({
612
+ dataType: ItemDataType.INT8,
613
+ value: (0, HexToNumber_1.HexToInt8)(value.toString('hex'))
614
+ });
615
+ }
616
+ else if (integerRealLength === 2) {
617
+ dataItems.push({
618
+ dataType: ItemDataType.INT16,
619
+ value: (0, HexToNumber_1.HexToInt16)(value.toString('hex'))
620
+ });
621
+ }
622
+ else if (integerRealLength === 4) {
623
+ dataItems.push({
624
+ dataType: ItemDataType.INT32,
625
+ value: (0, HexToNumber_1.HexToInt32)(value.toString('hex'))
626
+ });
627
+ }
628
+ else if (integerRealLength === 8) {
629
+ dataItems.push({
630
+ dataType: ItemDataType.INT64,
631
+ value: (0, HexToNumber_1.HexToInt64)(value.toString('hex'))
632
+ });
633
+ }
634
+ }
635
+ break;
636
+ case 0x86:
637
+ {
638
+ const unsignedIntegerRealLength = (0, GetBERIntegerLengthFromBuffer_1.GetBERIntegerLengthFromBuffer)(value);
639
+ switch (unsignedIntegerRealLength) {
640
+ case 1:
641
+ {
642
+ dataItems.push({
643
+ dataType: ItemDataType.INT8U,
644
+ value: (0, HexToNumber_1.HexToUInt8)(value.toString('hex'))
645
+ });
646
+ }
647
+ break;
648
+ case 2:
649
+ {
650
+ dataItems.push({
651
+ dataType: ItemDataType.INT16U,
652
+ value: (0, HexToNumber_1.HexToUInt16)(value.toString('hex'))
653
+ });
654
+ }
655
+ break;
656
+ case 4:
657
+ {
658
+ dataItems.push({
659
+ dataType: ItemDataType.INT32U,
660
+ value: (0, HexToNumber_1.HexToUInt32)(value.toString('hex'))
661
+ });
662
+ }
663
+ break;
664
+ }
665
+ }
666
+ break;
667
+ case 0x87:
668
+ {
669
+ dataItems.push({
670
+ dataType: ItemDataType.FLOAT32,
671
+ value: (0, HexToNumber_1.HexToFloat32)(value.toString('hex'))
672
+ });
673
+ }
674
+ break;
675
+ case 0x89:
676
+ {
677
+ dataItems.push({
678
+ dataType: ItemDataType.OCTETSTRING,
679
+ value: value.toString('hex')
680
+ });
681
+ }
682
+ break;
683
+ case 0x8a:
684
+ {
685
+ dataItems.push({
686
+ dataType: ItemDataType.VISIBLESTRING,
687
+ value: value.toString('ascii')
688
+ });
689
+ }
690
+ break;
691
+ case 0x91:
692
+ {
693
+ dataItems.push({
694
+ dataType: ItemDataType.TimeStamp,
695
+ value: parseInt(value.toString('hex'), 16).toString()
696
+ });
697
+ }
698
+ break;
699
+ case 0xa2:
700
+ {
701
+ const subDataTLVs = node_tlv_1.default.parseList(value);
702
+ dataItems.push({
703
+ dataType: ItemDataType.Structure,
704
+ value: this.decodeDataTLVItem(subDataTLVs)
705
+ });
706
+ }
707
+ break;
708
+ }
709
+ });
710
+ return dataItems;
711
+ }
712
+ /**
713
+ * Encode data TLV items
714
+ * @param dataItems
715
+ * @protected
716
+ */
717
+ encodeDataTLVItem(dataItems) {
718
+ return dataItems
719
+ .map((dataItem, index) => {
720
+ const errorNodePath = this.instance.goosePdu.allData.getPath(index);
721
+ switch (dataItem.dataType) {
722
+ case ItemDataType.Boolean: {
723
+ const booleanIntValue = dataItem.value ? 1 : 0;
724
+ return new node_tlv_1.default(0x83, Buffer.from(booleanIntValue.toString(16).padStart(2, '0'), 'hex'));
725
+ }
726
+ case ItemDataType.INT8: {
727
+ let intValue = dataItem.value;
728
+ if (isNaN(intValue))
729
+ this.recordError(errorNodePath, 'Invalid INT8 value');
730
+ intValue = intValue ? intValue : 0;
731
+ if (intValue < -128 || intValue > 127)
732
+ this.recordError(errorNodePath, 'Invalid INT8 value');
733
+ return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int8ToBERHex)(intValue), 'hex'));
734
+ }
735
+ case ItemDataType.INT16: {
736
+ let intValue = dataItem.value;
737
+ if (isNaN(intValue))
738
+ this.recordError(errorNodePath, 'Invalid INT16 value');
739
+ intValue = intValue ? intValue : 0;
740
+ if (intValue < -32768 || intValue > 32767)
741
+ this.recordError(errorNodePath, 'Invalid INT16 value');
742
+ return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int16ToBERHex)(intValue), 'hex'));
743
+ }
744
+ case ItemDataType.INT32: {
745
+ let intValue = dataItem.value;
746
+ if (isNaN(intValue))
747
+ this.recordError(errorNodePath, 'Invalid INT32 value');
748
+ intValue = intValue ? intValue : 0;
749
+ if (intValue < -2147483648 || intValue > 2147483647)
750
+ this.recordError(errorNodePath, 'Invalid INT32 value');
751
+ return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int32ToBERHex)(intValue), 'hex'));
752
+ }
753
+ case ItemDataType.INT64: {
754
+ const intValue = BigInt(dataItem.value);
755
+ if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807'))
756
+ this.recordError(errorNodePath, 'Invalid INT64 value');
757
+ return new node_tlv_1.default(0x85, Buffer.from((0, NumberToBERHex_1.Int64ToBERHex)(intValue), 'hex'));
758
+ }
759
+ case ItemDataType.INT8U: {
760
+ let uintValue = dataItem.value;
761
+ if (isNaN(uintValue))
762
+ this.recordError(errorNodePath, 'Invalid INT8U value');
763
+ uintValue = uintValue ? uintValue : 0;
764
+ if (uintValue < 0 || uintValue > 255)
765
+ this.recordError(errorNodePath, 'Invalid INT8U value');
766
+ return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt8ToBERHex)(uintValue), 'hex'));
767
+ }
768
+ case ItemDataType.INT16U: {
769
+ let uintValue = dataItem.value;
770
+ if (isNaN(uintValue))
771
+ this.recordError(errorNodePath, 'Invalid INT16U value');
772
+ uintValue = uintValue ? uintValue : 0;
773
+ if (uintValue < 0 || uintValue > 65535)
774
+ this.recordError(errorNodePath, 'Invalid INT16U value');
775
+ return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(uintValue), 'hex'));
776
+ }
777
+ case ItemDataType.INT32U: {
778
+ let uintValue = dataItem.value;
779
+ if (isNaN(uintValue))
780
+ this.recordError(errorNodePath, 'Invalid INT32U value');
781
+ uintValue = uintValue ? uintValue : 0;
782
+ if (uintValue < 0 || uintValue > 4294967295)
783
+ this.recordError(errorNodePath, 'Invalid INT32U value');
784
+ return new node_tlv_1.default(0x86, Buffer.from((0, NumberToBERHex_1.UInt32ToBERHex)(uintValue), 'hex'));
785
+ }
786
+ case ItemDataType.FLOAT32: {
787
+ let float32Value = dataItem.value;
788
+ if (isNaN(float32Value))
789
+ this.recordError(errorNodePath, 'Invalid FLOAT32 value');
790
+ float32Value = float32Value ? float32Value : 0;
791
+ return new node_tlv_1.default(0x87, Buffer.from((0, NumberToBERHex_1.Float32ToBERHex)(float32Value), 'hex'));
792
+ }
793
+ case ItemDataType.CODEDENUM: {
794
+ let codedEnumValue = dataItem.value;
795
+ if (isNaN(codedEnumValue))
796
+ this.recordError(errorNodePath, 'Invalid CODED-ENUM value');
797
+ codedEnumValue = codedEnumValue ? codedEnumValue : 0;
798
+ return new node_tlv_1.default(0x84, Buffer.from((0, NumberToBERHex_1.UInt16ToBERHex)(codedEnumValue), 'hex'));
799
+ }
800
+ case ItemDataType.OCTETSTRING: {
801
+ const hexText = dataItem.value;
802
+ if (!hexText) {
803
+ this.recordError(errorNodePath, 'Empty OCTET-STRING, ignored');
804
+ return null;
805
+ }
806
+ if (hexText.length > 20)
807
+ this.recordError(errorNodePath, 'OCTET-STRING too long');
808
+ return new node_tlv_1.default(0x89, Buffer.from(hexText, 'hex').subarray(0, 20));
809
+ }
810
+ case ItemDataType.VISIBLESTRING: {
811
+ const asciiText = dataItem.value;
812
+ if (!asciiText) {
813
+ this.recordError(errorNodePath, 'Empty VISIBLE-STRING, ignored');
814
+ return null;
815
+ }
816
+ if (asciiText.length > 35)
817
+ this.recordError(errorNodePath, 'VISIBLE-STRING too long');
818
+ const hex = Buffer.from(asciiText, 'ascii').toString('hex').padStart(35 * 2);
819
+ return new node_tlv_1.default(0x8a, Buffer.from(hex, 'hex').subarray(0, 35));
820
+ }
821
+ case ItemDataType.TimeStamp: {
822
+ let timestamp = parseInt(dataItem.value);
823
+ if (isNaN(timestamp))
824
+ this.recordError(errorNodePath, 'Invalid TimeStamp value');
825
+ timestamp = timestamp ? timestamp : 0;
826
+ return new node_tlv_1.default(0x91, Buffer.from(timestamp.toString(16), 'hex'));
827
+ }
828
+ case ItemDataType.Quality: {
829
+ const bitString = dataItem.value;
830
+ let intValue = parseInt(bitString, 2);
831
+ if (isNaN(intValue))
832
+ this.recordError(errorNodePath, 'Invalid Quality value');
833
+ intValue = intValue ? intValue : 0;
834
+ return new node_tlv_1.default(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'));
835
+ }
836
+ case ItemDataType.Structure: {
837
+ return new node_tlv_1.default(0xa2, Buffer.concat(this.encodeDataTLVItem(dataItem.value).map((tlv) => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))));
838
+ }
839
+ default: {
840
+ this.recordError(errorNodePath, 'Invalid dataType, ignored');
841
+ return null;
842
+ }
843
+ }
844
+ })
845
+ .filter((value) => !!value);
846
+ }
714
847
  match() {
715
848
  if (!this.prevCodecModule)
716
849
  return false;