diff --git a/index.js b/index.js index 0dcbe59..c0fbce3 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/lib/fromPgn.js b/lib/fromPgn.js index 4d626ce..f65a47e 100644 --- a/lib/fromPgn.js +++ b/lib/fromPgn.js @@ -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 diff --git a/lib/stringMsg.js b/lib/stringMsg.js index bc28729..5536275 100644 --- a/lib/stringMsg.js +++ b/lib/stringMsg.js @@ -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.')], +]) diff --git a/lib/toPgn.js b/lib/toPgn.js index fcbefa0..30da3d8 100644 --- a/lib/toPgn.js +++ b/lib/toPgn.js @@ -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') @@ -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 diff --git a/test/mxpgn.js b/test/mxpgn.js index 3b4bf2a..55153f1 100644 --- a/test/mxpgn.js +++ b/test/mxpgn.js @@ -45,6 +45,6 @@ describe('from mxpgn data converts', function () { } }) - fromPgn.parseString(mxpgn) + fromPgn.parseN2KOver0183(mxpgn) }) })