DIY decoder scripts

DIY decoder scripts are responsible for converting the data sent by your device into the SenML format required by KPN Things. The data sent by your device is enriched with some metadata and provided to your script as a stringified JSON structure.

Script setup

The JavaScript decoder must consist of a main function that accepts a single string parameter and returns a single string or null. The string should be a SenML pack as JSON. Returning null signifies that the messages should not be processed further.

The input parameter string contains the input data in JSON format. See inputString typedef for the structure of the JSON found in the input data.

Script Tester

The image below shows the script tester (indicated with a red square) next to the script editor.

The script tester can be used to test the script with the provided message data from the tester window and should be used to verify that the script is correct and returns valid SenML. This is done in an isolated environment. The result of the test execution is shown in the Output window and script logs are displayed in the Log window. Errors that may occur, will be displayed in both Output and Log windows.

It is safe to trial-and-error within the tester as it does not touch or involve anything outside of the isolated test environment.

The script tester provides a few options:

  • Payload data - the type of payload data that the device type supports. The drop-down list will contain every supported payload data to choose from.

  • Message detail - whether only the payload or the entire uplink message should be displayed in the text field below. The message data is editable.

  • Test button - executes the test with the script that is currently present in the editor and the message data. The tester always uses the full uplink message data, no matter which message detail is selected.

  • Output window - presents the result of the test execution.

  • Log window - custom script logs or various error messages will appear in this window.

Errors

Output window:

Script errors - when an error is thrown in the script itself. A warning toast will also pop-up at the top center of the page stating that the script did not execute successfully.

JavaScript errors (ReferenceError, SyntaxError, TypeError…) - when a JavaScript error occurs. The accompanying error message is also presented to clarify what went wrong.

Log window:

Output is not valid SenML - when the output contains invalid SenML records. Output will still be displayed in the output window.

SenML processing errors - when a SenML specific error occurs

Script examples

Example scripts can be found in this section for each protocol supported by the DIY decoders.

For a LoRa message

The below example script for LoRa messages presents decoding of payloads that might contain uplink or location data, or both.

For an MQTT message

The below example script for MQTT messages presents decoding of both text and binary payloads of an MQTT message.

For a SenML over HTTP(S) message

The example below decodes a SenML message sent over the HTTP(S) protocol.

Example decoder that supports multiple protocol messages

The example below presents a decoder supporting multiple protocol messages. It first determines which one of the protocols is being utilized and then decodes the message appropriately.

JavaScript typedef file

The snippet below describes the structure of the ScriptInput parameter provided to the DIY Decoder function.

See also:

Last updated

Was this helpful?