Splitting Lua Script into Multiple Files
To split example ucm.lua
file into several files and use it in the blueprint follow the steps below.
Create Lua Directory​
- Create a new directory named
src
. You can use another name if you wish. - Put into this directory two files
main.lua
andcommand.lua
.
Lua scrtip entry point
When you are using multiple files you should have main.lua
file as an entrypoint of your script.
- main.lua
- command.lua
-- Load module command from command.lua file
command = require("command")
-- Obtain device properties and send it to Enapter Cloud
function send_properties()
enapter.send_properties({
-- We could have called Modbus API here, but use
-- constant value for the example simplicity
serial_number = "AC001215"
})
end
-- Obtain device telemetry attributes and send it to Enapter Cloud
function send_telemetry()
enapter.send_telemetry({
-- Again, we use constant value for simplicity
h2_concentration = 0.01,
-- Pass active_alerts into telemetry
alerts = command.active_alerts
})
end
-- Register periodic function, will send properties every 30 seconds
scheduler.add(30000, send_properties)
-- Register periodic function, will send telemetry every 1 second
scheduler.add(1000, send_telemetry)
-- Register "beep" and "silence" commands, the function passed as a second argument
-- will be executed every time user starts the command from UI
enapter.register_command_handler("beep", command.beep)
enapter.register_command_handler("silence", command.silence)
-- Define local variable to incapsulate module logic
-- Note that command functions and active_alerts variable are referenced via mod.*
local mod={}
-- Store active alerts as a global array
-- Empty array means there are no alerts by default
mod.active_alerts = {}
-- "beep" command implementation
function mod.beep(ctx, args)
-- Add h2_high to active alerts array
mod.active_alerts = {"h2_high"}
-- Send some reply back to the user as a text message
return "Sound alarm started"
end
-- "silence" command implementation
function mod.silence(ctx, args)
-- Clean active alerts array
mod.active_alerts = {}
return "Sound alarm stopped"
end
return mod
Update Manifest File​
Update communication_modules
section in manifest.yml
. Replace lua_file
section with lua
to set Lua directory:
communication_module:
product: ENP-RS485
lua:
dir: src
To learn more about lua
section see the reference.
Upload Blueprint​
Now you can upload the blueprint.