From 9be1d36166eabdde60a16df23f3bff99d10e6cd0 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 27 Jul 2019 15:23:17 +0200 Subject: [PATCH] fixed TTN decoder for gps altitude --- src/TTN/packed_decoder.js | 8 +++++++- src/TTN/plain_decoder.js | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index 2c5752f8..66846cf7 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -151,7 +151,13 @@ var altitude = function (bytes) { if (bytes.length !== altitude.BYTES) { throw new Error('Altitude must have exactly 2 bytes'); } - return +(bytesToInt(bytes) / 4 - 1000).toFixed(1); + + var alt = bytesToInt(bytes); + if (alt > 32767) { + alt -= 65536; + } + + return +(alt / 4 - 1000).toFixed(1); }; altitude.BYTES = 2; diff --git a/src/TTN/plain_decoder.js b/src/TTN/plain_decoder.js index c6eba7b4..4158515a 100644 --- a/src/TTN/plain_decoder.js +++ b/src/TTN/plain_decoder.js @@ -41,7 +41,11 @@ function Decoder(bytes, port) { decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]); decoded.sats = bytes[i++]; decoded.hdop = (bytes[i++] << 8) | (bytes[i++]); - decoded.altitude = ((bytes[i++] << 8) | (bytes[i++])) / 4 - 1000; + var alt = ((bytes[i++] << 8) | (bytes[i++])); + if (alt > 32767) { + alt -= 65536; + } + decoded.altitude = alt / 4 - 1000; } if (port === 5) {