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
// 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});
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});
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);