Module scripts#
If the optional moduleMain property in the package.json file is specified, it refers to the module script. The module script is always run before instance scripts, and only once for each module. The global table resulting from running the module script is referred to as the module sandbox. Variables in the module sandbox are not visible to instance sandboxes, except by assigning them to the shared_table described below.
A module script may be necessary when dealing with finite controller resources. A set of instances may need to access the same UDP port; however, only one UdpSocket can be bound to a given UDP port. In this case, the UdpSocket object will have to be managed at the module level. This process is explained further by the examples in this section.
Module scripts are optional — if they are not needed and add too much complexity to the module package, an instanceMain is all that is required. Module properties and the shared_table can be used without a module script.
Module API#
A module object is defined within the module sandbox. In addition to the properties of the module object exposed to module instances, this object exposes lifetime functions similar to those of module instances.
Package information#
A set of read-only properties provide the values of a subset of fields of the package.json file:
name— The name of the module, as specified by the"name"field of thepackage.jsonfile.version— The version of the module, as specified by the"version"field of thepackage.jsonfile.api_version— The IO module API version used by the module, as specified by the"ioModuleApiVersion"field of thepackage.jsonfile.
Module lifetime functions#
You may define functions to run at key points in a module’s lifetime. These mirror the lifetime functions of module instances.
initialize()— called right after the project loads, but before theinitializehandlers of any module instances.net_up()— called when the controller network interface comes up or just after theinitializehandler if the network interface is up at the time of project load. Guaranteed to be called before thenet_uphandlers of any of the IO module’s instance sandboxes. Might be called before or after theinitializehandlers of any module instances.net_down()— called when the controller network interface goes down or just before thecleanuphandler if the network interface is up at the time of project unload. Guaranteed to be called after thenet_downhandlers of any of the IO module’s instance sandboxes. Might be called before or after thecleanuphandlers of any module instances.cleanup()— called just before the project unloads. Called after thecleanuphandlers of any module instances.
Module properties#
Properties defined in the moduleProperties field in the module configuration JSON file are set by the user in Designer for each module. To read the values of these properties in the module script, use the module:property() method, passing the name of the property as defined in the configuration.
For example:
local udp_socket = iomodules.net.UdpSocket.new()
local inbound_port = module:property("Inbound Port")
module.net_up = function()
udp_socket:bind(inbound_port)
end
Time changes#
Whenever the controller’s local time changes, the time_change handler is called. The controller’s new local time can be retrieved via controller.time.get_current_time():
module.time_change = function()
local dateTime = controller.time.get_current_time()
controller.log("New time is " .. dateTime.utc_timestamp)
end