#7 PLC session comms link, accept statuses, functional keep-alives

This commit is contained in:
Mikayla Fischler 2022-04-27 18:52:06 -04:00
parent 7f0f423450
commit f14d715070
3 changed files with 34 additions and 30 deletions

View File

@ -13,7 +13,7 @@ PLC_S_COMMANDS = {
} }
local PERIODICS = { local PERIODICS = {
KEEP_ALIVE = 1.0 KEEP_ALIVE = 2.0
} }
-- PLC supervisor session -- 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.env_loss = mek_data[7]
self.sDB.mek_status.fuel = mek_data[8] 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[9]
self.sDB.mek_status.fuel_fill = mek_data[10] self.sDB.mek_status.waste = mek_data[10]
self.sDB.mek_status.waste = mek_data[11] self.sDB.mek_status.waste_fill = mek_data[11]
self.sDB.mek_status.waste_need = mek_data[12] self.sDB.mek_status.cool_type = mek_data[12]
self.sDB.mek_status.waste_fill = mek_data[13] self.sDB.mek_status.cool_amnt = mek_data[13]
self.sDB.mek_status.cool_type = mek_data[14] self.sDB.mek_status.cool_fill = mek_data[14]
self.sDB.mek_status.cool_amnt = mek_data[15] self.sDB.mek_status.hcool_type = mek_data[15]
self.sDB.mek_status.cool_need = mek_data[16] self.sDB.mek_status.hcool_amnt = mek_data[16]
self.sDB.mek_status.cool_fill = mek_data[17] self.sDB.mek_status.hcool_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]
end end
local _copy_struct = function (mek_data) local _copy_struct = function (mek_data)
@ -152,12 +148,9 @@ function new_session(id, for_reactor, in_queue, out_queue)
end end
end end
local _handle_packet = function (message) local _handle_packet = function (rplc_pkt)
local checks_ok = true local checks_ok = true
-- handle an incoming packet from the PLC
rplc_pkt = message.get()
-- check sequence number -- check sequence number
if self.r_seq_num == nil then if self.r_seq_num == nil then
self.r_seq_num = rplc_pkt.scada_frame.seq_num() 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 self.last_rtt = srv_now - srv_start
if self.last_rtt < 0 then if self.last_rtt < 0 then
log._warning(log_header .. "PLC KEEP_ALIVE round trip time less than 0 (" .. trip_time .. ")") log._warning(log_header .. "PLC KEEP_ALIVE round trip time less than 0 (" .. self.last_rtt .. ")")
elseif trip_time > 1000 then elseif self.last_rtt > 1200 then
log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 1s (" .. trip_time .. ")") log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 1.2s (" .. self.last_rtt .. ")")
end 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 else
log._debug(log_header .. "RPLC keep alive packet length mismatch") log._debug(log_header .. "RPLC keep alive packet length mismatch")
end end
@ -330,16 +324,25 @@ function new_session(id, for_reactor, in_queue, out_queue)
-- handle 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 -- get a new message to process
local message = self.in_q.pop() local message = self.in_q.pop()
if message.qtype == mqueue.TYPE.PACKET then if message.qtype == mqueue.TYPE.PACKET then
-- handle a packet
_handle_packet(message.message) _handle_packet(message.message)
elseif message.qtype == mqueue.TYPE.COMMAND then elseif message.qtype == mqueue.TYPE.COMMAND then
-- handle instruction -- handle instruction
end 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 end
---------------------- ----------------------

View File

@ -34,19 +34,19 @@ end
function find_session(stype, remote_port) function find_session(stype, remote_port)
if stype == SESSION_TYPE.RTU_SESSION then if stype == SESSION_TYPE.RTU_SESSION then
for i = 1, #self.rtu_sessions do 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] return self.rtu_sessions[i]
end end
end end
elseif stype == SESSION_TYPE.PLC_SESSION then elseif stype == SESSION_TYPE.PLC_SESSION then
for i = 1, #self.plc_sessions do 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] return self.plc_sessions[i]
end end
end end
elseif stype == SESSION_TYPE.COORD_SESSION then elseif stype == SESSION_TYPE.COORD_SESSION then
for i = 1, #self.coord_sessions do 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] return self.coord_sessions[i]
end end
end end

View File

@ -18,7 +18,7 @@ os.loadAPI("session/svsessions.lua")
os.loadAPI("supervisor.lua") os.loadAPI("supervisor.lua")
local SUPERVISOR_VERSION = "alpha-v0.1.6" local SUPERVISOR_VERSION = "alpha-v0.1.7"
local print = util.print local print = util.print
local println = util.println local println = util.println
@ -43,8 +43,9 @@ end
-- start comms, open all channels -- start comms, open all channels
local superv_comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN) local superv_comms = supervisor.superv_comms(config.NUM_REACTORS, modem, config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN)
-- base loop clock (4Hz, 5 ticks) -- base loop clock (6.67Hz, 3 ticks)
local loop_clock = os.startTimer(0.25) local MAIN_CLOCK = 0.15
local loop_clock = os.startTimer(MAIN_CLOCK)
-- event loop -- event loop
while true do while true do
@ -87,7 +88,7 @@ while true do
-- free any closed sessions -- free any closed sessions
svsessions.free_all_closed() svsessions.free_all_closed()
loop_clock = os.startTimer(0.25) loop_clock = os.startTimer(MAIN_CLOCK)
elseif event == "timer" then elseif event == "timer" then
-- another timer event, check watchdogs -- another timer event, check watchdogs
svsessions.check_all_watchdogs(param1) svsessions.check_all_watchdogs(param1)