From 2aafc839d5e7bbdb297c3603566741a4eaa5ecfe Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Sat, 27 Jul 2019 02:32:39 +0200 Subject: [PATCH] Fix altitude in packed decoder to be signed (#403) See #403 --- src/TTN/packed_decoder.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index f1bf2c73..6db312a4 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -20,11 +20,11 @@ function Decoder(bytes, port) { } // combined wifi counter and gps data if (bytes.length === 15) { - return decode(bytes, [uint16, latLng, latLng, uint8, hdop, uint16], ['wifi', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']); + return decode(bytes, [uint16, latLng, latLng, uint8, hdop, altitude], ['wifi', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']); } // combined wifi + ble counter and gps data if (bytes.length === 17) { - return decode(bytes, [uint16, uint16, latLng, latLng, uint8, hdop, uint16], ['wifi', 'ble', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']); + return decode(bytes, [uint16, uint16, latLng, latLng, uint8, hdop, altitude], ['wifi', 'ble', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']); } } @@ -42,7 +42,7 @@ function Decoder(bytes, port) { if (port === 4) { // gps data - return decode(bytes, [latLng, latLng, uint8, hdop, uint16], ['latitude', 'longitude', 'sats', 'hdop', 'altitude']); + return decode(bytes, [latLng, latLng, uint8, hdop, altitude], ['latitude', 'longitude', 'sats', 'hdop', 'altitude']); } if (port === 5) { @@ -147,6 +147,18 @@ var hdop = function (bytes) { }; hdop.BYTES = 2; +var altitude = function (bytes) { + if (bytes.length !== altitude.BYTES) { + throw new Error('Altitude must have exactly 2 bytes'); + } + var alt = bytesToInt(bytes); + if (alt > 32767) { + alt -= 65536; + } + return alt; +}; +altitude.BYTES = 2; + var float = function (bytes) { if (bytes.length !== float.BYTES) { throw new Error('Float must have exactly 2 bytes'); @@ -253,6 +265,7 @@ if (typeof module === 'object' && typeof module.exports !== 'undefined') { pressure: pressure, latLng: latLng, hdop: hdop, + altitude: altitude, bitmap1: bitmap1, bitmap2: bitmap2, version: version,