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

Offline simpl

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #15 on: March 29, 2012, 04:48:18 am »
I set up an UDP listener in the startup of my plugin. It works, but of course this is blocking the thread. Is it somehow possible to
1) put this in a co-routine and put the handle somewhere so that my listener gets woke up again after a coroutine.yield()?
2) set up a listener in <startup>, return and get the own <incoming> invoked when a UDP packet arrives?

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Re: open a listening socket to activate a scene
« Reply #16 on: March 29, 2012, 05:10:09 am »
Lua coroutines don't work that way. But yes, you can definitely make your own listener.

This might work: make a Luup plugin that listens on a UDP port, in a loop. Make sure it times out every so often. Act on a packet when you receive it. The risk is that you might miss a packet while you aren't listening. Perhaps the OS will handle this for you; no one has tried so no one knows.

This will definitely work: make a separate process that listens on a UDP port, and when it receives a packet, turn it into an http request to Localhost. Your plugin can then be invoked when that http request is received. The separate process makes this less automatic than a straight Luup plugin, so you'd be making more of your own infrastructure.

I don't have any code to help you get started, sorry.

Offline simpl

  • Newbie
  • *
  • Posts: 11
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #17 on: March 29, 2012, 07:37:31 am »
This will definitely work: make a separate process that listens on a UDP port, and when it receives a packet, turn it into an http request to Localhost.
Do you have a link where I can see how to make a new process. I thought that separate processes don't exist in thias environment.

Offline futzle

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3258
  • Karma: +191/-9
Re: open a listening socket to activate a scene
« Reply #18 on: March 30, 2012, 06:07:15 pm »
Do you have a link where I can see how to make a new process. I thought that separate processes don't exist in thias environment.

It's Unix, so you can (among other ways) run a shell script that runs a program in the background (&).
« Last Edit: March 30, 2012, 07:30:54 pm by oTi@ »

Offline racarter

  • Hero Member
  • *****
  • Posts: 518
  • Karma: +12/-3
Re: open a listening socket to activate a scene
« Reply #19 on: July 03, 2012, 11:53:37 am »
Did anybody ever get anywhere with the Lua listener code?  I have an application it would be perfect for.  :)
Indigo, Vera Edge (for Datamine only), Everspring and TKBHOME socket switches, SM103 door detectors, Homepro ZRP210 appliance modules, Fibaro relay switches, EZMotion, Heatmiser thermostats, Netatmo Weather, Camera and Healthy Home, Hikvision DVR/CCTV, Sonos, Texecom, Diax motorised deadlock.

Offline chrisbirkinshaw

  • Sr. Newbie
  • *
  • Posts: 23
  • Karma: +0/-0
Re: open a listening socket to activate a scene
« Reply #20 on: July 12, 2012, 08:15:40 pm »
I have a different application for this:

1. Open sockets on Vera (as server)
2. iRule iPhone remote client(s) connect to TCP socket
3. Custom device on Vera registers for status changes on lights, sensors etc
4. On change feedback information is sent to the iPhone clients over the TCP connection
5. Instant status update on the iPhone (no polling)

The only way I can see this working now is as follows:

1. Add USB to serial to Vera
2. Null modem cable from USB-serial to a serial to IP adapter
3. iPhones connect to serial to IP adapter via IP

Seems a crazy way to do things though!

Offline samyoue

  • Jr. Member
  • **
  • Posts: 65
  • Karma: +5/-5
Re: open a listening socket to activate a scene
« Reply #21 on: March 27, 2018, 09:53:57 am »
Lua coroutines don't work that way. But yes, you can definitely make your own listener.

This might work: make a Luup plugin that listens on a UDP port, in a loop. Make sure it times out every so often. Act on a packet when you receive it. The risk is that you might miss a packet while you aren't listening. Perhaps the OS will handle this for you; no one has tried so no one knows.

This will definitely work: make a separate process that listens on a UDP port, and when it receives a packet, turn it into an http request to Localhost. Your plugin can then be invoked when that http request is received. The separate process makes this less automatic than a straight Luup plugin, so you'd be making more of your own infrastructure.

I don't have any code to help you get started, sorry.
I know this is old but anyone get anywhere with this? if not, any advice on how I could get started creating a separate process etc would be much appreciated!!
I'm thinking maybe this as a plugin init code:
Code: [Select]
io.open(lul_device,'192.168.1.255',56700)
then having code in the <incoming> tags to deal with the data, would this work?
bit nervous to try io code as I tried before (doing something else) and killed my Vera :S

Sam
« Last Edit: March 27, 2018, 10:04:59 am by samyoue »

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6201
  • Karma: +276/-70
  • "Less is more"
Re: open a listening socket to activate a scene
« Reply #22 on: March 27, 2018, 11:04:16 am »
Congratulations on finding a relevant, if very old, thread!

The easiest way to implement a server on Vera is undoubtedly to use the luup.register_handler() routine.  This does, however, limit you to HTTP protocol with very specific syntax (up to the point where you can specify your own parameters.)  Simple and reliable.

However, I have several plugins which implement servers with non-blocking code.  Actually the LuaSocket library has several specific routines to support this.  Take a look at: http://w3.impa.br/~diego/software/luasocket/socket.html#select

All my plugin code is on GitHub.  EventWatcher and DataYours (in the DataDaemon.lua file) implement servers, as does the stand-alone Vera emulator openLuup (in the openLuup.server.lua and openLuup.scheduler.lua files.)

Doing what you suggest with luup.io.open() will not do what you want, and io.open() on its own is specifically for file I/O.

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.