Docs
Payload conversion
The payload conversion is a script that is used to transform an arbitrary received payload to measurements that are ingested in the Blockbax Platform. A payload conversion has an expected payload format and is defined as a plain JavaScript function.
| Field | Description |
|---|---|
| Payload Format | The type of payload format that is sent to Blockbax. The received payload bytes are decoded based on the specified format. |
| Script | A JavaScript (ECMAScript 2022 compliant) function with signature convertPayload(payload, context) { ... } is used to convert the payload to measurements |
Payload formats
Currently, four different payload formats are supported by Inbound Connectors. Payloads must always be sent to the inbound connector as bytes, but the format specifies how these bytes are decoded and passed to the payload conversion function.
| Payload Format | Description | Passed to convertPayload as |
|---|---|---|
| JSON | Received payload is decoded as UTF-8 JSON string | JavaScript Object |
| CBOR | Received payload is decoded following the CBOR specification | JavaScript Object |
| String | Received payload is decoded as UTF-8 String | JavaScript String |
| Bytes | Received payload is passed as received | JavaScript ArrayBuffer |
| Avro | Received payload is decoded according the schema in the specified schema registry. This is exclusive to the Kafka consume connector | JavaScript Object |
Payload conversion script
The payload conversion script is a user-defined script that is used to transform the decoded payload to measurements that are to be ingested by the Blockbax Platform. Additionally, log messages can be generated to provide feedback to the user from the payload conversion script.
The payload conversion is defined as a JavaScript (ECMAScript 2022 compliant) function with required signature convertPayload(payload, context) { ... }. The web client provides a convenient editor with auto-completion and method signatures on hover. An example script is shown below:
| |
This script takes a JSON type payload and uses a top level property for prefixing the ingestion ids. The timestamp for all measurements contained by the payload is parsed from the top level object as well. The script then iterates over all key value pairs in the data field to ingest them as measurements.
Payload parameter
The payload parameter is the decoded payload sent to the inbound connector. This parameter can be either a JavaScript object, a string or an ArrayBuffer. The type of the payload depends on the specified payload format. The payload object is immutable and will throw a TypeError on modification.
Context parameter
The context parameter is an object used to pass measurements and logs to the Blockbax Platform. The context parameter cannot be modified. Its functionalities are described below:
addMeasurement(ingestionId, value, date)
This function is used for sending a measurement to the Blockbax platform. It records a single data point (measurement) for a specific metric and subject. It can be called multiple times in one payload conversion script.
| Parameter | Description |
|---|---|
| ingestionId | Ingestion ID of the target series for the measurement. |
| value | Either a number, string or a location object ({"lat": <number>, "lon": <number>, "alt": <number>}). Note that the alt field of the location object is optional. You can use the location(lat, lon, alt?) library function to create a location object. |
| date (optional) | Date object with the date of the measurement. You can use the date(input, format?) library function to create a date object from various input formats. |
logInfo(msg)
Logs a user message at INFO level.
| Parameter | Description |
|---|---|
| msg | A string representing the message to be logged. |
logWarn(msg)
Logs a user message at WARN level.
| Parameter | Description |
|---|---|
| msg | A string representing the message to be logged. |
logError(msg)
Logs a user message at ERROR level.
| Parameter | Description |
|---|---|
| msg | A string representing the message to be logged. |
Optionally when the MQTT subscribe connector is selected, it will also have the following functionality:
getTopic()
Returns the topic on which the payload was received. E.g. when subscribed to topic temperature/#, the function could return temperature/room-1/device-1.
Library functions
Several library functions are available when writing your conversion function to make writing a conversion easier and more concise. The available functions are described below.
number(value)
Parses provided input string value to a JavaScript number and rounds it to 8 decimal places if needed.
| Parameter | Description |
|---|---|
| value | A string value containing a number. |
location(lat, lon, alt?)
Creates a location object from the provided latitude, longitude and optionally altitude.
| Parameter | Description |
|---|---|
| lat | Number value representing the latitude of the location in degrees. |
| lon | Number value representing the longitude of the location in degrees. |
| alt (optional) | Number value representing the altitude of the location above the earth surface in meters. |
date(value, format?)
Attempts to parse the provided value to a JavaScript date object. If a date format string is provided it will use the date format string.
| Parameter | Description |
|---|---|
| value | If no format is specified: An input value containing a Unix timestamp in milliseconds or seconds since Epoch, or an ISO8601 string. If a format is specified: a date string that matches the provided date format string. |
| format (optional) | A date format string, or an array of date format strings if multiple formats could apply. |
Without a format specified this function accepts by default:
- Unix timestamps in milliseconds (13 digit number, since epoch 1 January 1970 00:00 UTC)
- Unix timestamps in seconds (10 digit number, since epoch 1 January 1970 00:00 UTC)
- ISO8601 strings
For the date format the following parsing tokens are supported:
| Input | Example | Description |
|---|---|---|
| YY | 01 | Two-digit year |
| YYYY | 2001 | Four-digit year |
| M | 1-12 | Month, beginning at 1 |
| MM | 01-12 | Month, 2-digits |
| MMM | Jan-Dec | The abbreviated month name |
| MMMM | January-December | The full month name |
| D | 1-31 | Day of month |
| DD | 01-31 | Day of month, 2-digits |
| H | 0-23 | Hours |
| HH | 00-23 | Hours, 2-digits |
| h | 1-12 | Hours, 12-hour clock |
| hh | 01-12 | Hours, 12-hour clock, 2-digits |
| m | 0-59 | Minutes |
| mm | 00-59 | Minutes, 2-digits |
| s | 0-59 | Seconds |
| ss | 00-59 | Seconds, 2-digits |
| S | 0-9 | Hundreds of milliseconds, 1-digit |
| SS | 00-99 | Tens of milliseconds, 2-digits |
| SSS | 000-999 | Milliseconds, 3-digits |
| Z | -05:00 | Offset from UTC |
| ZZ | -0500 | Compact offset from UTC, 2-digits |
| A | AM PM | Post or ante meridiem, upper-case |
| a | am pm | Post or ante meridiem, lower-case |
| Do | 1st… 31st | Day of Month with ordinal |
| X | 1410715640.579 | Unix timestamp |
| x | 1410715640579 | Unix ms timestamp |
Testing payload conversion scripts
Payload Conversion scripts can be tested in the Blockbax Web Client. Depending on the payload type a test payload can be provided, and the conversion script is then executed for that payload. For the JSON, CBOR, String and Avro types, a string can be provided as payload. For the CBOR and Bytes type, a hex string can be provided. The test payload can be saved with the conversion script. This allows doing basic regression testing when making changes to the script and verifying that the output measurements are correct for that payload. When the conversion script is executed for the payload the web client will show the measurements and logs that were produced by the payload conversion. Please note that this is only a visual representation of the outcome of the payload conversion, no actual measurements are ingested. Any problems with the script or resulting measurements are shown as log messages.