From 11c2fb6fa2749313f4cc740c6b8b952c77d3147c Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sat, 6 Apr 2019 21:10:33 +0200 Subject: [PATCH] TTN decoder updates --- src/TTN/packed_decoder.js | 14 +++++++------- src/TTN/plain_decoder.js | 33 +++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index 79b2def4..f1bf2c73 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -5,7 +5,7 @@ function Decoder(bytes, port) { var decoded = {}; - if (bytes.length === 0) { + if (bytes.length === 0) { return {}; } @@ -33,10 +33,6 @@ function Decoder(bytes, port) { if (bytes.length === 17) { return decode(bytes, [uint16, uptime, uint8, uint32, uint8, uint8], ['voltage', 'uptime', 'cputemp', 'memory', 'reset0', 'reset1']); } - // epoch time answer - if (bytes.length === 5) { - return decode(bytes, [uint32, uint8], ['time', 'timestatus']); - } } if (port === 3) { @@ -72,9 +68,13 @@ function Decoder(bytes, port) { if (port === 9) { // timesync request if (bytes.length === 1) { - decoded.timesync_seqno = bytes[0]; + decoded.timesync_seqno = bytes[0]; + return decoded; + } + // epoch time answer + if (bytes.length === 5) { + return decode(bytes, [uint32, uint8], ['time', 'timestatus']); } - return decoded; } } diff --git a/src/TTN/plain_decoder.js b/src/TTN/plain_decoder.js index 62249d08..d87fcb9e 100644 --- a/src/TTN/plain_decoder.js +++ b/src/TTN/plain_decoder.js @@ -8,10 +8,12 @@ function Decoder(bytes, port) { var i = 0; if (bytes.length >= 2) { - decoded.wifi = (bytes[i++] << 8) | bytes[i++];} - + decoded.wifi = (bytes[i++] << 8) | bytes[i++]; + } + if (bytes.length === 4 || bytes.length > 15) { - decoded.ble = (bytes[i++] << 8) | bytes[i++];} + decoded.ble = (bytes[i++] << 8) | bytes[i++]; + } if (bytes.length > 4) { decoded.latitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]); @@ -52,7 +54,7 @@ function Decoder(bytes, port) { decoded.rssi = bytes[i++]; decoded.beacon = bytes[i++]; } - + if (port === 7) { var i = 0; decoded.temperature = ((bytes[i++] << 8) | bytes[i++]); @@ -60,19 +62,26 @@ function Decoder(bytes, port) { decoded.humidity = ((bytes[i++] << 8) | bytes[i++]); decoded.air = ((bytes[i++] << 8) | bytes[i++]); } - + if (port === 8) { var i = 0; if (bytes.length >= 2) { - decoded.battery = (bytes[i++] << 8) | bytes[i++];} - } - - if (port === 9) { - if (bytes.length === 1) { - decoded.timesync_seqno = bytes[0]; + decoded.battery = (bytes[i++] << 8) | bytes[i++]; } } - return decoded; + if (port === 9) { + // timesync request + if (bytes.length === 1) { + decoded.timesync_seqno = bytes[0]; + } + // epoch time answer + if (bytes.length === 5) { + var i = 0; + decoded.time = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]); + decoded.timestatus = bytes[i++]; + } + return decoded; + } }