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.

FieldDescription
Payload FormatThe type of payload format that is sent to Blockbax. The received payload bytes are decoded based on the specified format.
ScriptA 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 FormatDescriptionPassed to convertPayload as
JSONReceived payload is decoded as UTF-8 JSON stringJavaScript Object
CBORReceived payload is decoded following the CBOR specificationJavaScript Object
StringReceived payload is decoded as UTF-8 StringJavaScript String
BytesReceived payload is passed as receivedJavaScript ArrayBuffer
AvroReceived payload is decoded according the schema in the specified schema registry. This is exclusive to the Kafka consume connectorJavaScript 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:

1
2
3
4
5
6
7
function convertPayload(payload, context) {
  const ingestionIdPrefix = payload.id + "$";
  const timestamp = date(payload.timestamp);
  for (const [key, value] of Object.entries(payload.data)) {
    context.addMeasurement(ingestionIdPrefix + key, value, timestamp);
  }
}
Payload Conversion

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.

ParameterDescription
ingestionIdIngestion ID of the target series for the measurement.
valueEither 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.

ParameterDescription
msgA string representing the message to be logged.
logWarn(msg)

Logs a user message at WARN level.

ParameterDescription
msgA string representing the message to be logged.
logError(msg)

Logs a user message at ERROR level.

ParameterDescription
msgA 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.

ParameterDescription
valueA string value containing a number.
location(lat, lon, alt?)

Creates a location object from the provided latitude, longitude and optionally altitude.

ParameterDescription
latNumber value representing the latitude of the location in degrees.
lonNumber 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.

ParameterDescription
valueIf 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:

InputExampleDescription
YY01Two-digit year
YYYY2001Four-digit year
M1-12Month, beginning at 1
MM01-12Month, 2-digits
MMMJan-DecThe abbreviated month name
MMMMJanuary-DecemberThe full month name
D1-31Day of month
DD01-31Day of month, 2-digits
H0-23Hours
HH00-23Hours, 2-digits
h1-12Hours, 12-hour clock
hh01-12Hours, 12-hour clock, 2-digits
m0-59Minutes
mm00-59Minutes, 2-digits
s0-59Seconds
ss00-59Seconds, 2-digits
S0-9Hundreds of milliseconds, 1-digit
SS00-99Tens of milliseconds, 2-digits
SSS000-999Milliseconds, 3-digits
Z-05:00Offset from UTC
ZZ-0500Compact offset from UTC, 2-digits
AAM PMPost or ante meridiem, upper-case
aam pmPost or ante meridiem, lower-case
Do1st… 31stDay of Month with ordinal
X1410715640.579Unix timestamp
x1410715640579Unix 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.

Payload testing