LoRaWAN-Signal-Mapping/server/ttn-app-server/DecodePayload.mjs

30 lines
845 B
JavaScript
Raw Permalink Normal View History

"use strict";
import aes from 'aes-js';
const offset_id = 0;
const offset_lat = 32 / 8;
const offset_lng = (32 / 8) + (32 / 8);
function decode_payload(payload_base64, key_hex) {
let payload_buffer_enc = Buffer.from(payload_base64, "base64");
let key_buffer = Buffer.from(key_hex, "hex");
2019-07-19 12:10:47 +00:00
// console.log(payload_buffer_enc);
let aes_ecb = new aes.ModeOfOperation.ecb(key_buffer);
let payload_buffer = Buffer.from(aes_ecb.decrypt(payload_buffer_enc).buffer);
2019-07-19 12:10:47 +00:00
// console.debug(payload_buffer);
2019-07-09 12:14:12 +00:00
// console.debug(`Offsets: ${offset_id}, ${offset_lat}, ${offset_lng}`);
// console.debug(`Message hex: ${payload_buffer.toString("hex")}`)
return {
id: payload_buffer.readUInt32LE(offset_id),
2019-07-09 12:14:12 +00:00
latitude: payload_buffer.readFloatLE(offset_lat),
longitude: payload_buffer.readFloatLE(offset_lng)
};
}
export { decode_payload };