2022-01-13 15:12:44 +00:00
|
|
|
--
|
|
|
|
-- RTU: Remote Terminal Unit
|
|
|
|
--
|
|
|
|
|
2022-05-14 17:32:42 +00:00
|
|
|
require("/initenv").init_env()
|
|
|
|
|
2023-07-27 00:48:44 +00:00
|
|
|
local audio = require("scada-common.audio")
|
2023-04-21 00:40:28 +00:00
|
|
|
local comms = require("scada-common.comms")
|
2022-11-13 20:56:27 +00:00
|
|
|
local crash = require("scada-common.crash")
|
2022-09-21 19:53:51 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
2023-06-25 16:59:38 +00:00
|
|
|
local network = require("scada-common.network")
|
2022-09-21 19:53:51 +00:00
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
local types = require("scada-common.types")
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local configure = require("rtu.configure")
|
2023-04-21 00:40:28 +00:00
|
|
|
local databus = require("rtu.databus")
|
2022-09-21 19:53:51 +00:00
|
|
|
local modbus = require("rtu.modbus")
|
2023-04-21 00:40:28 +00:00
|
|
|
local renderer = require("rtu.renderer")
|
2022-09-21 19:53:51 +00:00
|
|
|
local rtu = require("rtu.rtu")
|
|
|
|
local threads = require("rtu.threads")
|
|
|
|
|
|
|
|
local boilerv_rtu = require("rtu.dev.boilerv_rtu")
|
2023-07-11 21:27:03 +00:00
|
|
|
local dynamicv_rtu = require("rtu.dev.dynamicv_rtu")
|
2022-09-21 19:53:51 +00:00
|
|
|
local envd_rtu = require("rtu.dev.envd_rtu")
|
|
|
|
local imatrix_rtu = require("rtu.dev.imatrix_rtu")
|
|
|
|
local redstone_rtu = require("rtu.dev.redstone_rtu")
|
|
|
|
local sna_rtu = require("rtu.dev.sna_rtu")
|
|
|
|
local sps_rtu = require("rtu.dev.sps_rtu")
|
|
|
|
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
|
|
|
|
|
2023-12-15 01:51:54 +00:00
|
|
|
local RTU_VERSION = "v1.7.6"
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
2023-04-21 00:40:28 +00:00
|
|
|
local RTU_UNIT_HW_STATE = databus.RTU_UNIT_HW_STATE
|
2022-05-03 14:44:18 +00:00
|
|
|
|
2022-04-11 15:08:46 +00:00
|
|
|
local println = util.println
|
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
2022-06-05 19:09:02 +00:00
|
|
|
----------------------------------------
|
2023-11-06 14:25:44 +00:00
|
|
|
-- get configuration
|
2022-06-05 19:09:02 +00:00
|
|
|
----------------------------------------
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
if not rtu.load_config() then
|
|
|
|
-- try to reconfigure (user action)
|
|
|
|
local success, error = configure.configure(true)
|
|
|
|
if success then
|
|
|
|
assert(rtu.load_config(), "failed to load valid RTU configuration")
|
|
|
|
else
|
|
|
|
assert(success, "RTU configuration error: " .. error)
|
|
|
|
end
|
|
|
|
end
|
2022-06-05 19:09:02 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local config = rtu.config
|
2022-06-05 19:09:02 +00:00
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- log init
|
|
|
|
----------------------------------------
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
log.init(config.LogPath, config.LogMode, config.LogDebug)
|
2022-04-29 17:32:37 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info("========================================")
|
|
|
|
log.info("BOOTING rtu.startup " .. RTU_VERSION)
|
|
|
|
log.info("========================================")
|
2022-06-05 18:49:50 +00:00
|
|
|
println(">> RTU GATEWAY " .. RTU_VERSION .. " <<")
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
crash.set_env("rtu", RTU_VERSION)
|
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
2022-11-13 20:56:27 +00:00
|
|
|
-- main application
|
2022-03-23 19:41:08 +00:00
|
|
|
----------------------------------------
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
local function main()
|
|
|
|
----------------------------------------
|
|
|
|
-- startup
|
|
|
|
----------------------------------------
|
|
|
|
|
2023-04-21 00:40:28 +00:00
|
|
|
-- record firmware versions and ID
|
|
|
|
databus.tx_versions(RTU_VERSION, comms.version)
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- mount connected devices
|
|
|
|
ppm.mount_all()
|
|
|
|
|
2023-06-25 18:00:18 +00:00
|
|
|
-- message authentication init
|
2023-11-15 00:40:55 +00:00
|
|
|
if type(config.AuthKey) == "string" and string.len(config.AuthKey) > 0 then
|
2023-11-06 14:25:44 +00:00
|
|
|
network.init_mac(config.AuthKey)
|
2023-06-25 18:00:18 +00:00
|
|
|
end
|
|
|
|
|
2023-06-25 16:59:38 +00:00
|
|
|
-- get modem
|
|
|
|
local modem = ppm.get_wireless_modem()
|
|
|
|
if modem == nil then
|
|
|
|
println("boot> wireless modem not found")
|
|
|
|
log.fatal("no wireless modem on startup")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-07-27 00:48:44 +00:00
|
|
|
-- generate alarm tones
|
|
|
|
audio.generate_tones()
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
---@class rtu_shared_memory
|
|
|
|
local __shared_memory = {
|
|
|
|
-- RTU system state flags
|
|
|
|
---@class rtu_state
|
|
|
|
rtu_state = {
|
2023-04-21 00:40:28 +00:00
|
|
|
fp_ok = false,
|
2022-11-13 20:56:27 +00:00
|
|
|
linked = false,
|
|
|
|
shutdown = false
|
|
|
|
},
|
|
|
|
|
2023-07-27 00:48:44 +00:00
|
|
|
-- RTU gateway devices (not RTU units)
|
|
|
|
rtu_dev = {
|
|
|
|
sounders = {}
|
|
|
|
},
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- system objects
|
|
|
|
rtu_sys = {
|
2023-06-25 16:59:38 +00:00
|
|
|
nic = network.nic(modem),
|
2022-11-13 20:56:27 +00:00
|
|
|
rtu_comms = nil, ---@type rtu_comms
|
|
|
|
conn_watchdog = nil, ---@type watchdog
|
2023-06-25 16:59:38 +00:00
|
|
|
units = {}
|
2022-11-13 20:56:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
-- message queues
|
|
|
|
q = {
|
|
|
|
mq_comms = mqueue.new()
|
|
|
|
}
|
2022-04-27 16:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
local smem_sys = __shared_memory.rtu_sys
|
2022-04-27 16:46:04 +00:00
|
|
|
|
2023-04-21 00:40:28 +00:00
|
|
|
databus.tx_hw_modem(true)
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
----------------------------------------
|
|
|
|
-- interpret config and init units
|
|
|
|
----------------------------------------
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
local units = __shared_memory.rtu_sys.units
|
2022-04-27 16:46:04 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local rtu_redstone = config.Redstone
|
|
|
|
local rtu_devices = config.Peripherals
|
2022-04-18 02:36:18 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- configure RTU gateway based on settings file definitions
|
|
|
|
local function sys_config()
|
2022-11-13 20:56:27 +00:00
|
|
|
-- redstone interfaces
|
2023-11-06 14:25:44 +00:00
|
|
|
local rs_rtus = {}
|
2022-06-05 18:49:50 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- go through redstone definitions list
|
|
|
|
for entry_idx = 1, #rtu_redstone do
|
|
|
|
local entry = rtu_redstone[entry_idx] ---@type rtu_rs_definition
|
2023-11-12 17:00:42 +00:00
|
|
|
local assignment
|
2023-11-06 14:25:44 +00:00
|
|
|
local for_reactor = entry.unit
|
|
|
|
local iface_name = util.trinary(entry.color ~= nil, util.c(entry.side, "/", rsio.color_name(entry.color)), entry.side)
|
|
|
|
|
|
|
|
if util.is_int(entry.unit) and entry.unit > 0 and entry.unit < 5 then
|
|
|
|
---@cast for_reactor integer
|
|
|
|
assignment = "reactor unit " .. entry.unit
|
|
|
|
if rs_rtus[for_reactor] == nil then
|
2023-11-12 21:55:24 +00:00
|
|
|
log.debug(util.c("sys_config> allocated redstone RTU for reactor unit ", entry.unit))
|
2023-11-06 14:25:44 +00:00
|
|
|
rs_rtus[for_reactor] = { rtu = redstone_rtu.new(), capabilities = {} }
|
|
|
|
end
|
|
|
|
elseif entry.unit == nil then
|
|
|
|
assignment = "facility"
|
|
|
|
for_reactor = 0
|
|
|
|
if rs_rtus[for_reactor] == nil then
|
2023-11-12 21:55:24 +00:00
|
|
|
log.debug(util.c("sys_config> allocated redstone RTU for the facility"))
|
2023-11-06 14:25:44 +00:00
|
|
|
rs_rtus[for_reactor] = { rtu = redstone_rtu.new(), capabilities = {} }
|
|
|
|
end
|
|
|
|
else
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> invalid unit assignment at block index #", entry_idx)
|
2023-02-22 04:50:43 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- verify configuration
|
|
|
|
local valid = false
|
|
|
|
if rsio.is_valid_port(entry.port) and rsio.is_valid_side(entry.side) then
|
|
|
|
valid = util.trinary(entry.color == nil, true, rsio.is_color(entry.color))
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local rs_rtu = rs_rtus[for_reactor].rtu
|
|
|
|
local capabilities = rs_rtus[for_reactor].capabilities
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
if not valid then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> invalid redstone definition at block index #", entry_idx)
|
2023-11-06 14:25:44 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
-- link redstone in RTU
|
|
|
|
local mode = rsio.get_io_mode(entry.port)
|
|
|
|
if mode == rsio.IO_MODE.DIGITAL_IN then
|
|
|
|
-- can't have duplicate inputs
|
|
|
|
if util.table_contains(capabilities, entry.port) then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name)
|
2023-11-06 14:25:44 +00:00
|
|
|
println(message)
|
|
|
|
log.warning(message)
|
|
|
|
else
|
|
|
|
rs_rtu.link_di(entry.side, entry.color)
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2023-11-06 14:25:44 +00:00
|
|
|
elseif mode == rsio.IO_MODE.DIGITAL_OUT then
|
|
|
|
rs_rtu.link_do(entry.side, entry.color)
|
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_IN then
|
|
|
|
-- can't have duplicate inputs
|
|
|
|
if util.table_contains(capabilities, entry.port) then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> skipping duplicate input for port ", rsio.to_string(entry.port), " on side ", iface_name)
|
2022-11-13 20:56:27 +00:00
|
|
|
println(message)
|
2023-11-06 14:25:44 +00:00
|
|
|
log.warning(message)
|
2022-11-13 20:56:27 +00:00
|
|
|
else
|
2023-11-06 14:25:44 +00:00
|
|
|
rs_rtu.link_ai(entry.side)
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2023-11-06 14:25:44 +00:00
|
|
|
elseif mode == rsio.IO_MODE.ANALOG_OUT then
|
|
|
|
rs_rtu.link_ao(entry.side)
|
|
|
|
else
|
|
|
|
-- should be unreachable code, we already validated ports
|
2023-11-12 21:55:24 +00:00
|
|
|
log.error("sys_config> fell through if chain attempting to identify IO mode at block index #" .. entry_idx, true)
|
|
|
|
println("sys_config> encountered a software error, check logs")
|
2023-11-06 14:25:44 +00:00
|
|
|
return false
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
table.insert(capabilities, entry.port)
|
|
|
|
|
2023-11-12 21:55:24 +00:00
|
|
|
log.debug(util.c("sys_config> linked redstone ", #capabilities, ": ", rsio.to_string(entry.port), " (", iface_name, ") for ", assignment))
|
2023-11-06 14:25:44 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-21 17:27:16 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- create unit entries for redstone RTUs
|
|
|
|
for for_reactor, def in pairs(rs_rtus) do
|
|
|
|
---@class rtu_unit_registry_entry
|
|
|
|
local unit = {
|
|
|
|
uid = 0, ---@type integer
|
|
|
|
name = "redstone_io", ---@type string
|
|
|
|
type = RTU_UNIT_TYPE.REDSTONE, ---@type RTU_UNIT_TYPE
|
|
|
|
index = false, ---@type integer|false
|
|
|
|
reactor = for_reactor, ---@type integer
|
|
|
|
device = def.capabilities, ---@type table use device field for redstone ports
|
|
|
|
is_multiblock = false, ---@type boolean
|
|
|
|
formed = nil, ---@type boolean|nil
|
|
|
|
hw_state = RTU_UNIT_HW_STATE.OK, ---@type RTU_UNIT_HW_STATE
|
|
|
|
rtu = def.rtu, ---@type rtu_device|rtu_rs_device
|
|
|
|
modbus_io = modbus.new(def.rtu, false),
|
|
|
|
pkt_queue = nil, ---@type mqueue|nil
|
|
|
|
thread = nil ---@type parallel_thread|nil
|
|
|
|
}
|
2023-02-12 18:06:44 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
table.insert(units, unit)
|
2023-04-21 00:40:28 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local for_message = "facility"
|
|
|
|
if util.is_int(for_reactor) then
|
|
|
|
for_message = util.c("reactor unit ", for_reactor)
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2023-11-06 14:25:44 +00:00
|
|
|
|
2023-11-12 21:55:24 +00:00
|
|
|
log.info(util.c("sys_config> initialized RTU unit #", #units, ": redstone_io (redstone) [1] for ", for_message))
|
2023-11-06 14:25:44 +00:00
|
|
|
|
|
|
|
unit.uid = #units
|
|
|
|
|
|
|
|
databus.tx_unit_hw_status(unit.uid, unit.hw_state)
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- mounted peripherals
|
|
|
|
for i = 1, #rtu_devices do
|
2023-11-06 14:25:44 +00:00
|
|
|
local entry = rtu_devices[i] ---@type rtu_peri_definition
|
|
|
|
local name = entry.name
|
|
|
|
local index = entry.index
|
|
|
|
local for_reactor = util.trinary(entry.unit == nil, 0, entry.unit)
|
2022-06-05 18:49:50 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- CHECK: name is a string
|
|
|
|
if type(name) ~= "string" then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> device entry #", i, ": device ", name, " isn't a string")
|
2023-02-22 04:50:43 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- CHECK: index type
|
|
|
|
if (index ~= nil) and (not util.is_int(index)) then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> device entry #", i, ": index ", index, " isn't valid")
|
2023-02-22 04:50:43 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2022-04-29 17:19:01 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
-- CHECK: index range
|
|
|
|
local function validate_index(min, max)
|
2023-11-12 21:55:24 +00:00
|
|
|
if (not util.is_int(index)) or ((index < min) and (max ~= nil and index > max)) then
|
|
|
|
local message = util.c("sys_config> device entry #", i, ": index ", index, " isn't >= ", min)
|
|
|
|
if max ~= nil then message = util.c(message, " and <= ", max) end
|
2023-11-06 14:25:44 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
else return true end
|
|
|
|
end
|
|
|
|
|
2022-12-08 04:17:11 +00:00
|
|
|
-- CHECK: reactor is an integer >= 0
|
2023-11-06 14:25:44 +00:00
|
|
|
local function validate_assign(for_facility)
|
|
|
|
if for_facility and for_reactor ~= 0 then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> device entry #", i, ": must only be for the facility")
|
2023-11-06 14:25:44 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
elseif (not for_facility) and ((not util.is_int(for_reactor)) or (for_reactor < 1) or (for_reactor > 4)) then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> device entry #", i, ": unit assignment ", for_reactor, " isn't vaild")
|
2023-11-06 14:25:44 +00:00
|
|
|
println(message)
|
|
|
|
log.fatal(message)
|
|
|
|
return false
|
|
|
|
else return true end
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2022-04-29 17:19:01 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
local device = ppm.get_periph(name)
|
2022-03-23 19:41:08 +00:00
|
|
|
|
2023-04-12 20:02:29 +00:00
|
|
|
local type ---@type string|nil
|
|
|
|
local rtu_iface ---@type rtu_device
|
|
|
|
local rtu_type ---@type RTU_UNIT_TYPE
|
2023-04-06 16:52:25 +00:00
|
|
|
local is_multiblock = false ---@type boolean
|
2023-02-12 18:06:44 +00:00
|
|
|
local formed = nil ---@type boolean|nil
|
2023-04-06 16:52:25 +00:00
|
|
|
local faulted = nil ---@type boolean|nil
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
if device == nil then
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> '", name, "' not found, using placeholder")
|
2022-11-13 20:56:27 +00:00
|
|
|
println(message)
|
|
|
|
log.warning(message)
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- mount a virtual (placeholder) device
|
|
|
|
type, device = ppm.mount_virtual()
|
|
|
|
else
|
|
|
|
type = ppm.get_type(name)
|
|
|
|
end
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
if type == "boilerValve" then
|
|
|
|
-- boiler multiblock
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_index(1, 2) then return false end
|
|
|
|
if not validate_assign() then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.BOILER_VALVE
|
2023-04-06 16:52:25 +00:00
|
|
|
rtu_iface, faulted = boilerv_rtu.new(device)
|
2023-02-12 18:06:44 +00:00
|
|
|
is_multiblock = true
|
2022-11-13 20:56:27 +00:00
|
|
|
formed = device.isFormed()
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
if formed == ppm.UNDEFINED_FIELD or formed == ppm.ACCESS_FAULT then
|
2023-11-12 21:55:24 +00:00
|
|
|
println_ts(util.c("sys_config> failed to check if '", name, "' is formed"))
|
|
|
|
log.fatal(util.c("sys_config> failed to check if '", name, "' is a formed boiler multiblock"))
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif type == "turbineValve" then
|
|
|
|
-- turbine multiblock
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_index(1, 3) then return false end
|
|
|
|
if not validate_assign() then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.TURBINE_VALVE
|
2023-04-06 16:52:25 +00:00
|
|
|
rtu_iface, faulted = turbinev_rtu.new(device)
|
2023-02-12 18:06:44 +00:00
|
|
|
is_multiblock = true
|
2022-11-13 20:56:27 +00:00
|
|
|
formed = device.isFormed()
|
|
|
|
|
|
|
|
if formed == ppm.UNDEFINED_FIELD or formed == ppm.ACCESS_FAULT then
|
2023-11-12 21:55:24 +00:00
|
|
|
println_ts(util.c("sys_config> failed to check if '", name, "' is formed"))
|
|
|
|
log.fatal(util.c("sys_config> failed to check if '", name, "' is a formed turbine multiblock"))
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2023-07-11 21:27:03 +00:00
|
|
|
elseif type == "dynamicValve" then
|
|
|
|
-- dynamic tank multiblock
|
2023-11-06 14:25:44 +00:00
|
|
|
if entry.unit == nil then
|
|
|
|
if not validate_index(1, 4) then return false end
|
|
|
|
if not validate_assign(true) then return false end
|
|
|
|
else
|
|
|
|
if not validate_index(1, 1) then return false end
|
|
|
|
if not validate_assign() then return false end
|
|
|
|
end
|
|
|
|
|
2023-07-11 21:27:03 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.DYNAMIC_VALVE
|
|
|
|
rtu_iface, faulted = dynamicv_rtu.new(device)
|
|
|
|
is_multiblock = true
|
|
|
|
formed = device.isFormed()
|
|
|
|
|
|
|
|
if formed == ppm.UNDEFINED_FIELD or formed == ppm.ACCESS_FAULT then
|
2023-11-12 21:55:24 +00:00
|
|
|
println_ts(util.c("sys_config> failed to check if '", name, "' is formed"))
|
|
|
|
log.fatal(util.c("sys_config> failed to check if '", name, "' is a formed dynamic tank multiblock"))
|
2023-07-11 21:27:03 +00:00
|
|
|
return false
|
|
|
|
end
|
2022-11-13 20:56:27 +00:00
|
|
|
elseif type == "inductionPort" then
|
|
|
|
-- induction matrix multiblock
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_assign(true) then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.IMATRIX
|
2023-04-06 16:52:25 +00:00
|
|
|
rtu_iface, faulted = imatrix_rtu.new(device)
|
2023-02-12 18:06:44 +00:00
|
|
|
is_multiblock = true
|
2022-11-13 20:56:27 +00:00
|
|
|
formed = device.isFormed()
|
|
|
|
|
|
|
|
if formed == ppm.UNDEFINED_FIELD or formed == ppm.ACCESS_FAULT then
|
2023-11-12 21:55:24 +00:00
|
|
|
println_ts(util.c("sys_config> failed to check if '", name, "' is formed"))
|
|
|
|
log.fatal(util.c("sys_config> failed to check if '", name, "' is a formed induction matrix multiblock"))
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif type == "spsPort" then
|
|
|
|
-- SPS multiblock
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_assign(true) then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.SPS
|
2023-04-06 16:52:25 +00:00
|
|
|
rtu_iface, faulted = sps_rtu.new(device)
|
2023-02-12 18:06:44 +00:00
|
|
|
is_multiblock = true
|
2022-11-13 20:56:27 +00:00
|
|
|
formed = device.isFormed()
|
|
|
|
|
|
|
|
if formed == ppm.UNDEFINED_FIELD or formed == ppm.ACCESS_FAULT then
|
2023-11-12 21:55:24 +00:00
|
|
|
println_ts(util.c("sys_config> failed to check if '", name, "' is formed"))
|
|
|
|
log.fatal(util.c("sys_config> failed to check if '", name, "' is a formed SPS multiblock"))
|
2022-11-13 20:56:27 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif type == "solarNeutronActivator" then
|
|
|
|
-- SNA
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_assign() then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.SNA
|
2023-04-12 20:02:29 +00:00
|
|
|
rtu_iface, faulted = sna_rtu.new(device)
|
2022-11-13 20:56:27 +00:00
|
|
|
elseif type == "environmentDetector" then
|
|
|
|
-- advanced peripherals environment detector
|
2023-11-12 16:54:47 +00:00
|
|
|
if not validate_index(1) then return false end
|
2023-11-06 14:25:44 +00:00
|
|
|
if not validate_assign(entry.unit == nil) then return false end
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.ENV_DETECTOR
|
2023-04-12 20:02:29 +00:00
|
|
|
rtu_iface, faulted = envd_rtu.new(device)
|
2022-11-13 20:56:27 +00:00
|
|
|
elseif type == ppm.VIRTUAL_DEVICE_TYPE then
|
|
|
|
-- placeholder device
|
2023-02-21 17:27:16 +00:00
|
|
|
rtu_type = RTU_UNIT_TYPE.VIRTUAL
|
2022-11-13 20:56:27 +00:00
|
|
|
rtu_iface = rtu.init_unit().interface()
|
|
|
|
else
|
2023-11-12 21:55:24 +00:00
|
|
|
local message = util.c("sys_config> device '", name, "' is not a known type (", type, ")")
|
2022-11-13 20:56:27 +00:00
|
|
|
println_ts(message)
|
|
|
|
log.fatal(message)
|
2022-11-12 06:35:31 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-04-09 18:25:22 +00:00
|
|
|
if is_multiblock then
|
|
|
|
if not formed then
|
2023-11-12 21:55:24 +00:00
|
|
|
log.info(util.c("sys_config> device '", name, "' is not formed"))
|
2023-04-09 18:25:22 +00:00
|
|
|
elseif faulted then
|
|
|
|
-- sometimes there is a race condition on server boot where it reports formed, but
|
|
|
|
-- the other functions are not yet defined (that's the theory at least). mark as unformed to attempt connection later
|
|
|
|
formed = false
|
2023-11-12 21:55:24 +00:00
|
|
|
log.warning(util.c("sys_config> device '", name, "' is formed, but initialization had one or more faults: marked as unformed"))
|
2023-04-09 18:25:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
---@class rtu_unit_registry_entry
|
|
|
|
local rtu_unit = {
|
2023-02-21 17:27:16 +00:00
|
|
|
uid = 0, ---@type integer
|
|
|
|
name = name, ---@type string
|
|
|
|
type = rtu_type, ---@type RTU_UNIT_TYPE
|
2023-11-06 14:25:44 +00:00
|
|
|
index = index or false, ---@type integer|false
|
2023-02-21 17:27:16 +00:00
|
|
|
reactor = for_reactor, ---@type integer
|
|
|
|
device = device, ---@type table
|
|
|
|
is_multiblock = is_multiblock, ---@type boolean
|
2023-02-12 18:06:44 +00:00
|
|
|
formed = formed, ---@type boolean|nil
|
2023-06-22 19:46:17 +00:00
|
|
|
hw_state = RTU_UNIT_HW_STATE.OFFLINE, ---@type RTU_UNIT_HW_STATE
|
2023-02-12 18:06:44 +00:00
|
|
|
rtu = rtu_iface, ---@type rtu_device|rtu_rs_device
|
2022-11-13 20:56:27 +00:00
|
|
|
modbus_io = modbus.new(rtu_iface, true),
|
2023-02-12 18:06:44 +00:00
|
|
|
pkt_queue = mqueue.new(), ---@type mqueue|nil
|
2023-02-21 17:27:16 +00:00
|
|
|
thread = nil ---@type parallel_thread|nil
|
2022-11-13 20:56:27 +00:00
|
|
|
}
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
rtu_unit.thread = threads.thread__unit_comms(__shared_memory, rtu_unit)
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
table.insert(units, rtu_unit)
|
2022-11-12 06:35:31 +00:00
|
|
|
|
2022-12-08 04:17:11 +00:00
|
|
|
local for_message = "facility"
|
|
|
|
if for_reactor > 0 then
|
|
|
|
for_message = util.c("reactor ", for_reactor)
|
|
|
|
end
|
|
|
|
|
2023-11-12 21:55:24 +00:00
|
|
|
log.info(util.c("sys_config> initialized RTU unit #", #units, ": ", name, " (", types.rtu_type_to_string(rtu_type), ") [", index, "] for ", for_message))
|
2023-02-12 18:06:44 +00:00
|
|
|
|
|
|
|
rtu_unit.uid = #units
|
2023-04-21 00:40:28 +00:00
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
-- determine hardware status
|
2023-04-21 00:40:28 +00:00
|
|
|
if rtu_unit.type == RTU_UNIT_TYPE.VIRTUAL then
|
2023-06-22 19:46:17 +00:00
|
|
|
rtu_unit.hw_state = RTU_UNIT_HW_STATE.OFFLINE
|
2023-04-21 00:40:28 +00:00
|
|
|
else
|
|
|
|
if rtu_unit.is_multiblock then
|
2023-06-22 19:46:17 +00:00
|
|
|
rtu_unit.hw_state = util.trinary(rtu_unit.formed == true, RTU_UNIT_HW_STATE.OK, RTU_UNIT_HW_STATE.UNFORMED)
|
2023-04-21 00:40:28 +00:00
|
|
|
elseif faulted then
|
2023-06-22 19:46:17 +00:00
|
|
|
rtu_unit.hw_state = RTU_UNIT_HW_STATE.FAULTED
|
2023-04-21 00:40:28 +00:00
|
|
|
else
|
2023-06-22 19:46:17 +00:00
|
|
|
rtu_unit.hw_state = RTU_UNIT_HW_STATE.OK
|
2023-04-21 00:40:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
-- report hardware status
|
|
|
|
databus.tx_unit_hw_status(rtu_unit.uid, rtu_unit.hw_state)
|
2022-11-13 20:56:27 +00:00
|
|
|
end
|
2022-06-05 18:49:50 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
return true
|
|
|
|
end
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
----------------------------------------
|
|
|
|
-- start system
|
|
|
|
----------------------------------------
|
2022-03-15 16:02:31 +00:00
|
|
|
|
2023-04-21 00:40:28 +00:00
|
|
|
local rtu_state = __shared_memory.rtu_state
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
log.debug("boot> running sys_config()")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
if sys_config() then
|
2023-04-21 00:40:28 +00:00
|
|
|
-- start UI
|
|
|
|
local message
|
2023-09-30 17:31:41 +00:00
|
|
|
rtu_state.fp_ok, message = renderer.try_start_ui(units)
|
2023-04-21 00:40:28 +00:00
|
|
|
|
|
|
|
if not rtu_state.fp_ok then
|
|
|
|
println_ts(util.c("UI error: ", message))
|
2023-04-30 03:49:04 +00:00
|
|
|
println("startup> running without front panel")
|
2023-07-11 19:15:44 +00:00
|
|
|
log.error(util.c("front panel GUI render failed with error ", message))
|
2023-04-30 03:49:04 +00:00
|
|
|
log.info("startup> running in headless mode without front panel")
|
2023-04-21 00:40:28 +00:00
|
|
|
end
|
|
|
|
|
2023-07-27 00:48:44 +00:00
|
|
|
-- find and setup all speakers
|
|
|
|
local speakers = ppm.get_all_devices("speaker")
|
|
|
|
for _, s in pairs(speakers) do
|
|
|
|
local sounder = rtu.init_sounder(s)
|
|
|
|
|
|
|
|
table.insert(__shared_memory.rtu_dev.sounders, sounder)
|
|
|
|
|
|
|
|
log.debug(util.c("startup> added speaker, attached as ", sounder.name))
|
|
|
|
end
|
|
|
|
|
|
|
|
databus.tx_hw_spkr_count(#__shared_memory.rtu_dev.sounders)
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- start connection watchdog
|
2023-11-06 14:25:44 +00:00
|
|
|
smem_sys.conn_watchdog = util.new_watchdog(config.ConnTimeout)
|
2023-02-22 04:50:43 +00:00
|
|
|
log.debug("startup> conn watchdog started")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- setup comms
|
2023-11-06 14:25:44 +00:00
|
|
|
smem_sys.rtu_comms = rtu.comms(RTU_VERSION, smem_sys.nic, smem_sys.conn_watchdog)
|
2023-02-22 04:50:43 +00:00
|
|
|
log.debug("startup> comms init")
|
2022-04-11 21:27:57 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- init threads
|
|
|
|
local main_thread = threads.thread__main(__shared_memory)
|
|
|
|
local comms_thread = threads.thread__comms(__shared_memory)
|
2022-06-05 18:49:50 +00:00
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- assemble thread list
|
|
|
|
local _threads = { main_thread.p_exec, comms_thread.p_exec }
|
|
|
|
for i = 1, #units do
|
|
|
|
if units[i].thread ~= nil then
|
|
|
|
table.insert(_threads, units[i].thread.p_exec)
|
|
|
|
end
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
2022-11-13 20:56:27 +00:00
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
log.info("startup> completed")
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
-- run threads
|
|
|
|
parallel.waitForAll(table.unpack(_threads))
|
|
|
|
else
|
|
|
|
println("configuration failed, exiting...")
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
|
2023-04-21 00:40:28 +00:00
|
|
|
renderer.close_ui()
|
|
|
|
|
2022-11-13 20:56:27 +00:00
|
|
|
println_ts("exited")
|
|
|
|
log.info("exited")
|
2022-06-05 18:49:50 +00:00
|
|
|
end
|
2022-04-18 14:21:29 +00:00
|
|
|
|
2023-04-21 01:19:16 +00:00
|
|
|
if not xpcall(main, crash.handler) then
|
|
|
|
pcall(renderer.close_ui)
|
|
|
|
crash.exit()
|
|
|
|
else
|
|
|
|
log.close()
|
|
|
|
end
|