Skip to content

Instantly share code, notes, and snippets.

@tiefpunkt
Last active March 22, 2024 06:29
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tiefpunkt/9407f4049365c065fdd6905adf949993 to your computer and use it in GitHub Desktop.
Make a TTN v2 payload decoder work with TTI/N v3
function Decoder(bytes, port) {
// Your "old" TTN v2 decoder function
}
// add other functions (Converter, Validator) here if you used them. If you didn't you can just skip them.
function decodeUplink(input) {
var data = input.bytes;
var valid = true;
if (typeof Decoder === "function") {
data = Decoder(data, input.fPort);
}
if (typeof Converter === "function") {
data = Converter(data, input.fPort);
}
if (typeof Validator === "function") {
valid = Validator(data, input.fPort);
}
if (valid) {
return {
data: data
};
} else {
return {
data: {},
errors: ["Invalid data received"]
};
}
}
@ReiniervdL
Copy link

Hi Severin,

Thank you for the fast reply. See below for example.
Using below V2 decoder and your V3 converter, the variable names "b" and "at" are not in the decoder output.
When testing with byte 0 and 1, value HEX 64, both outputs for B and at are "100"

function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};

if (port === 2) decoded.b = bytes[0]/50;
if (port === 2) decoded.at = bytes[1]-100;

@tiefpunkt
Copy link
Author

You need to return the decoded object, and then they will be. I see where you might be coming from. If you use the following Decoder function you should get what you're aiming for:

function Decoder(bytes, port) {
  // Decode an uplink message from a buffer
  // (array) of bytes to an object of fields.
  var decoded = {};
  
  if (port === 2) decoded.b = bytes[0]/50;
  if (port === 2) decoded.at = bytes[1]-100;

  return decoded;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment