@canboat/canboatjs 1.24.2 → 1.24.4

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.
Files changed (2) hide show
  1. package/lib/toPgn.js +44 -0
  2. package/package.json +1 -1
package/lib/toPgn.js CHANGED
@@ -28,6 +28,8 @@ const { getIndustryCode, getManufacturerCode } = require('./codes')
28
28
  const debug = require('debug')('canboatjs:toPgn')
29
29
 
30
30
  const RES_STRINGLAU = 'ASCII or UNICODE string starting with length and control byte'
31
+ const RES_STRINGLZ = 'ASCII string starting with length byte'
32
+
31
33
 
32
34
  var fieldTypeWriters = {}
33
35
  var fieldTypeMappers = {}
@@ -115,6 +117,7 @@ function toPgn(data) {
115
117
 
116
118
  if ( pgnData.Length != 0xff
117
119
  && fields[fields.length-1].Type != RES_STRINGLAU
120
+ && fields[fields.length-1].Type != RES_STRINGLZ
118
121
  && !pgnData.RepeatingFields ) {
119
122
 
120
123
  var len = lengthsOff[pgnData.PGN] || pgnData.Length
@@ -327,6 +330,36 @@ const actisenseToiKonvert = _.flow(parseActisense, encodePDGY)
327
330
  const actisenseToN2KAsciiFormat = _.flow(parseActisense, encodeActisenseN2KACSII)
328
331
  const actisenseToN2KActisenseFormat = _.flow(parseActisense, encodeN2KActisense)
329
332
 
333
+ function bitIsSet(field, index, value) {
334
+ if (!field.value2name) {
335
+ field.value2name = {};
336
+ field.EnumBitValues.forEach(function(enumPair) {
337
+ var key = _.keys(enumPair)[0]
338
+ field.value2name[Number(key)] = enumPair[key]
339
+ })
340
+ }
341
+ return value.indexOf(field.value2name[index]) != -1
342
+ }
343
+
344
+ fieldTypeWriters['Bitfield'] = (pgn, field, value, bs) => {
345
+ if ( _.isUndefined(value) || value.length === 0 ) {
346
+ if ( field.BitLength % 8 == 0 ) {
347
+ var bytes = field.BitLength/8
348
+ var lastByte = field.Signed ? 0x7f : 0xff
349
+ for ( var i = 0; i < bytes-1; i++ ) {
350
+ bs.writeUint8(0x0)
351
+ }
352
+ bs.writeUint8(0x0)
353
+ } else {
354
+ bs.writeBits(0xffffffff, field.BitLength)
355
+ }
356
+ } else {
357
+ for ( var i = 0; i < field.BitLength; i++ ) {
358
+ bs.writeBits(bitIsSet(field, i, value) ? 1 : 0, 1)
359
+ }
360
+ }
361
+ }
362
+
330
363
  fieldTypeWriters['ASCII text'] = (pgn, field, value, bs) => {
331
364
 
332
365
  let fill = 0xff
@@ -353,6 +386,17 @@ fieldTypeWriters['ASCII text'] = (pgn, field, value, bs) => {
353
386
  }
354
387
  }
355
388
 
389
+ fieldTypeWriters[RES_STRINGLZ] = (pgn, field, value, bs) => {
390
+ if ( _.isUndefined(value) ) {
391
+ value = ""
392
+ }
393
+ bs.writeUint8(value.length)
394
+ for ( var i = 0; i < value.length; i++ ) {
395
+ bs.writeUint8(value.charCodeAt(i))
396
+ }
397
+ bs.writeUint8(0)
398
+ }
399
+
356
400
  fieldTypeWriters["String with start/stop byte"] = (pgn, field, value, bs) => {
357
401
  if ( _.isUndefined(value) ) {
358
402
  value = ""
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canboat/canboatjs",
3
- "version": "1.24.2",
3
+ "version": "1.24.4",
4
4
  "description": "Native javascript version of canboat",
5
5
  "main": "index.js",
6
6
  "scripts": {