From a1dbc15d1648d6288853f9042fc69f1086540dc9 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Fri, 13 Sep 2024 02:23:16 +0000 Subject: [PATCH] #545 supervisor type annotation updates --- supervisor/configure.lua | 36 +++++----- supervisor/facility.lua | 55 ++++++++-------- supervisor/facility_update.lua | 26 ++++---- supervisor/panel/pgi.lua | 19 ++++-- supervisor/session/coordinator.lua | 14 ++-- supervisor/session/rtu.lua | 16 ++--- supervisor/session/rtu/dynamicv.lua | 4 +- supervisor/session/rtu/turbinev.lua | 4 +- supervisor/session/rtu/txnctrl.lua | 3 +- supervisor/session/rtu/unit_session.lua | 1 - supervisor/session/svsessions.lua | 42 ++++++++---- supervisor/startup.lua | 2 +- supervisor/unit.lua | 87 +++++++++++++------------ supervisor/unitlogic.lua | 32 ++++----- 14 files changed, 178 insertions(+), 163 deletions(-) diff --git a/supervisor/configure.lua b/supervisor/configure.lua index 0c90538..a903cbe 100644 --- a/supervisor/configure.lua +++ b/supervisor/configure.lua @@ -77,31 +77,31 @@ local tool_ctl = { auth_key_textbox = nil, ---@type graphics_element auth_key_value = "", - cooling_elems = {}, - tank_elems = {}, + cooling_elems = {}, ---@type { line: graphics_element, turbines: graphics_element, boilers: graphics_element, tank: graphics_element }[] + tank_elems = {}, ---@type { div: graphics_element, tank_opt: graphics_element, no_tank: graphics_element }[] - vis_ftanks = {}, - vis_utanks = {} + vis_ftanks = {}, ---@type { line: graphics_element, pipe_conn?: graphics_element, pipe_chain?: graphics_element, pipe_direct?: graphics_element, label?: graphics_element }[] + vis_utanks = {} ---@type { line: graphics_element, label: graphics_element }[] } ---@class svr_config local tmp_cfg = { UnitCount = 1, - CoolingConfig = {}, + CoolingConfig = {}, ---@type { TurbineCount: integer, BoilerCount: integer, TankConnection: boolean }[] FacilityTankMode = 0, - FacilityTankDefs = {}, + FacilityTankDefs = {}, ---@type integer[] ExtChargeIdling = false, - SVR_Channel = nil, ---@type integer - PLC_Channel = nil, ---@type integer - RTU_Channel = nil, ---@type integer - CRD_Channel = nil, ---@type integer - PKT_Channel = nil, ---@type integer - PLC_Timeout = nil, ---@type number - RTU_Timeout = nil, ---@type number - CRD_Timeout = nil, ---@type number - PKT_Timeout = nil, ---@type number - TrustedRange = nil, ---@type number - AuthKey = nil, ---@type string|nil + SVR_Channel = nil, ---@type integer + PLC_Channel = nil, ---@type integer + RTU_Channel = nil, ---@type integer + CRD_Channel = nil, ---@type integer + PKT_Channel = nil, ---@type integer + PLC_Timeout = nil, ---@type number + RTU_Timeout = nil, ---@type number + CRD_Timeout = nil, ---@type number + PKT_Timeout = nil, ---@type number + TrustedRange = nil, ---@type number + AuthKey = nil, ---@type string|nil LogMode = 0, LogPath = "", LogDebug = false, @@ -294,6 +294,8 @@ local function config_view(display) tmp_cfg.CoolingConfig = {} for i = 1, tmp_cfg.UnitCount do local conf = tool_ctl.cooling_elems[i] + -- already verified fields are numbers +---@diagnostic disable-next-line: assign-type-mismatch tmp_cfg.CoolingConfig[i] = { TurbineCount = tonumber(conf.turbines.get_value()), BoilerCount = tonumber(conf.boilers.get_value()), TankConnection = conf.tank.get_value() } if conf.tank.get_value() then any_has_tank = true end end diff --git a/supervisor/facility.lua b/supervisor/facility.lua index 7b786a2..09c8195 100644 --- a/supervisor/facility.lua +++ b/supervisor/facility.lua @@ -40,7 +40,7 @@ local facility = {} function facility.new(config) ---@class _facility_self local self = { - units = {}, + units = {}, ---@type reactor_unit[] types = { AUTO_SCRAM = AUTO_SCRAM, START_STATUS = START_STATUS }, status_text = { "START UP", "initializing..." }, all_sys_ok = false, @@ -51,16 +51,16 @@ function facility.new(config) r_cool = config.CoolingConfig, fac_tank_mode = config.FacilityTankMode, fac_tank_defs = config.FacilityTankDefs, - fac_tank_list = {} + fac_tank_list = {} ---@type integer[] }, -- rtus rtu_conn_count = 0, - rtu_list = {}, - redstone = {}, - induction = {}, - sps = {}, - tanks = {}, - envd = {}, + rtu_list = {}, ---@type unit_session[][] + redstone = {}, ---@type unit_session[] + induction = {}, ---@type unit_session[] + sps = {}, ---@type unit_session[] + tanks = {}, ---@type unit_session[] + envd = {}, ---@type unit_session[] -- redstone I/O control io_ctl = nil, ---@type rs_controller -- process control @@ -105,11 +105,11 @@ function facility.new(config) sps_low_power = false, disabled_sps = false, -- alarm tones - tone_states = {}, + tone_states = {}, ---@type boolean[] test_tone_set = false, test_tone_reset = false, - test_tone_states = {}, - test_alarm_states = {}, + test_tone_states = {}, ---@type boolean[] + test_alarm_states = {}, ---@type boolean[] -- statistics im_stat_init = false, avg_charge = util.mov_avg(3), -- 3 seconds @@ -350,7 +350,7 @@ function facility.new(config) -- additionally sets the requested auto waste mode if applicable function public.update_units() for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] u.auto_set_waste(self.current_waste_product) u.update() end @@ -363,16 +363,14 @@ function facility.new(config) -- SCRAM all reactor units function public.scram_all() for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit - u.scram() + self.units[i].scram() end end -- ack all alarms on all reactor units function public.ack_all() for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit - u.ack_all() + local u = self.units[i].ack_all() end end @@ -393,8 +391,7 @@ function facility.new(config) -- load up current limits local limits = {} for i = 1, config.UnitCount do - local u = self.units[i] ---@type reactor_unit - limits[i] = u.get_control_inf().lim_br100 * 100 + limits[i] = self.units[i].get_control_inf().lim_br100 * 100 end -- only allow changes if not running @@ -565,7 +562,7 @@ function facility.new(config) if all or type == RTU_UNIT_TYPE.IMATRIX then build.induction = {} for i = 1, #self.induction do - local matrix = self.induction[i] ---@type unit_session + local matrix = self.induction[i] build.induction[i] = { matrix.get_db().formed, matrix.get_db().build } end end @@ -573,7 +570,7 @@ function facility.new(config) if all or type == RTU_UNIT_TYPE.SPS then build.sps = {} for i = 1, #self.sps do - local sps = self.sps[i] ---@type unit_session + local sps = self.sps[i] build.sps[i] = { sps.get_db().formed, sps.get_db().build } end end @@ -581,7 +578,7 @@ function facility.new(config) if all or type == RTU_UNIT_TYPE.DYNAMIC_VALVE then build.tanks = {} for i = 1, #self.tanks do - local tank = self.tanks[i] ---@type unit_session + local tank = self.tanks[i] build.tanks[tank.get_device_idx()] = { tank.get_db().formed, tank.get_db().build } end end @@ -649,8 +646,8 @@ function facility.new(config) -- status of induction matricies (including tanks) status.induction = {} for i = 1, #self.induction do - local matrix = self.induction[i] ---@type unit_session - local db = matrix.get_db() ---@type imatrix_session_db + local matrix = self.induction[i] + local db = matrix.get_db() ---@type imatrix_session_db status.induction[i] = { matrix.is_faulted(), db.formed, db.state, db.tanks } @@ -662,24 +659,24 @@ function facility.new(config) -- status of sps status.sps = {} for i = 1, #self.sps do - local sps = self.sps[i] ---@type unit_session - local db = sps.get_db() ---@type sps_session_db + local sps = self.sps[i] + local db = sps.get_db() ---@type sps_session_db status.sps[i] = { sps.is_faulted(), db.formed, db.state, db.tanks } end -- status of dynamic tanks status.tanks = {} for i = 1, #self.tanks do - local tank = self.tanks[i] ---@type unit_session - local db = tank.get_db() ---@type dynamicv_session_db + local tank = self.tanks[i] + local db = tank.get_db() ---@type dynamicv_session_db status.tanks[tank.get_device_idx()] = { tank.is_faulted(), db.formed, db.state, db.tanks } end -- radiation monitors (environment detectors) status.envds = {} for i = 1, #self.envd do - local envd = self.envd[i] ---@type unit_session - local db = envd.get_db() ---@type envd_session_db + local envd = self.envd[i] + local db = envd.get_db() ---@type envd_session_db status.envds[envd.get_device_idx()] = { envd.is_faulted(), db.radiation, db.radiation_raw } end diff --git a/supervisor/facility_update.lua b/supervisor/facility_update.lua index 43fe86b..d0ae3eb 100644 --- a/supervisor/facility_update.lua +++ b/supervisor/facility_update.lua @@ -84,7 +84,7 @@ local function allocate_burn_rate(burn_rate, ramp, abort_on_fault) -- go through all reactor units in this group for id = 1, #units do - local u = units[id] ---@type reactor_unit + local u = units[id] local ctl = u.get_control_inf() local lim_br100 = u.auto_get_effective_limit() @@ -139,7 +139,7 @@ function update.pre_auto() -- check if test routines are allowed right now self.allow_testing = true for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] self.allow_testing = self.allow_testing and u.is_safe_idle() end @@ -149,8 +149,8 @@ function update.pre_auto() -- calculate moving averages for induction matrix if self.induction[1] ~= nil then - local matrix = self.induction[1] ---@type unit_session - local db = matrix.get_db() ---@type imatrix_session_db + local matrix = self.induction[1] + local db = matrix.get_db() ---@type imatrix_session_db local build_update = db.build.last_update rate_update = db.state.last_update @@ -531,7 +531,7 @@ function update.auto_safety() -- check for critical unit alarms astatus.crit_alarm = false for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] if u.has_alarm_min_prio(PRIO.CRITICAL) then astatus.crit_alarm = true @@ -544,7 +544,7 @@ function update.auto_safety() local max_rad = 0 for i = 1, #self.envd do - local envd = self.envd[i] ---@type unit_session + local envd = self.envd[i] local e_db = envd.get_db() ---@type envd_session_db if e_db.radiation_raw > max_rad then max_rad = e_db.radiation_raw end end @@ -620,7 +620,7 @@ function update.auto_safety() -- reset PLC RPS trips if we should for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] u.auto_cond_rps_reset() end end @@ -647,7 +647,7 @@ function update.alarm_audio() else -- check all alarms for all units for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] for id, alarm in pairs(u.get_alarms()) do alarms[id] = alarms[id] or (alarm == ALARM_STATE.TRIPPED) end @@ -730,7 +730,7 @@ function update.redstone(ack_all) -- handle facility SCRAM if self.io_ctl.digital_read(IO.F_SCRAM) then for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] u.cond_scram() end end @@ -741,7 +741,7 @@ function update.redstone(ack_all) -- update facility alarm outputs local has_prio_alarm, has_any_alarm = false, false for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] if u.has_alarm_min_prio(PRIO.EMERGENCY) then has_prio_alarm, has_any_alarm = true, true @@ -771,7 +771,7 @@ function update.unit_mgmt() local need_emcool = false for i = 1, #self.units do - local u = self.units[i] ---@type reactor_unit + local u = self.units[i] -- update auto waste processing if u.get_control_inf().waste_mode == WASTE_MODE.AUTO then @@ -812,8 +812,8 @@ function update.unit_mgmt() -- there should be no need for any to be in fill only mode if need_emcool then for i = 1, #self.tanks do - local session = self.tanks[i] ---@type unit_session - local tank = session.get_db() ---@type dynamicv_session_db + local session = self.tanks[i] + local tank = session.get_db() ---@type dynamicv_session_db if tank.state.container_mode == CONTAINER_MODE.FILL then session.get_cmd_queue().push_data(DTV_RTU_S_DATA.SET_CONT_MODE, CONTAINER_MODE.BOTH) diff --git a/supervisor/panel/pgi.lua b/supervisor/panel/pgi.lua index 2d8ee93..aa57ef9 100644 --- a/supervisor/panel/pgi.lua +++ b/supervisor/panel/pgi.lua @@ -8,14 +8,19 @@ local util = require("scada-common.util") local pgi = {} local data = { - rtu_list = nil, ---@type nil|graphics_element - pdg_list = nil, ---@type nil|graphics_element - chk_list = nil, ---@type nil|graphics_element - rtu_entry = nil, ---@type function - pdg_entry = nil, ---@type function - chk_entry = nil, ---@type function + rtu_list = nil, ---@type nil|graphics_element + pdg_list = nil, ---@type nil|graphics_element + chk_list = nil, ---@type nil|graphics_element + rtu_entry = nil, ---@type function + pdg_entry = nil, ---@type function + chk_entry = nil, ---@type function -- list entries - entries = { rtu = {}, pdg = {}, chk = {}, missing = {} } + entries = { + rtu = {}, ---@type graphics_element[] + pdg = {}, ---@type graphics_element[] + chk = {}, ---@type graphics_element[][] + missing = {} ---@type graphics_element[] + } } -- link list boxes diff --git a/supervisor/session/coordinator.lua b/supervisor/session/coordinator.lua index 8b9c0d9..771e210 100644 --- a/supervisor/session/coordinator.lua +++ b/supervisor/session/coordinator.lua @@ -128,7 +128,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim local unit_builds = {} for i = 1, #self.units do - local unit = self.units[i] ---@type reactor_unit + local unit = self.units[i] unit_builds[unit.get_id()] = unit.get_build() end @@ -145,7 +145,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim local builds = {} for i = 1, #self.units do - local unit = self.units[i] ---@type reactor_unit + local unit = self.units[i] builds[unit.get_id()] = unit.get_build() end @@ -168,7 +168,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim local status = {} for i = 1, #self.units do - local unit = self.units[i] ---@type reactor_unit + local unit = self.units[i] status[unit.get_id()] = { unit.get_reactor_status(), @@ -308,7 +308,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim -- continue if valid unit id if util.is_int(uid) and uid > 0 and uid <= #self.units then - local unit = self.units[uid] ---@type reactor_unit + local unit = self.units[uid] local manual = facility.get_group(uid) == AUTO_GROUP.MANUAL if cmd == UNIT_COMMAND.SCRAM then @@ -432,8 +432,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim local unit_id = cmd.val local builds = {} - local unit = self.units[unit_id] ---@type reactor_unit - builds[unit_id] = unit.get_build(-1) + builds[unit_id] = self.units[unit_id].get_build(-1) _send(CRDN_TYPE.UNIT_BUILDS, { builds }) elseif cmd.key == CRD_S_DATA.RESEND_RTU_BUILD then @@ -446,8 +445,7 @@ function coordinator.new_session(id, s_addr, i_seq_num, in_queue, out_queue, tim local builds = {} - local unit = self.units[unit_id] ---@type reactor_unit - builds[unit_id] = unit.get_build(cmd.val.type) + builds[unit_id] = self.units[unit_id].get_build(cmd.val.type) _send(CRDN_TYPE.UNIT_BUILDS, { builds }) else diff --git a/supervisor/session/rtu.lua b/supervisor/session/rtu.lua index 7d2494a..a38559c 100644 --- a/supervisor/session/rtu.lua +++ b/supervisor/session/rtu.lua @@ -63,7 +63,7 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad keep_alive = 0, alarm_tones = 0 }, - units = {} + units = {} ---@type unit_session[] } ---@class rtu_session @@ -80,13 +80,13 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad _reset_config() for i = 1, #self.fac_units do - local unit = self.fac_units[i] ---@type reactor_unit + local unit = self.fac_units[i] unit.purge_rtu_devices(id) facility.purge_rtu_devices(id) end for i = 1, #self.advert do - local unit = nil ---@type unit_session|nil + local unit = nil ---@type rtu_advertisement local unit_advert = { @@ -96,7 +96,7 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad rsio = self.advert[i][4] } - local u_type = unit_advert.type ---@type integer|boolean + local u_type = unit_advert.type ---@type RTU_UNIT_TYPE|boolean -- validate unit advertisement @@ -127,7 +127,7 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad log.debug(log_tag .. "_handle_advertisement(): advertisement unit validation failure") else if unit_advert.reactor > 0 then - local target_unit = self.fac_units[unit_advert.reactor] ---@type reactor_unit + local target_unit = self.fac_units[unit_advert.reactor] -- unit RTUs if u_type == RTU_UNIT_TYPE.REDSTONE then @@ -255,8 +255,7 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad if pkt.scada_frame.protocol() == PROTOCOL.MODBUS_TCP then ---@cast pkt modbus_frame if self.units[pkt.unit_id] ~= nil then - local unit = self.units[pkt.unit_id] ---@type unit_session - unit.handle_packet(pkt) + self.units[pkt.unit_id].handle_packet(pkt) end elseif pkt.scada_frame.protocol() == PROTOCOL.SCADA_MGMT then ---@cast pkt mgmt_frame @@ -298,8 +297,7 @@ function rtu.new_session(id, s_addr, i_seq_num, in_queue, out_queue, timeout, ad if pkt.length == 1 then local unit_id = pkt.data[1] if self.units[unit_id] ~= nil then - local unit = self.units[unit_id] ---@type unit_session - unit.invalidate_cache() + self.units[unit_id].invalidate_cache() end else log.debug(log_tag .. "SCADA RTU GW device re-mount packet length mismatch") diff --git a/supervisor/session/rtu/dynamicv.lua b/supervisor/session/rtu/dynamicv.lua index 13239a7..57765c0 100644 --- a/supervisor/session/rtu/dynamicv.lua +++ b/supervisor/session/rtu/dynamicv.lua @@ -84,7 +84,7 @@ function dynamicv.new(session_id, unit_id, advert, out_queue) }, state = { last_update = 0, - container_mode = CONTAINER_MODE.BOTH ---@type container_mode + container_mode = CONTAINER_MODE.BOTH ---@type container_mode }, tanks = { last_update = 0, @@ -224,7 +224,7 @@ function dynamicv.new(session_id, unit_id, advert, out_queue) end elseif msg.qtype == mqueue.TYPE.DATA then -- instruction with body - local cmd = msg.message ---@type queue_data + local cmd = msg.message ---@type queue_data if cmd.key == DTV_RTU_S_DATA.SET_CONT_MODE then if cmd.val == types.CONTAINER_MODE.BOTH or cmd.val == types.CONTAINER_MODE.FILL or diff --git a/supervisor/session/rtu/turbinev.lua b/supervisor/session/rtu/turbinev.lua index 4541e56..1502754 100644 --- a/supervisor/session/rtu/turbinev.lua +++ b/supervisor/session/rtu/turbinev.lua @@ -95,7 +95,7 @@ function turbinev.new(session_id, unit_id, advert, out_queue) flow_rate = 0, prod_rate = 0, steam_input_rate = 0, - dumping_mode = DUMPING_MODE.IDLE ---@type dumping_mode + dumping_mode = DUMPING_MODE.IDLE ---@type dumping_mode }, tanks = { last_update = 0, @@ -254,7 +254,7 @@ function turbinev.new(session_id, unit_id, advert, out_queue) end elseif msg.qtype == mqueue.TYPE.DATA then -- instruction with body - local cmd = msg.message ---@type queue_data + local cmd = msg.message ---@type queue_data if cmd.key == TBV_RTU_S_DATA.SET_DUMP_MODE then if cmd.val == types.DUMPING_MODE.IDLE or cmd.val == types.DUMPING_MODE.DUMPING_EXCESS or diff --git a/supervisor/session/rtu/txnctrl.lua b/supervisor/session/rtu/txnctrl.lua index 25ab3ed..cb8de47 100644 --- a/supervisor/session/rtu/txnctrl.lua +++ b/supervisor/session/rtu/txnctrl.lua @@ -6,7 +6,8 @@ local util = require("scada-common.util") local txnctrl = {} -local TIMEOUT = 2000 -- 2000ms max wait +-- 2000ms max wait +local TIMEOUT = 2000 -- create a new transaction controller ---@nodiscard diff --git a/supervisor/session/rtu/unit_session.lua b/supervisor/session/rtu/unit_session.lua index 4f516c8..863c746 100644 --- a/supervisor/session/rtu/unit_session.lua +++ b/supervisor/session/rtu/unit_session.lua @@ -164,7 +164,6 @@ function unit_session.new(session_id, unit_id, advert, out_queue, log_tag, txn_t function public.get_cmd_queue() return protected.in_q end -- close this unit - ---@nodiscard function public.close() self.connected = false end -- check if this unit is connected ---@nodiscard diff --git a/supervisor/session/svsessions.lua b/supervisor/session/svsessions.lua index 30ef729..fba1403 100644 --- a/supervisor/session/svsessions.lua +++ b/supervisor/session/svsessions.lua @@ -40,13 +40,26 @@ local SESSION_TYPE = { svsessions.SESSION_TYPE = SESSION_TYPE local self = { - nic = nil, ---@type nic|nil + -- references to supervisor state and other data + nic = nil, ---@type nic|nil fp_ok = false, - config = nil, ---@type svr_config - facility = nil, ---@type facility|nil - sessions = { rtu = {}, plc = {}, crd = {}, pdg = {} }, + config = nil, ---@type svr_config + facility = nil, ---@type facility|nil + -- lists of connected sessions + sessions = { + rtu = {}, ---@type rtu_session_struct + plc = {}, ---@type plc_session_struct + crd = {}, ---@type crd_session_struct + pdg = {} ---@type pdg_session_struct + }, + -- next session IDs next_ids = { rtu = 0, plc = 0, crd = 0, pdg = 0 }, - dev_dbg = { duplicate = {}, out_of_range = {}, connected = {} } + -- rtu device tracking and invalid assignment detection + dev_dbg = { + duplicate = {}, ---@type unit_session + out_of_range = {}, ---@type unit_session + connected = {} ---@type { induction: boolean, sps: boolean, tanks: boolean[], units: unit_connections[] } + } } ---@alias sv_session_structs plc_session_struct|rtu_session_struct|crd_session_struct|pdg_session_struct @@ -119,10 +132,10 @@ local function _sv_handle_outq(session) end -- iterate all the given sessions ----@param sessions table +---@param sessions sv_session_structs[] local function _iterate(sessions) for i = 1, #sessions do - local session = sessions[i] ---@type sv_session_structs + local session = sessions[i] if session.open and session.instance.iterate() then _sv_handle_outq(session) @@ -150,20 +163,20 @@ local function _shutdown(session) end -- close connections ----@param sessions table +---@param sessions sv_session_structs[] local function _close(sessions) for i = 1, #sessions do - local session = sessions[i] ---@type sv_session_structs + local session = sessions[i] if session.open then _shutdown(session) end end end -- check if a watchdog timer event matches that of one of the provided sessions ----@param sessions table +---@param sessions sv_session_structs[] ---@param timer_event number local function _check_watchdogs(sessions, timer_event) for i = 1, #sessions do - local session = sessions[i] ---@type sv_session_structs + local session = sessions[i] if session.open then local triggered = session.instance.check_wd(timer_event) if triggered then @@ -175,8 +188,9 @@ local function _check_watchdogs(sessions, timer_event) end -- delete any closed sessions ----@param sessions table +---@param sessions sv_session_structs[] local function _free_closed(sessions) + ---@param session sv_session_structs local f = function (session) return session.open end ---@param session sv_session_structs @@ -189,7 +203,7 @@ end -- find a session by computer ID ---@nodiscard ----@param list table +---@param list sv_session_structs[] ---@param s_addr integer ---@return sv_session_structs|nil local function _find_session(list, s_addr) @@ -366,7 +380,7 @@ function svsessions.init(nic, fp_ok, config, facility) for i = 1, config.UnitCount do local r_cool = cool_conf.r_cool[i] - local conns = { boilers = {}, turbines = {}, tanks = {} } + local conns = { boilers = {}, turbines = {}, tanks = {} } ---@type unit_connections for b = 1, r_cool.BoilerCount do conns.boilers[b] = true end for t = 1, r_cool.TurbineCount do conns.turbines[t] = true end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 4f6726c..037f52e 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -22,7 +22,7 @@ local supervisor = require("supervisor.supervisor") local svsessions = require("supervisor.session.svsessions") -local SUPERVISOR_VERSION = "v1.5.3" +local SUPERVISOR_VERSION = "v1.5.4" local println = util.println local println_ts = util.println_ts diff --git a/supervisor/unit.lua b/supervisor/unit.lua index 3290af0..8bf7482 100644 --- a/supervisor/unit.lua +++ b/supervisor/unit.lua @@ -81,13 +81,13 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) num_turbines = num_turbines, types = { DT_KEYS = DT_KEYS, AISTATE = AISTATE }, -- rtus - rtu_list = {}, - redstone = {}, - boilers = {}, - turbines = {}, - tanks = {}, - snas = {}, - envd = {}, + rtu_list = {}, ---@type unit_session[][] + redstone = {}, ---@type unit_session[] + boilers = {}, ---@type unit_session[] + turbines = {}, ---@type unit_session[] + tanks = {}, ---@type unit_session[] + snas = {}, ---@type unit_session[] + envd = {}, ---@type unit_session[] -- redstone control io_ctl = nil, ---@type rs_controller valves = {}, ---@type unit_valves @@ -100,7 +100,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) auto_was_alarmed = false, ramp_target_br100 = 0, -- state tracking - deltas = {}, + deltas = {}, ---@type { last_t: number, last_v: number, dt: number }[] last_heartbeat = 0, last_radiation = 0, damage_decreasing = false, @@ -108,12 +108,12 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) damage_start = 0, damage_last = 0, damage_est_last = 0, - waste_product = WASTE.PLUTONIUM, ---@type WASTE_PRODUCT + waste_product = WASTE.PLUTONIUM, ---@type WASTE_PRODUCT status_text = { "UNKNOWN", "awaiting connection..." }, -- logic for alarms had_reactor = false, turbine_flow_stable = false, - turbine_stability_data = {}, + turbine_stability_data = {}, ---@type { time_state: integer, time_tanks: integer, rotation: number, input_rate: integer }[] last_rate_change_ms = 0, ---@type rps_status last_rps_trips = { @@ -211,15 +211,15 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) SteamFeedMismatch = false, MaxWaterReturnFeed = false, -- boilers - BoilerOnline = {}, - HeatingRateLow = {}, - WaterLevelLow = {}, + BoilerOnline = {}, ---@type boolean[] + HeatingRateLow = {}, ---@type boolean[] + WaterLevelLow = {}, ---@type boolean[] -- turbines - TurbineOnline = {}, - SteamDumpOpen = {}, - TurbineOverSpeed = {}, - GeneratorTrip = {}, - TurbineTrip = {} + TurbineOnline = {}, ---@type boolean[] + SteamDumpOpen = {}, ---@type integer[] + TurbineOverSpeed = {}, ---@type boolean[] + GeneratorTrip = {}, ---@type boolean[] + TurbineTrip = {} ---@type boolean[] }, ---@class alarms alarm_states = { @@ -244,7 +244,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) blade_count = 0, br100 = 0, lim_br100 = 0, - waste_mode = WASTE_MODE.AUTO ---@type WASTE_MODE + waste_mode = WASTE_MODE.AUTO ---@type WASTE_MODE } } } @@ -324,8 +324,8 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) end for i = 1, #self.boilers do - local boiler = self.boilers[i] ---@type unit_session - local db = boiler.get_db() ---@type boilerv_session_db + local boiler = self.boilers[i] + local db = boiler.get_db() ---@type boilerv_session_db local last_update_s = db.tanks.last_update / 1000.0 @@ -336,8 +336,8 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) end for i = 1, #self.turbines do - local turbine = self.turbines[i] ---@type unit_session - local db = turbine.get_db() ---@type turbinev_session_db + local turbine = self.turbines[i] + local db = turbine.get_db() ---@type turbinev_session_db local last_update_s = db.tanks.last_update / 1000.0 @@ -553,8 +553,8 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) -- check boilers formed/faulted for i = 1, #self.boilers do - local sess = self.boilers[i] ---@type unit_session - local boiler = sess.get_db() ---@type boilerv_session_db + local sess = self.boilers[i] + local boiler = sess.get_db() ---@type boilerv_session_db if sess.is_faulted() or not boiler.formed then self.db.control.degraded = true end @@ -562,8 +562,8 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) -- check turbines formed/faulted for i = 1, #self.turbines do - local sess = self.turbines[i] ---@type unit_session - local turbine = sess.get_db() ---@type turbinev_session_db + local sess = self.turbines[i] + local turbine = sess.get_db() ---@type turbinev_session_db if sess.is_faulted() or not turbine.formed then self.db.control.degraded = true end @@ -881,7 +881,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) if all or (filter == RTU_UNIT_TYPE.BOILER_VALVE) then build.boilers = {} for i = 1, #self.boilers do - local boiler = self.boilers[i] ---@type unit_session + local boiler = self.boilers[i] build.boilers[boiler.get_device_idx()] = { boiler.get_db().formed, boiler.get_db().build } end end @@ -889,7 +889,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) if all or (filter == RTU_UNIT_TYPE.TURBINE_VALVE) then build.turbines = {} for i = 1, #self.turbines do - local turbine = self.turbines[i] ---@type unit_session + local turbine = self.turbines[i] build.turbines[turbine.get_device_idx()] = { turbine.get_db().formed, turbine.get_db().build } end end @@ -897,7 +897,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) if all or (filter == RTU_UNIT_TYPE.DYNAMIC_VALVE) then build.tanks = {} for i = 1, #self.tanks do - local tank = self.tanks[i] ---@type unit_session + local tank = self.tanks[i] build.tanks[tank.get_device_idx()] = { tank.get_db().formed, tank.get_db().build } end end @@ -927,19 +927,20 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) -- check which RTUs are connected ---@nodiscard function public.check_rtu_conns() + ---@class unit_connections local conns = {} - conns.boilers = {} + conns.boilers = {} ---@type boolean[] for i = 1, #self.boilers do conns.boilers[self.boilers[i].get_device_idx()] = true end - conns.turbines = {} + conns.turbines = {} ---@type boolean[] for i = 1, #self.turbines do conns.turbines[self.turbines[i].get_device_idx()] = true end - conns.tanks = {} + conns.tanks = {} ---@type boolean[] for i = 1, #self.tanks do conns.tanks[self.tanks[i].get_device_idx()] = true end @@ -955,31 +956,31 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) -- status of boilers (including tanks) status.boilers = {} for i = 1, #self.boilers do - local boiler = self.boilers[i] ---@type unit_session - local db = boiler.get_db() ---@type boilerv_session_db + local boiler = self.boilers[i] + local db = boiler.get_db() ---@type boilerv_session_db status.boilers[boiler.get_device_idx()] = { boiler.is_faulted(), db.formed, db.state, db.tanks } end -- status of turbines (including tanks) status.turbines = {} for i = 1, #self.turbines do - local turbine = self.turbines[i] ---@type unit_session - local db = turbine.get_db() ---@type turbinev_session_db + local turbine = self.turbines[i] + local db = turbine.get_db() ---@type turbinev_session_db status.turbines[turbine.get_device_idx()] = { turbine.is_faulted(), db.formed, db.state, db.tanks } end -- status of dynamic tanks status.tanks = {} for i = 1, #self.tanks do - local tank = self.tanks[i] ---@type unit_session - local db = tank.get_db() ---@type dynamicv_session_db + local tank = self.tanks[i] + local db = tank.get_db() ---@type dynamicv_session_db status.tanks[tank.get_device_idx()] = { tank.is_faulted(), db.formed, db.state, db.tanks } end -- SNA statistical information local total_peak, total_avail, total_out = 0, 0, 0 for i = 1, #self.snas do - local db = self.snas[i].get_db() ---@type sna_session_db + local db = self.snas[i].get_db() ---@type sna_session_db total_peak = total_peak + db.state.peak_production total_avail = total_avail + db.state.production_rate total_out = total_out + math.min(db.tanks.input.amount / 10, db.state.production_rate) @@ -989,8 +990,8 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) -- radiation monitors (environment detectors) status.envds = {} for i = 1, #self.envd do - local envd = self.envd[i] ---@type unit_session - local db = envd.get_db() ---@type envd_session_db + local envd = self.envd[i] + local db = envd.get_db() ---@type envd_session_db status.envds[envd.get_device_idx()] = { envd.is_faulted(), db.radiation, db.radiation_raw } end @@ -1004,7 +1005,7 @@ function unit.new(reactor_id, num_boilers, num_turbines, ext_idle) local total_avail_rate = 0 for i = 1, #self.snas do - local db = self.snas[i].get_db() ---@type sna_session_db + local db = self.snas[i].get_db() ---@type sna_session_db total_avail_rate = total_avail_rate + db.state.production_rate end diff --git a/supervisor/unitlogic.lua b/supervisor/unitlogic.lua index b1e1fc8..cbee532 100644 --- a/supervisor/unitlogic.lua +++ b/supervisor/unitlogic.lua @@ -161,8 +161,8 @@ function logic.update_annunciator(self) local max_rad, any_faulted = 0, false for i = 1, #self.envd do - local envd = self.envd[i] ---@type unit_session - local db = envd.get_db() ---@type envd_session_db + local envd = self.envd[i] + local db = envd.get_db() ---@type envd_session_db any_faulted = any_faulted or envd.is_faulted() if db.radiation_raw > max_rad then max_rad = db.radiation_raw end end @@ -197,7 +197,7 @@ function logic.update_annunciator(self) if num_boilers > 0 then -- go through boilers for stats and online for i = 1, #self.boilers do - local session = self.boilers[i] ---@type unit_session + local session = self.boilers[i] local boiler = session.get_db() ---@type boilerv_session_db local idx = session.get_device_idx() @@ -225,9 +225,9 @@ function logic.update_annunciator(self) -- check for inactive boilers while reactor is active for i = 1, #self.boilers do - local boiler = self.boilers[i] ---@type unit_session + local boiler = self.boilers[i] local idx = boiler.get_device_idx() - local db = boiler.get_db() ---@type boilerv_session_db + local db = boiler.get_db() ---@type boilerv_session_db if r_db.mek_status.status then annunc.HeatingRateLow[idx] = db.state.boil_rate == 0 @@ -250,9 +250,9 @@ function logic.update_annunciator(self) if num_boilers > 0 then for i = 1, #self.boilers do - local boiler = self.boilers[i] ---@type unit_session + local boiler = self.boilers[i] local idx = boiler.get_device_idx() - local db = boiler.get_db() ---@type boilerv_session_db + local db = boiler.get_db() ---@type boilerv_session_db local gaining_hc = _get_dt(DT_KEYS.BoilerHCool .. idx) > 10.0 or db.tanks.hcool_fill == 1 @@ -294,7 +294,7 @@ function logic.update_annunciator(self) -- go through turbines for stats and online for i = 1, #self.turbines do - local session = self.turbines[i] ---@type unit_session + local session = self.turbines[i] local turbine = session.get_db() ---@type turbinev_session_db local idx = session.get_device_idx() @@ -380,8 +380,8 @@ function logic.update_annunciator(self) -- turbine safety checks for i = 1, #self.turbines do - local turbine = self.turbines[i] ---@type unit_session - local db = turbine.get_db() ---@type turbinev_session_db + local turbine = self.turbines[i] + local db = turbine.get_db() ---@type turbinev_session_db local idx = turbine.get_device_idx() -- check if steam dumps are open @@ -904,8 +904,8 @@ function logic.handle_redstone(self) if not cache.rps_trip then -- set turbines to not dump steam for i = 1, #self.turbines do - local session = self.turbines[i] ---@type unit_session - local turbine = session.get_db() ---@type turbinev_session_db + local session = self.turbines[i] + local turbine = session.get_db() ---@type turbinev_session_db if turbine.state.dumping_mode ~= DUMPING_MODE.IDLE then session.get_cmd_queue().push_data(TBV_RTU_S_DATA.SET_DUMP_MODE, DUMPING_MODE.IDLE) @@ -921,8 +921,8 @@ function logic.handle_redstone(self) elseif enable_emer_cool or self.emcool_opened then -- set turbines to dump excess steam for i = 1, #self.turbines do - local session = self.turbines[i] ---@type unit_session - local turbine = session.get_db() ---@type turbinev_session_db + local session = self.turbines[i] + local turbine = session.get_db() ---@type turbinev_session_db if turbine.state.dumping_mode ~= DUMPING_MODE.DUMPING_EXCESS then session.get_cmd_queue().push_data(TBV_RTU_S_DATA.SET_DUMP_MODE, DUMPING_MODE.DUMPING_EXCESS) @@ -931,8 +931,8 @@ function logic.handle_redstone(self) -- make sure dynamic tanks are allowing outflow for i = 1, #self.tanks do - local session = self.tanks[i] ---@type unit_session - local tank = session.get_db() ---@type dynamicv_session_db + local session = self.tanks[i] + local tank = session.get_db() ---@type dynamicv_session_db if tank.state.container_mode == CONTAINER_MODE.FILL then session.get_cmd_queue().push_data(DTV_RTU_S_DATA.SET_CONT_MODE, CONTAINER_MODE.BOTH)