Using separate .LUA files in XML implementation file

I am trying to expand on my initial “Hello World” example by adding separate ‘.lua’ files. After I add/update the files for some reason I keep getting the error “Lua Engine Failed to Load” after refreshing the unit. One thing I notice that I is kind of strange, whenever I change the ‘files’ xml node to point to a .lua file that doesn’t exist no errors are reported and the example will run. However, as would be expected, the target variable isn’t going to update to the “Hello World” string I’m wanting, again expected since file name is invalid. So when the file name is set correctly, Vera is recognizing it however it doesn’t like the contents for whatever reason. I’ve included code from the entire .LUA and implementation files. The .LUA file is very basic so I’m not sure what it doesn’t like about it.

I_HelloWorld1.xml

<?xml version="1.0"?>
<implementation>
   <files>L_HelloWorld1.lua</files>
   <actionList>
    <action>
      <serviceId>urn:upnp-org:serviceId:HelloWorld1</serviceId>
      <name>GetTarget</name>
      <run>
			local helloStr = GetHelloWorldString("I am requesting Hello World string")
			luup.variable_set("urn:upnp-org:serviceId:HelloWorld1", "Target", helloStr, lul_device)
			return 4, 5
      </run>
    </action>
   </actionList>
</implementation>

L_HelloWorld1.lua

function GetHelloWorldString(n)
  return "Hello World"
end

Here is my original post:
http://forum.micasaverde.com/index.php?topic=22915

is correct but please review what it should return:
http://wiki.micasaverde.com/index.php/Luup_Declarations#.3Crun.3E

The usage of urn:upnp-org:serviceId is really for that organization. You can invent your own - say:
urn:billv748:serviceId:HelloWorld1
Making sure it’s replaced appropriately in all the files.

While I haven’t checked, it may be compulsory to have a start up function:

[code]function myPluginStartUp(lul_device)

--  if I need some, all my great start up code goes here

-- on success
return true, 'My plugin', 'All OK'

end[/code]

And in your I_HelloWorld1.xml file underneath
L_HelloWorld1.lua
you would have
myPluginStartUp

http://wiki.micasaverde.com/index.php/Luup_Declarations#.3Cstartup.3E

Awesome! Adding the startup method did the trick.
And thanks for the explanation on service ids and returns.

I appear to have spoken too soon. That definitely did work for a little bit. Then I made a few changes and uploaded my new files and again saw the “Hello World Test[11] : Lua Engine Failed to Load” message. So I then I reverted my changes back to what worked earlier but now I still see the error. It works fine if I keep lua code in the xml implementation file. I’m really not sure what is going on. I got no idea why the same code that worked earlier is suddenly not. Any ideas or suggestions?

The code that I wish to write will contain a lot of “<” signs which create problems when used in the xml files. I know I can get around that problem by using “<” instead but to keep the code easier to read I’d like to avoid using that.

I went to the “Setup-Logs” screen hoping to find more details on the message I’m seeing “Hello World Test[11] : Lua Engine Failed to Load”. On my machine the log screen only looks like a log configuration screen. I don’t see any actual log data. I’ve even enabled “Verbose” messages and still nothing gets posted to that screen.

I’ve attached exact copies of the files that I am using.

You still have the “return 4, 5” in the I_HelloWorld1.xml file. Don’t forget a few Lupp engine restarts and browser refreshes to get everything working.

The Info Viewer plugin can display the log file(s) for you:

http://forum.micasaverde.com/index.php/topic,13477.0.html

The code that I wish to write will contain a lot of "<" signs which create problems when used in the xml files. I know I can get around that problem by using "<" instead but to keep the code easier to read I'd like to avoid using that.

You are taking the right approach to do this.

For info, I was having this problem and it might to be related to load order.

Putting my files declaration above the functions in my implementation and ensuring my startup function was the last code in the .lua file made it work.

None of that should matter and it may actually be that several uploads were needed to kick the thing into life, but it does work eventually.

The order of your functions does matter. You can not call a function from part of the code if that function has not already been defined prior.

  • Garrett

If you have cycles in your code. You can define the function names first:

local Func1
local Func2

Funct1 = function(a,b,c)
      ...
      Funct2(a, b, c)
      ...
end

Funct2 = function(a, b, c)
     ...
     Funct1(a, b, c)
     ...
end
The order of your functions does matter

Wow!

Nice to have that confirmed. I thought I was going mad

Well I’ve pinpointed the problem. Apparently I can’t close out the file on an “end” statement. I need to end the file on a line feed or something else. I added an extra line feed to the end of the file and it worked perfectly. Thanks for the help everyone. The support on here has been great. I really like the “Info Viewer” plugin. I used that to narrow down the problem. I kept seeing this message in the logs “‘end’ expected (to close ‘function’ at line 9) near ‘endfunction’” which helped me tremendously.
(I also attached the corrected lua file for any of those interested)

As a general rule, on Unix you are advised to end any text file with a newline. This is so ingrained in Unix circles that it sometimes goes unsaid.