@canboat/canboatjs 1.24.3 → 1.25.0

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/bin/candumpjs CHANGED
@@ -57,14 +57,18 @@ parser.on('pgn', (pgn) => {
57
57
 
58
58
  const canDevice = argv['_'][0]
59
59
 
60
- this.channel = socketcan.createRawChannel(canDevice);
60
+ const channel = socketcan.createRawChannel(canDevice);
61
61
 
62
- this.channel.addListener('onMessage', (msg) => {
62
+ channel.addListener('onStopped', (msg) => {
63
+ console.error('socketcan stopped')
64
+ })
65
+
66
+ channel.addListener('onMessage', (msg) => {
63
67
  var pgn = parseCanId(msg.id)
64
68
 
65
69
  pgn.timestamp = new Date().toISOString()
66
-
70
+
67
71
  parser.parse({ pgn, length: msg.data.length, data: msg.data })
68
72
  })
69
73
 
70
- this.channel.start()
74
+ channel.start()
package/lib/canbus.js CHANGED
@@ -55,25 +55,23 @@ function CanbusStream (options) {
55
55
  this.options = options
56
56
  this.start()
57
57
 
58
- const setProviderStatus = options.app && options.app.setProviderStatus
59
- ? (msg) => {
60
- options.app.setProviderStatus(options.providerId, msg)
61
- }
62
- : () => {}
63
- const setProviderError = options.app && options.app.setProviderError
64
- ? (msg) => {
65
- options.app.setProviderError(options.providerId, msg)
66
- }
67
- : () => {}
68
-
58
+ this.setProviderStatus = options.app && options.app.setProviderStatus
59
+ ? (msg) => {
60
+ options.app.setPluginStatus(options.providerId, msg)
61
+ }
62
+ : () => {}
63
+ this.setProviderError = options.app && options.app.setProviderError
64
+ ? (msg) => {
65
+ options.app.setPluginError(options.providerId, msg)
66
+ }
67
+ : () => {}
68
+
69
69
  if ( options.fromStdIn ) {
70
70
  return
71
71
  }
72
72
 
73
- var socketcan;
74
-
75
73
  try {
76
- socketcan = require('socketcan')
74
+ this.socketcan = require('socketcan')
77
75
  } catch ( err ) {
78
76
  console.error(err)
79
77
  var msg = 'WARNING unable to load native socketcan interface'
@@ -92,7 +90,7 @@ function CanbusStream (options) {
92
90
  }
93
91
 
94
92
  var canDevice = options.canDevice || 'can0'
95
- if ( !socketcan || this.options.useSocketCanWriter ) {
93
+ if ( !this.socketcan || this.options.useSocketCanWriter ) {
96
94
  this.socketCanWriter = null
97
95
  var hasWriter = spawn('sh', ['-c', 'which socketcan-writer'])
98
96
 
@@ -119,31 +117,47 @@ function CanbusStream (options) {
119
117
  }
120
118
  })
121
119
  } else {
122
- try {
123
- this.channel = socketcan.createRawChannelWithOptions(canDevice, { non_block_send: true} );
124
- this.channel.addListener('onMessage', (msg) => {
125
- var pgn = parseCanId(msg.id)
126
-
127
- if ( that.candevice && that.candevice.cansend && pgn.src == that.candevice.address ) {
128
- return
129
- }
130
-
131
- pgn.timestamp = new Date().toISOString()
132
- if ( that.plainText ) {
133
- this.push(binToActisense(pgn, msg.data, msg.data.length))
134
- } else {
135
- that.push({ pgn, length: msg.data.length, data: msg.data })
136
- }
137
- })
138
- this.channel.start()
139
- this.candevice = new CanDevice(this, options)
140
- this.candevice.start()
141
- setProviderStatus('Connected')
142
- } catch (e) {
143
- setProviderError(e.message)
144
- console.error(`unable to open canbus ${canDevice}: ${e}`)
145
- console.error(e.stack)
146
- }
120
+ this.connect()
121
+ }
122
+ }
123
+
124
+ CanbusStream.prototype.connect = function() {
125
+ try {
126
+ var that = this
127
+ var canDevice = this.options.canDevice || 'can0'
128
+ this.channel = this.socketcan.createRawChannelWithOptions(canDevice, { non_block_send: true} );
129
+ this.channel.addListener('onStopped', (msg) => {
130
+ this.setProviderError('Stopped, Retrying...')
131
+ if ( this.options.app ) {
132
+ console.error('socketcan stopped, retrying...')
133
+ }
134
+ setTimeout(() => {
135
+ this.setProviderError('Reconnecting...')
136
+ this.connect()
137
+ }, 2000)
138
+ })
139
+ this.channel.addListener('onMessage', (msg) => {
140
+ var pgn = parseCanId(msg.id)
141
+
142
+ if ( that.candevice && that.candevice.cansend && pgn.src == that.candevice.address ) {
143
+ return
144
+ }
145
+
146
+ pgn.timestamp = new Date().toISOString()
147
+ if ( that.plainText ) {
148
+ this.push(binToActisense(pgn, msg.data, msg.data.length))
149
+ } else {
150
+ that.push({ pgn, length: msg.data.length, data: msg.data })
151
+ }
152
+ })
153
+ this.channel.start()
154
+ this.candevice = new CanDevice(this, this.options)
155
+ this.candevice.start()
156
+ this.setProviderStatus('Connected')
157
+ } catch (e) {
158
+ console.error(`unable to open canbus ${canDevice}: ${e}`)
159
+ console.error(e.stack)
160
+ this.setProviderError(e.message)
147
161
  }
148
162
  }
149
163
 
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
@@ -383,6 +386,17 @@ fieldTypeWriters['ASCII text'] = (pgn, field, value, bs) => {
383
386
  }
384
387
  }
385
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
+
386
400
  fieldTypeWriters["String with start/stop byte"] = (pgn, field, value, bs) => {
387
401
  if ( _.isUndefined(value) ) {
388
402
  value = ""
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canboat/canboatjs",
3
- "version": "1.24.3",
3
+ "version": "1.25.0",
4
4
  "description": "Native javascript version of canboat",
5
5
  "main": "index.js",
6
6
  "scripts": {