From 91a49da61eb08ea8f16a6ea5adb8269e282a5546 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Tue, 27 Nov 2018 23:19:59 +0100 Subject: [PATCH] bugfix payload decoder packed_decoder.js --- src/TTN/packed_decoder.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/TTN/packed_decoder.js b/src/TTN/packed_decoder.js index a8f71f9e..938ab1fb 100644 --- a/src/TTN/packed_decoder.js +++ b/src/TTN/packed_decoder.js @@ -23,7 +23,7 @@ function Decoder(bytes, port) { if (port === 3) { // device config data - return decode(bytes, [uint8, uint8, uint16, uint8, uint8, uint8, uint8, bitmap, bitmap, version], ['lorasf', 'txpower', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags', 'payloadmask', 'version']); + return decode(bytes, [uint8, uint8, uint16, uint8, uint8, uint8, uint8, bitmap1, bitmap2, version], ['lorasf', 'txpower', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags', 'payloadmask', 'version']); } if (port === 4) { @@ -161,8 +161,8 @@ var pressure = function (bytes) { }; pressure.BYTES = 2; -var bitmap = function (byte) { - if (byte.length !== bitmap.BYTES) { +var bitmap1 = function (byte) { + if (byte.length !== bitmap1.BYTES) { throw new Error('Bitmap must have exactly 1 byte'); } var i = bytesToInt(byte); @@ -173,7 +173,21 @@ var bitmap = function (byte) { return obj; }, {}); }; -bitmap.BYTES = 1; +bitmap1.BYTES = 1; + +var bitmap2 = function (byte) { + if (byte.length !== bitmap2.BYTES) { + throw new Error('Bitmap must have exactly 1 byte'); + } + var i = bytesToInt(byte); + var bm = ('00000000' + Number(i).toString(2)).substr(-8).split('').map(Number).map(Boolean); + return ['gps', 'alarm', 'bme', 'counter', 'sensor1', 'sensor2', 'sensor3', 'battery'] + .reduce(function (obj, pos, index) { + obj[pos] = +bm[index]; + return obj; + }, {}); +}; +bitmap2.BYTES = 1; var decode = function (bytes, mask, names) { @@ -208,7 +222,8 @@ if (typeof module === 'object' && typeof module.exports !== 'undefined') { pressure: pressure, latLng: latLng, hdop: hdop, - bitmap: bitmap, + bitmap1: bitmap1, + bitmap2: bitmap2, version: version, decode: decode };