Skip to content

Commit

Permalink
fix: server crashes if logging is turned on with a canbus device (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored Sep 12, 2018
1 parent adda52a commit fca6703
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/canbus.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function CanbusStream (options) {
objectMode: true
})

this.plainText = false
this.reconnect = options.reconnect || true
this.options = options
this.start()
Expand Down Expand Up @@ -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)
Expand All @@ -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 () {
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/candevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fca6703

Please sign in to comment.