From b857a503323291b92dd0fe8c41ad6fa0d6bda088 Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Wed, 15 Jun 2022 16:56:58 -0400 Subject: [PATCH] fix: unknown proprietary pgns where not being forwarded properly (#194) --- lib/fromPgn.js | 55 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/fromPgn.js b/lib/fromPgn.js index 74c7022..4d626ce 100644 --- a/lib/fromPgn.js +++ b/lib/fromPgn.js @@ -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 ) { @@ -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 ) { @@ -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)