Skip to main content

Using Dependencies

Lua has a community which made a lot of modules for general tasks. You can use them in your Lua scripts.

Modules are hosted and managed using Luarocks. You don't need to setup this tool manually.

Supported Modules

Lua modules should:

  • support Lua 5.3
  • be pure Lua modules

Adding Dependencies to Manifest File​

Extend our lua section with dependencies:

communication_module:
product: ENP-RS485
lua:
dir: src
dependencies:
- lustache ~> 1.3

Dependencies is a list of Luarocks packages. They are described in luarocks format. It looks like:

lua-string ~> 1.2
beemovie == 1.0.1

:::Note Version Management It's strongly recommended to specify versions of your dependencies. Read more about dependencies' constraints. :::

Using Dependencies​

lustache is a template engine. Imagine that serial_number consists of two parts: high and low bits. We could concatenate them using Lua .. operator, but for education purposes we will use template engine.

  • First of all we need require lustache module.
  • Then we can use it to combine serial_number.

The essence of the changes in the main.lua:

 -- Load module command from command.lua file
command = require("command")
+-- Load lustache module
+lustache = require("lustache")


-- 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"
+ serial_number = lustache:render("{{high}}{{low}}", { high="AC00", low="1217" })
})
end
Show the whole main.lua
-- Load module command from command.lua file
command = require("command")
-- Load lustache module
lustache = require("lustache")

-- 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 = lustache:render("{{high}}{{low}}", { high="AC00", low="1217" })
})
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)

Using Existing Rockspec​

If you are an experienced Lua developer, then you probably already use rockspec. In that case you can use your rockspec file and dependencies will be gotten from relevant section. Example of communication_module section:

communication_module:
product: ENP-RS485
lua:
dir: src
rockspec: my-dev-1.rockspec

Upload Blueprint​

Now you are ready to upload updated blueprint.

Hardware diversity is welcome. Integrate any device into a unified energy network.
© 2024 Enapter
Developer toolkit
DocumentationReference