Author Topic: Setting up your own LUA Code library.  (Read 5407 times)

Online RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9614
  • Karma: +733/-136
    • RTS Services Plugins
Setting up your own LUA Code library.
« on: August 01, 2015, 09:14:12 am »
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:

Code: [Select]
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:
Code: [Select]
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()

Offline otakup0pe

  • Sr. Newbie
  • *
  • Posts: 44
  • Karma: +3/-2
    • Jonathan Freedman
Re: Setting up your own LUA Code library.
« Reply #1 on: August 06, 2015, 12:46:46 am »
This is what I have been successfully doing for about eight months now.

With the most recent update I'm finding that my require'd LUA modules are now disappearing and now my lights don't turn on like they should and now I'm sad.

I realize the proper solution is to migrate my code to a proper plugin but I don't want my lights to not work in the mean time :(

Online RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9614
  • Karma: +733/-136
    • RTS Services Plugins
Re: Setting up your own LUA Code library.
« Reply #2 on: August 06, 2015, 09:55:28 am »
Updates sometime clear out what it thinks are unused files.
But this only happens when you intentionally select an upgrade.
Just keep a xcopu of your LUA file handy ... and re-install (UpLoad it) after each upgrade.

Offline otakup0pe

  • Sr. Newbie
  • *
  • Posts: 44
  • Karma: +3/-2
    • Jonathan Freedman
Re: Setting up your own LUA Code library.
« Reply #3 on: August 06, 2015, 12:01:48 pm »
This is just during normal operation. And the files are not disappearing.

My intuition is that the LUA VM sometimes crashes and doesn't always execute the "Startup LUA" every time. I've opened a support ticket.

Offline amg0

  • Sr. Hero Member
  • ******
  • Posts: 2514
  • Karma: +154/-8
Re: Setting up your own LUA Code library.
« Reply #4 on: August 06, 2015, 05:05:51 pm »
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:

Code: [Select]
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:
Code: [Select]
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()
Excellent , I never got that right so far, now I understand.

What about the Lua files in App Store. Does it make a difference to declare them of type LUA versus type MISC ? mCV wiki makes a note about this but I am no sure I fully understand. They say declaring them as LUA carries an extra overhead. .??

Online RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9614
  • Karma: +733/-136
    • RTS Services Plugins
Re: Setting up your own LUA Code library.
« Reply #5 on: August 07, 2015, 09:09:37 am »
I saw that as well ... all of my LUA files are marked as Misc Files.
It was not clear what the result of making them as an LUA file would be ... LUV the clear documentation.
 

Offline akbooer

  • Master Member
  • *******
  • Posts: 5182
  • Karma: +223/-67
  • "Less is more"
Re: Setting up your own LUA Code library.
« Reply #6 on: August 07, 2015, 09:56:14 am »
I took it to mean they are loaded into an entirely separate process and run independently... but who knows?
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.