Skip to content

Commit

Permalink
Merge pull request #4 from sbender9/api-updates
Browse files Browse the repository at this point in the history
feature: support latest node server plugin api
  • Loading branch information
sbender9 authored Feb 5, 2018
2 parents cfa0e61 + b3792ff commit fffcc1d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
16 changes: 10 additions & 6 deletions lib/canbus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions lib/candevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -192,6 +190,8 @@ function handleISOAddressClaim(device, n2kMsg) {
return
}

debug('Checking ISO address claim. %j', n2kMsg)

const uint64ValueFromReceivedClaim = getISOAddressClaimAsUint64(n2kMsg)
const uint64ValueFromOurOwnClaim = getISOAddressClaimAsUint64(addressClaim)

Expand Down Expand Up @@ -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) => {
Expand Down
32 changes: 16 additions & 16 deletions lib/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/toPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
Expand Down

0 comments on commit fffcc1d

Please sign in to comment.