#68 check RTU unit configs while parsing

This commit is contained in:
Mikayla Fischler 2022-06-05 14:49:50 -04:00
parent 81345f5325
commit d6c8eb4d56

View File

@ -25,7 +25,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu")
local turbine_rtu = require("rtu.dev.turbine_rtu") local turbine_rtu = require("rtu.dev.turbine_rtu")
local turbinev_rtu = require("rtu.dev.turbinev_rtu") local turbinev_rtu = require("rtu.dev.turbinev_rtu")
local RTU_VERSION = "beta-v0.7.6" local RTU_VERSION = "beta-v0.7.7"
local rtu_t = types.rtu_t local rtu_t = types.rtu_t
@ -39,7 +39,7 @@ log.init(config.LOG_PATH, config.LOG_MODE)
log.info("========================================") log.info("========================================")
log.info("BOOTING rtu.startup " .. RTU_VERSION) log.info("BOOTING rtu.startup " .. RTU_VERSION)
log.info("========================================") log.info("========================================")
println(">> RTU " .. RTU_VERSION .. " <<") println(">> RTU GATEWAY " .. RTU_VERSION .. " <<")
---------------------------------------- ----------------------------------------
-- startup -- startup
@ -94,28 +94,47 @@ local units = __shared_memory.rtu_sys.units
local rtu_redstone = config.RTU_REDSTONE local rtu_redstone = config.RTU_REDSTONE
local rtu_devices = config.RTU_DEVICES local rtu_devices = config.RTU_DEVICES
-- redstone interfaces -- configure RTU gateway based on config file definitions
for entry_idx = 1, #rtu_redstone do local function configure()
-- redstone interfaces
for entry_idx = 1, #rtu_redstone do
local rs_rtu = redstone_rtu.new() local rs_rtu = redstone_rtu.new()
local io_table = rtu_redstone[entry_idx].io local io_table = rtu_redstone[entry_idx].io
local io_reactor = rtu_redstone[entry_idx].for_reactor local io_reactor = rtu_redstone[entry_idx].for_reactor
-- CHECK: reactor ID must be >= to 1
if type(io_reactor) ~= "number" or io_reactor <= 0 or io_reactor ~= math.floor(io_reactor) then
println(util.c("configure> redstone entry #", entry_idx, " : ", io_reactor, " isn't an integer >= 1"))
return false
end
-- CHECK: io table exists
if type(io_table) ~= "table" then
println(util.c("configure> redstone entry #", entry_idx, " no IO table found"))
return false
end
local capabilities = {} local capabilities = {}
log.debug(util.c("init> starting redstone RTU I/O linking for reactor ", io_reactor, "...")) log.debug(util.c("configure> starting redstone RTU I/O linking for reactor ", io_reactor, "..."))
local continue = true local continue = true
-- check for duplicate entries
for i = 1, #units do for i = 1, #units do
local unit = units[i] ---@type rtu_unit_registry_entry local unit = units[i] ---@type rtu_unit_registry_entry
if unit.reactor == io_reactor and unit.type == rtu_t.redstone then if unit.reactor == io_reactor and unit.type == rtu_t.redstone then
-- duplicate entry -- duplicate entry
log.warning(util.c("init> skipping definition block #", entry_idx, " for reactor ", io_reactor, " with already defined redstone I/O")) local message = util.c("configure> skipping definition block #", entry_idx, " for reactor ", io_reactor,
" with already defined redstone I/O")
println(message)
log.warning(message)
continue = false continue = false
break break
end end
end end
-- not a duplicate
if continue then if continue then
for i = 1, #io_table do for i = 1, #io_table do
local valid = false local valid = false
@ -131,17 +150,20 @@ for entry_idx = 1, #rtu_redstone do
end end
if not valid then if not valid then
local message = util.c("init> invalid redstone definition at index ", i, " in definition block #", entry_idx, local message = util.c("configure> invalid redstone definition at index ", i, " in definition block #", entry_idx,
" (for reactor ", io_reactor, ")") " (for reactor ", io_reactor, ")")
println_ts(message) println(message)
log.warning(message) log.error(message)
return false
else else
-- link redstone in RTU -- link redstone in RTU
local mode = rsio.get_io_mode(conf.channel) local mode = rsio.get_io_mode(conf.channel)
if mode == rsio.IO_MODE.DIGITAL_IN then if mode == rsio.IO_MODE.DIGITAL_IN then
-- can't have duplicate inputs -- can't have duplicate inputs
if util.table_contains(capabilities, conf.channel) then if util.table_contains(capabilities, conf.channel) then
log.warning(util.c("init> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)) local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
println(message)
log.warning(message)
else else
rs_rtu.link_di(conf.side, conf.bundled_color) rs_rtu.link_di(conf.side, conf.bundled_color)
end end
@ -150,7 +172,9 @@ for entry_idx = 1, #rtu_redstone do
elseif mode == rsio.IO_MODE.ANALOG_IN then elseif mode == rsio.IO_MODE.ANALOG_IN then
-- can't have duplicate inputs -- can't have duplicate inputs
if util.table_contains(capabilities, conf.channel) then if util.table_contains(capabilities, conf.channel) then
log.warning(util.c("init> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)) local message = util.c("configure> skipping duplicate input for channel ", rsio.to_string(conf.channel), " on side ", conf.side)
println(message)
log.warning(message)
else else
rs_rtu.link_ai(conf.side) rs_rtu.link_ai(conf.side)
end end
@ -158,14 +182,15 @@ for entry_idx = 1, #rtu_redstone do
rs_rtu.link_ao(conf.side) rs_rtu.link_ao(conf.side)
else else
-- should be unreachable code, we already validated channels -- should be unreachable code, we already validated channels
log.error("init> fell through if chain attempting to identify IO mode", true) log.error("configure> fell through if chain attempting to identify IO mode", true)
break println("configure> encountered a software error, check logs")
return false
end end
table.insert(capabilities, conf.channel) table.insert(capabilities, conf.channel)
log.debug(util.c("init> linked redstone ", #capabilities, ": ", rsio.to_string(conf.channel), " (", conf.side, ") for reactor ", log.debug(util.c("configure> linked redstone ", #capabilities, ": ", rsio.to_string(conf.channel),
io_reactor)) " (", conf.side, ") for reactor ", io_reactor))
end end
end end
@ -186,18 +211,41 @@ for entry_idx = 1, #rtu_redstone do
log.debug(util.c("init> initialized RTU unit #", #units, ": redstone_io (redstone) [1] for reactor ", io_reactor)) log.debug(util.c("init> initialized RTU unit #", #units, ": redstone_io (redstone) [1] for reactor ", io_reactor))
end end
end end
-- mounted peripherals -- mounted peripherals
for i = 1, #rtu_devices do for i = 1, #rtu_devices do
local device = ppm.get_periph(rtu_devices[i].name) local name = rtu_devices[i].name
local index = rtu_devices[i].index
local for_reactor = rtu_devices[i].for_reactor
-- CHECK: name is a string
if type(name) ~= "string" then
println(util.c("configure> device entry #", i, ": device ", name, " isn't a string"))
return false
end
-- CHECK: index is an integer >= 1
if type(index) ~= "number" or index <= 0 or index ~= math.floor(index) then
println(util.c("configure> device entry #", i, ": index ", index, " isn't an integer >= 1"))
return false
end
-- CHECK: reactor is an integer >= 1
if type(for_reactor) ~= "number" or for_reactor <= 0 or for_reactor ~= math.floor(for_reactor) then
println(util.c("configure> device entry #", i, ": reactor ", for_reactor, " isn't an integer >= 1"))
return false
end
local device = ppm.get_periph(name)
if device == nil then if device == nil then
local message = util.c("init> '", rtu_devices[i].name, "' not found") local message = util.c("configure> '", name, "' not found")
println_ts(message) println(message)
log.warning(message) log.fatal(message)
return false
else else
local type = ppm.get_type(rtu_devices[i].name) local type = ppm.get_type(name)
local rtu_iface = nil ---@type rtu_device local rtu_iface = nil ---@type rtu_device
local rtu_type = "" local rtu_type = ""
@ -231,18 +279,19 @@ for i = 1, #rtu_devices do
rtu_type = rtu_t.env_detector rtu_type = rtu_t.env_detector
rtu_iface = envd_rtu.new(device) rtu_iface = envd_rtu.new(device)
else else
local message = util.c("init> device '", rtu_devices[i].name, "' is not a known type (", type, ")") local message = util.c("configure> device '", name, "' is not a known type (", type, ")")
println_ts(message) println_ts(message)
log.warning(message) log.fatal(message)
return false
end end
if rtu_iface ~= nil then if rtu_iface ~= nil then
---@class rtu_unit_registry_entry ---@class rtu_unit_registry_entry
local rtu_unit = { local rtu_unit = {
name = rtu_devices[i].name, name = name,
type = rtu_type, type = rtu_type,
index = rtu_devices[i].index, index = index,
reactor = rtu_devices[i].for_reactor, reactor = for_reactor,
device = device, device = device,
rtu = rtu_iface, rtu = rtu_iface,
modbus_io = modbus.new(rtu_iface, true), modbus_io = modbus.new(rtu_iface, true),
@ -254,38 +303,47 @@ for i = 1, #rtu_devices do
table.insert(units, rtu_unit) table.insert(units, rtu_unit)
log.debug(util.c("init> initialized RTU unit #", #units, ": ", rtu_devices[i].name, " (", rtu_type, ") [", log.debug(util.c("configure> initialized RTU unit #", #units, ": ", name, " (", rtu_type, ") [", index, "] for reactor ", for_reactor))
rtu_devices[i].index, "] for reactor ", rtu_devices[i].for_reactor))
end end
end end
end
-- we made it through all that trusting-user-to-write-a-config-file chaos
return true
end end
---------------------------------------- ----------------------------------------
-- start system -- start system
---------------------------------------- ----------------------------------------
-- start connection watchdog log.debug("boot> running configure()")
smem_sys.conn_watchdog = util.new_watchdog(5)
log.debug("boot> conn watchdog started")
-- setup comms if configure() then
smem_sys.rtu_comms = rtu.comms(RTU_VERSION, smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT, smem_sys.conn_watchdog) -- start connection watchdog
log.debug("boot> comms init") smem_sys.conn_watchdog = util.new_watchdog(5)
log.debug("boot> conn watchdog started")
-- init threads -- setup comms
local main_thread = threads.thread__main(__shared_memory) smem_sys.rtu_comms = rtu.comms(RTU_VERSION, smem_dev.modem, config.LISTEN_PORT, config.SERVER_PORT, smem_sys.conn_watchdog)
local comms_thread = threads.thread__comms(__shared_memory) log.debug("boot> comms init")
-- assemble thread list -- init threads
local _threads = { main_thread.p_exec, comms_thread.p_exec } local main_thread = threads.thread__main(__shared_memory)
for i = 1, #units do local comms_thread = threads.thread__comms(__shared_memory)
-- assemble thread list
local _threads = { main_thread.p_exec, comms_thread.p_exec }
for i = 1, #units do
if units[i].thread ~= nil then if units[i].thread ~= nil then
table.insert(_threads, units[i].thread.p_exec) table.insert(_threads, units[i].thread.p_exec)
end end
end end
-- run threads -- run threads
parallel.waitForAll(table.unpack(_threads)) parallel.waitForAll(table.unpack(_threads))
else
println("configuration failed, exiting...")
end
println_ts("exited") println_ts("exited")
log.info("exited") log.info("exited")