@canboat/canboatjs 3.2.4 → 3.3.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.
@@ -8,18 +8,25 @@ import { binToActisense } from '../utilities'
8
8
  // eslint-disable-next-line @typescript-eslint/no-require-imports
9
9
  const socketcan = require('socketcan')
10
10
 
11
- const parser = new FromPgn()
12
-
13
11
  const argv = minimist(process.argv.slice(2), {
14
- alias: { h: 'help' }
12
+ alias: {
13
+ h: 'help',
14
+ boolean: ['n', 'r', 'camel', 'camel-compat', 'show-non-matches']
15
+ }
15
16
  })
16
17
 
17
18
  if (argv['help']) {
18
19
  console.error(`Usage: ${process.argv[0]} [options] candevice
19
20
 
20
21
  Options:
21
- --format <format> json, actisense
22
- -h, --help output usage information`)
22
+ --format <format> json, actisense
23
+ -c don't check for invalid values
24
+ -n output null values
25
+ -r parse $MXPGN as little endian
26
+ --camel output field names in camelCase
27
+ --camel-compat output field names in camelCase and regular
28
+ --show-non-matches show pgn data without any matches
29
+ -h, --help output usage information`)
23
30
  process.exit(1)
24
31
  }
25
32
 
@@ -28,6 +35,15 @@ if (argv['_'].length === 0) {
28
35
  process.exit(1)
29
36
  }
30
37
 
38
+ const parser = new FromPgn({
39
+ returnNulls: argv['n'] === true,
40
+ littleEndianMXPGN: argv['r'] === true,
41
+ checkForInvalidFields: argv['c'] !== true,
42
+ useCamel: argv['camel'],
43
+ useCamelCompat: argv['camel-compat'],
44
+ returnNonMatches: argv['show-non-matches']
45
+ })
46
+
31
47
  const format = argv['format'] || 'json'
32
48
 
33
49
  /*
package/lib/fromPgn.ts CHANGED
@@ -16,13 +16,7 @@
16
16
 
17
17
  // FIXME: MMSI sould be a string
18
18
 
19
- import {
20
- Definition,
21
- Field,
22
- PGN,
23
- FieldType,
24
- getPGNClass
25
- } from '@canboat/ts-pgns'
19
+ import { Definition, Field, PGN, FieldType, createPGN } from '@canboat/ts-pgns'
26
20
  import { createDebug } from './utilities'
27
21
  import { EventEmitter } from 'node:events'
28
22
  import pkg from '../package.json'
@@ -101,6 +95,10 @@ export class Parser extends EventEmitter {
101
95
  this.options.useCamelCompat = false
102
96
  }
103
97
 
98
+ if (this.options.returnNonMatches === undefined) {
99
+ this.options.returnNonMatches = false
100
+ }
101
+
104
102
  this.name = pkg.name
105
103
  this.version = pkg.version
106
104
  this.author = pkg.author
@@ -162,7 +160,6 @@ export class Parser extends EventEmitter {
162
160
  }
163
161
 
164
162
  let pgnData: Definition | undefined
165
- const origPGNList = pgnList
166
163
 
167
164
  if (pgnList.length > 1) {
168
165
  pgnData = this.findMatchPgn(pgnList)
@@ -294,6 +291,7 @@ export class Parser extends EventEmitter {
294
291
  try {
295
292
  let fields = pgnData.Fields
296
293
 
294
+ let continueReading = true
297
295
  for (let i = 0; i < fields.length - RepeatingFields; i++) {
298
296
  const field = fields[i]
299
297
  const hasMatch = field.Match !== undefined
@@ -305,30 +303,13 @@ export class Parser extends EventEmitter {
305
303
  //console.log(JSON.stringify(pgnList, null, 2))
306
304
  pgnList = pgnList.filter((f) => f.Fields[i].Match == value)
307
305
  if (pgnList.length == 0) {
308
- //this.emit('warning', pgn, `no conversion found for pgn`)
309
- trace('warning no conversion found for pgn %j', pgn)
310
-
311
- const nonMatch = this.findNonMatchPgn(origPGNList)
312
- if (nonMatch) {
313
- pgnList = [nonMatch]
314
- pgnData = pgnList[0]
315
- fields = pgnData.Fields
316
- const postProcessor = fieldTypePostProcessors[field.FieldType]
317
- if (postProcessor) {
318
- value = postProcessor(pgnData.Fields[i], value)
319
- }
306
+ if (this.options.returnNonMatches) {
307
+ //this.emit('warning', pgn, `no conversion found for pgn`)
308
+ trace('warning no conversion found for pgn %j', pgn)
309
+ continueReading = false
310
+ break
320
311
  } else {
321
- const ts = _.get(pgn, 'timestamp', new Date())
322
- pgn.timestamp = _.isDate(ts) ? ts.toISOString() : ts
323
- if (
324
- value === undefined &&
325
- (value != null || this.options.returnNulls)
326
- ) {
327
- this.setField(pgn.fields, field, value)
328
- }
329
- this.emit('pgn', pgn)
330
- cb && cb(undefined, pgn)
331
- return pgn
312
+ return undefined
332
313
  }
333
314
  } else {
334
315
  pgnData = pgnList[0]
@@ -351,7 +332,7 @@ export class Parser extends EventEmitter {
351
332
  this.setField(pgn.fields, field, value)
352
333
  }
353
334
  }
354
- if (RepeatingFields > 0) {
335
+ if (RepeatingFields > 0 && continueReading) {
355
336
  const repeating: Field[] = (fields as any).slice(
356
337
  fields.length - RepeatingFields
357
338
  )
@@ -399,24 +380,22 @@ export class Parser extends EventEmitter {
399
380
  }
400
381
  */
401
382
 
402
- const cf = getPGNClass(pgnData.Id)
383
+ const res = createPGN(pgnData.Id, pgn.fields)
403
384
 
404
- if (cf === undefined) {
385
+ if (res === undefined) {
405
386
  this.emit('error', pgn, 'no class')
406
387
  cb && cb('no class', undefined)
407
388
  return
408
389
  }
409
390
 
410
- const res = cf(pgn.fields)
411
-
412
391
  res.description = pgnData.Description
413
392
  res.src = pgn.src
414
393
  res.dst = pgn.dst
415
394
  res.prio = pgn.prio
416
- res.canId = (pgn as any).canId
417
- res.time = (pgn as any).time
418
- res.timer = (pgn as any).timer
419
- res.direction = (pgn as any).direction
395
+ ;(res as any).canId = (pgn as any).canId
396
+ ;(res as any).time = (pgn as any).time
397
+ ;(res as any).timer = (pgn as any).timer
398
+ ;(res as any).direction = (pgn as any).direction
420
399
 
421
400
  // Stringify timestamp because SK Server needs it that way.
422
401
  const ts = _.get(pgn, 'timestamp', new Date())
@@ -512,6 +491,7 @@ export class Parser extends EventEmitter {
512
491
  return this.parseString(sentence, cb)
513
492
  }
514
493
 
494
+ /*
515
495
  // Venus MQTT-N2K
516
496
  parseVenusMQTT(pgn_data: any, cb: FromPgnCallback) {
517
497
  try {
@@ -534,7 +514,8 @@ export class Parser extends EventEmitter {
534
514
  cb && cb(error, undefined)
535
515
  this.emit('error', pgn_data, error)
536
516
  }
537
- }
517
+ }
518
+ */
538
519
 
539
520
  //Yacht Devices NMEA2000 Wifi gateway
540
521
  parseYDGW02(pgn_data: any, cb: FromPgnCallback) {
package/lib/pgns.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {
2
- getPGN,
2
+ getPGNWithNumber,
3
3
  EnumBase,
4
4
  Enumeration,
5
5
  BitEnumeration,
@@ -152,7 +152,7 @@ export const lookupFieldTypeEnumerations = organizeEnumerations(
152
152
  getFieldTypeEnumerations()
153
153
  )
154
154
  export const lookupBitEnumerations = organizeEnumerations(getBitEnumerations())
155
- export const getPgn = getPGN
155
+ export const getPgn = getPGNWithNumber
156
156
  export const customPgns: any = {}
157
157
 
158
158
  export const addCustomPgns = (pgns: any, setter: any) => {
package/lib/ydvr.js CHANGED
@@ -18,12 +18,12 @@ var sequencePgns = new Set([
18
18
  130577, 130578, 130816
19
19
  ])
20
20
 
21
- function YdvrStream() {
21
+ function YdvrStream(options) {
22
22
  if (!(this instanceof YdvrStream)) {
23
- return new YdvrStream()
23
+ return new YdvrStream(options)
24
24
  }
25
25
 
26
- this.parser = new FromPgn()
26
+ this.parser = new FromPgn(options)
27
27
 
28
28
  this.messageCount = 0
29
29
  this.errorCount = 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canboat/canboatjs",
3
- "version": "3.2.4",
3
+ "version": "3.3.1",
4
4
  "description": "Native javascript version of canboat",
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -74,7 +74,7 @@
74
74
  "node": ">=20"
75
75
  },
76
76
  "dependencies": {
77
- "@canboat/ts-pgns": "^1.0.0",
77
+ "@canboat/ts-pgns": "^1.6.0",
78
78
  "bit-buffer": "0.2.3",
79
79
  "debug": "^4.3.4",
80
80
  "dnssd": "^0.4.1",