28.10.2023, 21:03
(28.10.2023, 08:25)Erwin van der Zwart Wrote: Just add a if condition like this:I've decided to go to an event script but cant get this to work.
But if the value is not change often i would make it event based, initiate the connection, write the command and close the connection..Code:if previousvalue ~= value then
-- send command
sock:send (command)
previousvalue == value
end
Event script by Keyword: Symetrix.
all GA's with keyword "Symetrix" also have there control comand number assigned to them by keyword " SYM=### " I'm trying to get the script to insert the control number into the "CS" Command Send line .
I'm getting a log of
* string: Error: sym=address tag missing
Code:
local value = event.getvalue()
local dest = event.dst
local grps = GetCBusByKW('Symtrix', 'or')
local k, v, alias, net, app, group
local addr = ''
local buffer = {}
log(grps)
for k, v in pairs(grps) do
alias = table.concat(v.address, '/')
if alias == dest then
local tags = v.keywords
--log(tags)
local t
for _, t in ipairs(tags) do
local tp = string.split(t, '=')
tp[1] = trim(tp[1])
if tp[2] then
tp[2] = trim(tp[2])
if tp[1] == 'sym' then
addr = tp[2] -- Get device address
break
end
end
end
break
end
end
if addr ~= '' then
-- create tcp socket
local sock = require('socket').tcp()
sock:settimeout(1)
local connected, err = sock:connect('192.168.1.246', 48631)
-- connect ok
if connected then
alert('[tcp-sock] connection ok')
-- error while connecting
else
log('[tcp-sock] connection failed: '..err)
sock:close()
do return end
end
local command = 'CS '..addr..' '..(value * 257).. '\r'
-- send command
sock:send (command)
local sent = socket.gettime()
local timeout = 5
while true do
char, err = sock:receive(1)
-- error while receiving, timeout or closed socket
if err then
-- remote server closed connection
log('[tcp-sock] connection closed'..err)
break
-- end of line, parse buffer
elseif char == '\r' then
local data = table.concat(buffer)
log(data)
break
-- other char, add to buffer
else
buffer[ #buffer + 1 ] = char
end
if socket.gettime() - sent > 5 then
log('Timeout receiving data')
break
end
end
sock:close()
else
log('Error: sym=address tag missing')
end