Different behaviour for handlers

Hi akbooer,

I am trying to get my Harmony Hub plug in running on openLuup and it is sure not a trivial exercise. Besides having the same challenges as Multi Switch as I rewrite the json and D…xml files I was banging my head on getting the settings working.

What I found is that the way openLuup deals with handers is different. First the name of the passed request. Vera strips the ‘lr_’ from the lul_request parameter.

I also found that the hander runs not in the same code space as the plugin but on level 0 as you can see in this log. This seems to be causing issues with anything I try with a call_delay() to result in an unknow function error.

2016-02-09 23:09:22.608 luup.register_handler:14: global_function_name=HTTP_HarmonyInt, request=lr_hamGetActivities14 2016-02-09 23:09:22.609 luup.register_handler:14: global_function_name=HTTP_HarmonyInt, request=lr_hamGetDevices14 2016-02-09 23:09:22.609 luup.register_handler:14: global_function_name=HTTP_HarmonyInt, request=lr_hamGetDeviceCommands14 2016-02-09 23:09:22.610 luup_log:14: HARMM Harmony Control: Harmony_CreateChildren for device 2016-02-09 23:09:31.464 openLuup.server:: /data_request/data_request?id=lr_hamGetActivities14&serviceId=urn%3Arboer-com%3AserviceId%3AHarmony1&DeviceNum=14&timestamp=1455055770933&HID=&output_format=json tcp{client}: 0x6ea470 2016-02-09 23:09:31.466 luup.variable_set:0: 14.urn:rboer-com:serviceId:Harmony1.IconSet was: 0 now: 2 #hooks:0 2016-02-09 23:09:31.467 luup_log:0: HARMM Harmony Control: request is: lr_hamGetActivities14 2016-02-09 23:09:31.467 luup_log:0: HARMM Harmony Control: outputformat is: json

Can you make this behave more like Vera? Or am I doing something wrong that Vera accepts, but is bad coding?

Cheers Rene

You mean in the first parameter of the callback handler? I’ll check it out. I only ever use a one-to-one mapping of requests and handlers, so I’ve not run across that myself. Easily fixed.

I also found that the hander runs not in the same code space as the plugin but on level 0 as you can see in this log.

Yes, a known issue (to me) which doesn’t usually cause problems. I’ll work harder on fixing it.

This seems to be causing issues with anything I try with a call_delay() to result in an unknow function error.

You use call_delay in callback handlers ?!! :o

Can you make this behave more like Vera?

Yes, assuredly. But I can only fix one error at a time… did the attribute fix work OK?

You mean in the first parameter of the callback handler? I’ll check it out. I only ever use a one-to-one mapping of requests and handlers, so I’ve not run across that myself. Easily fixed.[/quote]
Correct, first parameter. I prefer having as little entry point into my code as possible. All personal coding preferences I suppose.

[quote=“akbooer, post:2, topic:190983”]

I also found that the hander runs not in the same code space as the plugin but on level 0 as you can see in this log.

Yes, a known issue (to me) which doesn’t usually cause problems. I’ll work harder on fixing it.

This seems to be causing issues with anything I try with a call_delay() to result in an unknow function error.

You use call_delay in callback handlers ?!! :o[/quote]
Yes as I run an http request to the Harmony Hub from the handlers I use those to not lockup the tread for too long. I have worked around it for openLuup as that seems less sensitive to it and much faster in handling the resonses.

[quote=“akbooer, post:2, topic:190983”]

Can you make this behave more like Vera?

Yes, assuredly. But I can only fix one error at a time… did the attribute fix work OK?[/quote]
Great. And yes that seems to be working now.

The GitHub development branch openLuup/openLuup at development · akbooer/openLuup · GitHub has a fix for both the callback context and request parameter name.

I’ve tested with a luup.call_delay() in the handler and it seems to work fine now. Thanks for the encouragement to fix this at last!

If you say it’s OK for you, then I’ll merge with the master branch.

Hi akbooer,

Works like a charm. Thanks for the quick reply.

Cheers Rene

Excellent!

Did you mention a while ago that you had a similar issue with files loaded by [tt]require[/tt] ?

Nope, not me. Just got started with a Pi last Saturday. ;D

Cheers Rene

@reneboer does this means Harmony plugin is able to run on your openluup setup? :o

Almost. Only triggers I cannot get working yet. I will let you know.

Cheers Rene

Be aware that openLuup does not implement Vera triggers or notifications, but relies on AltUI device watches to provide similar (actually better) functionality. From a programming point of view, however, this means that JSON-encoded definitions of scenes added through an HTTP request will simply not have their triggers stored. You may, instead, do what AltUI does and use luup.variable_watch() to construct your own triggers.

Be aware that openLuup does not implement Vera triggers or notifications, but relies on AltUI device watches to provide similar (actually better) functionality. From a programming point of view, however, this means that JSON-encoded definitions of scenes added through an HTTP request will simply not have their triggers stored. You may, instead, do what AltUI does and use luup.variable_watch() to construct your own triggers.[/quote]
Aha, ok I’ll have a look at that then and add it to the documentation.

Cheers Rene

[quote=“akbooer, post:6, topic:190983”]Excellent!

Did you mention a while ago that you had a similar issue with files loaded by [tt]require[/tt] ?[/quote]

It was me :slight_smile:

yes, thought so, and looked for the comment, but couldn’t find.

Can you elaborate?

I think it’s here : http://forum.micasaverde.com/index.php/topic,35656.msg263642.html#msg263642

The code loaded by “require” has it own global environment (just string, table and some other objects that are shared).
If you defined a global variable in the startup of your plugin, it won’t be available in the loaded code.

implementation file :

<?xml version="1.0"?>
<implementation>
	<functions>
		function startup (lul_device)
			myLib = require("L_myLib1")
		end
	</functions>
	<startup>startup</startup>

L_myLib1.lua

module("L_myLib1", package.seeall)

require("L_myLib2")

L_myLib2.lua

module("L_myLib2", package.seeall)

-- In openLuup, the module myLib1 loaded during the startup sequence is in a dedicated environment.
-- In legacy Vera, the global variables defined in stratup sequence are global everywhere
local myLib = L_myLib1 or myLib

I don’t know if it’s normal (it seems to be the default behaviour of require) but in Vera, it’s possible.
I have found a workaround with “L_PluginName”

For me, it doesn’t seem like a good plan to have modules accessing the global environment. Of course, I invariably do use “package.seeall” but only to access the usual Lua system modules and the Luup environment.

I assume that ‘require’ uses somewhere ‘loadstring’ and that is not, by default, using the caller’s context in openLuup. I may see if I can fix this.

You should be aware, however, that in openLuup, the ‘module’ call is a no-op, and only there for compatibility with Vera code. Outside of the Vera environment, the ‘module’ keyword is now deprecated (in versions of Lua beyond 5.1) , since it’s really not needed.