ESP32-PaxCounter/src/TTN/packed_decoder.js

167 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-06-17 22:41:32 +02:00
// Decoder for device payload encoder "PACKED"
2018-06-17 13:24:20 +02:00
// copy&paste to TTN Console -> Applications -> PayloadFormat -> Decoder
function Decoder(bytes, port) {
var decoded = {};
if (port === 1) {
// only counter data, no gps
2018-06-18 13:13:13 +02:00
if (bytes.length === 4) {
2018-06-17 13:24:20 +02:00
return decode(bytes, [uint16, uint16], ['wifi', 'ble']);
}
// combined counter and gps data
2018-06-18 13:13:13 +02:00
if (bytes.length === 17) {
return decode(bytes, [uint16, uint16, latLng, latLng, uint8, hdop, uint16], ['wifi', 'ble', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']);
2018-06-17 13:24:20 +02:00
}
}
if (port === 2) {
// device status data
2018-06-18 13:13:13 +02:00
if (bytes.length === 12) {
2018-06-18 09:15:57 +02:00
return decode(bytes, [uint16, uptime, temperature], ['voltage', 'uptime', 'cputemp']);
2018-06-17 13:24:20 +02:00
}
// device config data
2018-06-18 13:13:13 +02:00
if (bytes.length === 8) {
2018-06-18 09:15:57 +02:00
return decode(bytes, [uint8, uint16, uint8, uint8, uint8, uint8, bitmap], ['lorasf', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags']);
2018-06-17 13:24:20 +02:00
}
}
}
// ----- contents of /src/decoder.js --------------------------------------------
// https://github.com/thesolarnomad/lora-serialization/blob/master/src/decoder.js
var bytesToInt = function (bytes) {
var i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << (x * 8));
}
return i;
};
2018-06-18 09:15:57 +02:00
var uptime = function (bytes) {
if (bytes.length !== uptime.BYTES) {
throw new Error('uptime must have exactly 8 bytes');
2018-06-17 13:24:20 +02:00
}
return bytesToInt(bytes);
};
2018-06-18 09:15:57 +02:00
uptime.BYTES = 4;
2018-06-17 13:24:20 +02:00
var uint8 = function (bytes) {
if (bytes.length !== uint8.BYTES) {
throw new Error('int must have exactly 1 byte');
}
return bytesToInt(bytes);
};
uint8.BYTES = 1;
var uint16 = function (bytes) {
if (bytes.length !== uint16.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
return bytesToInt(bytes);
};
uint16.BYTES = 2;
var latLng = function (bytes) {
if (bytes.length !== latLng.BYTES) {
2018-06-18 13:13:13 +02:00
throw new Error('Lat/Long must have exactly 4 bytes');
2018-06-17 13:24:20 +02:00
}
2018-06-18 13:13:13 +02:00
return bytesToInt(bytes) / 1e6;
};
latLng.BYTES = 4;
2018-06-17 13:24:20 +02:00
2018-06-18 13:13:13 +02:00
var hdop = function (bytes) {
if (bytes.length !== hdop.BYTES) {
throw new Error('hdop must have exactly 2 bytes');
}
return bytesToInt(bytes) / 100;
2018-06-17 13:24:20 +02:00
};
2018-06-18 13:13:13 +02:00
hdop.BYTES = 2;
2018-06-17 13:24:20 +02:00
var temperature = function (bytes) {
if (bytes.length !== temperature.BYTES) {
throw new Error('Temperature must have exactly 2 bytes');
}
var isNegative = bytes[0] & 0x80;
var b = ('00000000' + Number(bytes[0]).toString(2)).slice(-8)
+ ('00000000' + Number(bytes[1]).toString(2)).slice(-8);
if (isNegative) {
var arr = b.split('').map(function (x) { return !Number(x); });
for (var i = arr.length - 1; i > 0; i--) {
arr[i] = !arr[i];
if (arr[i]) {
break;
}
}
b = arr.map(Number).join('');
}
var t = parseInt(b, 2);
if (isNegative) {
t = -t;
}
return t / 1e2;
};
temperature.BYTES = 2;
var humidity = function (bytes) {
if (bytes.length !== humidity.BYTES) {
throw new Error('Humidity must have exactly 2 bytes');
}
var h = bytesToInt(bytes);
return h / 1e2;
};
humidity.BYTES = 2;
var bitmap = function (byte) {
if (byte.length !== bitmap.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);
2018-06-18 13:13:13 +02:00
return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'filter', 'gpsmode']
2018-06-17 13:24:20 +02:00
.reduce(function (obj, pos, index) {
obj[pos] = bm[index];
return obj;
}, {});
};
bitmap.BYTES = 1;
var decode = function (bytes, mask, names) {
var maskLength = mask.reduce(function (prev, cur) {
return prev + cur.BYTES;
}, 0);
if (bytes.length < maskLength) {
throw new Error('Mask length is ' + maskLength + ' whereas input is ' + bytes.length);
}
names = names || [];
var offset = 0;
return mask
.map(function (decodeFn) {
var current = bytes.slice(offset, offset += decodeFn.BYTES);
return decodeFn(current);
})
.reduce(function (prev, cur, idx) {
prev[names[idx] || idx] = cur;
return prev;
}, {});
};
if (typeof module === 'object' && typeof module.exports !== 'undefined') {
module.exports = {
2018-06-18 09:15:57 +02:00
uptime: uptime,
2018-06-17 13:24:20 +02:00
uint8: uint8,
uint16: uint16,
temperature: temperature,
humidity: humidity,
latLng: latLng,
2018-06-18 13:13:13 +02:00
hdop: hdop,
2018-06-17 13:24:20 +02:00
bitmap: bitmap,
decode: decode
};
}