Webhooks: downlink decoder

I am moving an application from V2 to V3, and I am now configuring the webhook for downlink. The uplink is already working. The downlink encoder is also working, since the device responds correctly to downlink schedules. The problem is the decoder, since the downlink event logs only include the raw payload and not the decoded payload. My javascript code is the following (very close to the example in the TTN help pages):

function encodeDownlink(input) {
  var enable_alert_sound = ["false", "true"];
  return {
    bytes: [enable_alert_sound.indexOf(input.data.enable_alert_sound)],
    fPort: 1,
    warnings: [],
    errors: []
  };
}

function decodeDownlink(input) {
  switch(input.fPort) {
    case 1:
      return {
        data: {
          enable_alert_sound: ["false", "true"][input.bytes[0]]
        }
     }
    default:
      throw Error("unknown FPort");
  }
}

Is there something wrong in the encoder code. If not, what could be preventing the decoded payload from appearing in the downlink event logs?

Another question: as it is, the encoder is sending a character, either 48 (‘0’) or 49 (‘1’). How should I change the code in order to send values 0 or 1?

Thank you in advance.

Pretty printing for easier debugging - I don’t have the answer to your question, but it will help to properly format your code listing.

function encodeDownlink(input) {
  var enable_alert_sound = [“false”, “true”];
  return {
    bytes: [enable_alert_sound.indexOf(input.data.enable_alert_sound)],
    fPort: 1,
    warnings: ,
    errors:
  };
}

function decodeDownlink(input) {
  switch (input.fPort) {
    case 1:
      return {
        data: {
          enable_alert_sound: [“false”, “true”][input.bytes[0]]
        }
      }
      default:
        throw Error(“unknown FPort”);
  }
}
2 Likes

As your formatted code was missing a couple of characters I formatted the original for the OP.

1 Like