ESP32-PaxCounter/src/TTN/plain_decoder.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-06-17 13:24:20 +02:00
// Decoder for device payload encoder "PLAIN"
// copy&paste to TTN Console -> Applications -> PayloadFormat -> Decoder
function Decoder(bytes, port) {
var decoded = {};
if (port === 1) {
var i = 0;
decoded.wifi = (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++]);
decoded.longitude = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
2018-08-11 21:47:51 +02:00
decoded.sats = bytes[i++];
2018-06-17 13:24:20 +02:00
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
decoded.altitude = (bytes[i++] << 8) | (bytes[i++]);
}
}
if (port === 2) {
var i = 0;
decoded.battery = ((bytes[i++] << 8) | bytes[i++]);
decoded.uptime = ((bytes[i++] << 56) | (bytes[i++] << 48) | (bytes[i++] << 40) | (bytes[i++] << 32) |
(bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
2018-08-11 21:47:51 +02:00
decoded.temp = bytes[i++];
2018-09-17 21:57:01 +02:00
decoded.memory = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
decoded.reset0 = bytes[i++];
decoded.reset1 = bytes[i++];
}
if (port === 5) {
var i = 0;
2018-08-11 21:47:51 +02:00
decoded.button = bytes[i++];
}
if (port === 6) {
var i = 0;
2018-08-11 21:47:51 +02:00
decoded.rssi = bytes[i++];
decoded.beacon = bytes[i++];
}
if (port === 7) {
var i = 0;
decoded.temperature = ((bytes[i++] << 8) | bytes[i++]);
decoded.pressure = ((bytes[i++] << 8) | bytes[i++]);
decoded.humidity = ((bytes[i++] << 8) | bytes[i++]);
decoded.air = ((bytes[i++] << 8) | bytes[i++]);
}
2018-06-17 13:24:20 +02:00
return decoded;
2018-06-17 13:24:20 +02:00
}