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.
@@ -12,6 +12,7 @@ import {
12
12
  HexToUInt8
13
13
  } from '../../helper/HexToNumber'
14
14
  import {
15
+ Float32ToBERHex,
15
16
  Int16ToBERHex,
16
17
  Int32ToBERHex,
17
18
  Int64ToBERHex,
@@ -20,16 +21,107 @@ import {
20
21
  UInt32ToBERHex,
21
22
  UInt8ToBERHex
22
23
  } from '../../helper/NumberToBERHex'
23
- import {Float32ToHex, UInt16ToHex} from '../../helper/NumberToHex'
24
+ import {UInt16ToHex} from '../../helper/NumberToHex'
24
25
  import {StringContentEncodingEnum} from '../lib/StringContentEncodingEnum'
25
26
  import {UInt32ToBERBuffer} from '../../helper/NumberToBERBuffer'
26
27
  import {BufferToUInt16} from '../../helper/BufferToNumber'
27
28
  import {UInt16ToBuffer} from '../../helper/NumberToBuffer'
29
+ import {GetBERIntegerLengthFromBuffer} from '../lib/GetBERIntegerLengthFromBuffer'
28
30
 
29
- type AllDataItem = {
30
- dataType: string
31
+ enum ItemDataType {
32
+ Boolean = 'Boolean',
33
+ INT8 = 'INT8',
34
+ INT16 = 'INT16',
35
+ INT32 = 'INT32',
36
+ INT64 = 'INT64',
37
+ INT8U = 'INT8U',
38
+ INT16U = 'INT16U',
39
+ INT32U = 'INT32U',
40
+ FLOAT32 = 'FLOAT32',
41
+ CODEDENUM = 'CODED-ENUM',
42
+ OCTETSTRING = 'OCTET-STRING',
43
+ VISIBLESTRING = 'VISIBLE-STRING',
44
+ TimeStamp = 'TimeStamp',
45
+ Quality = 'Quality',
46
+ Structure = 'Structure'
47
+ }
48
+
49
+ type BooleanDataItem = {
50
+ dataType: ItemDataType.Boolean
51
+ value: boolean
52
+ }
53
+ type INT8DataItem = {
54
+ dataType: ItemDataType.INT8
55
+ value: number
56
+ }
57
+ type INT16DataItem = {
58
+ dataType: ItemDataType.INT16
59
+ value: number
60
+ }
61
+ type INT32DataItem = {
62
+ dataType: ItemDataType.INT32
63
+ value: number
64
+ }
65
+ type INT64DataItem = {
66
+ dataType: ItemDataType.INT64
67
+ value: bigint
68
+ }
69
+ type INT8UDataItem = {
70
+ dataType: ItemDataType.INT8U
71
+ value: number
72
+ }
73
+ type INT16UDataItem = {
74
+ dataType: ItemDataType.INT16U
75
+ value: number
76
+ }
77
+ type INT32UDataItem = {
78
+ dataType: ItemDataType.INT32U
79
+ value: number
80
+ }
81
+ type FLOAT32DataItem = {
82
+ dataType: ItemDataType.FLOAT32
83
+ value: number
84
+ }
85
+ type CODEDENUMDataItem = {
86
+ dataType: ItemDataType.CODEDENUM
87
+ value: number
88
+ }
89
+ type OCTETSTRINGDataItem = {
90
+ dataType: ItemDataType.OCTETSTRING
91
+ value: string
92
+ }
93
+ type VISIBLESTRINGDataItem = {
94
+ dataType: ItemDataType.VISIBLESTRING
95
+ value: string
96
+ }
97
+ type TimeStampDataItem = {
98
+ dataType: ItemDataType.TimeStamp
31
99
  value: string
32
100
  }
101
+ type QualityDataItem = {
102
+ dataType: ItemDataType.Quality
103
+ value: string
104
+ }
105
+ type StructureDataItem = {
106
+ dataType: ItemDataType.Structure
107
+ value: DataItem[]
108
+ }
109
+
110
+ type DataItem = BooleanDataItem |
111
+ INT8DataItem |
112
+ INT16DataItem |
113
+ INT32DataItem |
114
+ INT64DataItem |
115
+ INT8UDataItem |
116
+ INT16UDataItem |
117
+ INT32UDataItem |
118
+ FLOAT32DataItem |
119
+ CODEDENUMDataItem |
120
+ OCTETSTRINGDataItem |
121
+ VISIBLESTRINGDataItem |
122
+ TimeStampDataItem |
123
+ QualityDataItem |
124
+ StructureDataItem
33
125
 
34
126
  export class Goose extends BaseHeader {
35
127
 
@@ -128,15 +220,18 @@ export class Goose extends BaseHeader {
128
220
  type: 'object',
129
221
  label: 'GOOSE PDU',
130
222
  decode: (): void => {
131
- let redTLVBufferLength: number = (this.instance.length.getValue() as number) - 8
223
+ let readTLVBufferLength: number = this.packet.length - this.startPos - 8
132
224
  while (!this.TLVInstance) {
133
225
  try {
134
- const buffer: Buffer = this.readBytes(8, redTLVBufferLength, true)
226
+ const buffer: Buffer = this.readBytes(8, readTLVBufferLength, true)
135
227
  this.TLVInstance = TLV.parse(buffer)
136
- this.readBytes(8, this.TLVInstance.length, false)//Update header length
228
+ const tagWithLength: string[] = buffer.toString('hex').replace(this.TLVInstance.bValue.toString('hex'), '#').split('#')
229
+ const tagWithLengthBytes: number = Math.max(...tagWithLength.map((value: string): number => Math.ceil(value.length / 2)))
230
+ const TLVBufferLength: number = this.TLVInstance.bValue.length + tagWithLengthBytes
231
+ this.TLVInstance = TLV.parse(this.readBytes(8, TLVBufferLength, false))
137
232
  } catch (e) {
138
- redTLVBufferLength -= 1
139
- if (!redTLVBufferLength) break
233
+ readTLVBufferLength -= 1
234
+ if (!readTLVBufferLength) break
140
235
  }
141
236
  }
142
237
  this.TLVChild = this.TLVInstance ? this.TLVInstance.getChild() : []
@@ -178,7 +273,7 @@ export class Goose extends BaseHeader {
178
273
  },
179
274
  timeAllowedtoLive: {
180
275
  type: 'number',
181
- minimum: 1,
276
+ minimum: 0,
182
277
  maximum: 4294967295,
183
278
  label: 'TimeAllowedtoLive',
184
279
  decode: (): void => {
@@ -258,7 +353,7 @@ export class Goose extends BaseHeader {
258
353
  },
259
354
  stNum: {
260
355
  type: 'integer',
261
- minimum: 1,
356
+ minimum: 0,
262
357
  maximum: 4294967295,
263
358
  label: 'StNum',
264
359
  decode: (): void => {
@@ -270,7 +365,7 @@ export class Goose extends BaseHeader {
270
365
  encode: (): void => {
271
366
  let stNumValue: number = this.instance.goosePdu.stNum.getValue(-1, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
272
367
  if (stNumValue < 0) return
273
- if (stNumValue < 1 || stNumValue > 4294967295) {
368
+ if (stNumValue > 4294967295) {
274
369
  stNumValue = 4294967295
275
370
  this.recordError(this.instance.goosePdu.stNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295')
276
371
  }
@@ -292,7 +387,7 @@ export class Goose extends BaseHeader {
292
387
  encode: (): void => {
293
388
  let sqNumValue: number = this.instance.goosePdu.sqNum.getValue(-1, (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
294
389
  if (sqNumValue < 0) return
295
- if (sqNumValue < 0 || sqNumValue > 4294967295) {
390
+ if (sqNumValue > 4294967295) {
296
391
  sqNumValue = 4294967295
297
392
  this.recordError(this.instance.goosePdu.sqNum.getPath(), 'This INTEGER value shall have a range of 1 to 4294967295')
298
393
  }
@@ -375,36 +470,115 @@ export class Goose extends BaseHeader {
375
470
  type: 'array',
376
471
  label: 'AllData',
377
472
  items: {
378
- type: 'object',
379
- label: 'Data',
380
- properties: {
381
- dataType: {
382
- type: 'string',
383
- label: 'DataType',
384
- contentEncoding: StringContentEncodingEnum.UTF8,
385
- enum: [
386
- 'Boolean',
387
- 'INT8',
388
- 'INT16',
389
- 'INT32',
390
- 'INT64',
391
- 'INT8U',
392
- 'INT16U',
393
- 'INT32U',
394
- 'FLOAT32',
395
- 'CODED-ENUM',
396
- 'OCTET-STRING',
397
- 'VISIBLE-STRING',
398
- 'TimeStamp',
399
- 'Quality'
400
- ]
473
+ anyOf: [
474
+ {
475
+ type: 'object',
476
+ properties: {
477
+ dataType: {type: 'string', enum: [ItemDataType.Boolean]},
478
+ value: {type: 'boolean'}
479
+ }
480
+ },
481
+ {
482
+ type: 'object',
483
+ properties: {
484
+ dataType: {type: 'string', enum: [ItemDataType.INT8]},
485
+ value: {type: 'integer'}
486
+ }
487
+ },
488
+ {
489
+ type: 'object',
490
+ properties: {
491
+ dataType: {type: 'string', enum: [ItemDataType.INT16]},
492
+ value: {type: 'integer'}
493
+ }
494
+ },
495
+ {
496
+ type: 'object',
497
+ properties: {
498
+ dataType: {type: 'string', enum: [ItemDataType.INT32]},
499
+ value: {type: 'integer'}
500
+ }
501
+ },
502
+ {
503
+ type: 'object',
504
+ properties: {
505
+ dataType: {type: 'string', enum: [ItemDataType.INT64]},
506
+ value: {type: 'string', contentEncoding: StringContentEncodingEnum.BIGINT}
507
+ }
508
+ },
509
+ {
510
+ type: 'object',
511
+ properties: {
512
+ dataType: {type: 'string', enum: [ItemDataType.INT8U]},
513
+ value: {type: 'integer'}
514
+ }
401
515
  },
402
- value: {
403
- type: 'string',
404
- contentEncoding: StringContentEncodingEnum.UTF8,
405
- label: 'Value'
516
+ {
517
+ type: 'object',
518
+ properties: {
519
+ dataType: {type: 'string', enum: [ItemDataType.INT16U]},
520
+ value: {type: 'integer'}
521
+ }
522
+ },
523
+ {
524
+ type: 'object',
525
+ properties: {
526
+ dataType: {type: 'string', enum: [ItemDataType.INT32U]},
527
+ value: {type: 'integer'}
528
+ }
529
+ },
530
+ {
531
+ type: 'object',
532
+ properties: {
533
+ dataType: {type: 'string', enum: [ItemDataType.FLOAT32]},
534
+ value: {type: 'number'}
535
+ }
536
+ },
537
+ {
538
+ type: 'object',
539
+ properties: {
540
+ dataType: {type: 'string', enum: [ItemDataType.CODEDENUM]},
541
+ value: {type: 'integer'}
542
+ }
543
+ },
544
+ {
545
+ type: 'object',
546
+ properties: {
547
+ dataType: {type: 'string', enum: [ItemDataType.OCTETSTRING]},
548
+ value: {type: 'string', contentEncoding: StringContentEncodingEnum.HEX}
549
+ }
550
+ },
551
+ {
552
+ type: 'object',
553
+ properties: {
554
+ dataType: {type: 'string', enum: [ItemDataType.VISIBLESTRING]},
555
+ value: {type: 'string', contentEncoding: StringContentEncodingEnum.ASCII}
556
+ }
557
+ },
558
+ {
559
+ type: 'object',
560
+ properties: {
561
+ dataType: {type: 'string', enum: [ItemDataType.TimeStamp]},
562
+ value: {type: 'string'}
563
+ }
564
+ },
565
+ {
566
+ type: 'object',
567
+ properties: {
568
+ dataType: {type: 'string', enum: [ItemDataType.Quality]},
569
+ value: {type: 'string', contentEncoding: StringContentEncodingEnum.BINARY}
570
+ }
571
+ },
572
+ {
573
+ type: 'object',
574
+ properties: {
575
+ dataType: {type: 'string', enum: [ItemDataType.Structure]},
576
+ value: {
577
+ $ref: '#/properties/goosePdu/properties/allData'
578
+ }
579
+ }
406
580
  }
407
- }
581
+ ]
408
582
  },
409
583
  /**
410
584
  * +-----------------------------------+-------------------+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -430,226 +604,14 @@ export class Goose extends BaseHeader {
430
604
  decode: (): void => {
431
605
  const allDataTLV: TLV | undefined = this.TLVChild.find(tlv => tlv.getTag('number') === 0xAB)
432
606
  const dataTLVs: TLV[] = allDataTLV ? allDataTLV.getChild() : []
433
- const allData: AllDataItem[] = []
434
- dataTLVs.forEach((dataTLV: TLV): void => {
435
- const length: number = dataTLV.getLength('number')
436
- const value: Buffer = dataTLV.getValue('buffer')
437
- const dataItem: AllDataItem = {
438
- dataType: '',
439
- value: ''
440
- }
441
- switch (dataTLV.getTag('number')) {
442
- case 0x83: {
443
- dataItem.dataType = 'Boolean'
444
- dataItem.value = String(!!parseInt(value.toString('hex'), 16))
445
- }
446
- break
447
- case 0x84: {
448
- switch (length) {
449
- case 2: {
450
- dataItem.dataType = 'CODED-ENUM'
451
- dataItem.value = parseInt(value.toString('hex'), 16).toString()
452
- }
453
- break
454
- case 3: {
455
- dataItem.dataType = 'Quality'
456
- dataItem.value = parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0')
457
- }
458
- break
459
- }
460
- }
461
- break
462
- case 0x85: {
463
- if (length === 2) {
464
- {
465
- dataItem.dataType = 'INT8'
466
- dataItem.value = HexToInt8(value.toString('hex')).toString()
467
- }
468
- } else if (length === 3) {
469
- {
470
- dataItem.dataType = 'INT16'
471
- dataItem.value = HexToInt16(value.toString('hex')).toString()
472
- }
473
- } else if (length === 5) {
474
- {
475
- dataItem.dataType = 'INT32'
476
- dataItem.value = HexToInt32(value.toString('hex')).toString()
477
- }
478
- } else if (length === 9) {
479
- {
480
- dataItem.dataType = 'INT64'
481
- dataItem.value = HexToInt64(value.toString('hex')).toString()
482
- }
483
- }
484
- }
485
- break
486
- case 0x86: {
487
- switch (length) {
488
- case 2: {
489
- dataItem.dataType = 'INT8U'
490
- dataItem.value = HexToUInt8(value.toString('hex')).toString()
491
- }
492
- break
493
- case 3: {
494
- dataItem.dataType = 'INT16U'
495
- dataItem.value = HexToUInt16(value.toString('hex')).toString()
496
- }
497
- break
498
- case 5: {
499
- dataItem.dataType = 'INT32U'
500
- dataItem.value = HexToUInt32(value.toString('hex')).toString()
501
- }
502
- break
503
- }
504
- }
505
- break
506
- case 0x87: {
507
- dataItem.dataType = 'FLOAT32'
508
- dataItem.value = HexToFloat32(value.toString('hex')).toString()
509
- }
510
- break
511
- case 0x89: {
512
- dataItem.dataType = 'OCTET-STRING'
513
- dataItem.value = value.toString('ascii')
514
- }
515
- break
516
- case 0x8a: {
517
- dataItem.dataType = 'VISIBLE-STRING'
518
- dataItem.value = value.toString('ascii')
519
- }
520
- break
521
- case 0x91: {
522
- dataItem.dataType = 'TimeStamp'
523
- dataItem.value = parseInt(value.toString('hex'), 16).toString()
524
- }
525
- break
526
- }
527
- if (!dataItem.dataType) return
528
- allData.push(dataItem)
529
- })
607
+ const allData: DataItem[] = this.decodeDataTLVItem(dataTLVs)
530
608
  if (allData.length) this.instance.goosePdu.allData.setValue(allData)
531
609
  },
532
610
  encode: (): void => {
533
- const allData: AllDataItem[] = this.instance.goosePdu.allData.getValue([], (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
534
- const dataItemTLVs: TLV[] = allData
535
- .map((dataItem: AllDataItem, index: number): TLV | null => {
536
- const errorNodePath: string = this.instance.goosePdu.allData.getPath(index)
537
- dataItem.value = dataItem.value.trim()
538
- switch (dataItem.dataType) {
539
- case 'Boolean': {
540
- const availableStringValues: string[] = ['FALSE', 'TRUE']
541
- const stringValue: string = dataItem.value.toUpperCase()
542
- let booleanIntValue: number = 0
543
- if (availableStringValues.includes(stringValue)) {
544
- booleanIntValue = availableStringValues.indexOf(stringValue)
545
- } else {
546
- this.recordError(errorNodePath, 'Invalid Boolean value')
547
- }
548
- return new TLV(0x83, Buffer.from(booleanIntValue.toString(16).padStart(2, '0'), 'hex'))
549
- }
550
- case 'INT8': {
551
- let intValue: number = parseInt(dataItem.value)
552
- if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT8 value')
553
- intValue = intValue ? intValue : 0
554
- if (intValue < -128 || intValue > 127) this.recordError(errorNodePath, 'Invalid INT8 value')
555
- return new TLV(0x85, Buffer.from(Int8ToBERHex(intValue).padStart(2 * 2, '0'), 'hex'))
556
- }
557
- case 'INT16': {
558
- let intValue: number = parseInt(dataItem.value)
559
- if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT16 value')
560
- intValue = intValue ? intValue : 0
561
- if (intValue < -32768 || intValue > 32767) this.recordError(errorNodePath, 'Invalid INT16 value')
562
- return new TLV(0x85, Buffer.from(Int16ToBERHex(intValue).padStart(3 * 2, '0'), 'hex'))
563
- }
564
- case'INT32': {
565
- let intValue: number = parseInt(dataItem.value)
566
- if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT32 value')
567
- intValue = intValue ? intValue : 0
568
- if (intValue < -2147483648 || intValue > 2147483647) this.recordError(errorNodePath, 'Invalid INT32 value')
569
- return new TLV(0x85, Buffer.from(Int32ToBERHex(intValue).padStart(5 * 2, '0'), 'hex'))
570
- }
571
- case'INT64': {
572
- const intValue: bigint = BigInt(dataItem.value)
573
- if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807')) this.recordError(errorNodePath, 'Invalid INT64 value')
574
- return new TLV(0x85, Buffer.from(Int64ToBERHex(intValue).padStart(9 * 2, '0'), 'hex'))
575
- }
576
- case'INT8U': {
577
- let uintValue: number = parseInt(dataItem.value)
578
- if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT8U value')
579
- uintValue = uintValue ? uintValue : 0
580
- if (uintValue < 0 || uintValue > 255) this.recordError(errorNodePath, 'Invalid INT8U value')
581
- return new TLV(0x86, Buffer.from(UInt8ToBERHex(uintValue).padStart(2 * 2, '0'), 'hex'))
582
- }
583
- case'INT16U': {
584
- let uintValue: number = parseInt(dataItem.value)
585
- if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT16U value')
586
- uintValue = uintValue ? uintValue : 0
587
- if (uintValue < 0 || uintValue > 65535) this.recordError(errorNodePath, 'Invalid INT16U value')
588
- return new TLV(0x86, Buffer.from(UInt16ToBERHex(uintValue).padStart(3 * 2, '0'), 'hex'))
589
- }
590
- case'INT32U': {
591
- let uintValue: number = parseInt(dataItem.value)
592
- if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT32U value')
593
- uintValue = uintValue ? uintValue : 0
594
- if (uintValue < 0 || uintValue > 4294967295) this.recordError(errorNodePath, 'Invalid INT32U value')
595
- return new TLV(0x86, Buffer.from(UInt32ToBERHex(uintValue).padStart(5 * 2, '0'), 'hex'))
596
- }
597
- case'FLOAT32': {
598
- let float32Value: number = parseFloat(dataItem.value)
599
- if (isNaN(float32Value)) this.recordError(errorNodePath, 'Invalid FLOAT32 value')
600
- float32Value = float32Value ? float32Value : 0
601
- return new TLV(0x86, Buffer.from(Float32ToHex(float32Value).padStart(4 * 2, '0'), 'hex'))
602
- }
603
- case'CODED-ENUM': {
604
- let codedEnumValue: number = parseInt(dataItem.value)
605
- if (isNaN(codedEnumValue)) this.recordError(errorNodePath, 'Invalid CODED-ENUM value')
606
- codedEnumValue = codedEnumValue ? codedEnumValue : 0
607
- return new TLV(0x84, Buffer.from(UInt16ToBERHex(codedEnumValue).padStart(2 * 2, '0'), 'hex'))
608
- }
609
- case'OCTET-STRING': {
610
- const asciiText: string = dataItem.value
611
- if (!asciiText) {
612
- this.recordError(errorNodePath, 'Empty OCTET-STRING, ignored')
613
- return null
614
- }
615
- if (asciiText.length > 20) this.recordError(errorNodePath, 'OCTET-STRING too long')
616
- const hex: string = Buffer.from(asciiText, 'ascii').toString('hex').padStart(20 * 2)
617
- return new TLV(0x89, Buffer.from(hex, 'hex').subarray(0, 20))
618
- }
619
- case'VISIBLE-STRING': {
620
- const asciiText: string = dataItem.value
621
- if (!asciiText) {
622
- this.recordError(errorNodePath, 'Empty VISIBLE-STRING, ignored')
623
- return null
624
- }
625
- if (asciiText.length > 35) this.recordError(errorNodePath, 'VISIBLE-STRING too long')
626
- const hex: string = Buffer.from(asciiText, 'ascii').toString('hex').padStart(35 * 2)
627
- return new TLV(0x8a, Buffer.from(hex, 'hex').subarray(0, 35))
628
- }
629
- case'TimeStamp': {
630
- let timestamp: number = parseInt(dataItem.value)
631
- if (isNaN(timestamp)) this.recordError(errorNodePath, 'Invalid TimeStamp value')
632
- timestamp = timestamp ? timestamp : 0
633
- return new TLV(0x91, Buffer.from(timestamp.toString(16).padStart(8 * 2, '0'), 'hex'))
634
- }
635
- case'Quality': {
636
- const bitString: string = dataItem.value
637
- let intValue: number = parseInt(bitString, 2)
638
- if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid Quality value')
639
- intValue = intValue ? intValue : 0
640
- return new TLV(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'))
641
- }
642
- default: {
643
- this.recordError(errorNodePath, 'Invalid dataType, ignored')
644
- return null
645
- }
646
- }
647
- })
648
- .filter((dataItemTLV: TLV | null): dataItemTLV is TLV => !!dataItemTLV)
611
+ const allData: DataItem[] = this.instance.goosePdu.allData.getValue([], (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
612
+ const dataItemTLVs: TLV[] = this.encodeDataTLVItem(allData)
649
613
  if (dataItemTLVs.length) {
650
- let dataItemsHex: string = ''
651
- dataItemTLVs.forEach(dataItemTLV => dataItemsHex = `${dataItemsHex}${dataItemTLV.toString()}`)
652
- const allDataTLV: TLV = new TLV(0xAB, dataItemsHex)
614
+ const allDataTLV: TLV = new TLV(0xAB, Buffer.concat(dataItemTLVs.map((dataItemTLV: TLV): Buffer => Buffer.concat([dataItemTLV.bTag, dataItemTLV.bLength, dataItemTLV.bValue]))))
653
615
  this.TLVChild.push(allDataTLV)
654
616
  }
655
617
  }
@@ -659,6 +621,253 @@ export class Goose extends BaseHeader {
659
621
  }
660
622
  }
661
623
 
624
+ /**
625
+ * Decode data TLV items
626
+ * @param dataTLVs
627
+ * @protected
628
+ */
629
+ protected decodeDataTLVItem(dataTLVs: TLV[]): DataItem[] {
630
+ const dataItems: DataItem[] = []
631
+ dataTLVs.forEach((dataTLV: TLV): void => {
632
+ const length: number = dataTLV.getLength('number')
633
+ const value: Buffer = dataTLV.getValue('buffer')
634
+ switch (dataTLV.getTag('number')) {
635
+ case 0x83: {
636
+ dataItems.push({
637
+ dataType: ItemDataType.Boolean,
638
+ value: !!parseInt(value.toString('hex'), 16)
639
+ })
640
+ }
641
+ break
642
+ case 0x84: {
643
+ switch (length) {
644
+ case 2: {
645
+ dataItems.push({
646
+ dataType: ItemDataType.CODEDENUM,
647
+ value: parseInt(value.toString('hex'), 16)
648
+ })
649
+ }
650
+ break
651
+ case 3: {
652
+ dataItems.push({
653
+ dataType: ItemDataType.Quality,
654
+ value: parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0')
655
+ })
656
+ }
657
+ break
658
+ }
659
+ }
660
+ break
661
+ case 0x85: {
662
+ const integerRealLength: number = GetBERIntegerLengthFromBuffer(value)
663
+ if (integerRealLength === 1) {
664
+ dataItems.push({
665
+ dataType: ItemDataType.INT8,
666
+ value: HexToInt8(value.toString('hex'))
667
+ })
668
+ } else if (integerRealLength === 2) {
669
+ dataItems.push({
670
+ dataType: ItemDataType.INT16,
671
+ value: HexToInt16(value.toString('hex'))
672
+ })
673
+ } else if (integerRealLength === 4) {
674
+ dataItems.push({
675
+ dataType: ItemDataType.INT32,
676
+ value: HexToInt32(value.toString('hex'))
677
+ })
678
+ } else if (integerRealLength === 8) {
679
+ dataItems.push({
680
+ dataType: ItemDataType.INT64,
681
+ value: HexToInt64(value.toString('hex'))
682
+ })
683
+ }
684
+ }
685
+ break
686
+ case 0x86: {
687
+ const unsignedIntegerRealLength: number = GetBERIntegerLengthFromBuffer(value)
688
+ switch (unsignedIntegerRealLength) {
689
+ case 1: {
690
+ dataItems.push({
691
+ dataType: ItemDataType.INT8U,
692
+ value: HexToUInt8(value.toString('hex'))
693
+ })
694
+ }
695
+ break
696
+ case 2: {
697
+ dataItems.push({
698
+ dataType: ItemDataType.INT16U,
699
+ value: HexToUInt16(value.toString('hex'))
700
+ })
701
+ }
702
+ break
703
+ case 4: {
704
+ dataItems.push({
705
+ dataType: ItemDataType.INT32U,
706
+ value: HexToUInt32(value.toString('hex'))
707
+ })
708
+ }
709
+ break
710
+ }
711
+ }
712
+ break
713
+ case 0x87: {
714
+ dataItems.push({
715
+ dataType: ItemDataType.FLOAT32,
716
+ value: HexToFloat32(value.toString('hex'))
717
+ })
718
+ }
719
+ break
720
+ case 0x89: {
721
+ dataItems.push({
722
+ dataType: ItemDataType.OCTETSTRING,
723
+ value: value.toString('hex')
724
+ })
725
+ }
726
+ break
727
+ case 0x8a: {
728
+ dataItems.push({
729
+ dataType: ItemDataType.VISIBLESTRING,
730
+ value: value.toString('ascii')
731
+ })
732
+ }
733
+ break
734
+ case 0x91: {
735
+ dataItems.push({
736
+ dataType: ItemDataType.TimeStamp,
737
+ value: parseInt(value.toString('hex'), 16).toString()
738
+ })
739
+ }
740
+ break
741
+ case 0xa2: {
742
+ const subDataTLVs: TLV[] = TLV.parseList(value)
743
+ dataItems.push({
744
+ dataType: ItemDataType.Structure,
745
+ value: this.decodeDataTLVItem(subDataTLVs)
746
+ })
747
+ }
748
+ break
749
+ }
750
+ })
751
+ return dataItems
752
+ }
753
+
754
+ /**
755
+ * Encode data TLV items
756
+ * @param dataItems
757
+ * @protected
758
+ */
759
+ protected encodeDataTLVItem(dataItems: DataItem[]): TLV[] {
760
+ return dataItems
761
+ .map((dataItem: DataItem, index: number): TLV | null => {
762
+ const errorNodePath: string = this.instance.goosePdu.allData.getPath(index)
763
+ switch (dataItem.dataType) {
764
+ case ItemDataType.Boolean: {
765
+ const booleanIntValue: number = dataItem.value ? 1 : 0
766
+ return new TLV(0x83, Buffer.from(booleanIntValue.toString(16).padStart(2, '0'), 'hex'))
767
+ }
768
+ case ItemDataType.INT8: {
769
+ let intValue: number = dataItem.value
770
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT8 value')
771
+ intValue = intValue ? intValue : 0
772
+ if (intValue < -128 || intValue > 127) this.recordError(errorNodePath, 'Invalid INT8 value')
773
+ return new TLV(0x85, Buffer.from(Int8ToBERHex(intValue), 'hex'))
774
+ }
775
+ case ItemDataType.INT16: {
776
+ let intValue: number = dataItem.value
777
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT16 value')
778
+ intValue = intValue ? intValue : 0
779
+ if (intValue < -32768 || intValue > 32767) this.recordError(errorNodePath, 'Invalid INT16 value')
780
+ return new TLV(0x85, Buffer.from(Int16ToBERHex(intValue), 'hex'))
781
+ }
782
+ case ItemDataType.INT32: {
783
+ let intValue: number = dataItem.value
784
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT32 value')
785
+ intValue = intValue ? intValue : 0
786
+ if (intValue < -2147483648 || intValue > 2147483647) this.recordError(errorNodePath, 'Invalid INT32 value')
787
+ return new TLV(0x85, Buffer.from(Int32ToBERHex(intValue), 'hex'))
788
+ }
789
+ case ItemDataType.INT64: {
790
+ const intValue: bigint = BigInt(dataItem.value)
791
+ if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807')) this.recordError(errorNodePath, 'Invalid INT64 value')
792
+ return new TLV(0x85, Buffer.from(Int64ToBERHex(intValue), 'hex'))
793
+ }
794
+ case ItemDataType.INT8U: {
795
+ let uintValue: number = dataItem.value
796
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT8U value')
797
+ uintValue = uintValue ? uintValue : 0
798
+ if (uintValue < 0 || uintValue > 255) this.recordError(errorNodePath, 'Invalid INT8U value')
799
+ return new TLV(0x86, Buffer.from(UInt8ToBERHex(uintValue), 'hex'))
800
+ }
801
+ case ItemDataType.INT16U: {
802
+ let uintValue: number = dataItem.value
803
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT16U value')
804
+ uintValue = uintValue ? uintValue : 0
805
+ if (uintValue < 0 || uintValue > 65535) this.recordError(errorNodePath, 'Invalid INT16U value')
806
+ return new TLV(0x86, Buffer.from(UInt16ToBERHex(uintValue), 'hex'))
807
+ }
808
+ case ItemDataType.INT32U: {
809
+ let uintValue: number = dataItem.value
810
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT32U value')
811
+ uintValue = uintValue ? uintValue : 0
812
+ if (uintValue < 0 || uintValue > 4294967295) this.recordError(errorNodePath, 'Invalid INT32U value')
813
+ return new TLV(0x86, Buffer.from(UInt32ToBERHex(uintValue), 'hex'))
814
+ }
815
+ case ItemDataType.FLOAT32: {
816
+ let float32Value: number = dataItem.value
817
+ if (isNaN(float32Value)) this.recordError(errorNodePath, 'Invalid FLOAT32 value')
818
+ float32Value = float32Value ? float32Value : 0
819
+ return new TLV(0x87, Buffer.from(Float32ToBERHex(float32Value), 'hex'))
820
+ }
821
+ case ItemDataType.CODEDENUM: {
822
+ let codedEnumValue: number = dataItem.value
823
+ if (isNaN(codedEnumValue)) this.recordError(errorNodePath, 'Invalid CODED-ENUM value')
824
+ codedEnumValue = codedEnumValue ? codedEnumValue : 0
825
+ return new TLV(0x84, Buffer.from(UInt16ToBERHex(codedEnumValue), 'hex'))
826
+ }
827
+ case ItemDataType.OCTETSTRING: {
828
+ const hexText: string = dataItem.value
829
+ if (!hexText) {
830
+ this.recordError(errorNodePath, 'Empty OCTET-STRING, ignored')
831
+ return null
832
+ }
833
+ if (hexText.length > 20) this.recordError(errorNodePath, 'OCTET-STRING too long')
834
+ return new TLV(0x89, Buffer.from(hexText, 'hex').subarray(0, 20))
835
+ }
836
+ case ItemDataType.VISIBLESTRING: {
837
+ const asciiText: string = dataItem.value
838
+ if (!asciiText) {
839
+ this.recordError(errorNodePath, 'Empty VISIBLE-STRING, ignored')
840
+ return null
841
+ }
842
+ if (asciiText.length > 35) this.recordError(errorNodePath, 'VISIBLE-STRING too long')
843
+ const hex: string = Buffer.from(asciiText, 'ascii').toString('hex').padStart(35 * 2)
844
+ return new TLV(0x8a, Buffer.from(hex, 'hex').subarray(0, 35))
845
+ }
846
+ case ItemDataType.TimeStamp: {
847
+ let timestamp: number = parseInt(dataItem.value)
848
+ if (isNaN(timestamp)) this.recordError(errorNodePath, 'Invalid TimeStamp value')
849
+ timestamp = timestamp ? timestamp : 0
850
+ return new TLV(0x91, Buffer.from(timestamp.toString(16), 'hex'))
851
+ }
852
+ case ItemDataType.Quality: {
853
+ const bitString: string = dataItem.value
854
+ let intValue: number = parseInt(bitString, 2)
855
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid Quality value')
856
+ intValue = intValue ? intValue : 0
857
+ return new TLV(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'))
858
+ }
859
+ case ItemDataType.Structure: {
860
+ return new TLV(0xa2, Buffer.concat(this.encodeDataTLVItem(dataItem.value).map((tlv: TLV): Buffer => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))))
861
+ }
862
+ default: {
863
+ this.recordError(errorNodePath, 'Invalid dataType, ignored')
864
+ return null
865
+ }
866
+ }
867
+ })
868
+ .filter((value: TLV | null): value is TLV => !!value)
869
+ }
870
+
662
871
  public readonly id: string = 'goose'
663
872
 
664
873
  public readonly name: string = 'IEC61850 GOOSE'