From b3792ffcfbdce513801ccc7a49f6993b0c9081e6 Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Sun, 4 Feb 2018 21:55:18 -0500 Subject: [PATCH] feature: support latest node server plugin api --- index.js | 1 + lib/canbus.js | 16 ++++++++++------ lib/candevice.js | 10 ++++++---- lib/serial.js | 32 ++++++++++++++++---------------- lib/toPgn.js | 4 ++++ 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/index.js b/index.js index 5e6dece..7de8d95 100644 --- a/index.js +++ b/index.js @@ -21,5 +21,6 @@ module.exports = { serial: require('./lib/serial'), toPgn: require('./lib/toPgn').toPgn, toActisenseSerialFormat: require('./lib/toPgn').toActisenseSerialFormat, + pgnToActisenseSerialFormat: require('./lib/toPgn').pgnToActisenseSerialFormat, canbus: require('./lib/canbus') } diff --git a/lib/canbus.js b/lib/canbus.js index 5857596..c712cc3 100644 --- a/lib/canbus.js +++ b/lib/canbus.js @@ -62,6 +62,16 @@ function CanbusStream (options) { } var that = this + + if ( options.app ) { + options.app.on('nmea2000out', (msg) => { + that.sendPGN(msg) + }) + options.app.on('nmea2000JsonOut', (msg) => { + that.sendPGN(msg) + }) + } + var canDevice = options.canDevice || 'can0' if ( !socketcan || this.options.useSocketCanWriter ) { this.socketCanWriter = null @@ -108,12 +118,6 @@ function CanbusStream (options) { this.candevice = new CanDevice(this, options) this.candevice.start() } - - if ( options.app ) { - options.app.on('nmea2000out', (msg) => { - that.sendPGN(msg) - }) - } } require('util').inherits(CanbusStream, Transform) diff --git a/lib/candevice.js b/lib/candevice.js index 227244c..d8151dd 100644 --- a/lib/candevice.js +++ b/lib/candevice.js @@ -179,11 +179,9 @@ function handleGroupFunction(device, n2kMsg) { } function handleISOAddressClaim(device, n2kMsg) { - debug('Checking ISO address claim. %j', n2kMsg) - if ( n2kMsg.src != device.address ) { if ( !device.devices[n2kMsg.src] ) { - debug(`regitering device ${n2kMsg.src}`) + debug(`registering device ${n2kMsg.src}`) device.devices[n2kMsg.src] = { addressClaim: n2kMsg } if ( device.cansend ) { //sendISORequest(device, 126996, undefined, n2kMsg.src) @@ -192,6 +190,8 @@ function handleISOAddressClaim(device, n2kMsg) { return } + debug('Checking ISO address claim. %j', n2kMsg) + const uint64ValueFromReceivedClaim = getISOAddressClaimAsUint64(n2kMsg) const uint64ValueFromOurOwnClaim = getISOAddressClaimAsUint64(addressClaim) @@ -233,7 +233,9 @@ function sendAddressClaim(device) { if ( !device.foundConflict ) { debug('no address conflics, enabling send') device.cansend = true - + if ( device.options.app ) { + device.options.app.emit('nmea2000OutAvailable') + } sendISORequest(device, 126996) /* _.keys(device.devices).forEach((address) => { diff --git a/lib/serial.js b/lib/serial.js index e3c1eb3..f5f9ad6 100644 --- a/lib/serial.js +++ b/lib/serial.js @@ -107,22 +107,22 @@ SerialStream.prototype.start = function () { readData(this, data) }) - const outEvent = this.options.outEvent - if ( outEvent ) { - this.options.app.on(this.options.outEvent, msg => { - //debug(`sending ${msg}`) - - if ( !_.isString(msg) ) { - var data = toPgn(msg) - msg = toActisenseSerialFormat(msg.pgn, data, msg.dst) - } - - var buf = parseInput(msg) - buf = composeMessage(N2K_MSG_SEND, buf, buf.length) - that.serial.write(buf) - }) - } - + this.options.app.on('nmea2000out', msg => { + //debug(`sending ${msg}`) + var buf = parseInput(msg) + buf = composeMessage(N2K_MSG_SEND, buf, buf.length) + that.serial.write(buf) + }) + + this.options.app.on('nmea2000JsonOut', msg => { + var data = toPgn(msg) + var actisense = toActisenseSerialFormat(msg.pgn, data, msg.dst) + var buf = parseInput(actisense) + buf = composeMessage(N2K_MSG_SEND, buf, buf.length) + that.serial.write(buf) + }) + + this.options.app.emit('nmea2000OutAvailable') this.serial.on('error', function (x) { console.log(x) diff --git a/lib/toPgn.js b/lib/toPgn.js index 75eb142..996d375 100644 --- a/lib/toPgn.js +++ b/lib/toPgn.js @@ -261,6 +261,10 @@ function canboat2Buffer(canboatData) { return new Buffer(canboatData.split(',').slice(6).map(parseHex), 'hex'); } +function pgnToActisenseSerialFormat(pgn) { + return toActisenseSerialFormat(pgn.pgn, toPgn(pgn), pgn.dst) +} + function toActisenseSerialFormat(pgn, data, dst=255, src=0) { return ( new Date().toISOString() +