Payload Decoder

I created a script to decode the payload sent by lora node for HR-3633, and I’d like to test it, because now the payload returns only 2 parameters, one is the battery, second is the valves status.

Help me to test the script by provide different payloads. The script is in nodejs v20

import { performance } from 'perf_hooks';

const hexString = (buf) => buf.toString('hex').match(/../g).join(' ');

const decodeMap = {
    // decode_array'
    0b0000: (buf, pos) => {
        
        const len = buf.readInt8(pos);
        return [ len + 1, Buffer.copyBytesFrom(buf, pos + 1, len )];
    },
    // decode_double
    0b0001: (buf, pos) => [8, buf.readDoubleLE(pos)],
    // 'decode_float'
    0b0010: (buf, pos) => [4, buf.readFloatLE(pos)],
    // decode_bool
    0b0011: (buf, pos) => [1, !!buf.readUInt8(pos)],
    // decode_int8
    0b0100: (buf, pos) => [1, buf.readInt8(pos)],
    // decode_uint8
    0b0101: (buf, pos) => [1, buf.readUInt8(pos)],
    // decode_int16
    0b0110: (buf, pos) => [2, buf.readInt16LE(pos)],
    // decode_uint16
    0b0111: (buf, pos) => [2, buf.readUInt16LE(pos)],
    // decode_int32
    0b1000: (buf, pos) => [4, buf.readInt32LE(pos)],
    // decode_uint32
    0b1001: (buf, pos) => [4, buf.readUInt32LE(pos)],
}

const decode_sensor_data = (buf) => {

    const sensor_data = [];
    let pointer = 0;
    while (pointer < buf.length) {
        // read first byte
        const b = buf.readUInt8(pointer);
        const pk_id = b >> 4;
        const data_type = b & 0x0f;
        pointer += 1;
        const [step, data] = decodeMap[data_type](buf, pointer);
        pointer += step;
        sensor_data.push({pk_id,data});
    }
    if (pointer > buf.length) {
        throw Error("Sensor data broken.");
    }

    return sensor_data;
}


const decode_payload  = (bytes) => {

    const data = [];

    let pointer = 0;
    while (pointer < bytes.length - 4) {
        // two bytes
        const sensor_id = bytes.readUInt16LE(pointer);
        // one byte
        const body_length = bytes.readInt8(pointer + 2);    
        pointer += 3;
        const sensor_data = bytes.subarray(pointer, pointer + body_length);
        data.push({
            sensor_id: sensor_id,
            sensor: decode_sensor_data(sensor_data),
        })    
        pointer += body_length;
    }
    return data;
}


const startTime = performance.now();

const frm_payload = "AAAEBV4VAAYADgMBEwEpAAAAADkAAAAA";

//  0  1    2    3  4  5  6    7  8    9    10 
// {00 00} [04] (05 5e 15 00) {06 00} [0e] (03 01 13 01 29 00 00 00 00 39 00 00 00 00)

// decode uplink create buffer
const bytes =  Buffer.from(frm_payload, 'base64');
console.log(hexString(bytes));


const result = decode_payload(bytes);

const endTime = performance.now();
console.log(`Execution time: ${endTime - startTime} milliseconds`);
console.log(JSON.stringify(result, null, 3));