Skip to content

Commit

Permalink
fix: unknown proprietary pgns where not being forwarded properly (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored Jun 15, 2022
1 parent 3db0133 commit b857a50
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions lib/fromPgn.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,17 @@ class Parser extends EventEmitter {
pgnList = [ ...customPgns.definitions, ...(pgnList||[]) ]
}

let pgnData = pgnList[0]
let pgnData
let origPGNList = pgnList

if ( pgnList.length > 1 ) {
pgnData = this.findMatchPgn(pgnList)
}

if ( !pgnData ) {
pgnData = pgnList[0]
}

let couldBeMulti = false;

if ( pgnList.length > 0 && len == 8 ) {
Expand Down Expand Up @@ -202,16 +212,29 @@ class Parser extends EventEmitter {
if ( pgnList.length == 0 ) {
//this.emit('warning', pgn, `no conversion found for pgn`)
trace('warning no conversion found for pgn %j', pgn)
const ts = _.get(pgn, 'timestamp', new Date())
pgn.timestamp = _.isDate(ts) ? ts.toISOString() : ts
this.emit('pgn', pgn)
cb && cb(undefined, pgn)
return pgn

let nonMatch = this.findNonMatchPgn(origPGNList)
if ( nonMatch ) {
pgnList = [ nonMatch ]
pgnData = pgnList[0]
fields = pgnData.Fields
var postProcessor = fieldTypePostProcessors[field.Type]
if ( postProcessor ) {
value = postProcessor(pgnData.Fields[i], value)
}
} else {
const ts = _.get(pgn, 'timestamp', new Date())
pgn.timestamp = _.isDate(ts) ? ts.toISOString() : ts
this.emit('pgn', pgn)
cb && cb(undefined, pgn)
return pgn
}
} else {
pgnData = pgnList[0]
fields = pgnData.Fields
//console.log(`using ${JSON.stringify(pgnData, null, 2)}`)
value = pgnData.Fields[i].Description
}
pgnData = pgnList[0]
fields = pgnData.Fields
//console.log(`using ${JSON.stringify(pgnData, null, 2)}`)
value = pgnData.Fields[i].Description
}

if ( !_.isUndefined(value) && value != null ) {
Expand Down Expand Up @@ -258,6 +281,18 @@ class Parser extends EventEmitter {
}
}

findNonMatchPgn(pgnList) {
return pgnList.find(f => {
return !f.Fields.find(f => !_.isUndefined(f.Match))
})
}

findMatchPgn(pgnList) {
return pgnList.find(f => {
return f.Fields.find(f => !_.isUndefined(f.Match))
})
}

parse(data, cb) {
if (_.isString(data) ) {
return this.parseString(data, cb)
Expand Down

0 comments on commit b857a50

Please sign in to comment.