Debugging javascript codec

Hi,

I’m really enjoying chirpstack v4. Great improvement over the older version in terms of installation. The simplification has helped a lot!

Of course, i’m fairly new to this, so i’ll probably have made a simple mistake.

My current codec looks like this:

function decodeUplink(bytes, fport, variables) {
  var device = bytes[0];
  var msgNr = bytes[1];
    var batteryVoltage = (bytes[2]<<8 | bytes[3]);
    var distance = 0;
    var msgCount = (bytes[4]<<8 | bytes[5]);
    var gpsTime = (bytes[6]<<8 | bytes[7]);
    var gpsSats = bytes[8];
    var gpsFixType = bytes[9];
  var gpsLat = (bytes[10] << 24 | bytes[11] << 16 | bytes[12]<<8 | bytes[13]);
    var gpsLong = (bytes[14] << 24 | bytes[15] << 16 | bytes[16]<<8 | bytes[17]);
    var gpsAltMSL = (bytes[18] << 8 | bytes[19])
  
  return {
    data: {
      DeviceByte: device,
      MsgNr: msgNr,
      Voltage: batteryVoltage,
      Distance: distance,
      MessageCount: msgCount,
      GPSTime: gpsTime,
      GPSSats: gpsSats,
      GPSFixType: gpsFixType,
      GPSLat: gpsLat,
      GPSLong: gpsLong,
      GPSAltitude: gpsAltMSL
    }
  }
}

The output however is nothing. Of course even DeviceByte should have a value, but it’s null.
I’ve checked the logfiles of chirpstack, but there’s no error there.

How can i debug/troubleshoot this?

I’ve obviously moved from v3 to v4 and expected ‘input’ to be the array.

The fixed code looks like this:

function decodeUplink(input) {
  var bytes = input.bytes;
  var device = bytes[0];
  var msgNr = bytes[1];
    var batteryVoltage = (bytes[2]<<8 | bytes[3]);
    var distance = 0;
    var msgCount = (bytes[4]<<8 | bytes[5]);
    var gpsTime = (bytes[6]<<8 | bytes[7]);
    var gpsSats = bytes[8];
    var gpsFixType = bytes[9];
  var gpsLat = (bytes[10] << 24 | bytes[11] << 16 | bytes[12]<<8 | bytes[13]);
    var gpsLong = (bytes[14] << 24 | bytes[15] << 16 | bytes[16]<<8 | bytes[17]);
    var gpsAltMSL = (bytes[18] << 8 | bytes[19])
  
  return {
    data: {
      DeviceByte: device,
      MsgNr: msgNr,
      Voltage: batteryVoltage,
      Distance: distance,
      MessageCount: msgCount,
      GPSTime: gpsTime,
      GPSSats: gpsSats,
      GPSFixType: gpsFixType,
      GPSLat: gpsLat,
      GPSLong: gpsLong,
      GPSAltitude: gpsAltMSL
    }
  }
}

The function gets only one variable which is an object with properties on it.

Hi all,

the “debugging” in this topic attracted me to find some clues how to make the codecs in more professional way than waiting for a frame and looking into the logs where the error is.

So I will extend the question:

  • is it possible inside codec function to write some log file (console.log is not possible)?
  • is it possible to connect to sqlite3 or other (redis?) database?
  • is it possible to send message via socket or similar IP-based communication?
  • any other possibilities to throw out data out of codec function code?

Unfortunatelly for someone who is not proficient in JavaScript (like me) it’s not easy to write more elaborated codec, because JavaScript engine version is very old. E.g. Map() is not available (along with plenty of other JavaScript language features). So if I look into JS manuals, I fall down because of not implemented language features.

Regards.

Codec should be for encoding and decoding only.

You may need “rule engine” from IoT servers such as ThingsBoard and NodeRed to do what you want.