@canboat/canboatjs 1.25.0 → 1.26.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.
Files changed (2) hide show
  1. package/lib/canbus.js +42 -8
  2. package/package.json +1 -1
package/lib/canbus.js CHANGED
@@ -118,6 +118,25 @@ function CanbusStream (options) {
118
118
  })
119
119
  } else {
120
120
  this.connect()
121
+
122
+ const noDataReceivedTimeout = typeof options.noDataReceivedTimeout !== 'undefined' ? options.noDataReceivedTimeout : -1
123
+ if ( noDataReceivedTimeout > 0 ) {
124
+ this.noDataInterval = setInterval(() => {
125
+ if ( this.channel && this.lastDataReceived && Date.now() - this.lastDataReceived > noDataReceivedTimeout * 1000 ) {
126
+ let channel = this.channel
127
+ delete this.channel
128
+ try {
129
+ channel.stop()
130
+ } catch ( error ) {
131
+ }
132
+ this.setProviderError('No data received, retrying...')
133
+ if ( this.options.app ) {
134
+ console.error('No data received, retrying...')
135
+ }
136
+ this.connect()
137
+ }
138
+ }, noDataReceivedTimeout * 1000)
139
+ }
121
140
  }
122
141
  }
123
142
 
@@ -127,18 +146,25 @@ CanbusStream.prototype.connect = function() {
127
146
  var canDevice = this.options.canDevice || 'can0'
128
147
  this.channel = this.socketcan.createRawChannelWithOptions(canDevice, { non_block_send: true} );
129
148
  this.channel.addListener('onStopped', (msg) => {
130
- this.setProviderError('Stopped, Retrying...')
131
- if ( this.options.app ) {
132
- console.error('socketcan stopped, retrying...')
149
+ if ( this.channel ) { // stoped by us?
150
+ delete this.channel
151
+ this.setProviderError('Stopped, Retrying...')
152
+ if ( this.options.app ) {
153
+ console.error('socketcan stopped, retrying...')
154
+ }
155
+ setTimeout(() => {
156
+ this.setProviderError('Reconnecting...')
157
+ this.connect()
158
+ }, 2000)
133
159
  }
134
- setTimeout(() => {
135
- this.setProviderError('Reconnecting...')
136
- this.connect()
137
- }, 2000)
138
160
  })
139
161
  this.channel.addListener('onMessage', (msg) => {
140
162
  var pgn = parseCanId(msg.id)
141
163
 
164
+ if ( this.noDataInterval ) {
165
+ this.lastDataReceived = Date.now()
166
+ }
167
+
142
168
  if ( that.candevice && that.candevice.cansend && pgn.src == that.candevice.address ) {
143
169
  return
144
170
  }
@@ -304,7 +330,15 @@ CanbusStream.prototype.end = function () {
304
330
  if ( this.socketCanWriter ) {
305
331
  debug('end, killing socketcan-writer process')
306
332
  this.socketCanWriter.kill()
307
- }
333
+ }
334
+ if ( this.channel ) {
335
+ let channel = this.channel
336
+ delete this.channel
337
+ channel.stop()
338
+ }
339
+ if ( this.noDataInterval ) {
340
+ clearInterval(this.noDataInterval)
341
+ }
308
342
  }
309
343
 
310
344
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canboat/canboatjs",
3
- "version": "1.25.0",
3
+ "version": "1.26.1",
4
4
  "description": "Native javascript version of canboat",
5
5
  "main": "index.js",
6
6
  "scripts": {