From 5a8bba51080d773225587986b5629125714fab86 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 3 Sep 2022 11:51:27 -0400 Subject: [PATCH] #85 handle loss of supervisor conn or comms modem --- coordinator/coordinator.lua | 8 +++-- coordinator/database.lua | 4 --- coordinator/renderer.lua | 3 -- coordinator/startup.lua | 70 ++++++++++++++++++++++++++----------- 4 files changed, 55 insertions(+), 30 deletions(-) diff --git a/coordinator/coordinator.lua b/coordinator/coordinator.lua index 0b62ed2..7ce510f 100644 --- a/coordinator/coordinator.lua +++ b/coordinator/coordinator.lua @@ -403,12 +403,12 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa -- acknowledge receipt of builds _send_sv(PROTOCOLS.SCADA_CRDN, SCADA_CRDN_TYPES.STRUCT_BUILDS, {}) else - log.error("supervisor build packet invalid") + log.error("received invalid build packet") end elseif packet.type == SCADA_CRDN_TYPES.UNIT_STATUSES then -- update statuses if not database.update_statuses(packet.data) then - log.error("supervisor unit status packet invalid") + log.error("received invalid unit statuses packet") end elseif packet.type == SCADA_CRDN_TYPES.COMMAND_UNIT then elseif packet.type == SCADA_CRDN_TYPES.ALARM then @@ -435,6 +435,7 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa elseif packet.type == SCADA_MGMT_TYPES.CLOSE then -- handle session close sv_watchdog.cancel() + self.sv_linked = false println_ts("server connection closed by remote host") log.warning("server connection closed by remote host") else @@ -448,6 +449,9 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa end end + -- check if the coordinator is still linked to the supervisor + function public.is_linked() return self.sv_linked end + return public end diff --git a/coordinator/database.lua b/coordinator/database.lua index 1edda7b..ecb72b3 100644 --- a/coordinator/database.lua +++ b/coordinator/database.lua @@ -8,10 +8,6 @@ database.WASTE = { Pu = 0, Po = 1, AntiMatter = 2 } ---@class coord_db local db = {} --- @todo -function database.purge_subscribers() -end - -- initialize the coordinator database ---@param conf facility_conf configuration function database.init(conf) diff --git a/coordinator/renderer.lua b/coordinator/renderer.lua index 9683f0d..986c0e0 100644 --- a/coordinator/renderer.lua +++ b/coordinator/renderer.lua @@ -93,9 +93,6 @@ end -- close out the UI ---@param recolor? boolean true to restore to color palette from style function renderer.close_ui(recolor) - -- delete all subscribers - database.purge_subscribers() - -- report ui as not ready engine.ui_ready = false diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 7b72f25..150547c 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -108,39 +108,52 @@ log_comms("comms initialized") local MAIN_CLOCK = 0.5 local loop_clock = util.new_clock(MAIN_CLOCK) -local tick_waiting, task_done = log_comms_connecting("attempting to connect to configured supervisor on channel " .. config.SCADA_SV_PORT) +-- attempt to connect to the supervisor or exit +local function init_connect_sv() + local tick_waiting, task_done = log_comms_connecting("attempting to connect to configured supervisor on channel " .. config.SCADA_SV_PORT) --- attempt to establish a connection with the supervisory computer -if not coord_comms.sv_connect(60, tick_waiting, task_done) then - log_comms("supervisor connection failed") - println("boot> failed to connect to supervisor") - log.fatal("failed to connect to supervisor") - log_sys("system shutdown") - return + -- attempt to establish a connection with the supervisory computer + if not coord_comms.sv_connect(60, tick_waiting, task_done) then + log_comms("supervisor connection failed") + println("boot> failed to connect to supervisor") + log.fatal("failed to connect to supervisor") + log_sys("system shutdown") + return + end end +init_connect_sv() + ---------------------------------------- -- start the UI ---------------------------------------- -log_graphics("starting UI...") --- util.psleep(3) +-- start up the UI +---@return boolean ui_ok started ok +local function init_start_ui() + log_graphics("starting UI...") + -- util.psleep(3) -local draw_start = util.time_ms() + local draw_start = util.time_ms() -local ui_ok, message = pcall(renderer.start_ui) -if not ui_ok then - renderer.close_ui(config.RECOLOR) - log_graphics(util.c("UI crashed: ", message)) - println_ts("UI crashed") - log.fatal(util.c("ui crashed with error ", message)) -else - log_graphics("first UI draw took " .. (util.time_ms() - draw_start) .. "ms") + local ui_ok, message = pcall(renderer.start_ui) + if not ui_ok then + renderer.close_ui(config.RECOLOR) + log_graphics(util.c("UI crashed: ", message)) + println_ts("UI crashed") + log.fatal(util.c("ui crashed with error ", message)) + else + log_graphics("first UI draw took " .. (util.time_ms() - draw_start) .. "ms") - -- start clock - loop_clock.start() + -- start clock + loop_clock.start() + end + + return ui_ok end +local ui_ok = init_start_ui() + ---------------------------------------- -- main event loop ---------------------------------------- @@ -165,6 +178,9 @@ while ui_ok do log_sys("comms modem disconnected") println_ts("wireless modem disconnected!") log.error("comms modem disconnected!") + + -- close out UI + renderer.close_ui() else log_sys("non-comms modem disconnected") log.warning("non-comms modem disconnected") @@ -185,6 +201,10 @@ while ui_ok do log_sys("comms modem reconnected") println_ts("wireless modem reconnected.") + + -- re-init system + init_connect_sv() + ui_ok = init_start_ui() else log_sys("wired modem reconnected") end @@ -206,6 +226,14 @@ while ui_ok do log_comms(msg) println_ts(msg) log.warning(msg) + + -- close connection and UI + coord_comms.close() + renderer.close_ui() + + -- try to re-connect to the supervisor + init_connect_sv() + ui_ok = init_start_ui() else -- a non-clock/main watchdog timer event