const payload_raw = payload.find(x => x.variable === 'payload_raw' || x.variable === 'payload' || x.variable === 'data');
if (payload_raw) {
try {
// Convert the data from Hex to Javascript Buffer.
const buffer = Buffer.from(payload_raw.value, 'hex');
const data = [
{ variable: 'Timestamp', value: Date(buffer.readUInt32BE(3)).toLocaleString() },
{ variable: 'latitude', value: buffer.readInt32BE(7) / 1000000 },
{ variable: 'longitude', value: buffer.readInt32BE(11) / 1000000 },
{ variable: 'speed', value: buffer.readInt8(15), unit: 'mph' },
{ variable: 'Angle', value: buffer.readInt16BE(16) , unit: 'C' },
{ variable: 'Altitude', value: buffer.readUInt16BE(18) },
{ variable: 'Electricity', value: buffer.readInt8(22), unit: '%' },
{ variable: 'Altitude', value: buffer.readUInt16BE(18) },
];
// This will concat the content sent by your device with the content generated in this payload parser.
// It also adds the field "serie" and "time" to it, copying from your sensor data.
payload = payload.concat(data.map(x => ({ ...x, serie: payload_raw.serie, time: payload_raw.time })));
} catch (e) {
// Print the error to the Live Inspector.
console.error(e);
// Return the variable parse_error for debugging.
payload = [{ variable: 'parse_error', value: e.message }];
}
console.log(device)
}