Skip to content

fetch_data() should prefer normalized_payload over decoded_payload #18

@dewet22

Description

@dewet22

I've implemented a data normalizer as per TTN's guidelines, in order to separate message decoding from data parsing. In my simple case, for a custom sensor with my own OTA data format, I've defined the following uplink payload formatter functions:

function decodeUplink(input) {
  var decoded = {
    data: {
      bytes: input.bytes
    },
    warnings: [],
    errors: []
  };

  if (input.fPort === 8) {
    decoded.data.temp = input.bytes[0] <<8 | input.bytes[1];
    decoded.data.humi = input.bytes[2] <<8 | input.bytes[3];
    decoded.data.co2 = input.bytes[4] <<8 | input.bytes[5];
  }

  return decoded;
}

function normalizeUplink(input) {
  return {
    data: {
      air: {
        temperature: input.data.temp / 10,
        relativeHumidity: input.data.humi / 10,
        co2: input.data.co2,
      },
    },
    // warnings: ["warning 1", "warning 2"], // optional
    // errors: ["error 1", "error 2"], // optional (if set, the normalization failed)
  };
}

This structure is defined by a common data dictionary, meaning sensors can now be represented more consistently. For instance, in my uplink_message stanza in the storage/uplink_message API call, I get access to both:

{
  "result": {
    // ...
    "uplink_message": {
      "f_port": 8,
      "f_cnt": 456,
      "frm_payload": "AOwBuAIu",
      "decoded_payload": {
        "bytes": [0, 236, 1, 184, 2, 46],
        "co2": 558,
        "humi": 440,
        "temp": 236
      },
      "normalized_payload": [
        { "air": { "co2": 558, "relativeHumidity": 44, "temperature": 23.6 } }
      ],
      // ...
}

I think the logic for fetch_data() should be to prefer normalized_payload data (if present) over decoded_payload. I wanted to run the idea by you first before I bother going off writing any code for it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions