diff --git a/supervisor/session/plc.lua b/supervisor/session/plc.lua index c36dbda..f799987 100644 --- a/supervisor/session/plc.lua +++ b/supervisor/session/plc.lua @@ -202,6 +202,12 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) self.sDB.mek_struct.max_burn = mek_data[8] end + -- mark this PLC session as closed, stop watchdog + local _close = function () + self.rtu_conn_watchdog.cancel() + self.connected = false + end + -- send an RPLC packet ---@param msg_type RPLC_TYPES ---@param msg table @@ -392,7 +398,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) end elseif pkt.type == SCADA_MGMT_TYPES.CLOSE then -- close the session - self.connected = false + _close() else log.debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type) end @@ -427,13 +433,12 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- check if a timer matches this session's watchdog public.check_wd = function (timer) - return self.plc_conn_watchdog.is_timer(timer) + return self.plc_conn_watchdog.is_timer(timer) and self.connected end -- close the connection public.close = function () - self.plc_conn_watchdog.cancel() - self.connected = false + _close() _send_mgmt(SCADA_MGMT_TYPES.CLOSE, {}) println("connection to reactor " .. self.for_reactor .. " PLC closed by server") log.info(log_header .. "session closed by server") @@ -506,7 +511,6 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- exit if connection was closed if not self.connected then - self.plc_conn_watchdog.cancel() println("connection to reactor " .. self.for_reactor .. " PLC closed by remote host") log.info(log_header .. "session closed by remote host") return self.connected diff --git a/supervisor/session/rtu.lua b/supervisor/session/rtu.lua index 59cf0b1..33a66d7 100644 --- a/supervisor/session/rtu.lua +++ b/supervisor/session/rtu.lua @@ -129,6 +129,17 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) end end + -- mark this RTU session as closed, stop watchdog + local _close = function () + self.rtu_conn_watchdog.cancel() + self.connected = false + + -- mark all RTU unit sessions as closed so the reactor unit knows + for i = 1, #self.units do + self.units[i].close() + end + end + -- send a SCADA management packet ---@param msg_type SCADA_MGMT_TYPES ---@param msg table @@ -186,7 +197,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) end elseif pkt.type == SCADA_MGMT_TYPES.CLOSE then -- close the session - self.connected = false + _close() elseif pkt.type == SCADA_MGMT_TYPES.RTU_ADVERT then -- RTU unit advertisement -- handle advertisement; this will re-create all unit sub-sessions @@ -206,13 +217,12 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- check if a timer matches this session's watchdog ---@param timer number public.check_wd = function (timer) - return self.rtu_conn_watchdog.is_timer(timer) + return self.rtu_conn_watchdog.is_timer(timer) and self.connected end -- close the connection public.close = function () - self.rtu_conn_watchdog.cancel() - self.connected = false + _close() _send_mgmt(SCADA_MGMT_TYPES.CLOSE, {}) println(log_header .. "connection to RTU closed by server") log.info(log_header .. "session closed by server") @@ -272,7 +282,6 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- exit if connection was closed if not self.connected then - self.rtu_conn_watchdog.cancel() println(log_header .. "connection to RTU closed by remote host") log.info(log_header .. "session closed by remote host") return self.connected diff --git a/supervisor/session/rtu/boiler.lua b/supervisor/session/rtu/boiler.lua index c0dd0e6..2b8047f 100644 --- a/supervisor/session/rtu/boiler.lua +++ b/supervisor/session/rtu/boiler.lua @@ -42,6 +42,7 @@ boiler.new = function (session_id, advert, out_queue) reactor = advert.reactor, out_q = out_queue, transaction_controller = txnctrl.new(), + connected = true, has_build = false, periodics = { next_build_req = 0, @@ -180,6 +181,9 @@ boiler.new = function (session_id, advert, out_queue) public.get_reactor = function () return self.reactor end public.get_db = function () return self.db end + public.close = function () self.connected = false end + public.is_connected = function () return self.connected end + -- update this runner ---@param time_now integer milliseconds public.update = function (time_now) diff --git a/supervisor/session/rtu/emachine.lua b/supervisor/session/rtu/emachine.lua index cdbf21e..8e2a2e8 100644 --- a/supervisor/session/rtu/emachine.lua +++ b/supervisor/session/rtu/emachine.lua @@ -41,6 +41,7 @@ emachine.new = function (session_id, advert, out_queue) reactor = 0, out_q = out_queue, transaction_controller = txnctrl.new(), + connected = true, has_build = false, periodics = { next_build_req = 0, @@ -130,6 +131,9 @@ emachine.new = function (session_id, advert, out_queue) public.get_reactor = function () return self.reactor end public.get_db = function () return self.db end + public.close = function () self.connected = false end + public.is_connected = function () return self.connected end + -- update this runner ---@param time_now integer milliseconds public.update = function (time_now) diff --git a/supervisor/session/rtu/redstone.lua b/supervisor/session/rtu/redstone.lua index 4380567..b5f44ea 100644 --- a/supervisor/session/rtu/redstone.lua +++ b/supervisor/session/rtu/redstone.lua @@ -60,6 +60,7 @@ redstone.new = function (session_id, advert, out_queue) in_q = mqueue.new(), out_q = out_queue, transaction_controller = txnctrl.new(), + connected = true, has_di = false, has_ai = false, periodics = { @@ -192,6 +193,9 @@ redstone.new = function (session_id, advert, out_queue) public.get_reactor = function () return self.reactor end public.get_db = function () return self.db end + public.close = function () self.connected = false end + public.is_connected = function () return self.connected end + -- update this runner ---@param time_now integer milliseconds public.update = function (time_now) diff --git a/supervisor/session/rtu/turbine.lua b/supervisor/session/rtu/turbine.lua index 292ff95..e6d41c2 100644 --- a/supervisor/session/rtu/turbine.lua +++ b/supervisor/session/rtu/turbine.lua @@ -43,6 +43,7 @@ turbine.new = function (session_id, advert, out_queue) reactor = advert.reactor, out_q = out_queue, transaction_controller = txnctrl.new(), + connected = true, has_build = false, periodics = { next_build_req = 0, @@ -171,6 +172,9 @@ turbine.new = function (session_id, advert, out_queue) public.get_reactor = function () return self.reactor end public.get_db = function () return self.db end + public.close = function () self.connected = false end + public.is_connected = function () return self.connected end + -- update this runner ---@param time_now integer milliseconds public.update = function (time_now)