2022-05-04 17:37:01 +00:00
|
|
|
local rtu = require("rtu")
|
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
|
|
|
|
local redstone_rtu = {}
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local digital_read = rsio.digital_read
|
|
|
|
local digital_is_active = rsio.digital_is_active
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
redstone_rtu.new = function ()
|
2022-02-08 20:42:06 +00:00
|
|
|
local self = {
|
2022-05-04 17:37:01 +00:00
|
|
|
rtu = rtu.init_unit()
|
2022-02-08 20:42:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local rtu_interface = function ()
|
|
|
|
return self.rtu
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local link_di = function (channel, side, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
local f_read = nil
|
|
|
|
|
|
|
|
if color then
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.testBundledInput(side, color))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.getInput(side))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-10 16:01:56 +00:00
|
|
|
|
2022-02-08 20:42:06 +00:00
|
|
|
self.rtu.connect_di(f_read)
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local link_do = function (channel, side, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
local f_read = nil
|
|
|
|
local f_write = nil
|
|
|
|
|
|
|
|
if color then
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(colors.test(rs.getBundledOutput(side), color))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
f_write = function (level)
|
2022-02-08 20:42:06 +00:00
|
|
|
local output = rs.getBundledOutput(side)
|
2022-03-23 19:36:14 +00:00
|
|
|
local active = digital_is_active(channel, level)
|
2022-02-08 20:42:06 +00:00
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
if active then
|
|
|
|
colors.combine(output, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
else
|
2022-03-23 19:36:14 +00:00
|
|
|
colors.subtract(output, color)
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
rs.setBundledOutput(side, output)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
f_read = function ()
|
2022-03-23 19:36:14 +00:00
|
|
|
return digital_read(rs.getOutput(side))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
f_write = function (level)
|
|
|
|
rs.setOutput(side, digital_is_active(channel, level))
|
2022-02-08 20:42:06 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-10 16:01:56 +00:00
|
|
|
|
2022-02-08 20:42:06 +00:00
|
|
|
self.rtu.connect_coil(f_read, f_write)
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local link_ai = function (channel, side)
|
2022-02-08 20:42:06 +00:00
|
|
|
self.rtu.connect_input_reg(
|
|
|
|
function ()
|
|
|
|
return rs.getAnalogInput(side)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-23 19:36:14 +00:00
|
|
|
local link_ao = function (channel, side)
|
2022-02-08 20:42:06 +00:00
|
|
|
self.rtu.connect_holding_reg(
|
|
|
|
function ()
|
|
|
|
return rs.getAnalogOutput(side)
|
|
|
|
end,
|
|
|
|
function (value)
|
|
|
|
rs.setAnalogOutput(side, value)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
rtu_interface = rtu_interface,
|
|
|
|
link_di = link_di,
|
|
|
|
link_do = link_do,
|
|
|
|
link_ai = link_ai,
|
|
|
|
link_ao = link_ao
|
|
|
|
}
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return redstone_rtu
|