@canboat/canboatjs 3.0.0-beta.5 → 3.0.0-beta.7
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 +1 -14
- package/bin/cansend +106 -0
- package/lib/canbus.js +12 -18
- package/lib/n2k-actisense.js +1 -17
- package/lib/n2kDevice.js +4 -3
- package/lib/simpleCan.js +1 -15
- package/lib/utilities.js +15 -0
- package/lib/ydgw02.js +1 -1
- package/package.json +1 -1
package/bin/candumpjs
CHANGED
|
@@ -4,7 +4,7 @@ const canboatjs = require('../index')
|
|
|
4
4
|
const Parser = require('../index').FromPgn
|
|
5
5
|
const { parseCanId } = require('../lib/canId')
|
|
6
6
|
const socketcan = require('socketcan')
|
|
7
|
-
|
|
7
|
+
const { binToActisense } = require('../lib/utilities')
|
|
8
8
|
var parser = new canboatjs.FromPgn()
|
|
9
9
|
|
|
10
10
|
|
|
@@ -83,16 +83,3 @@ channel.addListener('onMessage', (msg) => {
|
|
|
83
83
|
channel.start()
|
|
84
84
|
|
|
85
85
|
|
|
86
|
-
function binToActisense(pgn, data, length) {
|
|
87
|
-
return (
|
|
88
|
-
pgn.timestamp +
|
|
89
|
-
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
|
|
90
|
-
new Uint32Array(data)
|
|
91
|
-
.reduce(function(acc, i) {
|
|
92
|
-
acc.push(i.toString(16));
|
|
93
|
-
return acc;
|
|
94
|
-
}, [])
|
|
95
|
-
.map(x => (x.length === 1 ? "0" + x : x))
|
|
96
|
-
.join(",")
|
|
97
|
-
);
|
|
98
|
-
}
|
package/bin/cansend
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const canboatjs = require('../index')
|
|
4
|
+
const Parser = require('../index').FromPgn
|
|
5
|
+
const { parseCanId } = require('../lib/canId')
|
|
6
|
+
const { parseActisense } = require('../lib/stringMsg')
|
|
7
|
+
const socketcan = require('socketcan')
|
|
8
|
+
|
|
9
|
+
const { toPgn } = require('../lib/toPgn')
|
|
10
|
+
const { getPlainPGNs, binToActisense } = require('../lib/utilities')
|
|
11
|
+
const { encodeCanId } = require('../lib/canId')
|
|
12
|
+
|
|
13
|
+
const argv = require('minimist')(process.argv.slice(2), {
|
|
14
|
+
alias: { h: 'help' },
|
|
15
|
+
boolean: ['log-output'],
|
|
16
|
+
string: ['src'],
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
if ( argv['help'] ) {
|
|
20
|
+
console.error(`Usage: ${process.argv[0]} [options] candevice
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--src <src> use src for all messages
|
|
24
|
+
--log-output log messages sent
|
|
25
|
+
-h, --help output usage information`)
|
|
26
|
+
process.exit(1)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if ( argv['_'].length === 0 ) {
|
|
30
|
+
console.error('Please specify a device')
|
|
31
|
+
process.exit(1)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const canDevice = argv['_'][0]
|
|
35
|
+
const srcArg = argv.src
|
|
36
|
+
const logOut = argv['log-output']
|
|
37
|
+
|
|
38
|
+
const channel = socketcan.createRawChannel(canDevice);
|
|
39
|
+
|
|
40
|
+
channel.addListener('onStopped', (msg) => {
|
|
41
|
+
console.error('socketcan stopped')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
channel.start()
|
|
45
|
+
|
|
46
|
+
var readline = require('readline')
|
|
47
|
+
var rl = readline.createInterface({
|
|
48
|
+
input: process.stdin,
|
|
49
|
+
output: process.stdout,
|
|
50
|
+
terminal: false
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
var input = []
|
|
54
|
+
|
|
55
|
+
rl.on('line', function (line) {
|
|
56
|
+
let msg = line[0] === '{' ? JSON.parse(line) : line
|
|
57
|
+
|
|
58
|
+
if ( typeof msg === 'string' ) {
|
|
59
|
+
var split = msg.split(',')
|
|
60
|
+
if ( srcArg !== undefined ) {
|
|
61
|
+
split[3] = srcArg
|
|
62
|
+
}
|
|
63
|
+
msg = split.join(',')
|
|
64
|
+
} else {
|
|
65
|
+
if ( msg.prio === undefined ) {
|
|
66
|
+
msg.prio = 3
|
|
67
|
+
}
|
|
68
|
+
if ( msg.dst === undefined ) {
|
|
69
|
+
msg.dst = 255
|
|
70
|
+
}
|
|
71
|
+
if ( srcArg !== undefined ) {
|
|
72
|
+
msg.src = srcArg
|
|
73
|
+
}
|
|
74
|
+
if ( msg.src === undefined ) {
|
|
75
|
+
msg.src = 100
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
var pgn, canid, buffer
|
|
80
|
+
if ( typeof msg === 'object' ) {
|
|
81
|
+
canid = encodeCanId(msg)
|
|
82
|
+
buffer = toPgn(msg)
|
|
83
|
+
pgn = msg
|
|
84
|
+
} else {
|
|
85
|
+
pgn = parseActisense(msg)
|
|
86
|
+
canid = encodeCanId(pgn)
|
|
87
|
+
buffer = pgn.data
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
pgn.timestamp = new Date().toISOString()
|
|
91
|
+
|
|
92
|
+
if ( buffer.length > 8 || pgn.pgn == 126720 ) {
|
|
93
|
+
var pgns = getPlainPGNs(buffer)
|
|
94
|
+
pgns.forEach(pbuffer => {
|
|
95
|
+
channel.send({id: canid, ext:true, data: pbuffer})
|
|
96
|
+
if ( logOut ) {
|
|
97
|
+
console.log(binToActisense(pgn, pbuffer, pbuffer.length))
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
} else {
|
|
101
|
+
channel.send({id: canid, ext:true, data: buffer})
|
|
102
|
+
if ( logOut ) {
|
|
103
|
+
console.log(binToActisense(pgn, buffer, buffer.length))
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
})
|
package/lib/canbus.js
CHANGED
|
@@ -23,7 +23,7 @@ const { toPgn } = require('./toPgn')
|
|
|
23
23
|
const Parser = require('./fromPgn').Parser
|
|
24
24
|
const _ = require('lodash')
|
|
25
25
|
const CanDevice = require('./candevice')
|
|
26
|
-
const { getPlainPGNs } = require('./utilities')
|
|
26
|
+
const { getPlainPGNs, binToActisense } = require('./utilities')
|
|
27
27
|
const { encodeCanId, parseCanId } = require('./canId')
|
|
28
28
|
const { toActisenseSerialFormat, parseActisense } = require('./stringMsg')
|
|
29
29
|
|
|
@@ -73,7 +73,7 @@ function CanbusStream (options) {
|
|
|
73
73
|
this.socketcan = require('socketcan')
|
|
74
74
|
} catch ( err ) {
|
|
75
75
|
console.error(err)
|
|
76
|
-
var msg = '
|
|
76
|
+
var msg = 'unable to load native socketcan interface'
|
|
77
77
|
console.error(msg)
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -90,7 +90,9 @@ function CanbusStream (options) {
|
|
|
90
90
|
|
|
91
91
|
var canDevice = options.canDevice || 'can0'
|
|
92
92
|
|
|
93
|
-
this.connect()
|
|
93
|
+
if ( this.connect() == false ) {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
94
96
|
|
|
95
97
|
const noDataReceivedTimeout = typeof options.noDataReceivedTimeout !== 'undefined' ? options.noDataReceivedTimeout : -1
|
|
96
98
|
if ( noDataReceivedTimeout > 0 ) {
|
|
@@ -114,6 +116,11 @@ function CanbusStream (options) {
|
|
|
114
116
|
|
|
115
117
|
CanbusStream.prototype.connect = function() {
|
|
116
118
|
try {
|
|
119
|
+
if ( this.socketcan === undefined ) {
|
|
120
|
+
this.setProviderError('unable to load native socketcan interface')
|
|
121
|
+
return false
|
|
122
|
+
}
|
|
123
|
+
|
|
117
124
|
var that = this
|
|
118
125
|
var canDevice = this.options.canDevice || 'can0'
|
|
119
126
|
this.channel = this.socketcan.createRawChannelWithOptions(canDevice, { non_block_send: true} );
|
|
@@ -153,28 +160,15 @@ CanbusStream.prototype.connect = function() {
|
|
|
153
160
|
this.setProviderStatus('Connected to socketcan')
|
|
154
161
|
this.candevice = new CanDevice(this, this.options)
|
|
155
162
|
this.candevice.start()
|
|
163
|
+
return true
|
|
156
164
|
} catch (e) {
|
|
157
165
|
console.error(`unable to open canbus ${canDevice}: ${e}`)
|
|
158
166
|
console.error(e.stack)
|
|
159
167
|
this.setProviderError(e.message)
|
|
168
|
+
return false
|
|
160
169
|
}
|
|
161
170
|
}
|
|
162
171
|
|
|
163
|
-
function binToActisense(pgn, data, length) {
|
|
164
|
-
return (
|
|
165
|
-
pgn.timestamp +
|
|
166
|
-
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
|
|
167
|
-
new Uint32Array(data)
|
|
168
|
-
.reduce(function(acc, i) {
|
|
169
|
-
acc.push(i.toString(16));
|
|
170
|
-
return acc;
|
|
171
|
-
}, [])
|
|
172
|
-
.map(x => (x.length === 1 ? "0" + x : x))
|
|
173
|
-
.join(",")
|
|
174
|
-
);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
172
|
require('util').inherits(CanbusStream, Transform)
|
|
179
173
|
|
|
180
174
|
CanbusStream.prototype.start = function () {
|
package/lib/n2k-actisense.js
CHANGED
|
@@ -2,6 +2,7 @@ const debug = require('debug')('canboatjs:w2k01')
|
|
|
2
2
|
const debugData = require('debug')('canboatjs:w2k01-data')
|
|
3
3
|
const { parseCanId, encodeCanId } = require('./canId')
|
|
4
4
|
const BitStream = require('bit-buffer').BitStream
|
|
5
|
+
const { binToActisense } = require('./utilities')
|
|
5
6
|
|
|
6
7
|
exports.readN2KActisense = function (data, plainText, context, cb) {
|
|
7
8
|
const inBuf = Buffer.from(data)
|
|
@@ -105,20 +106,3 @@ exports.encodeN2KActisense = ({
|
|
|
105
106
|
|
|
106
107
|
return bs.view.buffer
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
function binToActisense(pgn, data, length) {
|
|
111
|
-
return (
|
|
112
|
-
pgn.timestamp +
|
|
113
|
-
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
|
|
114
|
-
new Uint32Array(data)
|
|
115
|
-
.reduce(function(acc, i) {
|
|
116
|
-
acc.push(i.toString(16));
|
|
117
|
-
return acc;
|
|
118
|
-
}, [])
|
|
119
|
-
.map(x => (x.length === 1 ? "0" + x : x))
|
|
120
|
-
.join(",")
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
package/lib/n2kDevice.js
CHANGED
|
@@ -184,9 +184,10 @@ function handleISORequest(device, n2kMsg) {
|
|
|
184
184
|
|
|
185
185
|
function handleGroupFunction(device, n2kMsg) {
|
|
186
186
|
debug('handleGroupFunction %j', n2kMsg)
|
|
187
|
-
|
|
187
|
+
const functionCode = n2kMsg.fields.functionCode || n2kMsg.fields["Function Code"]
|
|
188
|
+
if(functionCode === 'Request') {
|
|
188
189
|
handleRequestGroupFunction(device, n2kMsg)
|
|
189
|
-
} else if(
|
|
190
|
+
} else if(functionCode === 'Command') {
|
|
190
191
|
handleCommandGroupFunction(device, n2kMsg)
|
|
191
192
|
} else {
|
|
192
193
|
debug('Got unsupported Group Function PGN: %j', n2kMsg)
|
|
@@ -358,7 +359,7 @@ function sendNAKAcknowledgement(device, src, requestedPGN) {
|
|
|
358
359
|
"Group Function": 255,
|
|
359
360
|
PGN: requestedPGN
|
|
360
361
|
}
|
|
361
|
-
device.sendPGN(
|
|
362
|
+
device.sendPGN(acknowledgement)
|
|
362
363
|
}
|
|
363
364
|
|
|
364
365
|
function sendPGNList(device, src) {
|
package/lib/simpleCan.js
CHANGED
|
@@ -3,7 +3,7 @@ const debug = require('debug')('canboatjs:simpleCan')
|
|
|
3
3
|
const { encodeCanId, parseCanId } = require('./canId')
|
|
4
4
|
const { toActisenseSerialFormat, parseActisense } = require('./stringMsg')
|
|
5
5
|
const { toPgn } = require('./toPgn')
|
|
6
|
-
const { getPlainPGNs } = require('./utilities')
|
|
6
|
+
const { getPlainPGNs, binToActisense } = require('./utilities')
|
|
7
7
|
const _ = require('lodash')
|
|
8
8
|
|
|
9
9
|
function SimpleCan (options, messageCb) {
|
|
@@ -102,18 +102,4 @@ SimpleCan.prototype.sendActisenseFormat = function (msg) {
|
|
|
102
102
|
this.sendPGN(msg)
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
-
function binToActisense(pgn, data, length) {
|
|
106
|
-
return (
|
|
107
|
-
pgn.timestamp +
|
|
108
|
-
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
|
|
109
|
-
new Uint32Array(data)
|
|
110
|
-
.reduce(function(acc, i) {
|
|
111
|
-
acc.push(i.toString(16));
|
|
112
|
-
return acc;
|
|
113
|
-
}, [])
|
|
114
|
-
.map(x => (x.length === 1 ? "0" + x : x))
|
|
115
|
-
.join(",")
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
105
|
module.exports = SimpleCan
|
package/lib/utilities.js
CHANGED
|
@@ -85,6 +85,21 @@ function compute0183Checksum (sentence) {
|
|
|
85
85
|
return '*' + toHexString(c1)
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
function binToActisense(pgn, data, length) {
|
|
89
|
+
return (
|
|
90
|
+
pgn.timestamp +
|
|
91
|
+
`,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` +
|
|
92
|
+
new Uint32Array(data)
|
|
93
|
+
.reduce(function(acc, i) {
|
|
94
|
+
acc.push(i.toString(16));
|
|
95
|
+
return acc;
|
|
96
|
+
}, [])
|
|
97
|
+
.map(x => (x.length === 1 ? "0" + x : x))
|
|
98
|
+
.join(",")
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.binToActisense = binToActisense
|
|
88
103
|
exports.compute0183Checksum = compute0183Checksum
|
|
89
104
|
exports.trimWrap = trimChars('()<>[]')
|
|
90
105
|
exports.rmChecksum = str => str.includes('*') ? str.split('*', 1)[0] : str
|
package/lib/ydgw02.js
CHANGED