Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: more robust retry of setting the tx pgn list on ngt-1 #135

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 46 additions & 19 deletions lib/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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)
Expand All @@ -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
Expand Down