Using lua modules in plugins, how?

I am trying to use a LUA module (namely: the dkjson.lua) module, but I cannot get it to load properly.

At the start of my code, I use: local json = require(“dkjson”), but this always results in errors resulting from an empty (null) variable.

From the comments from another plugin (RTCOA Wifi Thermostat) it seems that using modules from compressed files doesn’t work properly, so they uncompress the lua file before using it. At the moment my module is linking to the RTCOA json module (it’s the same dkjson module, only renamed), but I would sure like to know how to link to other modules without having to uncompress them. Especially as other plugins seem to get away with that, somehow.

Any thoughts?

On UI5 … just upload the file from: Apps → Develop Apps → Luup files
This will result in a compressed lua file on Vera.
Add a reference to this inside your plugin implementation:
I_XXXX.xml
dkjson.lua

If you are calling the from the Apps ->Develop Apps → Test Luup code
You may want to leave off the local. Otherwise the scope of the json variable will
only be valid for the duration of your test code evaluation.

Are you sure this does not reference files that have not been previously loaded ?

I believe the LUA loader in Vera will load compressed or uncompressed files … which ever it finds … but usually they are compressed.

Adding the section results in an error on loading the implementation xml. Not a very specific error:
LuaInterface::LoadCode: [string “…”]:1006: ‘’ expected near ‘function’ <0x400>

Welcome to the software development environment for Vera …

Check your XML carefully.
Try a dummy LUA File … to make sure it’s not causing the problem.

I develop my LUA files with an external LUA environment … then I only have to deal with integration issues on Vera.

The “require” style is probably more common, since it’s part of the language spec for Lua so it works everywhere.

The [tt][/tt] stuff is a UI5-ism, but it’ll force you remove the “[tt]module[/tt]” declaration (etc) in the .lua file… Later in UI5, we managed to convince them to simply “override” [tt]require[/tt] so it would do the necessary stuff to work with their compression (and in some cases encryption)

If you want examples, look at the stuff over on http://code.mios.com, there are a bunch of examples there that use Libraries (either packaged ones, already on Vera, or newly built ones as part of the plugin)

Personally, I avoid the [tt][/tt] declaration like the plague. Having a Library that can be tested “outside” of Vera is extremely handy (the Sonos XMPP library is developed like that)

Fixed it :slight_smile: Only required a certain amount of banging the desk with my head.

For the dkjson module specifically:

  • Move the Global - Local declarations to the top of the file and add module(“json”) underneath. Apparently this is necessary to uncompress the .lzo file.
  • Remove the json. prefix from the public functions
  • Include with local dkjson = require(“dkjson”)

Then call with json.decode or json.encode.

[quote=“RichardTSchaefer, post:4, topic:173288”]Welcome to the software development environment for Vera …

Check your XML carefully.
Try a dummy LUA File … to make sure it’s not causing the problem.

I develop my LUA files with an external LUA environment … then I only have to deal with integration issues on Vera.[/quote]

Okay, I am trying to build a plugin and it is going badly. What is the external Lua environment you are using?

https://code.google.com/p/luaforwindows/

[url=http://www.eclipse.org/koneki/ldt/]Archived Projects | The Eclipse Foundation