Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: server crashes if logging is turned on with a canbus device #22

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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