The following shows how you can create your own library with public and private code and data.
It also shows how you interface with Vera's functions (luup.variable_watch, luup.call_timer) that require a function NAME (as a string).
You can load a file called MyLUACode.lua in the startup LUA with:
MyCode = require("MyLuaCode")
-- Reference a variable in your module:
local VariableCopy = MyCode.VariableNamePublic
-- Call a function in your module:
MyCode.FunctionPublic()
-- Call Vera's function using the Name of a function ... Bound in the module using _G.XXX
luup.variable_watch(... "MyWatchFunction")
The file contents would look something like:
module("MyLUACode", package.seeall)
--- NOTE: Variable assignments/initialization are made when module is loaded!
--- The following Variable is only visible from functions in this module. (NOTE: the local!)
local VariableNamePrivate = 1
--- The following Variable is visible outside of this module. (NOTE: absence of the local!)
VariableNamePublic = 2
--- The following function is visible only to functions in this module. (NOTE: the local!)
local function FunctionPrivate
...
end
--- The following function is visible outside of this module. (NOTE: absence of the local!)
function FunctionPublic
...
end
--- Some Function Attached to luup.variable_watch
local function MyWatchFunction()
...
end
--- Module Init Code ... Local scope only
local function Init()
--- Promote the functions used by Vera's luup.xxx functions to the Global Name Space
_G.MyWatchFunction = MyWatchFunction
--- Other Module Loading initialization code ...
...
end
--- Call the Init Code
Init()