netkitty 0.0.27 → 0.0.28

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,116 @@ 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
+ type: 'array',
578
+ $ref: '#/properties/goosePdu/properties/allData'
579
+ }
580
+ }
406
581
  }
407
- }
582
+ ]
408
583
  },
409
584
  /**
410
585
  * +-----------------------------------+-------------------+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
@@ -430,226 +605,14 @@ export class Goose extends BaseHeader {
430
605
  decode: (): void => {
431
606
  const allDataTLV: TLV | undefined = this.TLVChild.find(tlv => tlv.getTag('number') === 0xAB)
432
607
  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
- })
608
+ const allData: DataItem[] = this.decodeDataTLVItem(dataTLVs)
530
609
  if (allData.length) this.instance.goosePdu.allData.setValue(allData)
531
610
  },
532
611
  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)
612
+ const allData: DataItem[] = this.instance.goosePdu.allData.getValue([], (nodePath: string): void => this.recordError(nodePath, 'Not Found'))
613
+ const dataItemTLVs: TLV[] = this.encodeDataTLVItem(allData)
649
614
  if (dataItemTLVs.length) {
650
- let dataItemsHex: string = ''
651
- dataItemTLVs.forEach(dataItemTLV => dataItemsHex = `${dataItemsHex}${dataItemTLV.toString()}`)
652
- const allDataTLV: TLV = new TLV(0xAB, dataItemsHex)
615
+ const allDataTLV: TLV = new TLV(0xAB, Buffer.concat(dataItemTLVs.map((dataItemTLV: TLV): Buffer => Buffer.concat([dataItemTLV.bTag, dataItemTLV.bLength, dataItemTLV.bValue]))))
653
616
  this.TLVChild.push(allDataTLV)
654
617
  }
655
618
  }
@@ -659,6 +622,253 @@ export class Goose extends BaseHeader {
659
622
  }
660
623
  }
661
624
 
625
+ /**
626
+ * Decode data TLV items
627
+ * @param dataTLVs
628
+ * @protected
629
+ */
630
+ protected decodeDataTLVItem(dataTLVs: TLV[]): DataItem[] {
631
+ const dataItems: DataItem[] = []
632
+ dataTLVs.forEach((dataTLV: TLV): void => {
633
+ const length: number = dataTLV.getLength('number')
634
+ const value: Buffer = dataTLV.getValue('buffer')
635
+ switch (dataTLV.getTag('number')) {
636
+ case 0x83: {
637
+ dataItems.push({
638
+ dataType: ItemDataType.Boolean,
639
+ value: !!parseInt(value.toString('hex'), 16)
640
+ })
641
+ }
642
+ break
643
+ case 0x84: {
644
+ switch (length) {
645
+ case 2: {
646
+ dataItems.push({
647
+ dataType: ItemDataType.CODEDENUM,
648
+ value: parseInt(value.toString('hex'), 16)
649
+ })
650
+ }
651
+ break
652
+ case 3: {
653
+ dataItems.push({
654
+ dataType: ItemDataType.Quality,
655
+ value: parseInt(value.toString('hex'), 16).toString(2).padStart(24, '0')
656
+ })
657
+ }
658
+ break
659
+ }
660
+ }
661
+ break
662
+ case 0x85: {
663
+ const integerRealLength: number = GetBERIntegerLengthFromBuffer(value)
664
+ if (integerRealLength === 1) {
665
+ dataItems.push({
666
+ dataType: ItemDataType.INT8,
667
+ value: HexToInt8(value.toString('hex'))
668
+ })
669
+ } else if (integerRealLength === 2) {
670
+ dataItems.push({
671
+ dataType: ItemDataType.INT16,
672
+ value: HexToInt16(value.toString('hex'))
673
+ })
674
+ } else if (integerRealLength === 4) {
675
+ dataItems.push({
676
+ dataType: ItemDataType.INT32,
677
+ value: HexToInt32(value.toString('hex'))
678
+ })
679
+ } else if (integerRealLength === 8) {
680
+ dataItems.push({
681
+ dataType: ItemDataType.INT64,
682
+ value: HexToInt64(value.toString('hex'))
683
+ })
684
+ }
685
+ }
686
+ break
687
+ case 0x86: {
688
+ const unsignedIntegerRealLength: number = GetBERIntegerLengthFromBuffer(value)
689
+ switch (unsignedIntegerRealLength) {
690
+ case 1: {
691
+ dataItems.push({
692
+ dataType: ItemDataType.INT8U,
693
+ value: HexToUInt8(value.toString('hex'))
694
+ })
695
+ }
696
+ break
697
+ case 2: {
698
+ dataItems.push({
699
+ dataType: ItemDataType.INT16U,
700
+ value: HexToUInt16(value.toString('hex'))
701
+ })
702
+ }
703
+ break
704
+ case 4: {
705
+ dataItems.push({
706
+ dataType: ItemDataType.INT32U,
707
+ value: HexToUInt32(value.toString('hex'))
708
+ })
709
+ }
710
+ break
711
+ }
712
+ }
713
+ break
714
+ case 0x87: {
715
+ dataItems.push({
716
+ dataType: ItemDataType.FLOAT32,
717
+ value: HexToFloat32(value.toString('hex'))
718
+ })
719
+ }
720
+ break
721
+ case 0x89: {
722
+ dataItems.push({
723
+ dataType: ItemDataType.OCTETSTRING,
724
+ value: value.toString('hex')
725
+ })
726
+ }
727
+ break
728
+ case 0x8a: {
729
+ dataItems.push({
730
+ dataType: ItemDataType.VISIBLESTRING,
731
+ value: value.toString('ascii')
732
+ })
733
+ }
734
+ break
735
+ case 0x91: {
736
+ dataItems.push({
737
+ dataType: ItemDataType.TimeStamp,
738
+ value: parseInt(value.toString('hex'), 16).toString()
739
+ })
740
+ }
741
+ break
742
+ case 0xa2: {
743
+ const subDataTLVs: TLV[] = TLV.parseList(value)
744
+ dataItems.push({
745
+ dataType: ItemDataType.Structure,
746
+ value: this.decodeDataTLVItem(subDataTLVs)
747
+ })
748
+ }
749
+ break
750
+ }
751
+ })
752
+ return dataItems
753
+ }
754
+
755
+ /**
756
+ * Encode data TLV items
757
+ * @param dataItems
758
+ * @protected
759
+ */
760
+ protected encodeDataTLVItem(dataItems: DataItem[]): TLV[] {
761
+ return dataItems
762
+ .map((dataItem: DataItem, index: number): TLV | null => {
763
+ const errorNodePath: string = this.instance.goosePdu.allData.getPath(index)
764
+ switch (dataItem.dataType) {
765
+ case ItemDataType.Boolean: {
766
+ const booleanIntValue: number = dataItem.value ? 1 : 0
767
+ return new TLV(0x83, Buffer.from(booleanIntValue.toString(16).padStart(2, '0'), 'hex'))
768
+ }
769
+ case ItemDataType.INT8: {
770
+ let intValue: number = dataItem.value
771
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT8 value')
772
+ intValue = intValue ? intValue : 0
773
+ if (intValue < -128 || intValue > 127) this.recordError(errorNodePath, 'Invalid INT8 value')
774
+ return new TLV(0x85, Buffer.from(Int8ToBERHex(intValue), 'hex'))
775
+ }
776
+ case ItemDataType.INT16: {
777
+ let intValue: number = dataItem.value
778
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT16 value')
779
+ intValue = intValue ? intValue : 0
780
+ if (intValue < -32768 || intValue > 32767) this.recordError(errorNodePath, 'Invalid INT16 value')
781
+ return new TLV(0x85, Buffer.from(Int16ToBERHex(intValue), 'hex'))
782
+ }
783
+ case ItemDataType.INT32: {
784
+ let intValue: number = dataItem.value
785
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid INT32 value')
786
+ intValue = intValue ? intValue : 0
787
+ if (intValue < -2147483648 || intValue > 2147483647) this.recordError(errorNodePath, 'Invalid INT32 value')
788
+ return new TLV(0x85, Buffer.from(Int32ToBERHex(intValue), 'hex'))
789
+ }
790
+ case ItemDataType.INT64: {
791
+ const intValue: bigint = BigInt(dataItem.value)
792
+ if (intValue < BigInt('-9223372036854775808') || intValue > BigInt('9223372036854775807')) this.recordError(errorNodePath, 'Invalid INT64 value')
793
+ return new TLV(0x85, Buffer.from(Int64ToBERHex(intValue), 'hex'))
794
+ }
795
+ case ItemDataType.INT8U: {
796
+ let uintValue: number = dataItem.value
797
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT8U value')
798
+ uintValue = uintValue ? uintValue : 0
799
+ if (uintValue < 0 || uintValue > 255) this.recordError(errorNodePath, 'Invalid INT8U value')
800
+ return new TLV(0x86, Buffer.from(UInt8ToBERHex(uintValue), 'hex'))
801
+ }
802
+ case ItemDataType.INT16U: {
803
+ let uintValue: number = dataItem.value
804
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT16U value')
805
+ uintValue = uintValue ? uintValue : 0
806
+ if (uintValue < 0 || uintValue > 65535) this.recordError(errorNodePath, 'Invalid INT16U value')
807
+ return new TLV(0x86, Buffer.from(UInt16ToBERHex(uintValue), 'hex'))
808
+ }
809
+ case ItemDataType.INT32U: {
810
+ let uintValue: number = dataItem.value
811
+ if (isNaN(uintValue)) this.recordError(errorNodePath, 'Invalid INT32U value')
812
+ uintValue = uintValue ? uintValue : 0
813
+ if (uintValue < 0 || uintValue > 4294967295) this.recordError(errorNodePath, 'Invalid INT32U value')
814
+ return new TLV(0x86, Buffer.from(UInt32ToBERHex(uintValue), 'hex'))
815
+ }
816
+ case ItemDataType.FLOAT32: {
817
+ let float32Value: number = dataItem.value
818
+ if (isNaN(float32Value)) this.recordError(errorNodePath, 'Invalid FLOAT32 value')
819
+ float32Value = float32Value ? float32Value : 0
820
+ return new TLV(0x87, Buffer.from(Float32ToBERHex(float32Value), 'hex'))
821
+ }
822
+ case ItemDataType.CODEDENUM: {
823
+ let codedEnumValue: number = dataItem.value
824
+ if (isNaN(codedEnumValue)) this.recordError(errorNodePath, 'Invalid CODED-ENUM value')
825
+ codedEnumValue = codedEnumValue ? codedEnumValue : 0
826
+ return new TLV(0x84, Buffer.from(UInt16ToBERHex(codedEnumValue), 'hex'))
827
+ }
828
+ case ItemDataType.OCTETSTRING: {
829
+ const hexText: string = dataItem.value
830
+ if (!hexText) {
831
+ this.recordError(errorNodePath, 'Empty OCTET-STRING, ignored')
832
+ return null
833
+ }
834
+ if (hexText.length > 20) this.recordError(errorNodePath, 'OCTET-STRING too long')
835
+ return new TLV(0x89, Buffer.from(hexText, 'hex').subarray(0, 20))
836
+ }
837
+ case ItemDataType.VISIBLESTRING: {
838
+ const asciiText: string = dataItem.value
839
+ if (!asciiText) {
840
+ this.recordError(errorNodePath, 'Empty VISIBLE-STRING, ignored')
841
+ return null
842
+ }
843
+ if (asciiText.length > 35) this.recordError(errorNodePath, 'VISIBLE-STRING too long')
844
+ const hex: string = Buffer.from(asciiText, 'ascii').toString('hex').padStart(35 * 2)
845
+ return new TLV(0x8a, Buffer.from(hex, 'hex').subarray(0, 35))
846
+ }
847
+ case ItemDataType.TimeStamp: {
848
+ let timestamp: number = parseInt(dataItem.value)
849
+ if (isNaN(timestamp)) this.recordError(errorNodePath, 'Invalid TimeStamp value')
850
+ timestamp = timestamp ? timestamp : 0
851
+ return new TLV(0x91, Buffer.from(timestamp.toString(16), 'hex'))
852
+ }
853
+ case ItemDataType.Quality: {
854
+ const bitString: string = dataItem.value
855
+ let intValue: number = parseInt(bitString, 2)
856
+ if (isNaN(intValue)) this.recordError(errorNodePath, 'Invalid Quality value')
857
+ intValue = intValue ? intValue : 0
858
+ return new TLV(0x84, Buffer.from(intValue.toString(16).padStart(3 * 2, '0'), 'hex'))
859
+ }
860
+ case ItemDataType.Structure: {
861
+ return new TLV(0xa2, Buffer.concat(this.encodeDataTLVItem(dataItem.value).map((tlv: TLV): Buffer => Buffer.concat([tlv.bTag, tlv.bLength, tlv.bValue]))))
862
+ }
863
+ default: {
864
+ this.recordError(errorNodePath, 'Invalid dataType, ignored')
865
+ return null
866
+ }
867
+ }
868
+ })
869
+ .filter((value: TLV | null): value is TLV => !!value)
870
+ }
871
+
662
872
  public readonly id: string = 'goose'
663
873
 
664
874
  public readonly name: string = 'IEC61850 GOOSE'