Decoder testing

Hi!

SInce i very new to javascript please forgive my very basic question.

I want to run the decoder script in html page to be able to test and se what happens with different payloads.

But how can i simulate the data being sent to the decoder?

I tried simply adding

var data0 = 0x70;
var data1 = 0x00;

and so on but

console.log(data[0]);

in decoder wont show anything in console of chrome.

I fell like iam missing somethin really basic here.

Hi Patrik,

You’d be better off running the code as a local program from the command line using nodeJS rather than using the browser just to get a JS environment.

Phil.

Yes, thanks, i also came to that conclusion.

Do you know how i can fake a payload to the script with node.js?

Hi Patrik,

Here’s a very simple script that sets up the input data as an array of bytes, calls the Decode function, and prints the result, based on a ThingsNode. Hope that helps

$ node ./test.js
{ event: ‘interval’, battery: 258, light: 772, temperature: 12.86 }

// 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]
//  - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(port, bytes, variables) {

  var decoded = {};
  var events = {
    1: 'setup',
    2: 'interval',
    3: 'motion',
    4: 'button'
  };
  decoded.event = events[port];
  decoded.battery = (bytes[0] << 8) + bytes[1];
  decoded.light = (bytes[2] << 8) + bytes[3];
  if (bytes[4] & 0x80)
    decoded.temperature = ((0xffff << 16) + (bytes[4] << 8) + bytes[5]) / 100;
  else
    decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
  return decoded;
}

// define the input data
let data = Buffer.from([01, 02, 03, 04, 05, 06])

// call the decode function
let res = Decode(2, data, null)

// print the result
console.dir(res, {depth:null});

If you are using MQTT you could use our tool - it uses the same JS-Engine as chirpstack.


https://gitlab.com/wobcom/iot/homebrew-iot (if you want to use homebrew to download the binary)

Hi,
I adapted it for Chirpstack V4, i.e. for decodeUplink(input).
For me it works fine with Visual Studio Code.

// define the input data
 let data = Buffer.from([0x01, 0xCC, 0xB8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE7, 0x85, 0x25, 0x00, 0x66, 0x30, 0x28, 0x02])

//object oinput
 oinput={
  bytes: data,
  fPort: 1,
  variables: null
}
// call the decode function
// let res = Decode(100, data, null)
  let res = decodeUplink(oinput) 

// print the result
  console.dir(res, {depth:null});

Hi,

I found this post whilst looking for a way to test my decoders.
I keep my decoders in a git repo and wanted a way to test them without having the test code at the bottom of the file that I would have to remember to remove whenever I pasted the decoder into my server.
What I wanted was a way to call nodejs on a javascript file that imported my decoder and then called the Decode function and displayed the result.

Years later I found this Stack Overflow answer that let me solve my problem.

If we have our decoder in a file called Test_Decoder.js:

// 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 Decode(fPort, bytes) {
  return {
      'a': bytes[0],
      'b': bytes[1],
      'c': bytes[2],
      'd': bytes[3],
  };
}

We can create another file called launch_Test_Decoder.js:

// Trick from: https://stackoverflow.com/a/5809968
var fs = require('fs');
eval(fs.readFileSync("Test_Decoder.js")+'');

data = [1, 2, 3, 4];
var result = Decode(1, data);
console.log(result);

Then we can run our test file:

$> nodejs launch_Test_Decoder.js
{ a: 1, b: 2, c: 3, d: 4 }

Hope this helps.