@canboat/canboatjs 1.24.4 → 1.26.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 +8 -4
- package/lib/canbus.js +82 -39
- package/package.json +1 -1
package/bin/candumpjs
CHANGED
|
@@ -57,14 +57,18 @@ parser.on('pgn', (pgn) => {
|
|
|
57
57
|
|
|
58
58
|
const canDevice = argv['_'][0]
|
|
59
59
|
|
|
60
|
-
|
|
60
|
+
const channel = socketcan.createRawChannel(canDevice);
|
|
61
61
|
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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,34 +117,73 @@ function CanbusStream (options) {
|
|
|
119
117
|
}
|
|
120
118
|
})
|
|
121
119
|
} else {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (
|
|
128
|
-
|
|
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
|
+
try {
|
|
127
|
+
this.channel.stop()
|
|
128
|
+
} catch ( error ) {
|
|
129
|
+
}
|
|
130
|
+
delete this.channel
|
|
131
|
+
this.setProviderError('No data received, retrying...')
|
|
132
|
+
if ( this.options.app ) {
|
|
133
|
+
console.error('No data received, retrying...')
|
|
134
|
+
}
|
|
135
|
+
this.connect()
|
|
129
136
|
}
|
|
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)
|
|
137
|
+
}, noDataReceivedTimeout * 1000)
|
|
146
138
|
}
|
|
147
139
|
}
|
|
148
140
|
}
|
|
149
141
|
|
|
142
|
+
CanbusStream.prototype.connect = function() {
|
|
143
|
+
try {
|
|
144
|
+
var that = this
|
|
145
|
+
var canDevice = this.options.canDevice || 'can0'
|
|
146
|
+
this.channel = this.socketcan.createRawChannelWithOptions(canDevice, { non_block_send: true} );
|
|
147
|
+
this.channel.addListener('onStopped', (msg) => {
|
|
148
|
+
delete this.channel
|
|
149
|
+
this.setProviderError('Stopped, Retrying...')
|
|
150
|
+
if ( this.options.app ) {
|
|
151
|
+
console.error('socketcan stopped, retrying...')
|
|
152
|
+
}
|
|
153
|
+
setTimeout(() => {
|
|
154
|
+
this.setProviderError('Reconnecting...')
|
|
155
|
+
this.connect()
|
|
156
|
+
}, 2000)
|
|
157
|
+
})
|
|
158
|
+
this.channel.addListener('onMessage', (msg) => {
|
|
159
|
+
var pgn = parseCanId(msg.id)
|
|
160
|
+
|
|
161
|
+
if ( this.noDataInterval ) {
|
|
162
|
+
this.lastDataReceived = Date.now()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if ( that.candevice && that.candevice.cansend && pgn.src == that.candevice.address ) {
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
pgn.timestamp = new Date().toISOString()
|
|
170
|
+
if ( that.plainText ) {
|
|
171
|
+
this.push(binToActisense(pgn, msg.data, msg.data.length))
|
|
172
|
+
} else {
|
|
173
|
+
that.push({ pgn, length: msg.data.length, data: msg.data })
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
this.channel.start()
|
|
177
|
+
this.candevice = new CanDevice(this, this.options)
|
|
178
|
+
this.candevice.start()
|
|
179
|
+
this.setProviderStatus('Connected')
|
|
180
|
+
} catch (e) {
|
|
181
|
+
console.error(`unable to open canbus ${canDevice}: ${e}`)
|
|
182
|
+
console.error(e.stack)
|
|
183
|
+
this.setProviderError(e.message)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
150
187
|
function binToActisense(pgn, data, length) {
|
|
151
188
|
return (
|
|
152
189
|
pgn.timestamp +
|
|
@@ -290,7 +327,13 @@ CanbusStream.prototype.end = function () {
|
|
|
290
327
|
if ( this.socketCanWriter ) {
|
|
291
328
|
debug('end, killing socketcan-writer process')
|
|
292
329
|
this.socketCanWriter.kill()
|
|
293
|
-
|
|
330
|
+
}
|
|
331
|
+
if ( this.channel ) {
|
|
332
|
+
this.channel.stop()
|
|
333
|
+
}
|
|
334
|
+
if ( this.noDataInterval ) {
|
|
335
|
+
clearInterval(this.noDataInterval)
|
|
336
|
+
}
|
|
294
337
|
}
|
|
295
338
|
|
|
296
339
|
|