Payload functions


#1

I tried to define payload functions several times now, but I get stuck everytime...

What language/dialect is used for the payload functions?

Is there a list/site that describes all objects and functions that are available?

For instance: where can I find all methods of the 'bytes' object that is a parameter of the decode function? I've read that it is a Buffer object but I don't know where to find the methods that can be used. slice() seems to work but toString('hex') not...

Another thing: Is it possible to call a webservice from a converter function?


(Arjan) #2

The TTN code uses:

...which says:

Otto targets ES5. ES6 features (eg: Typed Arrays) are not supported.

And some other TTN code documents:

// Decoder is a JavaScript function that accepts the payload as byte array and
// returns an object containing the decoded values
...
// Converter is a JavaScript function that accepts the data as decoded by
// Decoder and returns an object containing the converted values
...

More browsing shows that the first parameter is a Go []byte, which will become a JavaScript array of numbers. (JavaScript does not have bytes.) That is confirmed by a Decoder that prints it, just to investigate a bit:

function Decoder(bytes, port) {
  return { 
     bytes: {
       toString: Object.prototype.toString.call(bytes),
       typeof: typeof bytes
     },
     item: {
       toString: Object.prototype.toString.call(bytes[0]),
       typeof: typeof bytes[0]
     }
  };
}

...which yields:

{
  "bytes": {
    "toString": "[object Array]",
    "typeof": "object"
  },
  "item": {
    "toString": "[object Number]",
    "typeof": "number"
  }
}

So, JavaScript (ECMAScript 5) with an array of numbers. The best resource is Mozilla Developer Network, I feel, and "mdn" is a great keyword for search engines: mdn array. Also, you can use auto-completion in the developer console of your browser. Just start with something like:

var bytes = [0x31, 0x31, 0x31, 0x20, 0x30, 0x31, 0x34, 0x00];

...after which typing bytes. will show you the supported functions and all.

I'm quite sure it's not, but just repeating that question here :slight_smile:


Is there any documentation on payload functions?
(Arjan) #3

Ah, one can even use console.log, which is shown in TTN Console during testing:

console.log(Object.prototype.toString.call(bytes));
console.log(typeof bytes, typeof bytes[0]);

And, though it makes little sense to log the values that one has just entered for the test:

// Prints array of decimal values (numbers)
console.log(bytes); 

// Prints array of 2-character hexadecimal values (strings)
console.log(bytes.map(function(b) {
  return ("0" + b.toString(16)).substr(-2);
}));

// Prints single string of 2-character hexadecimal values
console.log(bytes.map(function(b) {
  return ("0" + b.toString(16)).substr(-2);
}).join(" "));

Note that it’s ES5, so ES6 fat arrows are not supported. So, this will NOT work:

bytes.map( b => ("0" + b.toString(16)).substr(-2) ).join(" ")

But of course, payload functions are not full blown programs. :slight_smile:


(S54mtb) #4

Hello everyone! Is there any option to get the metadata of the device/payload (e.g. rssi, location, frequency, etc) inside the payload decoder function?


(Anilkumar Komanduru) #5

hi, im even looking for the same, if some one knows the solution please let me know


#6

That data is avalable in the Data tab of your application. In addition, you can add HTTP integration to your application and use for example Google sheets:


(Anilkumar Komanduru) #7

Hi,
Thanks for your reply.
Actually we are trying to automate our sensor project, as a part of it we have used ‘data storage’ integration to read data but there are some scenarios like multiple packet repetitions where no packet counter is being stored in that. So we are in the way of exploring more.
And your solution ‘google spreadsheets’ is nice idea, however this is going to helpful for us due to some corporate restrictions.
Please let me know if you have any other solution.


(Arjan) #8

This is getting quite off-topic, but:

The meta data (except for the timestamp) is never stored in that; see How to retrieve metadata from Data Storage integration?

See also Access Metadata in decoder, and https://www.thethingsnetwork.org/docs/applications/integrations.html


(Anilkumar Komanduru) #9

Thanks you so much :smile: