RTU startup code and comms
This commit is contained in:
117
rtu/startup.lua
117
rtu/startup.lua
@@ -19,6 +19,13 @@ local RTU_VERSION = "alpha-v0.1.0"
|
||||
|
||||
local print_ts = util.print_ts
|
||||
|
||||
----------------------------------------
|
||||
-- startup
|
||||
----------------------------------------
|
||||
|
||||
local units = {}
|
||||
local linked = false
|
||||
|
||||
-- mount connected devices
|
||||
ppm.mount_all()
|
||||
|
||||
@@ -36,8 +43,68 @@ end
|
||||
|
||||
local rtu_comms = comms.rtu_comms(config.REACTOR_ID, modem, config.LISTEN_PORT, config.SERVER_PORT, reactor)
|
||||
|
||||
----------------------------------------
|
||||
-- determine configuration
|
||||
local units = {}
|
||||
----------------------------------------
|
||||
|
||||
-- redstone interfaces
|
||||
for reactor_idx = 1, #RTU_REDSTONE do
|
||||
local rs_rtu = redstone_rtu()
|
||||
local io_table = RTU_REDSTONE[reactor_idx].io
|
||||
|
||||
local capabilities = {}
|
||||
|
||||
for i = 1, #io_table do
|
||||
local valid = false
|
||||
local config = io_table[i]
|
||||
|
||||
-- verify configuration
|
||||
if is_valid_channel(config.channel) and is_valid_side(config.side) then
|
||||
if config.bundled_color then
|
||||
valid = is_color(config.bundled_color)
|
||||
else
|
||||
valid = true
|
||||
end
|
||||
end
|
||||
|
||||
if ~valid then
|
||||
local message = "invalid redstone configuration at index " .. i
|
||||
print_ts(message .. "\n")
|
||||
log._warning(message)
|
||||
else
|
||||
-- link redstone in RTU
|
||||
local mode = rsio.get_io_mode(config.channel)
|
||||
if mode == rsio.IO_MODE.DIGITAL_IN then
|
||||
rs_rtu.link_di(config.channel, config.side, config.bundled_color)
|
||||
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
|
||||
rs_rtu.link_do(config.channel, config.side, config.bundled_color)
|
||||
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
||||
rs_rtu.link_ai(config.channel, config.side)
|
||||
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
||||
rs_rtu.link_ao(config.channel, config.side)
|
||||
else
|
||||
-- should be unreachable code, we already validated channels
|
||||
log._error("fell through if chain attempting to identify IO mode", true)
|
||||
break
|
||||
end
|
||||
|
||||
table.insert(capabilities, config.channel)
|
||||
|
||||
log._debug("startup> linked redstone " .. #capabilities .. ": " .. rsio.to_string(config.channel) .. " (" .. config.side ..
|
||||
") for reactor " .. RTU_REDSTONE[reactor_idx].for_reactor)
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(units, {
|
||||
name = "redstone_io",
|
||||
type = "redstone",
|
||||
index = 1,
|
||||
reactor = RTU_REDSTONE[reactor_idx].for_reactor,
|
||||
device = capabilities, -- use device field for redstone channels
|
||||
rtu = rs_rtu,
|
||||
modbus_io = modbus_init(rs_rtu)
|
||||
})
|
||||
end
|
||||
|
||||
-- mounted peripherals
|
||||
for i = 1, #RTU_DEVICES do
|
||||
@@ -45,7 +112,7 @@ for i = 1, #RTU_DEVICES do
|
||||
|
||||
if device == nil then
|
||||
local message = "'" .. RTU_DEVICES[i].name .. "' not found"
|
||||
print_ts(message)
|
||||
print_ts(message .. "\n")
|
||||
log._warning(message)
|
||||
else
|
||||
local type = ppm.get_type(RTU_DEVICES[i].name)
|
||||
@@ -66,7 +133,7 @@ for i = 1, #RTU_DEVICES do
|
||||
rtu_iface = imatrix_rtu(device)
|
||||
else
|
||||
local message = "device '" .. RTU_DEVICES[i].name .. "' is not a known type (" .. type .. ")"
|
||||
print_ts(message)
|
||||
print_ts(message .. "\n")
|
||||
log._warning(message)
|
||||
end
|
||||
|
||||
@@ -77,16 +144,46 @@ for i = 1, #RTU_DEVICES do
|
||||
index = RTU_DEVICES[i].index,
|
||||
reactor = RTU_DEVICES[i].for_reactor,
|
||||
device = device,
|
||||
rtu = rtu_iface
|
||||
rtu = rtu_iface,
|
||||
modbus_io = modbus_init(rtu_iface)
|
||||
})
|
||||
|
||||
log._debug("startup> initialized RTU unit #" .. #units .. ": " .. RTU_DEVICES[i].name .. " (" .. rtu_type .. ") [" ..
|
||||
RTU_DEVICES[i].index .. "] for reactor " .. RTU_DEVICES[i].for_reactor)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- redstone devices
|
||||
for i = 1, #RTU_REDSTONE do
|
||||
----------------------------------------
|
||||
-- main loop
|
||||
----------------------------------------
|
||||
|
||||
-- advertisement/heartbeat clock (every 2 seconds)
|
||||
local loop_tick = os.startTimer(2)
|
||||
|
||||
-- event loop
|
||||
while true do
|
||||
local event, param1, param2, param3, param4, param5 = os.pullEventRaw()
|
||||
|
||||
if event == "peripheral_detach" then
|
||||
ppm.handle_unmount(param1)
|
||||
|
||||
-- todo: handle unit change
|
||||
elseif event == "timer" and param1 == loop_tick then
|
||||
-- period tick, if we are linked send heartbeat, if not send advertisement
|
||||
if linked then
|
||||
rtu_comms.send_heartbeat()
|
||||
else
|
||||
-- advertise units
|
||||
rtu_comms.send_advertisement(units)
|
||||
end
|
||||
elseif event == "modem_message" then
|
||||
-- got a packet
|
||||
|
||||
local packet = rtu_comms.parse_packet(p1, p2, p3, p4, p5)
|
||||
rtu_comms.handle_packet(packet)
|
||||
|
||||
elseif event == "terminate" then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- advertise units
|
||||
rtu_comms.send_advertisement(units)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user