22.11.2017, 13:50
1. Create resident script to keep TCP connection open and handle reconnect, change IP as needed:
2. Add to common functions:
3. Call from event scripts:
Code:
if not udpserver then
require('socket')
tcphost, tcpport = '192.168.2.15', 4998
function tcpclose()
if tcpclient then
tcpclient:close()
tcpclient = nil
end
end
function tcpconnect()
local res, err
-- socket already open
if tcpclient then
return tcpclient
end
-- create new sock
tcpclient = socket.tcp()
-- create ok
if tcpclient then
tcpclient:settimeout(1)
res, err = tcpclient:connect(tcphost, tcpport)
-- connect ok
if res then
return tcpclient
-- connect failed
else
alert('connection failed ' .. tostring(err))
tcpclose()
end
else
alert('failed to create socket ' .. tostring(err))
end
end
function tcpsend(cmd, retry)
local sock = tcpconnect()
if sock then
local res, err = sock:send(cmd)
if res then
sock:receive()
end
-- send failed, retry once
if not res and not retry then
tcpclose()
tcpsend(cmd, true)
end
end
end
udpserver = socket.udp()
udpserver:setsockname('127.0.0.1', 4321)
udpserver:settimeout(1)
end
cmd = udpserver:receive()
if cmd then
tcpsend(cmd)
end
2. Add to common functions:
Code:
function irsend(cmd)
require('socket').udp():sendto(cmd, '127.0.0.1', 4321)
end
3. Call from event scripts:
Code:
irsend('sendir,1:1,1,37650,1,1,341,171,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,21,21,64,21,64,21,21,21,64,21,64,21,64,21,64,21,64,21,64,21,21,21,21,21,21,21,64,21,21,21,21,21,21,21,21,21,64,21,64,21,64,21,21,21,64,21,64,21,64,21,1520,342,85,21,3765\r')