Transforming payloads
The embedded MQTT and HTTP endpoints allow the device agent to receive data from external sources using the esthesis Line Protocol. When the external sources are under your control, eLP is a simple protocol you can easily work with. However, when the external sources are not under your control, you may need to transform their payloads before they are sent to the device agent.
esthesis device agent allows you to transform payloads using external Lua scripts, for both MQTT and HTTP endpoints (see the LUA* parameters in Configuration parameters for more details).
Lua incoming payload variable
In your Lua script, you have access to the original payload in the payload variable; the variable is a string. You can modify the payload as you wish, and simply return it at the end.
Example Lua script
Let us consider an external data source that pushes data in the following format:
The first line is always the category name, whereas the remaining lines contain individual measurements for that category. Measurements are separated by an equal sign.
We want to transform this incoming payload to eLP format, such as:
The following Lua script could be used to achieve this:
Lua incoming endpoint variable
In your Lua script, you also have access to an additional variable called endpoint. This variable contains the MQTT topic or HTTP URI, depending on the source of the incoming payload. This can be used to help determine the logic to apply when modifying the payload based on the specific endpoint on which data was received.
Example Lua script making use of the endpoint variable
You have a system that sends data into the following MQTT topics:
sensor/hardware/cpu:
sensor/hardware/memory:
The format of the data is exactly the same, so you want to implement your Lua script logic only once, however the resulting eLP should be differentiated based on the topic in which the data was received.
Below is an example of how to utilise the endpoint variable in your Lua script to handle that: