From f14d7150707b18cb65a24eedcd2471c2e938219c Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 27 Apr 2022 18:52:06 -0400 Subject: [PATCH] #7 PLC session comms link, accept statuses, functional keep-alives --- supervisor/session/plc.lua | 49 ++++++++++++++++--------------- supervisor/session/svsessions.lua | 6 ++-- supervisor/startup.lua | 9 +++--- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/supervisor/session/plc.lua b/supervisor/session/plc.lua index 3a51666..48c4a53 100644 --- a/supervisor/session/plc.lua +++ b/supervisor/session/plc.lua @@ -13,7 +13,7 @@ PLC_S_COMMANDS = { } local PERIODICS = { - KEEP_ALIVE = 1.0 + KEEP_ALIVE = 2.0 } -- PLC supervisor session @@ -117,19 +117,15 @@ function new_session(id, for_reactor, in_queue, out_queue) self.sDB.mek_status.env_loss = mek_data[7] self.sDB.mek_status.fuel = mek_data[8] - self.sDB.mek_status.fuel_need = mek_data[9] - self.sDB.mek_status.fuel_fill = mek_data[10] - self.sDB.mek_status.waste = mek_data[11] - self.sDB.mek_status.waste_need = mek_data[12] - self.sDB.mek_status.waste_fill = mek_data[13] - self.sDB.mek_status.cool_type = mek_data[14] - self.sDB.mek_status.cool_amnt = mek_data[15] - self.sDB.mek_status.cool_need = mek_data[16] - self.sDB.mek_status.cool_fill = mek_data[17] - self.sDB.mek_status.hcool_type = mek_data[18] - self.sDB.mek_status.hcool_amnt = mek_data[19] - self.sDB.mek_status.hcool_need = mek_data[20] - self.sDB.mek_status.hcool_fill = mek_data[21] + self.sDB.mek_status.fuel_fill = mek_data[9] + self.sDB.mek_status.waste = mek_data[10] + self.sDB.mek_status.waste_fill = mek_data[11] + self.sDB.mek_status.cool_type = mek_data[12] + self.sDB.mek_status.cool_amnt = mek_data[13] + self.sDB.mek_status.cool_fill = mek_data[14] + self.sDB.mek_status.hcool_type = mek_data[15] + self.sDB.mek_status.hcool_amnt = mek_data[16] + self.sDB.mek_status.hcool_fill = mek_data[17] end local _copy_struct = function (mek_data) @@ -152,12 +148,9 @@ function new_session(id, for_reactor, in_queue, out_queue) end end - local _handle_packet = function (message) + local _handle_packet = function (rplc_pkt) local checks_ok = true - -- handle an incoming packet from the PLC - rplc_pkt = message.get() - -- check sequence number if self.r_seq_num == nil then self.r_seq_num = rplc_pkt.scada_frame.seq_num() @@ -189,12 +182,13 @@ function new_session(id, for_reactor, in_queue, out_queue) self.last_rtt = srv_now - srv_start if self.last_rtt < 0 then - log._warning(log_header .. "PLC KEEP_ALIVE round trip time less than 0 (" .. trip_time .. ")") - elseif trip_time > 1000 then - log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 1s (" .. trip_time .. ")") + log._warning(log_header .. "PLC KEEP_ALIVE round trip time less than 0 (" .. self.last_rtt .. ")") + elseif self.last_rtt > 1200 then + log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 1.2s (" .. self.last_rtt .. ")") end - log._debug(log_header .. "RPLC RTT = ".. trip_time .. "ms") + -- log._debug(log_header .. "RPLC RTT = ".. self.last_rtt .. "ms") + -- log._debug(log_header .. "RPLC TT = ".. (srv_now - plc_send) .. "ms") else log._debug(log_header .. "RPLC keep alive packet length mismatch") end @@ -330,16 +324,25 @@ function new_session(id, for_reactor, in_queue, out_queue) -- handle queue -- ------------------ - if self.in_q.ready() then + local handle_start = util.time() + + while self.in_q.ready() do -- get a new message to process local message = self.in_q.pop() if message.qtype == mqueue.TYPE.PACKET then + -- handle a packet _handle_packet(message.message) elseif message.qtype == mqueue.TYPE.COMMAND then -- handle instruction end + + -- max 100ms spent processing queue + if util.time() - handle_start > 100 then + log._warning(log_header .. "exceeded 100ms queue process limit") + break + end end ---------------------- diff --git a/supervisor/session/svsessions.lua b/supervisor/session/svsessions.lua index 9c02ce0..76fdea3 100644 --- a/supervisor/session/svsessions.lua +++ b/supervisor/session/svsessions.lua @@ -34,19 +34,19 @@ end function find_session(stype, remote_port) if stype == SESSION_TYPE.RTU_SESSION then for i = 1, #self.rtu_sessions do - if self.rtu_sessions[i].r_host == remote_port then + if self.rtu_sessions[i].r_port == remote_port then return self.rtu_sessions[i] end end elseif stype == SESSION_TYPE.PLC_SESSION then for i = 1, #self.plc_sessions do - if self.plc_sessions[i].r_host == remote_port then + if self.plc_sessions[i].r_port == remote_port then return self.plc_sessions[i] end end elseif stype == SESSION_TYPE.COORD_SESSION then for i = 1, #self.coord_sessions do - if self.coord_sessions[i].r_host == remote_port then + if self.coord_sessions[i].r_port == remote_port then return self.coord_sessions[i] end end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 138cfd8..71f0eae 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -18,7 +18,7 @@ os.loadAPI("session/svsessions.lua") os.loadAPI("supervisor.lua") -local SUPERVISOR_VERSION = "alpha-v0.1.6" +local SUPERVISOR_VERSION = "alpha-v0.1.7" local print = util.print local println = util.println @@ -43,8 +43,9 @@ end -- start comms, open all channels local superv_comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN) --- base loop clock (4Hz, 5 ticks) -local loop_clock = os.startTimer(0.25) +-- base loop clock (6.67Hz, 3 ticks) +local MAIN_CLOCK = 0.15 +local loop_clock = os.startTimer(MAIN_CLOCK) -- event loop while true do @@ -87,7 +88,7 @@ while true do -- free any closed sessions svsessions.free_all_closed() - loop_clock = os.startTimer(0.25) + loop_clock = os.startTimer(MAIN_CLOCK) elseif event == "timer" then -- another timer event, check watchdogs svsessions.check_all_watchdogs(param1)