28.10.2023, 05:14
Code:
-- socket connected
if connected then
-- Get level of the Volume control group in the Symetrix Control 127 application on the local network
value = GetCBusLevel(0, 127, 10)
-- Convert level to command
--command = 'cs 34' ..(value*257).. cr
command = 'CS 34 '..(value * 257).. '\r'
-- send command
sock:send (command)
-- read until one line received or error occured
while true do
char, err = sock:receive(1)
-- error while receiving, timeout or closed socket
if err then
-- remote server closed connection, reconnect
if err == 'closed' then
connected = false
alert('[tcp-sock] connection closed')
os.sleep(1)
end
break
-- end of line, parse buffer
elseif char == '\r' then
data = table.concat(buffer)
log(data)
buffer = {}
-- wait some time before next request
os.sleep(1)
break
-- other char, add to buffer
else
buffer[ #buffer + 1 ] = char
end
end
-- first call or previously disconnected
else
-- close previous connection when disconnected
if sock then
sock:close()
sock = nil
end
-- create tcp socket
sock = require('socket').tcp()
sock:settimeout(1)
connected, err = sock:connect('192.168.1.246', 48631)
-- connect ok, reset buffer
if connected then
alert('[tcp-sock] connection ok')
buffer = {}
-- error while connecting
else
alert('[tcp-sock] connection failed: %s', err)
os.sleep(5)
end
end
This script continues to send command at every 1 second interval is there any way i can change this so that it only sends socket command apon a new change of cbus level value. similar to an event script that waits for an event before acting?