V4 payload codecs

Hey guys, having a little trouble converting some of my older v3 payload codecs over to work with v4 and its kind of getting annoying… I admit i’m a js dummy though, always hated the language haha!

the ability to test a codec against bytes etc in the dash would be great as what works in vscode against node etc doesnt work in the payload codec and waiting for sensors to send data and check if what I did works takes for ever.

the main error I get seems to be this.

code:"UPLINK_CODEC"
description:"Exception generated by quickjs: 'decodeUplink' is not defined at <eval> (eval_script:146) "

this is where i could be going wrong. i’m of the understanding that uplinks get passed to
function decodeBytes(input) where we used to get (fPort, bytes) ← made more sense to me. this is where i am totally lost on how to get the objects from the input i’ve tried to do input.bytes and input.fPort etc but i just seem to get the error above.

this is one sensor i’m having trouble with.

function decodeBytes(input) {
    var data = input.bytes;
    var fPort = input.fPort;
    var bytes = Array.from(data, function(byte) {
        return ('0' + (byte & 0xFF).toString(16)).slice(-2);
    }).join('')
    //console.log(input);
    //console.log(data);
    //console.log(bytes)
    return rakSensorDataDecode(bytes);
}

// convert string to short integer
function parseShort(str, base) {
    var n = parseInt(str, base);
    return (n << 16) >> 16;
}

// convert string to triple bytes integer
function parseTriple(str, base) {
    var n = parseInt(str, base);
    return (n << 8) >> 8;
}

// decode Hex sensor string data to object
function rakSensorDataDecode(hexStr) {
    var str = hexStr;
    var myObj = {};

    while (str.length > 4) {
        var flag = parseInt(str.substring(0, 4), 16);
        switch (flag) {
        case 0x0768:// Humidity
            myObj.humidity = parseFloat(((parseShort(str.substring(4, 6), 16) * 0.01 / 2) * 100).toFixed(1));// + "%RH";//unit:%RH
            str = str.substring(6);
            break;
        case 0x0673:// Atmospheric pressure
            myObj.barometer = parseFloat((parseShort(str.substring(4, 8), 16) * 0.1).toFixed(2));// + "hPa";//unit:hPa
            str = str.substring(8);
            break;
        case 0x0267:// Temperature
            myObj.temperature = parseFloat((parseShort(str.substring(4, 8), 16) * 0.1).toFixed(2));// + "°C";//unit: °C
            str = str.substring(8);
            break;
        case 0x0188:// GPS
            myObj.latitude = parseFloat((parseTriple(str.substring(4, 10), 16) * 0.0001).toFixed(4));// + "°";//unit:°
            myObj.longitude = parseFloat((parseTriple(str.substring(10, 16), 16) * 0.0001).toFixed(4));// + "°";//unit:°
            myObj.altitude = parseFloat((parseTriple(str.substring(16, 22), 16) * 0.01).toFixed(1));// + "m";//unit:m
            str = str.substring(22);
            break;
        case 0x0371:// Triaxial acceleration
            myObj.acceleration_x = parseFloat((parseShort(str.substring(4, 8), 16) * 0.001).toFixed(3));// + "g";//unit:g
            myObj.acceleration_y = parseFloat((parseShort(str.substring(8, 12), 16) * 0.001).toFixed(3));// + "g";//unit:g
            myObj.acceleration_z = parseFloat((parseShort(str.substring(12, 16), 16) * 0.001).toFixed(3));// + "g";//unit:g
            str = str.substring(16);
            break;
        case 0x0402:// air resistance
            myObj.gasResistance = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "KΩ";//unit:KΩ
            str = str.substring(8);
            break;
        case 0x0802:// Battery Voltage
            myObj.battery = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "V";//unit:V
            str = str.substring(8);
            break;
        case 0x0586:// gyroscope
            myObj.gyroscope_x = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "°/s";//unit:°/s
            myObj.gyroscope_y = parseFloat((parseShort(str.substring(8, 12), 16) * 0.01).toFixed(2));// + "°/s";//unit:°/s
            myObj.gyroscope_z = parseFloat((parseShort(str.substring(12, 16), 16) * 0.01).toFixed(2));// + "°/s";//unit:°/s
            str = str.substring(16);
            break;
        case 0x0902:// magnetometer x
            myObj.magnetometer_x = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "μT";//unit:μT
            str = str.substring(8);
            break;
        case 0x0a02:// magnetometer y
            myObj.magnetometer_y = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "μT";//unit:μT
            str = str.substring(8);
            break;
        case 0x0b02:// magnetometer z
            myObj.magnetometer_z = parseFloat((parseShort(str.substring(4, 8), 16) * 0.01).toFixed(2));// + "μT";//unit:μT
            str = str.substring(8);
            break;
        default:
            str = str.substring(7);
            break;
        }
    }
    return {data: myObj};
}


// test input using node and vscode... (assuming this is how it comes?)
input = {fPort: 8, bytes: [1, 136, 251, 64, 57, 23, 6, 238, 0, 169, 196, 8, 2, 1, 124]}

console.log(decodeBytes(input));

seems to work in vscode/node.

looks like i figured it out, uplink function literally has to be called decodedUplink(input) ← taking input object.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.