sPhone plugin modification

Hi,

I have never programmed lua nor have I ever done anything with html, js or upnp. So please be kind and forgive my ignorance if I made any obvoius mistakes.

During the last 2 hours, I have modified the L_sPhone.lua plugin to show the state of a motion sensor which is missing in the actual implementation, amongst other things.

For installation you only have to load the L_sPhoneUI.lua in the devices/luup files and check restart Luup.(I had to zip the file because uploading lua isn’t allowed.)

After that you have the Tripped state of a motion sensor on your smartphone.

Note:
I used the imagefiles that where already available in the cmh/images directory so I didn’t have to modify any images in the flash-rom except letting Vera upload the new plugin. (Personally I would have preferred putting the images into the iPhone_file directory because all other images for the plugin reside there too.)

Cheers
Umtauscher

Congrats on implementing this! We had a similar discussion back here:

http://forum.micasaverde.com/index.php?topic=2159.0

but don’t think folks got to completing it. Note that any changes to this file will likely need to be redone as new Vera firmware versions are released, just a heads up.

I’ve also gone ahead and added [tt].lua[/tt] as an acceptable File upload type so it’ll be easier next time :wink:

it must have been the weekend for polishing the smartphone interface.

i made a very similar change so that the red or green circle gif would show the status of my door sensors. however, i didn’t make the tripped indicator (the red or green circle) a link. would there be a reason to do this? i didn’t because it is already hard enough to click the right link on my Storm2, so i figured a third link in that area would make it more difficult.

just wondering if I missed something when i made my modifications.

@umtauscher, thanks for the fix!

BTW, here is the modified version to properly handle dedicated Temperature and Brightness sensors - separate or those found in 3-in-1…

Thanks a lot. I was coming to that… :wink:

Cheers
Umtauscher

Nice !

How do you request the sPhone plugin?
The old one isn’t working anymore (http://192.168.81.1/port_49451/data_request?id=lr_sPhone)

There has nothing changed. Just Install the plugin as I described in my initial post and you are done.

thanx, it is working again :slight_smile:

Thanks for the fix.

I just added a pointer to this fix to bugs http://bugs.micasaverde.com/view.php?id=662 and http://bugs.micasaverde.com/view.php?id=731

Thanks for contributing to our Smarthphone Plugin. Our Lua developer will have a look at the fix and integrated in the default plugin.

yes, i’ve been reading the fix. one thing that would really make this kind of code easier is a simple templating function, to replace code like this:

	return html_header_action .. 
		'<div id="toolbar" class="toolbar"> <a href="data_request?id=lr_sPhone_room&room='
		.. luup.devices[tonumber(lul_device)].room_num .. 
		'&time=' .. tostring(os.clock()) ..
		 '"><img src="/cmh/iphone_file/images/backButton.gif" border="0"></a> </div>' ..
		'<div><img src="/cmh/iphone_file/buttons/wait.gif" alt="" border="0" class="icon" align="absmiddle"/> Running command now...<br>' ..
		'<a href="data_request?id=lr_sPhone_job&device=' .. lul_device .. '&room=' ..
		 luup.devices[tonumber(lul_device)].room_num .. '&job=' .. lul_job ..
 		'&time=' .. tostring(os.clock()) .. '">Check the status</a></div>' .. 
		html_footer

with something like this:

return template.replace([[
	%{header_action}%
	<div id="toolbar" class="toolbar">
		<a href="data_request?id=lr_sPhone_room&room=%{room_num}%&time=%{time}%">
			<img src="/cmh/iphone_file/images/backButton.gif" border="0">
		</a>
	</div>
	<div>
		<img src="/cmh/iphone_file/buttons/wait.gif" alt="" border="0" class="icon" align="absmiddle"/>
		Running command now...<br>
		<a href="data_request?id=lr_sPhone_job&device=%{lul_device}%&room=%{room_num}%&job=%{lul_job}%&time=%{time}%">
			Check the status
		</a>
	</div>
	%{footer}%
]], {
	header_action = html_header_action,
	time = tostring(os.clock()),
	lul_device = lul_device,
	room_num = luup.devices[tonumber(lul_device)].room_num,
	lul_job = lul_job,
	footer = html_footer
})

might seem a small thing, but it makes for a far more maintainable code. i’ll hack something tonight to have such a templating system available soon.

Yes and while he’s at it, he could add an piece of code that also shows the humitdity sensor of the weather plugin.
Thanks

Umtauscher

BTW, does anybody know how to suppress hidden rooms in der sPhone plugin?
My “Dead Nodes Room” still gets listed on the main screen on my iPhone.

Thanks

Umtauscher

A device with device number x is hidden, if luup.devices[ x ].hidden ist true.

There is no corresponding attribute ‘hidden’ for luup.rooms[]
(at least according to http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#variable:_rooms),
so I am afraid you will have to parse
http://myip:49451/data_request?id=user_data&output_format=xml

I just added a feature request for consistent Luup Lua extensions (bug #892).

Well, finally got some time to check this plugin…

First of all, I see the stream camera has been removed. Any reason for that?

Then, I did two new versions. The first one has only slight changes:

[ul][li]added some missing [font=courier]local[/font] kewords.[/li]
[li]wrapped the whole code in a [font=courier]do … end[/font] block, that allows for better encapsulation. For example, [font=courier]html_header[/font] is a local variable for the whole block, almost like a global but isolated from globals from other plugins (which run on the same Lua context).[/li]
[li]Used ‘long string’ syntax ([font=courier][[ … ]][/font])where appropriate.[/li][/ul]

I’m attaching this version. Should be equivalent to the last contributed one, please test and comment.

… And here’s the other version I did. This one has a bigger restructuring, and is so far incomplete. I’m attaching it mostly to show where i’m going in terms of Lua code, check the first few functions to see this modifications:

[ul][li]Added a simple [font=courier]string:template(vars)[/font] function.[/li]
[li]Replaced the [font=courier]lul_html = lul_html … “…”[/font] with [font=courier]table.insert (out, "…')[/font], where ‘out’ is a table of strings that get concatenated at the very end ([font=courier]return table.concat (out)[/font]). This is more efficient both in terms of time and space. Especially noticeable on long loops that add content to the page.[/li][/ul]

As said, it’s still unfinished. i’m posting mostly to hear some feedback.

@javier,

Oh, how I wish we had a version control system at the moment to easily compare versions… :slight_smile:

Anyway, our changes were relatively simple. We tried to not change the code structure or its flow, just add the missing functionality with the limited intervention.

So, umtauscher used the version of the plugin available at that moment and just modified [tt]lug_sPhoneRequest_sensor()[/tt] function to add the sensor’s tripped state indicator in form of green/red dot. And I added two new functions - [tt]lug_sPhoneRequest_temp()[/tt] and [tt]lug_sPhoneRequest_bright()[/tt], as well as the necessary hooks into the [tt]lug_sPhoneRequest_room()[/tt] function where it checks for [tt]DEVICE_CATEGORY_*[/tt] values of the devices…

I see that you are now rewriting parts of the plugin to be more elegant and easy to read, which is always appreciated. I’m still not done reading through your changes yet, so I’ll provide my feedback later. Thanks.

Hello
do you have instruction how to use this plagins?
i’ve instaled it but i don’t know how to connect to it from my local network (i have problems with findvera.com on the scope on my isp and i use dynamic dns)
please provide for advantage guide of how to connect to it

Hi MrFicha,

try this:
http://192.168.1.4/port_49451/data_request?id=lr_sPhone_room&room=0&time=4.71&js=1

Just replace the ip-address (192.168.1.4) with the one of your Vera.
The sceenes are a bit messed up in the rooms now, but I really don’t mind any more. I use SQRemote instead.
Hope this helps

Umtauscher