Skip to content

Commit

Permalink
fix: handle 0183 messages with v4 tag blocks (#258)
Browse files Browse the repository at this point in the history
fixes #257
  • Loading branch information
sbender9 authored Oct 15, 2023
1 parent 9eb83f9 commit c23b1f9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/stringMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down
41 changes: 41 additions & 0 deletions test/mxpgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
45 changes: 45 additions & 0 deletions test/pcdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit c23b1f9

Please sign in to comment.