10.05.2023, 08:24
This would lead to the possibility of many errors, can't it be done with a resident script that monitors states and processes events? I tried to write something similar, but apparently I don't understand the system's workings very well and it doesn't work for me.
Code:
local function object_exists(obj_address)
local obj = grp.find(obj_address)
return obj ~= nil
end
-- table with the addresses of function_one
local function_one = {}
for i = 1, 60 do
for j = 11, 241, 10 do
local function_one_address = i .. "/2/" .. j
if object_exists(function_one_address) then
function_one[(i - 1) * 24 + (j - 11) / 10 + 1] = function_one_address
end
end
end
-- table with the addresses of group_numbers
local group_numbers = {}
for i = 1, 60 do
for j = 11, 241, 10 do
local group_numbers_address = i .. "/3/" .. j
if object_exists(group_numbers_address) then
group_numbers[(i - 1) * 24 + (j - 11) / 10 + 1] = group_numbers_address
end
end
end
-- create client
client = require('localbus').new(0.1)
local function update_group_numbers_for_group(group_number)
local has_true_function_one = false
for j, function_one_item in ipairs(function_one) do
if tonumber(function_one_item:match("(%d+)/2/%d+")) == group_number then
if grp.getvalue(function_one_item) == 1 then
has_true_function_one = true
break
end
end
end
for j, group_number_item in ipairs(group_numbers) do
if tonumber(group_number_item:match("(%d+)/3/%d+")) == group_number then
local current_group_number_state = grp.getvalue(group_number_item)
if has_true_function_one and current_group_number_state == 1 then
grp.write(group_number_item, 0)
elseif not has_true_function_one and current_group_number_state == 0 then
grp.write(group_number_item, 1)
end
end
end
end
-- set function to handle events from group addresses
client:sethandler('groupwrite', function(event)
local address = event.dst
local value = tonumber(event.datahex, 16) or 0
-- check if the address is in the list of function_one addresses
for i, function_one_item in ipairs(function_one) do
if address == function_one_item then
local group_number = tonumber(function_one_item:match("(%d+)/2/%d+"))
update_group_numbers_for_group(group_number)
break
end
end
end)
-- start the client for data exchange on the local bus
client:loop(1)