@canboat/canboatjs 1.24.4 → 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 +8 -4
- package/lib/canbus.js +54 -40
- 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,31 +117,47 @@ function CanbusStream (options) {
|
|
|
119
117
|
}
|
|
120
118
|
})
|
|
121
119
|
} else {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
|