Difference between revisions of "NETIO Lua Tips & Philosophy"
Line 6: | Line 6: | ||
*Example switching output 1 every 10 seconds: | *Example switching output 1 every 10 seconds: | ||
− | + | local doPeriod = 10 -- repeat period in seconds | |
------------------------------------------------- | ------------------------------------------------- | ||
function doPeriodically() | function doPeriodically() | ||
Line 15: | Line 15: | ||
-- your periodical code | -- your periodical code | ||
-- your periodical code | -- your periodical code | ||
− | delay( | + | delay(doPeriod,function() doPeriodically() end) |
end | end | ||
Line 28: | Line 28: | ||
== Global variables == | == Global variables == | ||
For sharing variables between scripts, their initialization must be done without keyword '''local'''. Conventions in Lua specify that global variables should start with '''_G.''' (e.q. '''_G.globalVariable = true'''). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables. | For sharing variables between scripts, their initialization must be done without keyword '''local'''. Conventions in Lua specify that global variables should start with '''_G.''' (e.q. '''_G.globalVariable = true'''). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables. | ||
+ | |||
+ | == Deactivating scripts using global variables == | ||
+ | Periodical scripts described above can be paused using global variables. The only necessary thing is adding '''if-else''' condition at the beginning of callback function. This condition is checking state of global variable and based on its state it may pause the script. Value of global variable may be changed in another script triggered for example by URL API request. | ||
+ | |||
+ | Example of a periodical script which can be paused by second script. Second script should be triggered by URL API request. Main script is then paused by: ''http://<NETIO_IP>/event?globalVariable=0'' and resumed by ''http://<NETIO_IP>/event?globalVariable=1''. | ||
+ | |||
+ | Main script: | ||
+ | |||
+ | local doPeriod = 10 -- repeat period in seconds | ||
+ | globalVariable = true -- initialization of global variable | ||
+ | ------------------------------------------------- | ||
+ | function doPeriodically() | ||
+ | if globalVariable == true then | ||
+ | -- your periodical code | ||
+ | -- your periodical code | ||
+ | -- your periodical code | ||
+ | -- your periodical code | ||
+ | -- your periodical code | ||
+ | -- your periodical code | ||
+ | end | ||
+ | delay(doPeriod,function() doPeriodically() end) | ||
+ | end | ||
+ | |||
+ | doPeriodically() | ||
+ | |||
+ | Controling script (trigger: Incoming URL API request): | ||
+ | |||
+ | if event.args.globalVariable == "0" then | ||
+ | globalVariable = false | ||
+ | elseif event.args.globalVariable == "1" then | ||
+ | globalVariable = true | ||
+ | end |
Latest revision as of 11:57, 17 October 2019
Contents
Script starting
LUA Scripts in NETIO are event driven. That means if some event is triggered the script run exactly once. One of "triggers" is for example System started up. You can find list of all events called "triggers" in NETIO Lua Reference.
Periodical scipts
In NETIO there is no way for triggering scripts periodically (e.g. every 10 seconds) usng standard triggers. For such scripts the trigger System started up and the function delay() must be used. This function calls itself and allows to run some procedures periodically.
- Example switching output 1 every 10 seconds:
local doPeriod = 10 -- repeat period in seconds ------------------------------------------------- function doPeriodically() -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code delay(doPeriod,function() doPeriodically() end) end doPeriodically()
- This script can be launch using e.g. System started up trigger.
- Only way how to disable these scripts is restart of NETIO, or usage of global variables.
Debugging
To debug your code, use log() and logf() functions. See the output in system events log.
Global variables
For sharing variables between scripts, their initialization must be done without keyword local. Conventions in Lua specify that global variables should start with _G. (e.q. _G.globalVariable = true). After initialization global variable is available in all scripts. After restarting device, all global variables are deleted. It is recommended to use different names for local and global variables.
Deactivating scripts using global variables
Periodical scripts described above can be paused using global variables. The only necessary thing is adding if-else condition at the beginning of callback function. This condition is checking state of global variable and based on its state it may pause the script. Value of global variable may be changed in another script triggered for example by URL API request.
Example of a periodical script which can be paused by second script. Second script should be triggered by URL API request. Main script is then paused by: http://<NETIO_IP>/event?globalVariable=0 and resumed by http://<NETIO_IP>/event?globalVariable=1.
Main script:
local doPeriod = 10 -- repeat period in seconds globalVariable = true -- initialization of global variable ------------------------------------------------- function doPeriodically() if globalVariable == true then -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code -- your periodical code end delay(doPeriod,function() doPeriodically() end) end doPeriodically()
Controling script (trigger: Incoming URL API request):
if event.args.globalVariable == "0" then globalVariable = false elseif event.args.globalVariable == "1" then globalVariable = true end