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

feature: add support for sending N2k over 0183 sentences #151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
VenusMQTT: require('./lib/venus-mqtt'),
discover: require('./lib/discovery'),
SimpleCan: require('./lib/simpleCan'),
setupN2KOver0183Output: require('./lib/toPgn').setupN2KOver0183Output,
addCustomPgn: require('./lib/pgns').addCustomPgn
}

Expand Down
22 changes: 21 additions & 1 deletion lib/fromPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,27 @@ class Parser extends EventEmitter {
}

parseN2KOver0183(sentence, cb) {
return this.parseString(sentence, cb)
try {
const { coalesced, data, error, len, ...pgn } = parseN2KOver0183(sentence)
if (error) {
cb && cb(error)
this.emit('error', pgn, error)
return
}

const bs = new BitStream(data)
delete pgn.format
delete pgn.type
delete pgn.prefix
const res = this._parse(pgn, bs, len || data.length, coalesced, cb, sentence)
if (res) {
debug('parsed pgn %j', pgn)
}
return res
} catch ( error ) {
cb && cb(error)
this.emit('error', sentence, error)
}
}

// Venus MQTT-N2K
Expand Down
8 changes: 6 additions & 2 deletions lib/stringMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ exports.isN2KString = cond([

exports.isN2KOver0183 = (msg) => { return exports.isPCDIN(msg) || exports.isMXPGN(msg) }

exports.parseN2KOver0183 = (msg) => { return exports.parsePCDIN(msg) || exports.parseMXPGN(msg) }

exports.parseN2KOver0183 = cond([
[hasErr, buildErr('INVALID', 'Input not string or empty.')],
[exports.isPCDIN, exports.parsePCDIN],
[exports.isMXPGN, exports.parseMXPGN],
[stubTrue, buildErr('MISSING_PARSER', 'Parser not found for input.')],
])

19 changes: 18 additions & 1 deletion lib/toPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const BitStream = require('bit-buffer').BitStream
const Int64LE = require('int64-buffer').Int64LE
const Uint64LE = require('int64-buffer').Uint64LE
const { getPlainPGNs } = require('./utilities')
const { encodeActisense, encodeYDRAW, parseActisense, encodePCDIN, encodeMXPGN, encodePDGY } = require('./stringMsg')
const { encodeActisense, encodeYDRAW, parseActisense, encodePCDIN, encodeMXPGN, encodePDGY, isPCDIN } = require('./stringMsg')
const { encodeCanId } = require('./canId')
const { getIndustryCode, getManufacturerCode } = require('./codes')
const debug = require('debug')('canboatjs:toPgn')
Expand Down Expand Up @@ -423,6 +423,23 @@ fieldTypeMappers['Pressure'] = (field, value) => {
return value
}

module.exports.setupN2KOver0183Output = (app, options, outEvent, sentence) => {
const actisenseConverter = isPCDIN(sentence) ? actisenseToPCDIN : actisenseToMXPGN
const pgnConverter = isPCDIN(sentence) ? pgnToPCDIN : pgnToMXPGN

app.on('nmea2000out', (input) => {
const sentence = actisenseConverter(input)
if ( sentence ) {
app.emit(outEvent, sentence)
}
})
app.on('nmea2000JsonOut', (pgn) => {
const sentence = pgnConverter(pgn)
if ( sentence ) {
app.emit(outEvent, sentence)
}
})
}

module.exports.canboat2Buffer = canboat2Buffer
module.exports.toPgn = toPgn
Expand Down
2 changes: 1 addition & 1 deletion test/mxpgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ describe('from mxpgn data converts', function () {
}
})

fromPgn.parseString(mxpgn)
fromPgn.parseN2KOver0183(mxpgn)
})
})