Skip to content

Instantly share code, notes, and snippets.

@tiefpunkt
Last active March 22, 2024 06:29
Show Gist options
  • Save tiefpunkt/9407f4049365c065fdd6905adf949993 to your computer and use it in GitHub Desktop.
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"]
};
}
}
@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