Skip to content

Lua Transform Component

The Lua Transform component provides a Lua scripting engine with several Edge Xrt-specific Lua functions.

Available Xrt-specific Lua Functions

The following Xrt-specific Lua functions are available for use:

pub_alloc Lua Function

The pub_alloc Lua function allocates an Edge Xrt Bus publisher.

Example pub_alloc Lua Function:

pub_alloc (Bus, topic)

pub_alloc Lua Function Parameters

The following table describes the parameters used with the pub_alloc Lua Function:

Parameter Description
Bus Reference to the Bus component

This is available as the iot_Bus global variable
topic String to publish on
return Returns a Lua object representing the publisher

sub_alloc Lua Function

The sub_alloc Lua function allocates an Edge Xrt Bus subscriber.

Example sub_alloc Lua Function

sub_alloc (Bus, callback, match)

sub_alloc Lua Function Parameters

The following table describes the parameters used with the sub_alloc Lua Function:

Parameter Description
Bus Reference to the Bus component

This is available as the iot_Bus global variable
callback Lua function invoked when data matching the topic is published

The function is passed two parameters, which consist of a Lua table containing the following:
  • The published event data
  • A string containing the matching topic
match String to match against event topics
return Returns a Lua object representing the subscriber

pub_free Lua Function

The pub_free Lua function frees the publisher object.

Example pub_free Lua Function

pub_free (pub)

pub_free Lua Function Parameters

The following table describes the parameters used with the pub_free Lua Function:

Parameter Description
pub Publisher object created using pub_alloc

sub_free Lua Function

The sub_free Lua function frees the subscriber object.

Example sub_free Lua Function

sub_free (sub)

sub_free Lua Function Parameters

The following table describes the parameters used with the sub_free Lua Function:

Parameter Description
sub Subscriber object created using sub_alloc

publish Lua Function

The publish Lua function publishes event data using the supplied publisher object.

Example publish Lua Function

publish (pub, data)

publish Lua Function Parameters

The following table describes the parameters used with the publish Lua Function:

Parameter Description
pub Publisher object created using pub_alloc
data Lua value to publish

Lua Transform Configuration Variables

The following table describes the configuration variables that can be used with the Lua Transform component:

Parameter Type Description Required Y/N
Bus String The name of the Bus component to use Y
Logger String The name of the Logger component to use N
ScriptFile String The name of the Lua script file to load N
Script Base64 encoded string Lua script, base64 encoded N
ForceGC Boolean Specifies whether to trigger the Lua garbage collector after a Lua script callback has been invoked

The default value is as follows:
  • Azure Sphere: true
  • Other: false
N

Configuration Example

The following example configures a Lua Transform component that uses the script.lua script located in the deployment/config directory:

{
  "Bus": "bus",
  "Logger": "logger",
  "ScriptFile": "deployment/config/script.lua"
}

Note

Normally either ScriptFile or Script would be set in a configuration. If both are set Script will be used in preference to ScriptFile. If neither is set, the Lua Transform will start but not actually do anything, although scripts can be loaded subsequently via dynamic component reconfiguration.

Lua Script Example

The following example subscribes to a BACnet device service telemetry topic and adds an additional synthetic reading, re-publishing the changed data on a separate topic.

function sub_callback (data)
  data.readings.Comment = {}
  data.readings.Comment.value = "Sample Lua Value Injection"
  data.readings.Comment.type = "string"
  publish (pub, data)
end

sub = sub or sub_alloc (iot_bus, sub_callback, "xrt/devices/bacnet/data")
pub = pub or pub_alloc (iot_bus, "xrt/devices/bacnet/data2")