diff --git a/lib/canbus.js b/lib/canbus.js index 19b460c..27e5916 100644 --- a/lib/canbus.js +++ b/lib/canbus.js @@ -49,6 +49,7 @@ function CanbusStream (options) { objectMode: true }) + this.plainText = false this.reconnect = options.reconnect || true this.options = options this.start() @@ -122,7 +123,11 @@ function CanbusStream (options) { } pgn.timestamp = new Date().toISOString() - that.push({pgn: pgn, length: msg.data.length, data:msg.data}) + if ( that.plainText ) { + this.push(binToActisense(pgn, msg.data, msg.data.length)) + } else { + that.push({pgn: pgn, length: msg.data.length, data:msg.data}) + } }) this.channel.start() this.candevice = new CanDevice(this, options) @@ -135,6 +140,21 @@ function CanbusStream (options) { } } +function binToActisense(pgn, data, length) { + return ( + pgn.timestamp + + `,${pgn.prio},${pgn.pgn},${pgn.src},${pgn.dst},${length},` + + new Uint32Array(data) + .reduce(function(acc, i) { + acc.push(i.toString(16)); + return acc; + }, []) + .map(x => (x.length === 1 ? "0" + x : x)) + .join(",") + ); +} + + require('util').inherits(CanbusStream, Transform) CanbusStream.prototype.start = function () { @@ -289,13 +309,20 @@ CanbusStream.prototype.end = function () { } } + CanbusStream.prototype.pipe = function (pipeTo) { + if ( !pipeTo.fromPgn ) { + this.plainText = true + } + /* pipeTo.fromPgn.on('pgn', (pgn) => { if ( this.candevice ) { this.candevice.n2kMessage(pgn) } }) + */ return CanbusStream.super_.prototype.pipe.call(this, pipeTo) } + module.exports = CanbusStream diff --git a/lib/candevice.js b/lib/candevice.js index d8151dd..3abc718 100644 --- a/lib/candevice.js +++ b/lib/candevice.js @@ -71,6 +71,8 @@ class CanDevice extends EventEmitter { this.transmitPGNs = _.union(this.transmitPGNs, this.options.transmitPGNs) } + + options.app.on('N2KAnalyzerOut', this.n2kMessage.bind(this)) } start() {