Decoder doesn't work, error

Hi
I have a decoder for my device and when I take the string in the data field under device data and put it in my decoder I get the results that I expect but when I put the code under the device profile under the decoder I get the following error:

  • error:“execute js error: js vm error: (anonymous): Line 17:5 Unexpected reserved word (and 1 more errors)”

This is my decoder:

// Decode decodes an array of bytes into an object.
//  - fPort contains the LoRaWAN fPort number
//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
// The function must return an object, e.g. {"temperature": 22.5}

function base64ToHex(str) {
  var raw = atob(str);
  var res = "";
  for (var i = 0; i < raw.length; i++) {
    var hex = raw.charCodeAt(i).toString(16);
    res += (hex.length === 2 ? hex : '0' + hex);
  }
  return res.toUpperCase();
}

function hexStringToFloat(hexStr) {
    const hex = parseInt(hexStr, 16);
    const sign = hex >> 31 ? -1 : 1;
    const exponent = (hex >> 23) & 0xFF;
    return sign * (hex & 0x7fffff | 0x800000) * 1.0 / Math.pow(2, 23) * Math.pow(2, (exponent - 127));
}

function Decode(fPort, bytes) {
    var incomingHexData = base64ToHex(bytes);
  	var deviceSerial = parseInt(incomingHexData.substring(0, 8), 16);
  	var deviceTotalEnergy = hexStringToFloat(incomingHexData.substring(12, 20));
  	var deviceVoltage = hexStringToFloat(incomingHexData.substring(20, 28));
  	var deviceCurrent = hexStringToFloat(incomingHexData.substring(28, 36));
  
    return {
        "serialNumber": deviceSerial,
        "totalEnergy": deviceTotalEnergy,
        "voltage": deviceVoltage,
	"current": deviceCurrent
    };
}

This is a frame:

The data as text:

data:"AU4D/gEUPnS8akNtlH0+W+dYP3tp0UJHzgyb+w=="

Any help appreciated :blush:

I don’t know, if it works outside ChirpStack, then the cause could be with the JS VM used by ChirpStack which is a bit outdated in terms of JS features that are supported. Please note that ChirpStack v4 (please see the announcements section for test releases) contains a new JS VM, which should resolve many issues.

I installed Chirpstack v4 test release and now it works perfectly!

Thank you.

1 Like