Page 1 of 1
library of functions
Posted: Wednesday 23 November 2016 21:28
by manjh
After a very short period of toying with Blockly, I have been learning and playing with LUA scripts, and found they are very flexible.
But now that the number of scripts is growing, I find that I would like to put some frequently used sequences of LUA lines into a library of functions or macro's, whichever is the better word for it. I would want to be able to call these functions from any LUA script.
Is this possible, and if so: how?
Re: library of functions
Posted: Wednesday 23 November 2016 21:34
by trixwood
Re: library of functions
Posted: Wednesday 23 November 2016 21:37
by stlaha2007
For python there's import,
Also LUA has function libraries. Take a look at
http://www.lua.org/manual
Section 5.2 talks about how to call them.
Downside is you need to program these in C etcetera.
Re: library of functions
Posted: Thursday 24 November 2016 15:21
by simonrg
You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at
https://www.tutorialspoint.com/lua/lua_modules.htm.
You can equally make it really simple by just reading in a file using dofile -
http://luatut.com/dofile.html
Just put your code in a file and then:
Code: Select all
dofile("/home/pi/domoticz/scripts/lua/mylibraryfile.lua")
If you learn how to use Lua in this way then this will be transferable to any other uses of Lua.
The alterantive is to use a framework, which you will find several mentioned in the forum, but they may be too complicated for what you need and any learning is only relevant to that framework with Domoticz.
Re: RE: Re: library of functions
Posted: Thursday 24 November 2016 15:54
by stlaha2007
simonrg wrote:You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at
https://www.tutorialspoint.com/lua/lua_modules.htm.
Wasn't aware of this. As i only read briefly my quoted site.
Used to use libraries in perl/python/bash and couldn't imagion it wasn't available in lua.
Re: library of functions
Posted: Saturday 26 November 2016 12:27
by manjh
simonrg wrote:You don't need to program in C or use Python, Lua has really clever ways of incorporating common code using modules with a function called require. This can become quite sophisiticated allowing you to have flexible ways of keeping specific functions together, have a look at
https://www.tutorialspoint.com/lua/lua_modules.htm.
You can equally make it really simple by just reading in a file using dofile -
http://luatut.com/dofile.html
Just put your code in a file and then:
Code: Select all
dofile("\home\pi\domoticz\scripts\lua\mylibraryfile.lua")
If you learn how to use Lua in this way then this will be transferable to any other uses of Lua.
The alterantive is to use a framework, which you will find several mentioned in the forum, but they may be too complicated for what you need and any learning is only relevant to that framework with Domoticz.
I suspect this will not work for me: I use a Windows-10 based server to run Domoticz...

Re: library of functions
Posted: Saturday 26 November 2016 18:14
by simonrg
manjh wrote:I suspect this will not work for me: I use a Windows-10 based server to run Domoticz...

Windows shouldn't make any difference, you just need to put the paths into the right format.
As I don't use Windows for Domoticz, I just Googled lua dofile windows (other search engines are available), found things like
http://stackoverflow.com/questions/2234 ... in-windows.
So either put the file in the same directory as Domoticz is executing Lua (which could be the directory you put the script_device_something.lua files)
Or this may work
Code: Select all
dofile("c:\\users\\pi\\domoticz\\scripts\\lua\\mylibraryfile.lua")
Or according to stackoverflow this may also work
Code: Select all
dofile([[c:\users\pi\domoticz\scripts\lua\mylibraryfile.lua")
Oops I did all the slashes Windows way round in the first email, for Linux it should be:
Code: Select all
dofile("/home/pi/domoticz/scripts/lua/mylibraryfile.lua")