diff --git a/lib/stringMsg.js b/lib/stringMsg.js index c0da724..6ae357a 100644 --- a/lib/stringMsg.js +++ b/lib/stringMsg.js @@ -112,10 +112,26 @@ exports.encodeYDRAW = ({ data, ...canIdInfo }) => { return pgns.map(buffer => canId + ' ' + byteString(buffer, ' ')) } +const get0183Sentence = (msg) => { + let sentence = msg + if (sentence.charAt(0) === '\\') { + split = sentence.split('\\') + if ( split.length < 3 ) { + return false + } + sentence = split[2] + } + return sentence +} + // $PCDIN,01F119,00000000,0F,2AAF00D1067414FF*59 -exports.isPCDIN = startsWith('$PCDIN,') +exports.isPCDIN = (msg) => { + const sentence = get0183Sentence(msg) + return sentence.startsWith('$PCDIN,') +} exports.parsePCDIN = (input) => { - const [ prefix, pgn, timeHex, src, data ] = input.split(',') + const sentence = get0183Sentence(input) + const [ prefix, pgn, timeHex, src, data ] = sentence.split(',') let timer = parseInt(timeHex, 32) timer = timer / 1024 @@ -136,9 +152,13 @@ exports.encodePCDIN = ({ prefix = '$PCDIN', pgn, data, dst = 255}) => { } // $MXPGN,01F801,2801,C1308AC40C5DE343*19 -exports.isMXPGN = startsWith('$MXPGN,') +exports.isMXPGN = (msg) => { + const sentence = get0183Sentence(msg) + return sentence.startsWith('$MXPGN,') +} exports.parseMXPGN = (input) => { - const [ prefix, pgn, attr_word, data ] = input.split(',') + const sentence = get0183Sentence(input) + const [ prefix, pgn, attr_word, data ] = sentence.split(',') const send_prio_len = (parseInt(attr_word.substr(0,2), 16).toString(2)).padStart(8, '0'); const addr = (parseInt(attr_word.substr(2,2), 16)); diff --git a/test/mxpgn.js b/test/mxpgn.js index 3b4bf2a..6f88339 100644 --- a/test/mxpgn.js +++ b/test/mxpgn.js @@ -47,4 +47,45 @@ describe('from mxpgn data converts', function () { fromPgn.parseString(mxpgn) }) + + it(`from 129025 converts with tags`, function (done) { + var mxpgn = '\\s:serial,c:1696759212*3E\\$MXPGN,01F801,2801,C1308AC40C5DE343*19' + var expected = { + "pgn":129025, + "src":1, + "dst":255, + "prio":0, + "fields":{ + "Latitude": -99.7576511, + "Longitude": 113.8973964, + }, + "description":"Position, Rapid Update" + } + + var fromPgn = new FromPgn() + + fromPgn.on('error', (pgn, error) => { + console.error(`Error parsing ${pgn.pgn} ${error}`) + console.error(error.stack) + done(error) + }) + + fromPgn.on('warning', (pgn, warning) => { + done(new Error(`${pgn.pgn} ${warning}`)) + }) + + fromPgn.on('pgn', (pgn) => { + try { + //console.log(JSON.stringify(pgn)) + delete pgn.input + delete pgn.timestamp + pgn.should.jsonEqual(expected) + done() + } catch ( e ) { + done(e) + } + }) + + fromPgn.parseString(mxpgn) + }) }) diff --git a/test/pcdin.js b/test/pcdin.js index 58e5456..39a0395 100644 --- a/test/pcdin.js +++ b/test/pcdin.js @@ -50,4 +50,49 @@ describe('from pcdin data converts', function () { fromPgn.parseString(pcdin) }) + + + it(`from 127257 converts with tag blocks`, function (done) { + var pcdin = '\\s:serial,c:1696759212*3E\\$PCDIN,01F119,00000000,0F,2AAF00D1067414FF*59' + var expected = { + "pgn":127257, + "timestamp": "2010-01-01T00:00:00.000Z", + "timer": 1262304000000, + "src":15, + "dst":255, + "prio":0, + "fields":{ + "SID": 42, + "Pitch": 0.1745, + "Roll": 0.5236, + "Yaw": 0.0175 + }, + "description":"Attitude" + } + + var fromPgn = new FromPgn() + + fromPgn.on('error', (pgn, error) => { + console.error(`Error parsing ${pgn.pgn} ${error}`) + console.error(error.stack) + done(error) + }) + + fromPgn.on('warning', (pgn, warning) => { + done(new Error(`${pgn.pgn} ${warning}`)) + }) + + fromPgn.on('pgn', (pgn) => { + try { + //console.log(JSON.stringify(pgn)) + delete pgn.input + pgn.should.jsonEqual(expected) + done() + } catch ( e ) { + done(e) + } + }) + + fromPgn.parseString(pcdin) + }) })