Skip to content

Instantly share code, notes, and snippets.

@tiefpunkt
Last active Nov 4, 2021
Embed
What would you like to do?
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

ReiniervdL commented May 6, 2021

Partially useful. It does not copy calculated values or variable names.

@tiefpunkt
Copy link
Author

tiefpunkt commented May 7, 2021

@ReiniervdL do you have an example where stuff gets left out?

@ReiniervdL
Copy link

ReiniervdL commented May 7, 2021

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

tiefpunkt commented May 8, 2021

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