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 = {};
|
|
|
|
|
2019-04-06 21:10:33 +02:00
|
|
|
if (bytes.length === 0) {
|
2018-12-16 15:38:59 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-06-17 13:24:20 +02:00
|
|
|
if (port === 1) {
|
2018-12-02 14:08:50 +01:00
|
|
|
// only wifi counter data, no gps
|
|
|
|
if (bytes.length === 2) {
|
|
|
|
return decode(bytes, [uint16], ['wifi']);
|
|
|
|
}
|
|
|
|
// wifi + ble 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']);
|
|
|
|
}
|
2018-12-31 11:26:05 +01:00
|
|
|
// combined wifi counter and gps data
|
|
|
|
if (bytes.length === 15) {
|
2019-07-27 02:32:39 +02:00
|
|
|
return decode(bytes, [uint16, latLng, latLng, uint8, hdop, altitude], ['wifi', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']);
|
2018-12-31 11:26:05 +01:00
|
|
|
}
|
|
|
|
// combined wifi + ble counter and gps data
|
2018-06-18 13:13:13 +02:00
|
|
|
if (bytes.length === 17) {
|
2019-07-27 02:32:39 +02:00
|
|
|
return decode(bytes, [uint16, uint16, latLng, latLng, uint8, hdop, altitude], ['wifi', 'ble', 'latitude', 'longitude', 'sats', 'hdop', 'altitude']);
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port === 2) {
|
|
|
|
// device status data
|
2019-03-27 19:37:21 +01:00
|
|
|
if (bytes.length === 17) {
|
|
|
|
return decode(bytes, [uint16, uptime, uint8, uint32, uint8, uint8], ['voltage', 'uptime', 'cputemp', 'memory', 'reset0', 'reset1']);
|
|
|
|
}
|
2018-07-15 19:08:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (port === 3) {
|
|
|
|
// device config data
|
2018-11-27 23:19:59 +01:00
|
|
|
return decode(bytes, [uint8, uint8, uint16, uint8, uint8, uint8, uint8, bitmap1, bitmap2, version], ['lorasf', 'txpower', 'rssilimit', 'sendcycle', 'wifichancycle', 'blescantime', 'rgblum', 'flags', 'payloadmask', 'version']);
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-15 19:08:18 +02:00
|
|
|
if (port === 4) {
|
|
|
|
// gps data
|
2019-07-27 02:32:39 +02:00
|
|
|
return decode(bytes, [latLng, latLng, uint8, hdop, altitude], ['latitude', 'longitude', 'sats', 'hdop', 'altitude']);
|
2018-07-15 19:08:18 +02:00
|
|
|
}
|
2018-07-24 19:20:54 +02:00
|
|
|
|
|
|
|
if (port === 5) {
|
|
|
|
// button pressed
|
|
|
|
return decode(bytes, [uint8], ['button']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port === 6) {
|
|
|
|
// beacon proximity alarm
|
|
|
|
return decode(bytes, [uint8, uint8], ['rssi', 'beacon']);
|
|
|
|
}
|
|
|
|
|
2018-11-17 21:39:49 +01:00
|
|
|
if (port === 7) {
|
|
|
|
// BME680 sensor data
|
2018-11-25 23:39:12 +01:00
|
|
|
return decode(bytes, [float, uint16, ufloat, ufloat], ['temperature', 'pressure', 'humidity', 'air']);
|
2018-11-17 21:39:49 +01:00
|
|
|
}
|
|
|
|
|
2018-12-09 13:48:03 +01:00
|
|
|
if (port === 8) {
|
|
|
|
// battery voltage
|
|
|
|
return decode(bytes, [uint16], ['voltage']);
|
|
|
|
}
|
|
|
|
|
2019-03-09 20:40:21 +01:00
|
|
|
if (port === 9) {
|
|
|
|
// timesync request
|
|
|
|
if (bytes.length === 1) {
|
2019-04-06 21:10:33 +02:00
|
|
|
decoded.timesync_seqno = bytes[0];
|
|
|
|
return decoded;
|
|
|
|
}
|
|
|
|
// epoch time answer
|
|
|
|
if (bytes.length === 5) {
|
|
|
|
return decode(bytes, [uint32, uint8], ['time', 'timestatus']);
|
2019-03-09 20:40:21 +01:00
|
|
|
}
|
2019-03-09 22:08:57 +01:00
|
|
|
}
|
2019-03-09 20:40:21 +01:00
|
|
|
|
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++) {
|
2018-06-23 13:42:01 +02:00
|
|
|
i |= (bytes[x] << (x * 8));
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
2018-09-17 21:57:01 +02:00
|
|
|
var version = function (bytes) {
|
|
|
|
if (bytes.length !== version.BYTES) {
|
|
|
|
throw new Error('version must have exactly 10 bytes');
|
|
|
|
}
|
|
|
|
return String.fromCharCode.apply(null, bytes).split('\u0000')[0];
|
2018-09-17 22:03:03 +02:00
|
|
|
};
|
2018-09-17 21:57:01 +02:00
|
|
|
version.BYTES = 10;
|
|
|
|
|
2018-06-17 13:24:20 +02:00
|
|
|
var uint8 = function (bytes) {
|
|
|
|
if (bytes.length !== uint8.BYTES) {
|
2018-08-04 22:49:12 +02:00
|
|
|
throw new Error('uint8 must have exactly 1 byte');
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
return bytesToInt(bytes);
|
|
|
|
};
|
|
|
|
uint8.BYTES = 1;
|
|
|
|
|
|
|
|
var uint16 = function (bytes) {
|
|
|
|
if (bytes.length !== uint16.BYTES) {
|
2018-08-04 22:49:12 +02:00
|
|
|
throw new Error('uint16 must have exactly 2 bytes');
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
return bytesToInt(bytes);
|
|
|
|
};
|
|
|
|
uint16.BYTES = 2;
|
|
|
|
|
2018-08-04 22:49:12 +02:00
|
|
|
var uint32 = function (bytes) {
|
|
|
|
if (bytes.length !== uint32.BYTES) {
|
|
|
|
throw new Error('uint32 must have exactly 4 bytes');
|
|
|
|
}
|
|
|
|
return bytesToInt(bytes);
|
|
|
|
};
|
|
|
|
uint32.BYTES = 4;
|
|
|
|
|
2018-06-17 13:24:20 +02:00
|
|
|
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-08-05 17:49:53 +02:00
|
|
|
var uptime = function (bytes) {
|
|
|
|
if (bytes.length !== uptime.BYTES) {
|
|
|
|
throw new Error('Uptime must have exactly 8 bytes');
|
|
|
|
}
|
|
|
|
return bytesToInt(bytes);
|
|
|
|
};
|
|
|
|
uptime.BYTES = 8;
|
|
|
|
|
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');
|
|
|
|
}
|
2019-07-27 13:35:12 +02:00
|
|
|
return +(bytesToInt(bytes) / 100).toFixed(1);
|
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
|
|
|
|
2019-07-27 02:32:39 +02:00
|
|
|
var altitude = function (bytes) {
|
|
|
|
if (bytes.length !== altitude.BYTES) {
|
|
|
|
throw new Error('Altitude must have exactly 2 bytes');
|
|
|
|
}
|
2019-07-27 15:23:17 +02:00
|
|
|
return +(alt / 4 - 1000).toFixed(1);
|
2019-07-27 02:32:39 +02:00
|
|
|
};
|
|
|
|
altitude.BYTES = 2;
|
|
|
|
|
2018-11-25 19:16:31 +01:00
|
|
|
var float = function (bytes) {
|
|
|
|
if (bytes.length !== float.BYTES) {
|
|
|
|
throw new Error('Float must have exactly 2 bytes');
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2018-11-17 21:39:49 +01:00
|
|
|
return +(t / 100).toFixed(1);
|
2018-06-17 13:24:20 +02:00
|
|
|
};
|
2018-11-25 19:16:31 +01:00
|
|
|
float.BYTES = 2;
|
2018-06-17 13:24:20 +02:00
|
|
|
|
2018-11-25 19:16:31 +01:00
|
|
|
var ufloat = function (bytes) {
|
|
|
|
if (bytes.length !== ufloat.BYTES) {
|
|
|
|
throw new Error('Ufloat must have exactly 2 bytes');
|
2018-06-17 13:24:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var h = bytesToInt(bytes);
|
2018-11-17 21:39:49 +01:00
|
|
|
return +(h / 100).toFixed(1);
|
2018-06-17 13:24:20 +02:00
|
|
|
};
|
2018-11-25 19:16:31 +01:00
|
|
|
ufloat.BYTES = 2;
|
2018-06-17 13:24:20 +02:00
|
|
|
|
2018-11-25 23:39:12 +01:00
|
|
|
var pressure = function (bytes) {
|
|
|
|
if (bytes.length !== pressure.BYTES) {
|
|
|
|
throw new Error('Pressure must have exactly 2 bytes');
|
|
|
|
}
|
|
|
|
|
|
|
|
var h = bytesToInt(bytes);
|
|
|
|
return +(h / 10).toFixed(1);
|
|
|
|
};
|
|
|
|
pressure.BYTES = 2;
|
|
|
|
|
2018-11-27 23:19:59 +01:00
|
|
|
var bitmap1 = function (byte) {
|
|
|
|
if (byte.length !== bitmap1.BYTES) {
|
2018-06-17 13:24:20 +02:00
|
|
|
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-11-19 00:41:15 +01:00
|
|
|
return ['adr', 'screensaver', 'screen', 'countermode', 'blescan', 'antenna', 'filter', 'alarm']
|
2018-06-17 13:24:20 +02:00
|
|
|
.reduce(function (obj, pos, index) {
|
2018-06-23 13:42:01 +02:00
|
|
|
obj[pos] = +bm[index];
|
2018-06-17 13:24:20 +02:00
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
};
|
2018-11-27 23:19:59 +01:00
|
|
|
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;
|
2018-06-17 13:24:20 +02:00
|
|
|
|
|
|
|
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 = {
|
|
|
|
uint8: uint8,
|
|
|
|
uint16: uint16,
|
2018-08-05 17:49:53 +02:00
|
|
|
uint32: uint32,
|
|
|
|
uptime: uptime,
|
2018-11-25 19:16:31 +01:00
|
|
|
float: float,
|
|
|
|
ufloat: ufloat,
|
2018-11-25 23:39:12 +01:00
|
|
|
pressure: pressure,
|
2018-06-17 13:24:20 +02:00
|
|
|
latLng: latLng,
|
2018-06-18 13:13:13 +02:00
|
|
|
hdop: hdop,
|
2019-07-27 02:32:39 +02:00
|
|
|
altitude: altitude,
|
2018-11-27 23:19:59 +01:00
|
|
|
bitmap1: bitmap1,
|
|
|
|
bitmap2: bitmap2,
|
2018-09-17 21:57:01 +02:00
|
|
|
version: version,
|
2018-06-17 13:24:20 +02:00
|
|
|
decode: decode
|
|
|
|
};
|
|
|
|
}
|