Author Topic: How to create my own common set of functions?  (Read 4294 times)

Offline axill

  • Full Member
  • ***
  • Posts: 183
  • Karma: +3/-0
How to create my own common set of functions?
« on: May 18, 2011, 06:28:37 am »
For example I want to have function to send email which can be used from any scene but without necesarity to dublicate function definition in every scene

I've tried a search but it not able to find an answer

Offline sjolshagen

  • Sr. Member
  • ****
  • Posts: 416
  • Karma: +5/-0
Re: How to create my own common set of functions?
« Reply #1 on: May 18, 2011, 07:32:53 am »
I achieved that via the "Mios Developers", "Edit Startup LUA" & defined my function(s) there. I also decided to use that location as the place where I define labels for the device IDs (simplifies my luup in scenes further).

Do wish there could be a slightly cleaner way to achieve this and the device ID abstraction, but it works as is.
Vera3/UI5 @ v1.5.622

Offline axill

  • Full Member
  • ***
  • Posts: 183
  • Karma: +3/-0
Re: How to create my own common set of functions?
« Reply #2 on: May 18, 2011, 08:03:29 am »
Thank you for sharing expirience.

I also thought about using startup Lua but not sure because I saw no referencies from the forum to it.

Do I need to restart Lua somehow for changes to take effect?

Offline shady

  • Hero Member
  • *****
  • Posts: 1331
  • Karma: +11/-0
  • This monkey knows his Window Coverings!
Re: How to create my own common set of functions?
« Reply #3 on: May 18, 2011, 05:58:09 pm »
You probably need to restart the Luup Engine, by going to the Luup Files tab of Mios Developers and going to the bottom of that tab clicking the checkbox to restart after upload and click upload (without any files selected in the browse fields).  This will restart the Luup Engine.
Vera3 1.5.346, Schlage (3)DB (2)Lever, Kwikset (1)Lever, RCS TZ43 Thermo, (2) Vizia RZI06-1LX 600W Dimmers, (17) Monster (Leviton) Dimmers (6) Monster (Leviton) IWC Scene Controllers (1) Etherrain-8, (3) HSM 100's (1)GE 3-Way set

Offline axill

  • Full Member
  • ***
  • Posts: 183
  • Karma: +3/-0
Re: How to create my own common set of functions?
« Reply #4 on: May 19, 2011, 04:26:59 am »
You probably need to restart the Luup Engine, by going to the Luup Files tab of Mios Developers and going to the bottom of that tab clicking the checkbox to restart after upload and click upload (without any files selected in the browse fields).  This will restart the Luup Engine.
By my current practice with latest beta there is no need to restart Lua, just need to press Go button on "Edit Startup Lua" tab

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to create my own common set of functions?
« Reply #5 on: May 19, 2011, 11:12:55 pm »
You can also put your common stuff into a ".lua" file, like "shared.lua", and upload it via the standard MiOS developers UI, as you would with Plugin files.

Then, in your code, you place the following construct at the top:

    require("shared.lua")

and it'll load that library in, and it's functions will become available for you to use.  This will work "as-is" on the newest MiOS releases

On older MiOS releases, the "right" directories weren't setup, so uploaded .lua wouldn't be seen.  In this case, you'll likely have to add the explicit path, something along the lines of:
   
    require("/etc/cmh-ludl/shared.lua")

as the "/etc/cmh-ludl/" directory is where the UI uploads files to on the Vera box.  On earlier versions, if you didn't put in the full path information, you'd get an error in the Vera logs along the lines of:

01   xx/xx/11 xx:xx:xx.133   LuaInterface::StartEngine failed run: 0 [string "x = require("test1")"]:1: module 'test1' not found:
   no field package.preload['test1']
   no file './test1.lua'
   no file '/usr/share/lua/test1.lua'
   no file '/usr/share/lua/test1/init.lua'
   no file '/usr/lib/lua/test1.lua'
   no file '/usr/lib/lua/test1/init.lua'
   no file './test1.so'
   no file '/usr/lib/lua/test1.so'
   no file '/usr/lib/lua/loadall.so'
<0x2c0c>
01   xx/xx/xx xx:xx:xx.134   JobHandler_LuaUPnP::RunLua failed: x = require("test1") <0x2c0c>


For reference, the details of this are here:
    http://www.lua.org/pil/8.1.html

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: How to create my own common set of functions?
« Reply #6 on: May 20, 2011, 02:36:10 am »
You can also put your common stuff into a ".lua" file, like "shared.lua", and upload it via the standard MiOS developers UI, as you would with Plugin files.

Then, in your code, you place the following construct at the top:

    require("shared.lua")

and it'll load that library in, and it's functions will become available for you to use.  This will work "as-is" on the newest MiOS releases

On older MiOS releases, the "right" directories weren't setup, so uploaded .lua wouldn't be seen.  In this case, you'll likely have to add the explicit path, something along the lines of:
   
    require("/etc/cmh-ludl/shared.lua")

as the "/etc/cmh-ludl/" directory is where the UI uploads files to on the Vera box.  On earlier versions, if you didn't put in the full path information, you'd get an error in the Vera logs along the lines of:

01   xx/xx/11 xx:xx:xx.133   LuaInterface::StartEngine failed run: 0 [string "x = require("test1")"]:1: module 'test1' not found:
   no field package.preload['test1']
   no file './test1.lua'
   no file '/usr/share/lua/test1.lua'
   no file '/usr/share/lua/test1/init.lua'
   no file '/usr/lib/lua/test1.lua'
   no file '/usr/lib/lua/test1/init.lua'
   no file './test1.so'
   no file '/usr/lib/lua/test1.so'
   no file '/usr/lib/lua/loadall.so'
<0x2c0c>
01   xx/xx/xx xx:xx:xx.134   JobHandler_LuaUPnP::RunLua failed: x = require("test1") <0x2c0c>


For reference, the details of this are here:
    http://www.lua.org/pil/8.1.html

@guessed

Looks like a page for the How To Wiki?
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |

Offline guessed

  • Community Beta
  • Master Member
  • ******
  • Posts: 5301
  • Karma: +92/-22
  • Release compat is not a bolted-on afterthought
Re: How to create my own common set of functions?
« Reply #7 on: May 21, 2011, 04:28:28 pm »
@Henk,
Likely so, let's wait a little while though so that people have an opportunity to thrash it out in the Forums (test in various scenarios). 

I've only tested the technique with the "Test Lua" dialog, and I'm sure folks will find nuances and post-back here in the Forums.

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: How to create my own common set of functions?
« Reply #8 on: May 21, 2011, 04:57:55 pm »
@guessed
Agreed, let me know when you think its matured enough!
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |