@canboat/canboatjs 2.4.5 → 2.5.1
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.
- package/lib/fromPgn.js +38 -12
- package/lib/pgns.js +42 -0
- package/lib/toPgn.js +29 -9
- package/package.json +1 -1
package/lib/fromPgn.js
CHANGED
|
@@ -19,7 +19,7 @@ const trace = require('debug')('canboatjs:fromPgn:trace')
|
|
|
19
19
|
const EventEmitter = require('events')
|
|
20
20
|
const pkg = require('../package.json')
|
|
21
21
|
const _ = require('lodash')
|
|
22
|
-
const { pgns, getCustomPgn, addCustomPgns, lookupEnumerationName, lookupBitEnumerationName } = require('./pgns')
|
|
22
|
+
const { pgns, getCustomPgn, addCustomPgns, lookupEnumerationName, lookupFieldTypeEnumerationName, lookupBitEnumerationName, lookupFieldTypeEnumerationBits, lookupFieldTypeEnumerationValue } = require('./pgns')
|
|
23
23
|
const BitStream = require('bit-buffer').BitStream
|
|
24
24
|
const BitView = require('bit-buffer').BitView
|
|
25
25
|
const Int64LE = require('int64-buffer').Int64LE
|
|
@@ -212,7 +212,7 @@ class Parser extends EventEmitter {
|
|
|
212
212
|
var field = fields[i]
|
|
213
213
|
var hasMatch = !_.isUndefined(field.Match)
|
|
214
214
|
|
|
215
|
-
var value = readField(this.options, !hasMatch, pgn, field, bs)
|
|
215
|
+
var value = readField(this.options, !hasMatch, pgn, field, bs, fields)
|
|
216
216
|
|
|
217
217
|
if ( hasMatch ) {
|
|
218
218
|
//console.log(`looking for ${field.Name} == ${value}`)
|
|
@@ -270,7 +270,7 @@ class Parser extends EventEmitter {
|
|
|
270
270
|
var group = {}
|
|
271
271
|
repeating.forEach(field => {
|
|
272
272
|
if ( bs.bitsLeft > 0 ) {
|
|
273
|
-
var value = readField(this.options, true, pgn, field, bs)
|
|
273
|
+
var value = readField(this.options, true, pgn, field, bs, fields)
|
|
274
274
|
if ( !_.isUndefined(value) && value != null ) {
|
|
275
275
|
group[field.Name] = value
|
|
276
276
|
}
|
|
@@ -513,12 +513,16 @@ function getField(pgn, index, data) {
|
|
|
513
513
|
var pgnList = pgns[pgn]
|
|
514
514
|
if ( pgnList ) {
|
|
515
515
|
var pgn = pgnList[0]
|
|
516
|
+
let dataList = data.list ? data.list : data.fields.list
|
|
516
517
|
|
|
517
518
|
if ( pgnList.length > 1 ) {
|
|
518
|
-
|
|
519
|
+
let idx = 0
|
|
520
|
+
while ( idx < pgn.Fields.count )
|
|
521
|
+
{
|
|
522
|
+
field = pgn.Fields[idx]
|
|
519
523
|
var hasMatch = !_.isUndefined(field.Match)
|
|
520
|
-
if ( hasMatch &&
|
|
521
|
-
let param =
|
|
524
|
+
if ( hasMatch && dataList.length > 0 ) {
|
|
525
|
+
let param = dataList.find(f => f.Parameter === idx+1)
|
|
522
526
|
|
|
523
527
|
if ( param ) {
|
|
524
528
|
pgnList = pgnList.filter(f => f.Fields[idx].Match == param.Value)
|
|
@@ -530,7 +534,8 @@ function getField(pgn, index, data) {
|
|
|
530
534
|
}
|
|
531
535
|
}
|
|
532
536
|
}
|
|
533
|
-
|
|
537
|
+
idx++
|
|
538
|
+
}
|
|
534
539
|
}
|
|
535
540
|
|
|
536
541
|
if ( index >= 0 && index < pgn.Fields.length ) {
|
|
@@ -561,11 +566,17 @@ function pad(n, p, c)
|
|
|
561
566
|
}
|
|
562
567
|
|
|
563
568
|
function lookup(field, value) {
|
|
564
|
-
var name
|
|
569
|
+
var name
|
|
570
|
+
if ( field.LookupEnumeration ) {
|
|
571
|
+
name = lookupEnumerationName(field.LookupEnumeration, value)
|
|
572
|
+
} else {
|
|
573
|
+
name = lookupFieldTypeEnumerationName(field.LookupFieldTypeEnumeration, value)
|
|
574
|
+
}
|
|
575
|
+
|
|
565
576
|
return name ? name : value
|
|
566
577
|
}
|
|
567
578
|
|
|
568
|
-
function readField(options, runPostProcessor, pgn, field, bs) {
|
|
579
|
+
function readField(options, runPostProcessor, pgn, field, bs, fields) {
|
|
569
580
|
var value
|
|
570
581
|
|
|
571
582
|
var reader = fieldTypeReaders[field.FieldType]
|
|
@@ -577,7 +588,7 @@ function readField(options, runPostProcessor, pgn, field, bs) {
|
|
|
577
588
|
bs.readBits(bs.bitsLeft, false)
|
|
578
589
|
return
|
|
579
590
|
}
|
|
580
|
-
value = readValue(options, pgn, field, bs)
|
|
591
|
+
value = readValue(options, pgn, field, bs, fields)
|
|
581
592
|
}
|
|
582
593
|
|
|
583
594
|
//console.log(`${field.Name} ${value} ${field.Resolution}`)
|
|
@@ -650,10 +661,14 @@ function readField(options, runPostProcessor, pgn, field, bs) {
|
|
|
650
661
|
return value
|
|
651
662
|
}
|
|
652
663
|
|
|
653
|
-
function readValue(options, pgn, field, bs, bitLength) {
|
|
664
|
+
function readValue(options, pgn, field, bs, fields, bitLength) {
|
|
654
665
|
var value
|
|
655
666
|
if ( _.isUndefined(bitLength) ) {
|
|
656
|
-
|
|
667
|
+
if ( field.BitLengthVariable && field.FieldType === "KEY_VALUE" ) {
|
|
668
|
+
bitLength = lookupKeyBitLength(pgn.fields, fields)
|
|
669
|
+
} else {
|
|
670
|
+
bitLength = field.BitLength
|
|
671
|
+
}
|
|
657
672
|
}
|
|
658
673
|
if ( field.FieldType == 'VARIABLE' ) {
|
|
659
674
|
return readVariableLengthField(options, pgn, field, bs)
|
|
@@ -896,6 +911,17 @@ fieldTypeReaders['BITLOOKUP'] = (pgn, field, bs) => {
|
|
|
896
911
|
return value
|
|
897
912
|
}
|
|
898
913
|
|
|
914
|
+
function lookupKeyBitLength(data, fields)
|
|
915
|
+
{
|
|
916
|
+
let field = fields.find(field => (field.Name === 'Key'))
|
|
917
|
+
|
|
918
|
+
let val = data['Key']
|
|
919
|
+
if ( typeof val === 'string' ) {
|
|
920
|
+
val = lookupFieldTypeEnumerationValue(field.LookupFieldTypeEnumeration, val)
|
|
921
|
+
}
|
|
922
|
+
return lookupFieldTypeEnumerationBits(field.LookupFieldTypeEnumeration, val)
|
|
923
|
+
}
|
|
924
|
+
|
|
899
925
|
fieldTypePostProcessors['DATE'] = (field, value) => {
|
|
900
926
|
if ( value >= 0xfffd ) {
|
|
901
927
|
value = undefined
|
package/lib/pgns.js
CHANGED
|
@@ -54,6 +54,29 @@ function getEnumeration(name) {
|
|
|
54
54
|
return enumeration
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
function getFieldTypeEnumeration(name) {
|
|
58
|
+
const enumeration = lookupFieldTypeEnumerations[name]
|
|
59
|
+
if ( enumeration ) {
|
|
60
|
+
if ( !enumeration.value2name ) {
|
|
61
|
+
enumeration.value2name = {}
|
|
62
|
+
enumeration.EnumFieldTypeValues.forEach((enumPair) => {
|
|
63
|
+
enumeration.value2name[Number(enumPair.value)] = enumPair.name
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
enumeration.name2value = {}
|
|
67
|
+
enumeration.EnumFieldTypeValues.forEach((enumPair) => {
|
|
68
|
+
enumeration.name2value[enumPair.name] = Number(enumPair.value)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
enumeration.value2bits = {}
|
|
72
|
+
enumeration.EnumFieldTypeValues.forEach((enumPair) => {
|
|
73
|
+
enumeration.value2bits[enumPair.value] = Number(enumPair.Bits)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return enumeration
|
|
78
|
+
}
|
|
79
|
+
|
|
57
80
|
function getBitEnumeration(name) {
|
|
58
81
|
const enumeration = lookupBitEnumerations[name]
|
|
59
82
|
if ( enumeration ) {
|
|
@@ -82,6 +105,21 @@ function lookupEnumerationValue(enumName, name) {
|
|
|
82
105
|
return enumeration && enumeration.name2value[name]
|
|
83
106
|
}
|
|
84
107
|
|
|
108
|
+
function lookupFieldTypeEnumerationName(enumName, value) {
|
|
109
|
+
const enumeration = getFieldTypeEnumeration(enumName)
|
|
110
|
+
return enumeration && enumeration.value2name[value]
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function lookupFieldTypeEnumerationBits(enumName, value) {
|
|
114
|
+
const enumeration = getFieldTypeEnumeration(enumName)
|
|
115
|
+
return enumeration && enumeration.value2bits[value]
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function lookupFieldTypeEnumerationValue(enumName, name) {
|
|
119
|
+
const enumeration = getFieldTypeEnumeration(enumName)
|
|
120
|
+
return enumeration && enumeration.name2value[name]
|
|
121
|
+
}
|
|
122
|
+
|
|
85
123
|
function lookupBitEnumerationName(enumName, value) {
|
|
86
124
|
const enumeration = getBitEnumeration(enumName)
|
|
87
125
|
return enumeration && enumeration.value2name[value]
|
|
@@ -101,6 +139,7 @@ function organizeEnumerations(enums) {
|
|
|
101
139
|
}
|
|
102
140
|
|
|
103
141
|
const lookupEnumerations = organizeEnumerations(pgns.LookupEnumerations)
|
|
142
|
+
const lookupFieldTypeEnumerations = organizeEnumerations(pgns.LookupFieldTypeEnumerations)
|
|
104
143
|
const lookupBitEnumerations = organizeEnumerations(pgns.LookupBitEnumerations)
|
|
105
144
|
const organizedPGNs = organizePGNs()
|
|
106
145
|
const getPgn = pgn => organizedPGNs[pgn]
|
|
@@ -113,6 +152,9 @@ module.exports = {
|
|
|
113
152
|
pgns: organizedPGNs,
|
|
114
153
|
lookupEnumerationName,
|
|
115
154
|
lookupEnumerationValue,
|
|
155
|
+
lookupFieldTypeEnumerationName,
|
|
156
|
+
lookupFieldTypeEnumerationValue,
|
|
157
|
+
lookupFieldTypeEnumerationBits,
|
|
116
158
|
lookupBitEnumerationName,
|
|
117
159
|
lookupBitEnumerationValue,
|
|
118
160
|
addCustomPgns: (pgns, setter) => {
|
package/lib/toPgn.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
const { getField } = require('./fromPgn')
|
|
18
|
-
const { pgns, getCustomPgn, lookupEnumerationValue, lookupBitEnumerationName } = require('./pgns')
|
|
18
|
+
const { pgns, getCustomPgn, lookupEnumerationValue, lookupFieldTypeEnumerationValue, lookupBitEnumerationName, lookupFieldTypeEnumerationBits } = require('./pgns')
|
|
19
19
|
const _ = require('lodash')
|
|
20
20
|
const BitStream = require('bit-buffer').BitStream
|
|
21
21
|
const Int64LE = require('int64-buffer').Int64LE
|
|
@@ -95,7 +95,7 @@ function toPgn(data) {
|
|
|
95
95
|
RepeatingFields = pgnData.RepeatingFieldSet1Size ? pgnData.RepeatingFieldSet1Size : 0
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
writeField(bs, pgn_number, field, data, value)
|
|
98
|
+
writeField(bs, pgn_number, field, data, value, fields)
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
if ( data.list ) {
|
|
@@ -104,7 +104,7 @@ function toPgn(data) {
|
|
|
104
104
|
var field = fields[pgnData.Fields.length-RepeatingFields+index]
|
|
105
105
|
var value = repeat[field.Name];
|
|
106
106
|
|
|
107
|
-
writeField(bs, pgn_number, field, data, value)
|
|
107
|
+
writeField(bs, pgn_number, field, data, value, fields)
|
|
108
108
|
}
|
|
109
109
|
})
|
|
110
110
|
}
|
|
@@ -147,11 +147,15 @@ function dumpWritten(bs, field, startPos, value) {
|
|
|
147
147
|
console.log(string + `] ${value}`)
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
function writeField(bs, pgn_number, field, data, value, bitLength) {
|
|
150
|
+
function writeField(bs, pgn_number, field, data, value, fields, bitLength) {
|
|
151
151
|
var startPos = bs.byteIndex
|
|
152
152
|
|
|
153
153
|
if ( _.isUndefined(bitLength) ) {
|
|
154
|
-
|
|
154
|
+
if ( field.BitLengthVariable && field.FieldType === "KEY_VALUE" ) {
|
|
155
|
+
bitLength = lookupKeyBitLength(data, fields)
|
|
156
|
+
} else {
|
|
157
|
+
bitLength = field.BitLength
|
|
158
|
+
}
|
|
155
159
|
}
|
|
156
160
|
|
|
157
161
|
// console.log(`${field.Name}:${value}(${bitLength}-${field.Resolution})`)
|
|
@@ -204,7 +208,7 @@ function writeField(bs, pgn_number, field, data, value, bitLength) {
|
|
|
204
208
|
}
|
|
205
209
|
|
|
206
210
|
if ( field.FieldType === 'VARIABLE' ) {
|
|
207
|
-
writeVariableLengthField(bs, pgn_number, data, field, value)
|
|
211
|
+
writeVariableLengthField(bs, pgn_number, data, field, value, fields)
|
|
208
212
|
} else if ( _.isBuffer(value) ) {
|
|
209
213
|
value.copy(bs.view.buffer, bs.byteIndex)
|
|
210
214
|
bs.byteIndex += value.length
|
|
@@ -259,21 +263,37 @@ function writeField(bs, pgn_number, field, data, value, bitLength) {
|
|
|
259
263
|
//dumpWritten(bs, field, startPos, value)
|
|
260
264
|
}
|
|
261
265
|
|
|
262
|
-
function writeVariableLengthField(bs, pgn_number, pgn, field, value) {
|
|
266
|
+
function writeVariableLengthField(bs, pgn_number, pgn, field, value, fields) {
|
|
263
267
|
var refField = getField(pgn.PGN, bs.view.buffer[bs.byteIndex-1]-1, pgn)
|
|
264
268
|
|
|
265
269
|
if ( refField ) {
|
|
266
270
|
var bits = (refField.BitLength + 7) & ~7; // Round # of bits in field refField up to complete bytes: 1->8, 7->8, 8->8 etc.
|
|
267
271
|
|
|
268
|
-
return writeField(bs, pgn_number, refField, pgn, value, bits)
|
|
272
|
+
return writeField(bs, pgn_number, refField, pgn, value, fields, bits)
|
|
269
273
|
}
|
|
270
274
|
}
|
|
271
275
|
|
|
272
276
|
function lookup(field, stringValue) {
|
|
273
|
-
var res
|
|
277
|
+
var res
|
|
278
|
+
if ( field.LookupEnumeration ) {
|
|
279
|
+
res = lookupEnumerationValue(field.LookupEnumeration, stringValue)
|
|
280
|
+
} else {
|
|
281
|
+
res = lookupFieldTypeEnumerationValue(field.LookupFieldTypeEnumeration, stringValue)
|
|
282
|
+
}
|
|
274
283
|
return _.isUndefined(res) ? stringValue : res
|
|
275
284
|
}
|
|
276
285
|
|
|
286
|
+
function lookupKeyBitLength(data, fields)
|
|
287
|
+
{
|
|
288
|
+
let field = fields.find(field => (field.Name === 'Key'))
|
|
289
|
+
|
|
290
|
+
let val = data['Key']
|
|
291
|
+
if ( typeof val === 'string' ) {
|
|
292
|
+
val = lookupFieldTypeEnumerationValue(field.LookupFieldTypeEnumeration, val)
|
|
293
|
+
}
|
|
294
|
+
return lookupFieldTypeEnumerationBits(field.LookupFieldTypeEnumeration, val)
|
|
295
|
+
}
|
|
296
|
+
|
|
277
297
|
function isDefined(value) {
|
|
278
298
|
return typeof value !== 'undefined' && value != null
|
|
279
299
|
}
|