Author Topic: Scene running a xxx.lua file on mapped drive. ? (Lua Test)  (Read 225 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« on: August 06, 2017, 02:03:47 pm »
Hi

Hopefully a quick one for someone to answer ..

I have a mapped drive (CIFS) set up on my Vera and I use the great Lua Test tool a lot to try out bits of code I'm learning etc via dedicated xxxxx.lua files.

My question is - is it possible to have vera execute (e.g via a scene) the code that is held in a .lua file/document ?

How would you do that ?
Would this be an os.execute command ?
« Last Edit: August 06, 2017, 02:13:09 pm by parkerc »

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #1 on: August 06, 2017, 03:01:58 pm »
is it possible to have vera execute (e.g via a scene) the code that is held in a .lua file/document ?

Yes, it is.

Quote
Would this be an os.execute command ?

No, it wouldn't.

Quote
How would you do that ?

It slightly depends on what type of code this is.  Would it need to be run in the Vera context (using luup.xxx calls) or is it otherwise stand-alone.

If this is really a vanilla Lua question, then take a look at: Lua dofile

For a more readable introduction (and reference) to the Lua language, see here: Progamming in Lua
« Last Edit: August 06, 2017, 03:03:48 pm by akbooer »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #2 on: August 06, 2017, 03:37:44 pm »
Hi @akbooer

Thanks - as it has luup calls in it, so what would need to be done differently for that ?

Vera file location (used for Lua Test) = /nas/luatest/bedtime-countdown2.lua

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #3 on: August 06, 2017, 04:15:56 pm »
as it has luup calls in it, so what would need to be done differently for that ?

You'd probably need to read the file into a string, use loadstring(), instead of dofile(), and use setfenv() to set the environment to the scene's _G variable, before calling it. 

I don't know for sure, since I've never tried in the context of Vera itself, but this is generally the way to approach it.

The openLuup loader does much the same for .lua files.  You can see the relevant function compile_lua() here: https://github.com/akbooer/openLuup/blob/master/openLuup/loader.lua#L382

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #4 on: August 06, 2017, 04:23:33 pm »
If the file is not changed dynamically between invocations, you could, instead, simply use require() to load it as a regular module (which actually does similar to the previous list of functions I gave.)

What's not clear is your use-case, ie. WHY the file is external and what it does.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #5 on: August 06, 2017, 04:37:06 pm »
Lots to look at there.

The file is external so it's something that I can then work on from multiple places / devices etc. and I can make changes on the fly (there is a lot of text (TTS) in this first one) which would not require any access to or reload of Vera

As it is a mapped drive - i'm just thinking out loud but is this the right file io.open and read command to use ?

Code: [Select]
function readAll(file)
    local f = io.open(file, "rb")
    local content = f:read("*all")
    f:close()
    return content
end

readAll(/nas/luatest/bedroom-countdown.lua)
« Last Edit: August 07, 2017, 02:17:55 am by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #6 on: August 06, 2017, 05:44:40 pm »
At last some progress..

After some googling this seems to take me a little further as the code within the lua file is executed this time.

Code: [Select]
function readAll(file)
    local f = io.open(file, "rb")
    local content = f:read("*all")
    f:close()
    return content
end

script = readAll("/nas/luatest/polly_kitchen.lua")
print (readAll("/nas/luatest/polly_kitchen.lua"))
test = loadstring(script)
test()

I have also added the above to a scene (in the luup section) and while it seems to work - it takes a while to run compared to running it via Lua Test?  Any ideas why it takes longer ??
« Last Edit: August 06, 2017, 06:30:20 pm by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #7 on: August 06, 2017, 06:43:58 pm »
Also I should add

@akbooer if you are reading this still  I did not understand what you meant by the following - therefore i was not able to include it into the script inposted just earlier .

Whats does that mean / do ?
Where do I need to put it ?

... use setfenv() to set the environment to the scene's _G variable, before calling it. 
« Last Edit: August 07, 2017, 02:14:49 am by parkerc »

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #8 on: August 07, 2017, 03:03:24 am »
Depending on the context, you may not need this.  An example of how/where it's used is in the code I pointed to.  It's called before the invocation of the load function.  If your test code works, then forget it.

Yes, I thought you would be changing it on the fly.  It is also possible to do this with the require statement, but you have to explicitly clear the loaded module before using require again.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2330
  • Karma: +32/-44
  • Life Moves Pretty Fast....
    • Node Central
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #9 on: August 07, 2017, 03:36:43 am »
Out of interest how would you use setfenv() if you needed it? Is it used instead of loadstring()

Also.

.... I thought you would be changing it on the fly.  It is also possible to do this with the require statement, but you have to explicitly clear the loaded module before using require again.

If I recall correctly don't the require statements get added into the lua start up process ?

I'm probably getting way a head of myself now - but if that so -  does that mean I could 'load' various lua files at start up which can be reference working a scene (I assume they are loaded into memory)?

An then within a scene before it runs somehow check if it's the latest version e.g have the scene check the last modified date of the file with the last start up time ? ?

This suggests there is a third party library to do that ?

https://stackoverflow.com/questions/33296834/how-can-i-get-last-modified-timestamp-in-lua

@akbooer youre probably getting to know me a bit now (over the years)  - so I'm probably completely overreaching here :) but as I've got a load of Lua (luup) scripts on my NAS which has its folder mapped to Vera - it'll be interesting to be able to pre-load , update and execute externally hosted scripts at the touch of a button. 

(I used Textastic on my iPad/iPhone  for much of my coding now and that maps to the same share Vera does )
« Last Edit: August 07, 2017, 03:42:34 am by parkerc »

Online akbooer

  • Master Member
  • *******
  • Posts: 5131
  • Karma: +221/-67
  • "Less is more"
Re: Scene running a xxx.lua file on mapped drive. ? (Lua Test)
« Reply #10 on: August 07, 2017, 07:06:01 am »
Out of interest how would you use setfenv() if you needed it? Is it used instead of loadstring()

The code example I pointed to on GitHub shows exactly how this is done.  The other Lua documentation explains in more detail.  In a nutshell, it allows you to construct a 'sandbox' which completely defines the global environment (ie. other variables and functions) available to it.  It's actually nothing to do with loadstring() and can be used quite separately.

Quote
If I recall correctly don't the require statements get added into the lua start up process ?

No, they can be used anywhere.  If the module has already been loaded, it will not be loaded again (once again, unless you explicitly purge it from the list of loaded modules.)  I really do encourage you to look more closely at the Lua documentation to understand how these things really work.

Quote
does that mean I could 'load' various lua files at start up which can be reference working a scene (I assume they are loaded into memory)?

Absolutely.  That's a really good way of doing so.  I load a large number of modules in this way so that I can use them in scenes.

Quote
An then within a scene before it runs somehow check if it's the latest version e.g have the scene check the last modified date of the file with the last start up time ? ?

This suggests there is a third party library to do that ?

Well, that would be slightly clunky, but one way of doing it.  You might have a separate scene which check every 10 minutes or so, ... or something else.  Your individual scene code needs to be entirely focused on the logic of the scene, not extraneous mechanisms to do with the system.

The luafilesystem, documented here https://keplerproject.github.io/luafilesystem/manual.html, can get all sorts of file attributes included the last modified date. It's on the standard Vera system and can be loaded with:

Code: [Select]
local lfs = require "lfs"

lfs.attributes() is the call you need.

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.