From 1de4102e717fe1880baae17bc7153a706c9d8019 Mon Sep 17 00:00:00 2001 From: Scott Bender Date: Fri, 9 Oct 2020 12:02:36 -0400 Subject: [PATCH] fix: more robust retry of setting the tx pgn list on ngt-1 --- lib/serial.js | 65 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/lib/serial.js b/lib/serial.js index 7846ea3..914bcb2 100644 --- a/lib/serial.js +++ b/lib/serial.js @@ -25,6 +25,7 @@ const { toPgn } = require('./toPgn') const { encodeActisense } = require('./stringMsg') const { defaultTransmitPGNs } = require('./codes') const _ = require('lodash') +const FromPgn = require('./fromPgn').Parser /* ASCII characters used to mark packet start/stop */ @@ -180,11 +181,12 @@ SerialStream.prototype.start = function () { var buf = composeMessage(NGT_MSG_SEND, Buffer.from(NGT_STARTUP_MSG), NGT_STARTUP_MSG.length) that.serial.write(buf) debug('sent startup message') + that.gotStartupResponse = false if ( that.options.disableSetTransmitPGNs ) { enableOutput(that) } else { setTimeout(() => { - if ( that.outAvailable === false ) { + if ( that.gotStartupResponse === false ) { debug('retry startup message...') that.serial.write(buf) } @@ -296,6 +298,24 @@ function enableOutput(that) { } } +function requestTransmitPGNList(that) { + debug('request tx pgns...') + that.serial.write(composeRequestTXPGNList()) + setTimeout(() => { + if ( !that.gotTXPGNList ) { + if ( that.transmitPGNRetries-- > 0 ) { + debug('did not get tx pgn list, retrying...') + requestTransmitPGNList(that) + } else { + const msg = 'could not set transmit pgn list' + that.options.app.setProviderStatus(msg) + console.warn(msg) + enableOutput(that) + } + } + }, 10000) +} + function processNTGMessage(that, buffer, len) { var checksum = 0 @@ -304,12 +324,14 @@ function processNTGMessage(that, buffer, len) checksum = addUInt8(checksum, buffer[i]) } + const command = buffer[2] + if ( checksum != 0 ) { - debug('received message with invalid checksum') + debug('received message with invalid checksum (%d,%d)', command, len) return } - if ( that.options.sendNetworkStats ) { + if ( that.options.sendNetworkStats || debug.enabled ) { let newbuf = new Buffer.alloc(len + 7 ) var bs = new BitStream(newbuf) const pgn = 0x40000 + buffer[2] @@ -328,16 +350,29 @@ function processNTGMessage(that, buffer, len) } else { that.push(bs.view.buffer, len+7) } + if ( debug.enabled && command != 0xf2 ) { //don't log system status + if ( !that.parser ) { + that.parser = new FromPgn() + } + const js = that.parser.parseBuffer(bs.view.buffer) + if ( js ) { + debug('got ntg message: %j', js) + } + } + } + + if ( command === 0x11 ) { + //confirm startup + that.gotStartupResponse = true + debug('got startup response') } if ( !that.outAvailable ) { - const command = buffer[2] - if ( command === 0x11 ) { - //confirm startup - debug('request tx pgns...') - that.serial.write(composeRequestTXPGNList()) + that.gotTXPGNList = false + requestTransmitPGNList(that) } else if ( command === 0x49 && buffer[3] === 1 ) { + that.gotTXPGNList = true const pgnCount = buffer[14]; let bv = new BitView(buffer.slice(15, that.bufferOffset)); let bs = new BitStream(bv) @@ -353,20 +388,12 @@ function processNTGMessage(that, buffer, len) debug('needed pgns: %j', that.neededTransmitPGNs) } else if ( command === 0x49 && buffer[3] === 4 ) { //I think this means done receiving the pgns list - if ( !that.neededTransmitPGNs ) { - if ( that.transmitPGNRetries-- > 0 ) { - debug('did not get tx pgn list, retrying...') - that.serial.write(composeRequestTXPGNList()) + if ( that.neededTransmitPGNs ) { + if ( that.neededTransmitPGNs.length ) { + enableTXPGN(that.serial, that.neededTransmitPGNs[0]) } else { - const msg = 'could not set transmit pgn list' - that.options.app.setProviderStatus(msg) - console.warn(msg) enableOutput(that) } - } else if ( that.neededTransmitPGNs.length ) { - enableTXPGN(that.serial, that.neededTransmitPGNs[0]) - } else { - enableOutput(that) } } else if ( command === 0x47 ) { //response from enable a pgn