Author Topic: open a listening socket to activate a scene  (Read 7362 times)

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
open a listening socket to activate a scene
« on: December 20, 2011, 10:10:42 am »
Is it possible to listen on a UDP socket and action a scene when a given string is received?
Or listen over a TCP/IP connection to action a scene?

How would I start?

Offline garrettwp

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6372
  • Karma: +227/-128
  • Vera 3, Lite, ISY994
Re: open a listening socket to activate a scene
« Reply #1 on: December 20, 2011, 12:28:23 pm »
Is there any reason why vera's api's will not do the job? It is a simple http request.

- Garrett

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #2 on: December 20, 2011, 01:17:37 pm »
I'm afraid I don't know if Vera's API will do this. Can you point me towards some documentation for listening asynchronously on a socket?

In my experience http requests are used from the client end of the connection; in this scenario Vera would be the server. And http is a TCP-based protocol, not UDP.

Maybe some elaboration would help: remote RFID tag sensor programmed to send the ID code for a tag as a UDP packet to a given port on the server (Vera) - I'd like that to activate a scene, depending on the ID string.



« Last Edit: December 20, 2011, 01:19:08 pm by autotoronto »

Offline garrettwp

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6372
  • Karma: +227/-128
  • Vera 3, Lite, ISY994
Re: open a listening socket to activate a scene
« Reply #3 on: December 20, 2011, 02:17:37 pm »
There are a few people running rfid setups with Vera. Do a search on the forum for more info.

- Garrett

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #4 on: December 20, 2011, 02:20:17 pm »
Thanks Garrett.

Yes, I'm aware of the RFID threads on this forum, and I've read all of them, in great depth. They don't use the method to connect to Vera that I'd like to use.

I'd really like some suggestions in how to open a socket listener, if that's possible?


Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3250
  • Karma: +190/-9
Re: open a listening socket to activate a scene
« Reply #5 on: December 20, 2011, 03:46:14 pm »
Socket listening will lead to blocked Lua threads, timeouts and probable Luup Engine resets.  You may have some luck with non-blocking UDP listens, but you could easily miss packets.

I don't think anyone's actually coded up a proof of concept, so the above paragraph may differ from reality.

TCP (using luup.io.open()) and serial won't suffer from this issue.

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #6 on: December 20, 2011, 05:52:49 pm »
Thanks.

UDP has an advantage over TCP in that multiple readers can send to the same socket.

Could one start a sever process in the underlying OS? On 'regular' linux you could do it in a bash script in maybe 5 lines. Triggering a scene would then just be referencing a url on localhost.

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3250
  • Karma: +190/-9
Re: open a listening socket to activate a scene
« Reply #7 on: December 21, 2011, 02:28:57 am »
Could one start a sever process in the underlying OS? On 'regular' linux you could do it in a bash script in maybe 5 lines. Triggering a scene would then just be referencing a url on localhost.

Yeah, definitely.  Probably your best choice of language to implement it in on the Vera is standalone Lua (since there's no heavyweight scripting language like Perl, and the netcat that ships on Vera doesn't have the -l option compiled in, making shell-scripting it hard).  Otherwise you would be reduced to cross-compiling a little C program for the Vera's architecture, which is somewhat messy.

If you're going to go to that much trouble, though, it's only one step further to move the daemon onto another machine where you have more headroom and choice of scripting languages.  Moving things onto an always-on server (if you have one) is generally the path of least resistance.

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #8 on: December 21, 2011, 09:40:01 pm »
Interesting. I'll look into the standalone Lua idea. Vera *is* my standalone server, so that seems the place to do it. (*)

I was looking into the Luup features surrounding networking but I didn't get very far. Why would opening a tcp socket be much different in terms of blocking and load than a UDP socket? I noticed ser2net installed too, that handles tcp and udp in a like manner.

(*) At least, I bought Vera to do stuff I otherwise would have done on a "server-under-the-stairs" because I have a dislike of having to have a pc running all the time to do low-level stuff like look after lighting. Hard to explain, but Vera feels more "dedicated".
« Last Edit: December 21, 2011, 09:41:49 pm by autotoronto »

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3250
  • Karma: +190/-9
open a listening socket to activate a scene
« Reply #9 on: December 22, 2011, 04:47:27 am »
Luup plugins really can't do either TCP or UDP listening, only serial. You can fake TCP *outgoing* with luup.io.open(), but you still only get the data-stream aspect of TCP, essentially the same as serial.

Using luasocket for native TCP or UDP, listening or connecting, is an option, and it might work most of the time, but when it fails it will fail spectacularly, probably by crash-restarting the Luup engine. Hence why standalone Lua is more desirable.

ser2net handles TCP but not UDP.
« Last Edit: December 22, 2011, 04:57:22 am by futzle »

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #10 on: December 22, 2011, 07:49:16 am »
Thanks - this is invaluable help.

How does a plugin do serial listening without having the same issues with blocking? What if I configure a UDP->serial process (either with ser2net directly or there's a discussion of how to add serial/TCP devices on this forum that might have some technique I could use), that means I could use luup.io.open()? I only need simplex communication (rx only) so there's no difficulty with having to specify different endpoints for different transmissions.

By the way, when I see "server busy" for 10-15 seconds, and/or hit the "reload" button top right on the web interface, is that the Luup engine restarting?




Offline guessed

  • Master Member
  • *******
  • Posts: 5293
  • Karma: +90/-22
  • Release compat is not a bolted-on afterthought
Re: open a listening socket to activate a scene
« Reply #11 on: December 22, 2011, 01:13:59 pm »
Everything serial effectively gets turned into local TCP via the existing ser2net process.  I've never looked to see if it can be turned to map (remote) UDP to local TCP or not.

Effectively the LuaUPnP process is a TCP client to those connections, so no listening is involved in this part of the mix, and I'm guessing a regular FD array based set of non-blocking calls can be done in the C part of LuaUPnP before its event dispatched to the per Device Lua queue...

LuaUPnP does listen for new TCP connections, but these are the fixed format HTTP ones for Luup based control of Vera itself... And presumably the real UPnP calls.

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #12 on: December 22, 2011, 02:58:58 pm »
ser2net will do a UDP-> serial map as easily as a TCP->serial map.

If Vera is a TCP client (via a serial port) then it opens a connection and polls for new data? That's not really any different to opening a UDP port and polling it (which would work fine.)

Turns out the device I have in mind can be configured to forward serial data in the payload of a GET request; so I'm thinking the easiest way to do this is to have a virtual device on Vera in which a state variable is set via a Vera UPnP url, that url being the GET request configured in the device with the data being the id of the last tag received. It should be possible to trigger a scene on the change of this variable (akin to switching a light on/off to trigger a scene.)



Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3250
  • Karma: +190/-9
open a listening socket to activate a scene
« Reply #13 on: December 22, 2011, 06:37:58 pm »
(One confusing factor is that there's more than one program out there called ser2net. The one on the Vera doesn't know about UDP. It works only with TCP. To bridge it to UDP you would need an additional program like netcat.)

Offline autotoronto

  • Full Member
  • ***
  • Posts: 190
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #14 on: December 22, 2011, 06:44:26 pm »
You are totally right. It was netcat I was thinking about; ser2net doesn't support UDP after all.