Author Topic: LuaTest - A Tool for Testing Scene Lua Code  (Read 26175 times)

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest - A Tool for Testing Scene Lua Code
« on: March 15, 2014, 06:26:57 pm »
LuaTest is a tool for editing and testing scene Lua code. It runs on Vera as an http request handler and provides several advantages over Test Luup code (Lua):
  • Allows code to be saved in files on Vera
  • Displays the return value from your code
  • Reports code errors directly
  • Indicates code execution time
  • Shows output from print statements
  • Provides values of local variables after a runtime error
  • Creates lists of variables and actions for your system
  • Includes option to display all variables for any device

To install LuaTest, upload the attached file RBLuaTest.lua using APPS -> Develop Apps -> Luup files -> Choose file -> GO and then restart Vera.

Enter the following code into APPS -> Develop Apps -> Test Luup code (Lua) and click GO:
Code: [Select]
local rblt = require("RBLuaTest")
rbLuaTest = rblt.rbLuaTest
luup.register_handler("rbLuaTest","LuaTest")

When used this way, the handler will work until Vera is restarted. The three lines can, alternatively, be added to Startup Lua for permanent availability.

To open a new or existing Lua file on Vera, enter:
Code: [Select]
<veraip>:3480/data_request?id=lr_LuaTest&file=<filename.lua>
Where <veraip> is the IP address of your Vera (without the <>). If <filename.lua> starts with a / it is assumed to be a full pathname otherwise it will be located in /etc/cmh-ludl/. If you do not provide a filename, the default luatest.lua will be used. If the file does not exist, it will be created when you save your code.

The following command will open LuaTest to edit the default file luatest.lua in the default folder /etc/cmh-ludl/:
Code: [Select]
<veraip>:3480/data_request?id=lr_LuaTest
Now enter your Lua code in the Code box. You can use simple copy/cut/paste commands by clicking the right mouse button.

You can use the buttons provided to create lists of the variables and actions for all the devices on your system. These are presented in a form that allows simple copy/paste into the luup.variable_get(...) and luup.call_action(...) calls in your code.

The Show Device Status button will open a new browser tab and display all variables for the specified Device Number. The values will be updated whenever the browser page is refreshed (usually F5 key). You may use the button to open pages for multiple devices.

When you have finished editing your code, click Save Code to create or update the file. Now you can click Test Code to have your code run as if it was in a scene. A new browser tab will open to show the results of the test.

Lua's print statements can be very useful when testing and debugging. With a few strategically placed in your code, you can quickly determine which sections are being executed and the value of key variables. You can leave the print statements in your code, if you wish, as Vera usually sends the output to a black hole.
 
There is now a LuaTest User Guide initiated by Tim Brien.

Edit: 18/03/2014 19:05  Version 1.1 Added edit and save functions.
Edit: 21/03/2014 11:24  Version 1.2 Minor tweaks and cleanup.
Edit: 22/03/2014 13:38  Version 1.2.1 Simplified runtime/code error messages.
Edit: 23/03/2014 14:08  Version 1.3 Added Device Variable List and Value buttons.
Edit: 24/03/2014 09:03  Version 1.4 Added Device Actions List.
Edit: 26/03/2014 09:39  Version 1.5 Added Device Status button.
Edit: 03/04/2014 13:16  Added link to User Guide.
Edit: 03/04/2014 14:26  Version 1.5.2 Allows use of pretty(...) function. See here.
Edit: 20/08/2014 09:08  Version 1.6 Fixed Device Actions List for UI7.
 
« Last Edit: August 20, 2014, 04:08:59 am by RexBeckett »

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest 1.1 - A Tool for Testing Scene Lua Code
« Reply #1 on: March 18, 2014, 03:10:00 pm »
LuaTest 1.1 now allows you to create, edit and save Lua code as well as providing a useful test facility. See the first post for the code and instructions.

 

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #2 on: March 21, 2014, 07:30:02 am »
LuaTest 1.2 uploaded. This is just some minor clean-up of the UI. If you modify the code, the Test Code button will not work until you have saved it. This helps to avoid the now why didn't that change fix the problem, problem.  ;)

See the first post for the code and instructions.

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 9820
  • Karma: +742/-136
    • RTS Services Plugins
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #3 on: March 21, 2014, 08:13:58 am »
Nice job ... do you trap/capture errors ?  That's what noobs need help with.
If you don't already you can wrap the code in pcall  and print the error.



Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #4 on: March 21, 2014, 08:46:47 am »
Nice job ... do you trap/capture errors ?  That's what noobs need help with.
If you don't already you can wrap the code in pcall  and print the error.

Thanks, Richard. Yes I am trapping errors using xpcall so I can use my own error-handler to dump the local variables from the stack.


Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #5 on: March 22, 2014, 05:13:07 am »
If any of the MCV forum admins are reading this, I like to propose this be made a pinned item on this board.
 
Anything that helps people to test lua/Luup code has to be a good thing, especially when the current way via the browser is so cumbersome.

I for one love it !! :)

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest 1.3 - A Tool for Testing Scene Lua Code
« Reply #6 on: March 23, 2014, 10:30:08 am »
LuaTest 1.3 uploaded. I have added a couple of additional buttons:

Device Variable List opens a new browser tab and produces a list of existing variables for each device on your Vera. The serviceId and variable name are formatted as required for luup.variable_get(...) calls so may be copy/pasted straight into your code. Incorrect serviceId and variable names are one of the most common reasons why scene code fails. This should help to avoid the usual pitfalls.

Note that, just because a variable exists, it doesn't mean it is useful. Some are rarely, if ever, updated by Vera and/or plugins.

Device Variable Values is as above but also includes the current value of the variables. This can be helpful to see what your code should receive from a luup.variable_get(...) call. I made this a separate button because some device variables can have very long values which can make the listing hard to follow.

It will take a few seconds for these lists to appear after you click the buttons. The code has to run through a fairly long list of data to extract the relevant items.

See the first post for the code and instructions.
« Last Edit: March 23, 2014, 10:57:08 am by RexBeckett »

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest 1.4 - A Tool for Testing Scene Lua Code
« Reply #7 on: March 24, 2014, 05:17:56 am »
LuaTest 1.4 uploaded. I have added a button:

Device Action List opens a new browser tab and produces a list of possible actions for each device on your Vera. The serviceId, action name and argument list are formatted as required for the luup.call_action(...) function so may be copy/pasted straight into your code. Using an incorrect serviceId is one of the most common reasons why scene code fails. This should help to avoid that.

Note that, just because an action exists, doesn't mean it will actually do anything. The list is produced by reference to Vera's invoke function. Not all devices support the full range of actions that Vera thinks that they should.  ;)

It will take a few seconds for this list to appear after you click the button. The code has to run through a fairly long list of data to extract the relevant items.

See the first post for the code and instructions.

Offline C4Vette

  • Full Member
  • ***
  • Posts: 157
  • Karma: +3/-8
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #8 on: March 26, 2014, 04:36:11 am »
@RexBeckett
Just to let you know how much I appreciate this tool, although I did not have the time yet to try it. This will definitely help me learn (a little) LUA/LUUP. I do have some ideas I want to try it for: SendData to a Danfoss LC13 to send it a schedule.

Thank you!
VeraLite UI7 1.7.649
Fan of: PLEG, DataYours, dutchSmartMeter, MySensors

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #9 on: March 26, 2014, 04:56:38 am »
Thanks @C4Vette. Let us know how you get on with it.

I recommend sprinkling print statements through your code so you can see which parts are working and which are not.

Code: [Select]
...
print("Initialisation done.")
...
print("Status is " .. status)
...

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest 1.5 - A Tool for Testing Scene Lua Code
« Reply #10 on: March 26, 2014, 05:56:52 am »
LuaTest 1.5 uploaded. I have added another button:

Show Device Status opens a new browser tab and shows the values of all the variables for the device specified in the Device Number field. The displayed values may be updated at any time by refreshing the browser page (usually the F5 key). You can use the button to open status pages for multiple devices.  You can also open a second page for the same device so you can compare before and after results.

To use this function directly (without needing to edit or run Lua code), enter the following in your browser:
Code: [Select]
<veraIP>:3480/data_request?id=lr_LuaTest&list=values&device=123
Replace <veraIP> with the IP address of your Vera and 123 with the number of the device to be displayed.

See the first post for the code and instructions.
« Last Edit: March 26, 2014, 06:05:15 am by RexBeckett »

Offline ilikelife

  • Full Member
  • ***
  • Posts: 129
  • Karma: +2/-0
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #11 on: March 28, 2014, 01:50:08 pm »
OK, I'm a noob, but this is amazing!  I agree with parkerc that it should be pinned, and i also think it should be make a permanent addition to Vera. (with a nice royalty paid to RexBeckett

I've been reading and playing and trying to code some things, but it's SO difficult to know what is going on (or what I'm doing wrong).  I've been able to accomplish things in other languages, but it helps to "see" my mistakes.  This tool allows that.  I spent most of yesterday trying things, and came back today to find a bunch of enhancements!

This is just to say THANK YOU to RexBeckett.


Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #12 on: April 03, 2014, 08:20:25 am »
There is now a LuaTest User Guide. Credit to @Brientim for much of the preparation.
 

Offline RexBeckett

  • Master Member
  • *******
  • Posts: 3888
  • Karma: +478/-10
LuaTest 1.5.2 - A Tool for Testing Scene Lua Code
« Reply #13 on: April 03, 2014, 09:04:43 am »
LuaTest 1.5.2 uploaded. This is a minor change to make LuaTest's pretty(...) function available to your code.

If you use Lua's print function on a table , it just shows that it is a table - which isn't much help. The function pretty(...) will expand the table and expose every element. It can be used with any type of variable but it does not expose the contents of a function, userdata or thread.

print(pretty(variable)) will print the contents of variable.
print(pretty(variable,"Name")) will print the contents of variable prefixed by Name=

See attached example.

See the first post for the code and instructions.
« Last Edit: April 04, 2014, 03:45:39 am by RexBeckett »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2366
  • Karma: +33/-45
  • Life Moves Pretty Fast....
    • Node Central
Re: LuaTest - A Tool for Testing Scene Lua Code
« Reply #14 on: May 05, 2014, 02:42:00 pm »
Hi @Rex

I'm using 1.5.2 and store my test files on my NAS (so I can update them via TextTastic on the iPad)

When I click on my test URLs (e.g.)  ..

http://192.168.1.234:3480/data_request?id=lr_LuaTest&file=/nas/luatest/test.lua

it brings up the LuaTest code window with the code populated very quickly, however when I click on the Test Code button, it can freezes for a while and then not return the results page? 

I'm doing some test but wanted to make you aware just in case there was something you could think of I could look for?
« Last Edit: August 01, 2017, 02:18:26 pm by parkerc »