Logic Machine Forum
Check connected tcp ports - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Check connected tcp ports (/showthread.php?tid=862)



Check connected tcp ports - buuuudzik - 22.06.2017

Hello

do somebody know:
- how can I check currently connected tcp ports?
- how can I reach the socket which is created and permanent used in resident script via event script?

The solution is necessary for Sony Bravia TV. I must have permanent tcp connection to have ability to read status when TV is off. So for this solution I've prepared the resident script 0s:
Code:
require('socket')

if not sock_bravia then
-- bravia IP
ip = '192.168.2.102'

sock_bravia = socket.tcp()
sock_bravia:settimeout(3)

res, err = sock_bravia:connect(ip, 20060)
end

-- power-status
cmd = '*SEPOWR################\n' -- Check status power

if res then
 res, err = sock_bravia:send(cmd)

 if res then
   res, err = sock_bravia:receive()

   if res then
     log('receive OK: ' .. tostring(res)) -- received ok
   else
     log('receive failed: ' .. tostring(err)) -- receive failed
   end
 else
   log('send failed: ' .. tostring(err)) -- send failed
 end
else
 log('connect failed: ' .. tostring(err)) -- connect failed
end

os.sleep(5)

But sometimes I want send command e.g. switch on but from event-based script. How can I reach currently port from other script?


RE: Check connected tcp ports - admin - 22.06.2017

Each script is a separate OS process, this means you cannot access other process resources directly. You can create a local UDP server in your resident script which to proxy data from event scripts: https://forum.logicmachine.net/showthread.php?tid=399&pid=2015#pid2015

You can get a list of currently open TCP client connections like this:
Code:
clients = io.readproc('netstat -ptn | grep ESTABLISHED')



RE: Check connected tcp ports - mlaudren - 23.06.2017

What about using storage to send command?
in you resident script you read a table in storage and send all element you read, and in your event script you write all your command

This way you have a FIFO and can send multiple command at a time:
On, switch to channel X, volume Y, ....


RE: Check connected tcp ports - buuuudzik - 23.06.2017

(23.06.2017, 12:27)mlaudren Wrote: What about using storage to send command?
in you resident script you read a table in storage and send all element you read, and in your event script you write all your command

This way you have a FIFO and can send multiple command at a time:
On, switch to channel X, volume Y, ....

I've tested in the past this solution but storage is not so fast as must be.


RE: Check connected tcp ports - mlaudren - 23.06.2017

(23.06.2017, 14:12)buuuudzik Wrote:
(23.06.2017, 12:27)mlaudren Wrote: What about using storage to send command?
in you resident script you read a table in storage and send all element you read, and in your event script you write all your command

This way you have a FIFO and can send multiple command at a time:
On, switch to channel X, volume Y, ....

I've tested in the past this solution but storage is not so fast as must be.

What speed/ time response do you target? I have some script that use storage and I didn't notice delay during the execution.
I'll do some test to see it that.


RE: Check connected tcp ports - buuuudzik - 23.06.2017

I don't know exactly but this was my subject when I used the storage for such solution:
https://forum.logicmachine.net/showthread.php?tid=399&highlight=somfy

In that case I've tried push button to e.g. rotate the slat step by step and Somfy was very slow(~0,6s for sending one command). So when I clicked very fast 3 times then I had a problem also with storage and e.g. Somfy did only 2 steps. I've tested this on LM4 on old processor so I think it could also had some influence. When I've used UDP server for this the problem gone and CPU usage go to minimum level from very high.


RE: Check connected tcp ports - Erwin van der Zwart - 23.06.2017

Storage is moved to redis DB so should be much faster then inside older FW.

I would retry your previous tests as i think it's fast enough now.

We used same kind of methode that mlaudren adviced in a TCP connection to Lutron and works pretty good.

BR,

Erwin


RE: Check connected tcp ports - admin - 23.06.2017

You still have race condition if you do get - change - set, no matter what kind of storage engine is used. You can use Redis list operations (lpush/lpop) which are atomic, but then you will have to poll for list changes where you can use receive with timeout when using UDP.


RE: Check connected tcp ports - rocfusion - 25.06.2017

Hi,

I have been using this, https://forum.logicmachine.net/showthread.php?tid=842

This way storage is not required to send something from the event based objects. Works well.

Thanks,

Roger