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;
|
|
|
|
|
2019-01-19 19:54:54 +01:00
|
|
|
if (bytes.length >= 2) {
|
2019-04-06 21:10:33 +02:00
|
|
|
decoded.wifi = (bytes[i++] << 8) | bytes[i++];
|
|
|
|
}
|
2019-12-27 12:57:22 +01:00
|
|
|
|
2019-02-23 18:20:10 +01:00
|
|
|
if (bytes.length === 4 || bytes.length > 15) {
|
2019-04-06 21:10:33 +02:00
|
|
|
decoded.ble = (bytes[i++] << 8) | bytes[i++];
|
|
|
|
}
|
2018-06-17 13:24:20 +02:00
|
|
|
|
|
|
|
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++]);
|
2019-07-28 17:22:04 +02:00
|
|
|
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:20:54 +02:00
|
|
|
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++];
|
2018-07-24 19:20:54 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 10:43:59 +01:00
|
|
|
if (port === 4) {
|
|
|
|
var i = 0;
|
|
|
|
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++]);
|
|
|
|
decoded.sats = bytes[i++];
|
|
|
|
decoded.hdop = (bytes[i++] << 8) | (bytes[i++]);
|
2019-07-28 17:22:04 +02:00
|
|
|
decoded.altitude = ((bytes[i++] << 8) | (bytes[i++]));
|
2018-12-18 10:43:59 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 19:20:54 +02:00
|
|
|
if (port === 5) {
|
|
|
|
var i = 0;
|
2018-08-11 21:47:51 +02:00
|
|
|
decoded.button = bytes[i++];
|
2018-07-24 19:20:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (port === 6) {
|
|
|
|
var i = 0;
|
2018-08-11 21:47:51 +02:00
|
|
|
decoded.rssi = bytes[i++];
|
|
|
|
decoded.beacon = bytes[i++];
|
2018-07-24 19:20:54 +02:00
|
|
|
}
|
2019-04-06 21:10:33 +02:00
|
|
|
|
2018-11-17 21:39:49 +01:00
|
|
|
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++]);
|
2018-11-18 15:50:57 +01:00
|
|
|
decoded.air = ((bytes[i++] << 8) | bytes[i++]);
|
2018-11-17 21:39:49 +01:00
|
|
|
}
|
2019-04-06 21:10:33 +02:00
|
|
|
|
2019-02-23 18:20:10 +01:00
|
|
|
if (port === 8) {
|
|
|
|
var i = 0;
|
|
|
|
if (bytes.length >= 2) {
|
2019-04-06 21:10:33 +02:00
|
|
|
decoded.battery = (bytes[i++] << 8) | bytes[i++];
|
|
|
|
}
|
2019-02-23 18:20:10 +01:00
|
|
|
}
|
2018-07-24 19:20:54 +02:00
|
|
|
|
2019-03-09 22:08:57 +01:00
|
|
|
if (port === 9) {
|
2019-04-06 21:10:33 +02:00
|
|
|
// timesync request
|
2019-03-09 22:08:57 +01:00
|
|
|
if (bytes.length === 1) {
|
2019-04-06 21:10:33 +02:00
|
|
|
decoded.timesync_seqno = bytes[0];
|
2019-03-09 22:08:57 +01:00
|
|
|
}
|
2019-04-06 21:10:33 +02:00
|
|
|
// epoch time answer
|
|
|
|
if (bytes.length === 5) {
|
|
|
|
var i = 0;
|
|
|
|
decoded.time = ((bytes[i++] << 24) | (bytes[i++] << 16) | (bytes[i++] << 8) | bytes[i++]);
|
|
|
|
decoded.timestatus = bytes[i++];
|
|
|
|
}
|
2019-03-09 22:08:57 +01:00
|
|
|
}
|
2019-05-24 10:19:34 +02:00
|
|
|
return decoded;
|
2019-02-23 18:20:10 +01:00
|
|
|
}
|