2022-08-28 16:12:30 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2023-05-23 23:22:22 +00:00
|
|
|
local databus = require("supervisor.databus")
|
2024-08-19 03:04:44 +00:00
|
|
|
|
|
|
|
local pgi = require("supervisor.panel.pgi")
|
2023-02-13 17:27:22 +00:00
|
|
|
|
2022-05-11 15:31:02 +00:00
|
|
|
local coordinator = require("supervisor.session.coordinator")
|
2022-05-31 19:36:17 +00:00
|
|
|
local plc = require("supervisor.session.plc")
|
2023-04-18 17:49:59 +00:00
|
|
|
local pocket = require("supervisor.session.pocket")
|
2022-05-31 19:36:17 +00:00
|
|
|
local rtu = require("supervisor.session.rtu")
|
2023-12-29 18:58:28 +00:00
|
|
|
local svqtypes = require("supervisor.session.svqtypes")
|
2022-04-22 15:07:59 +00:00
|
|
|
|
|
|
|
-- Supervisor Sessions Handler
|
|
|
|
|
2022-10-06 17:54:52 +00:00
|
|
|
local SV_Q_DATA = svqtypes.SV_Q_DATA
|
|
|
|
|
|
|
|
local PLC_S_CMDS = plc.PLC_S_CMDS
|
|
|
|
local PLC_S_DATA = plc.PLC_S_DATA
|
2022-11-06 23:41:52 +00:00
|
|
|
local CRD_S_DATA = coordinator.CRD_S_DATA
|
2022-10-03 01:17:13 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local svsessions = {}
|
|
|
|
|
2023-05-05 17:04:13 +00:00
|
|
|
---@enum SESSION_TYPE
|
2022-05-04 17:37:01 +00:00
|
|
|
local SESSION_TYPE = {
|
2023-04-18 17:49:59 +00:00
|
|
|
RTU_SESSION = 0, -- RTU gateway
|
|
|
|
PLC_SESSION = 1, -- reactor PLC
|
2023-06-05 21:24:00 +00:00
|
|
|
CRD_SESSION = 2, -- coordinator
|
2023-06-03 18:33:08 +00:00
|
|
|
PDG_SESSION = 3 -- pocket diagnostics
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
svsessions.SESSION_TYPE = SESSION_TYPE
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
local self = {
|
2023-06-25 16:59:38 +00:00
|
|
|
nic = nil, ---@type nic|nil
|
2023-06-03 19:45:48 +00:00
|
|
|
fp_ok = false,
|
2023-12-29 18:58:28 +00:00
|
|
|
config = nil, ---@type svr_config
|
2023-04-18 17:49:59 +00:00
|
|
|
facility = nil, ---@type facility|nil
|
2023-06-05 21:24:00 +00:00
|
|
|
sessions = { rtu = {}, plc = {}, crd = {}, pdg = {} },
|
2024-08-16 18:08:53 +00:00
|
|
|
next_ids = { rtu = 0, plc = 0, crd = 0, pdg = 0 },
|
2024-08-19 03:04:44 +00:00
|
|
|
dev_dbg = { duplicate = {}, out_of_range = {}, connected = {} }
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
---@alias sv_session_structs plc_session_struct|rtu_session_struct|crd_session_struct|pdg_session_struct
|
2023-04-18 17:49:59 +00:00
|
|
|
|
2024-08-21 01:32:54 +00:00
|
|
|
--#region PRIVATE FUNCTIONS
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2022-10-06 17:54:52 +00:00
|
|
|
-- handle a session output queue
|
2023-04-18 17:49:59 +00:00
|
|
|
---@param session sv_session_structs
|
2022-10-06 17:54:52 +00:00
|
|
|
local function _sv_handle_outq(session)
|
|
|
|
-- record handler start time
|
|
|
|
local handle_start = util.time()
|
|
|
|
|
|
|
|
-- process output queue
|
|
|
|
while session.out_queue.ready() do
|
|
|
|
-- get a new message to process
|
|
|
|
local msg = session.out_queue.pop()
|
|
|
|
|
|
|
|
if msg ~= nil then
|
|
|
|
if msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- handle a packet to be sent
|
2023-12-29 18:58:28 +00:00
|
|
|
self.nic.transmit(session.r_chan, self.config.SVR_Channel, msg.message)
|
2022-10-06 17:54:52 +00:00
|
|
|
elseif msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction/notification
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction/notification with body
|
|
|
|
local cmd = msg.message ---@type queue_data
|
|
|
|
|
2022-10-20 16:22:03 +00:00
|
|
|
if cmd.key < SV_Q_DATA.__END_PLC_CMDS__ then
|
|
|
|
-- PLC commands from coordinator
|
|
|
|
local plc_s = svsessions.get_reactor_session(cmd.val[1])
|
|
|
|
|
|
|
|
if plc_s ~= nil then
|
|
|
|
if cmd.key == SV_Q_DATA.START then
|
|
|
|
plc_s.in_queue.push_command(PLC_S_CMDS.ENABLE)
|
|
|
|
elseif cmd.key == SV_Q_DATA.SCRAM then
|
|
|
|
plc_s.in_queue.push_command(PLC_S_CMDS.SCRAM)
|
|
|
|
elseif cmd.key == SV_Q_DATA.RESET_RPS then
|
|
|
|
plc_s.in_queue.push_command(PLC_S_CMDS.RPS_RESET)
|
|
|
|
elseif cmd.key == SV_Q_DATA.SET_BURN and type(cmd.val) == "table" and #cmd.val == 2 then
|
|
|
|
plc_s.in_queue.push_data(PLC_S_DATA.BURN_RATE, cmd.val[2])
|
|
|
|
else
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: unknown PLC SV queue command ", cmd.key))
|
2022-10-20 16:22:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
local crd_s = svsessions.get_crd_session()
|
2023-02-20 05:49:37 +00:00
|
|
|
if crd_s ~= nil then
|
|
|
|
if cmd.key == SV_Q_DATA.CRDN_ACK then
|
|
|
|
-- ack to be sent to coordinator
|
2022-11-06 23:41:52 +00:00
|
|
|
crd_s.in_queue.push_data(CRD_S_DATA.CMD_ACK, cmd.val)
|
2023-02-20 05:49:37 +00:00
|
|
|
elseif cmd.key == SV_Q_DATA.PLC_BUILD_CHANGED then
|
|
|
|
-- a PLC build has changed
|
|
|
|
crd_s.in_queue.push_data(CRD_S_DATA.RESEND_PLC_BUILD, cmd.val)
|
|
|
|
elseif cmd.key == SV_Q_DATA.RTU_BUILD_CHANGED then
|
|
|
|
-- an RTU build has changed
|
|
|
|
crd_s.in_queue.push_data(CRD_S_DATA.RESEND_RTU_BUILD, cmd.val)
|
2022-11-06 23:41:52 +00:00
|
|
|
end
|
2022-10-06 17:54:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- max 100ms spent processing queue
|
|
|
|
if util.time() - handle_start > 100 then
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug("SVS: supervisor out queue handler exceeded 100ms queue process limit")
|
|
|
|
log.debug(util.c("SVS: offending session: ", session))
|
2022-10-06 17:54:52 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
-- iterate all the given sessions
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param sessions table
|
2022-05-04 17:37:01 +00:00
|
|
|
local function _iterate(sessions)
|
|
|
|
for i = 1, #sessions do
|
2023-04-18 17:49:59 +00:00
|
|
|
local session = sessions[i] ---@type sv_session_structs
|
2022-10-06 17:54:52 +00:00
|
|
|
|
2022-10-03 01:17:13 +00:00
|
|
|
if session.open and session.instance.iterate() then
|
2022-10-06 17:54:52 +00:00
|
|
|
_sv_handle_outq(session)
|
2022-10-03 01:17:13 +00:00
|
|
|
else
|
|
|
|
session.open = false
|
2022-05-04 17:37:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- cleanly close a session
|
2023-04-18 17:49:59 +00:00
|
|
|
---@param session sv_session_structs
|
2022-05-04 17:37:01 +00:00
|
|
|
local function _shutdown(session)
|
|
|
|
session.open = false
|
|
|
|
session.instance.close()
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- send packets in out queue (for the close packet)
|
2022-05-04 17:37:01 +00:00
|
|
|
while session.out_queue.ready() do
|
|
|
|
local msg = session.out_queue.pop()
|
2022-05-11 16:31:19 +00:00
|
|
|
if msg ~= nil and msg.qtype == mqueue.TYPE.PACKET then
|
2023-12-29 18:58:28 +00:00
|
|
|
self.nic.transmit(session.r_chan, self.config.SVR_Channel, msg.message)
|
2022-05-04 17:37:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: closed session ", session))
|
2022-05-04 17:37:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close connections
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param sessions table
|
2022-05-04 17:37:01 +00:00
|
|
|
local function _close(sessions)
|
|
|
|
for i = 1, #sessions do
|
2023-04-18 17:49:59 +00:00
|
|
|
local session = sessions[i] ---@type sv_session_structs
|
2023-04-17 23:48:03 +00:00
|
|
|
if session.open then _shutdown(session) end
|
2022-05-04 17:37:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if a watchdog timer event matches that of one of the provided sessions
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param sessions table
|
|
|
|
---@param timer_event number
|
2022-05-04 17:37:01 +00:00
|
|
|
local function _check_watchdogs(sessions, timer_event)
|
|
|
|
for i = 1, #sessions do
|
2023-04-18 17:49:59 +00:00
|
|
|
local session = sessions[i] ---@type sv_session_structs
|
2022-05-04 17:37:01 +00:00
|
|
|
if session.open then
|
|
|
|
local triggered = session.instance.check_wd(timer_event)
|
|
|
|
if triggered then
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: watchdog closing session ", session, "..."))
|
2022-05-04 17:37:01 +00:00
|
|
|
_shutdown(session)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- delete any closed sessions
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param sessions table
|
2022-05-04 17:37:01 +00:00
|
|
|
local function _free_closed(sessions)
|
2022-05-16 21:11:46 +00:00
|
|
|
local f = function (session) return session.open end
|
2022-10-03 01:17:13 +00:00
|
|
|
|
2023-04-18 17:49:59 +00:00
|
|
|
---@param session sv_session_structs
|
2022-10-03 01:17:13 +00:00
|
|
|
local on_delete = function (session)
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: free'ing closed session ", session))
|
2022-10-03 01:17:13 +00:00
|
|
|
end
|
2022-05-16 21:11:46 +00:00
|
|
|
|
|
|
|
util.filter_table(sessions, f, on_delete)
|
2022-05-04 17:37:01 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- find a session by computer ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-21 16:30:14 +00:00
|
|
|
---@param list table
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param s_addr integer
|
2023-04-18 17:49:59 +00:00
|
|
|
---@return sv_session_structs|nil
|
2023-06-05 21:24:00 +00:00
|
|
|
local function _find_session(list, s_addr)
|
2022-05-21 16:30:14 +00:00
|
|
|
for i = 1, #list do
|
2023-06-05 21:24:00 +00:00
|
|
|
if list[i].s_addr == s_addr then return list[i] end
|
2022-05-21 16:30:14 +00:00
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2024-08-16 18:08:53 +00:00
|
|
|
local function _update_dev_dbg()
|
|
|
|
local f = function (unit) return unit.is_connected() end
|
|
|
|
|
2024-08-16 21:19:25 +00:00
|
|
|
util.filter_table(self.dev_dbg.duplicate, f, pgi.delete_chk_entry)
|
|
|
|
util.filter_table(self.dev_dbg.out_of_range, f, pgi.delete_chk_entry)
|
2024-08-19 03:04:44 +00:00
|
|
|
|
|
|
|
local conns = self.dev_dbg.connected
|
|
|
|
local units = self.facility.get_units()
|
|
|
|
for i = 1, #units do
|
|
|
|
local unit = units[i] ---@type reactor_unit
|
|
|
|
local rtus = unit.check_rtu_conns()
|
|
|
|
|
|
|
|
end
|
2024-08-16 18:08:53 +00:00
|
|
|
end
|
|
|
|
|
2024-08-21 01:32:54 +00:00
|
|
|
--#endregion
|
|
|
|
|
|
|
|
--#region PUBLIC FUNCTIONS
|
2024-08-16 18:08:53 +00:00
|
|
|
|
|
|
|
---@param unit unit_session RTU session
|
|
|
|
---@param list table table of RTU sessions
|
|
|
|
---@param max integer max of this type of RTU
|
|
|
|
---@return 0|1|2|3 fail_code, string fail_str 0 = success, 1 = out-of-range, 2 = duplicate, 3 = exceeded table max
|
2024-08-21 01:32:54 +00:00
|
|
|
function svsessions.check_rtu_id(unit, list, max)
|
2024-08-16 18:08:53 +00:00
|
|
|
local fail_code, fail_str = 0, "OK"
|
|
|
|
|
|
|
|
if (unit.get_device_idx() < 1 and max ~= 1) or unit.get_device_idx() > max then
|
|
|
|
-- out-of-range
|
|
|
|
fail_code, fail_str = 1, "index out of range"
|
|
|
|
table.insert(self.dev_dbg.out_of_range, unit)
|
|
|
|
else
|
|
|
|
for _, u in ipairs(list) do
|
|
|
|
if u.get_device_idx() == unit.get_device_idx() then
|
|
|
|
-- duplicate
|
|
|
|
fail_code, fail_str = 2, "duplicate index"
|
|
|
|
table.insert(self.dev_dbg.duplicate, unit)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- make sure this won't exceed the maximum allowable devices
|
|
|
|
if fail_code == 0 and #list >= max then
|
|
|
|
fail_code, fail_str = 3, "too many of this type"
|
|
|
|
end
|
|
|
|
|
2024-08-16 21:19:25 +00:00
|
|
|
-- add to the list for the user
|
2024-08-19 03:04:44 +00:00
|
|
|
if fail_code > 0 and fail_code ~= 3 then
|
2024-08-16 21:19:25 +00:00
|
|
|
local cmp_id
|
|
|
|
|
|
|
|
for i = 1, #self.sessions.rtu do
|
|
|
|
if self.sessions.rtu[i].instance.get_id() == unit.get_session_id() then
|
|
|
|
cmp_id = self.sessions.rtu[i].s_addr
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pgi.create_chk_entry(unit, fail_code, cmp_id)
|
|
|
|
end
|
|
|
|
|
2024-08-16 18:08:53 +00:00
|
|
|
return fail_code, fail_str
|
|
|
|
end
|
|
|
|
|
2022-08-28 16:12:30 +00:00
|
|
|
-- initialize svsessions
|
2023-06-25 16:59:38 +00:00
|
|
|
---@param nic nic network interface device
|
2023-06-03 19:45:48 +00:00
|
|
|
---@param fp_ok boolean front panel active
|
2023-12-29 18:58:28 +00:00
|
|
|
---@param config svr_config supervisor configuration
|
2024-08-21 01:32:54 +00:00
|
|
|
---@param facility facility
|
|
|
|
function svsessions.init(nic, fp_ok, config, facility)
|
2023-06-25 16:59:38 +00:00
|
|
|
self.nic = nic
|
2023-06-03 19:45:48 +00:00
|
|
|
self.fp_ok = fp_ok
|
2023-12-29 18:58:28 +00:00
|
|
|
self.config = config
|
2024-08-21 01:32:54 +00:00
|
|
|
self.facility = facility
|
2024-08-19 03:04:44 +00:00
|
|
|
|
|
|
|
-- initialize connection tracking table
|
|
|
|
self.dev_dbg.connected = { imatrix = nil, sps = nil, tanks = {}, units = {} }
|
|
|
|
for i = 1, config.UnitCount do
|
|
|
|
self.dev_dbg.connected.units[i] = { boilers = {}, turbines = {}, tanks = {} }
|
|
|
|
end
|
2022-08-28 16:12:30 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- find an RTU session by the computer ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param source_addr integer
|
2022-05-13 15:38:56 +00:00
|
|
|
---@return rtu_session_struct|nil
|
2023-06-05 21:24:00 +00:00
|
|
|
function svsessions.find_rtu_session(source_addr)
|
2022-05-13 15:38:56 +00:00
|
|
|
-- check RTU sessions
|
2023-06-05 21:24:00 +00:00
|
|
|
local session = _find_session(self.sessions.rtu, source_addr)
|
2023-04-20 00:35:42 +00:00
|
|
|
---@cast session rtu_session_struct|nil
|
2023-02-25 04:36:16 +00:00
|
|
|
return session
|
2022-05-13 15:38:56 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- find a PLC session by the computer ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param source_addr integer
|
2022-05-13 15:38:56 +00:00
|
|
|
---@return plc_session_struct|nil
|
2023-06-05 21:24:00 +00:00
|
|
|
function svsessions.find_plc_session(source_addr)
|
2022-05-13 15:38:56 +00:00
|
|
|
-- check PLC sessions
|
2023-06-05 21:24:00 +00:00
|
|
|
local session = _find_session(self.sessions.plc, source_addr)
|
2023-04-20 00:35:42 +00:00
|
|
|
---@cast session plc_session_struct|nil
|
2023-02-25 04:36:16 +00:00
|
|
|
return session
|
2022-05-13 15:38:56 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- find a coordinator session by the computer ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param source_addr integer
|
|
|
|
---@return crd_session_struct|nil
|
|
|
|
function svsessions.find_crd_session(source_addr)
|
2023-06-05 05:13:22 +00:00
|
|
|
-- check coordinator sessions
|
2023-06-05 21:24:00 +00:00
|
|
|
local session = _find_session(self.sessions.crd, source_addr)
|
|
|
|
---@cast session crd_session_struct|nil
|
2023-02-25 04:36:16 +00:00
|
|
|
return session
|
2022-05-13 15:38:56 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
-- find a pocket diagnostics session by the computer ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param source_addr integer
|
2023-06-05 05:18:13 +00:00
|
|
|
---@return pdg_session_struct|nil
|
2023-06-05 21:24:00 +00:00
|
|
|
function svsessions.find_pdg_session(source_addr)
|
2023-04-20 00:35:42 +00:00
|
|
|
-- check diagnostic sessions
|
2023-06-05 22:10:53 +00:00
|
|
|
local session = _find_session(self.sessions.pdg, source_addr)
|
2023-06-05 05:18:13 +00:00
|
|
|
---@cast session pdg_session_struct|nil
|
2023-02-25 04:36:16 +00:00
|
|
|
return session
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-10-20 17:53:39 +00:00
|
|
|
-- get the a coordinator session if exists
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-06-05 21:24:00 +00:00
|
|
|
---@return crd_session_struct|nil
|
|
|
|
function svsessions.get_crd_session()
|
|
|
|
return self.sessions.crd[1]
|
2022-10-20 17:53:39 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get a session by reactor ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param reactor integer
|
2022-05-13 15:38:56 +00:00
|
|
|
---@return plc_session_struct|nil session
|
2022-05-31 19:36:17 +00:00
|
|
|
function svsessions.get_reactor_session(reactor)
|
2022-04-22 15:07:59 +00:00
|
|
|
local session = nil
|
|
|
|
|
2023-04-18 17:49:59 +00:00
|
|
|
for i = 1, #self.sessions.plc do
|
|
|
|
if self.sessions.plc[i].reactor == reactor then
|
|
|
|
session = self.sessions.plc[i]
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return session
|
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- establish a new PLC session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param source_addr integer PLC computer ID
|
2024-06-29 18:10:58 +00:00
|
|
|
---@param i_seq_num integer initial (most recent) sequence number
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param for_reactor integer unit ID
|
|
|
|
---@param version string PLC version
|
2022-05-11 16:31:19 +00:00
|
|
|
---@return integer|false session_id
|
2024-06-29 02:27:55 +00:00
|
|
|
function svsessions.establish_plc_session(source_addr, i_seq_num, for_reactor, version)
|
2023-12-31 01:18:58 +00:00
|
|
|
if svsessions.get_reactor_session(for_reactor) == nil and for_reactor >= 1 and for_reactor <= self.config.UnitCount then
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class plc_session_struct
|
2022-04-22 15:07:59 +00:00
|
|
|
local plc_s = {
|
2022-10-06 17:54:52 +00:00
|
|
|
s_type = "plc",
|
2022-04-22 15:07:59 +00:00
|
|
|
open = true,
|
|
|
|
reactor = for_reactor,
|
2022-05-19 14:21:04 +00:00
|
|
|
version = version,
|
2023-12-29 18:58:28 +00:00
|
|
|
r_chan = self.config.PLC_Channel,
|
2023-06-05 21:24:00 +00:00
|
|
|
s_addr = source_addr,
|
2022-04-22 15:07:59 +00:00
|
|
|
in_queue = mqueue.new(),
|
|
|
|
out_queue = mqueue.new(),
|
2022-10-20 16:22:03 +00:00
|
|
|
instance = nil ---@type plc_session
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local id = self.next_ids.plc
|
|
|
|
|
2024-06-29 02:27:55 +00:00
|
|
|
plc_s.instance = plc.new_session(id, source_addr, i_seq_num, for_reactor, plc_s.in_queue, plc_s.out_queue, self.config.PLC_Timeout, self.fp_ok)
|
2023-04-18 17:49:59 +00:00
|
|
|
table.insert(self.sessions.plc, plc_s)
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-12-18 18:56:04 +00:00
|
|
|
local units = self.facility.get_units()
|
|
|
|
units[for_reactor].link_plc_session(plc_s)
|
2022-08-28 16:12:30 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local mt = {
|
|
|
|
---@param s plc_session_struct
|
2023-12-29 18:58:28 +00:00
|
|
|
__tostring = function (s) return util.c("PLC [", s.instance.get_id(), "] for reactor #", s.reactor, " (@", s.s_addr, ")") end
|
2023-06-05 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(plc_s, mt)
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
databus.tx_plc_connected(for_reactor, version, source_addr)
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: established new session: ", plc_s))
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
self.next_ids.plc = id + 1
|
2022-04-22 15:07:59 +00:00
|
|
|
|
|
|
|
-- success
|
|
|
|
return plc_s.instance.get_id()
|
|
|
|
else
|
2022-10-06 17:54:52 +00:00
|
|
|
-- reactor already assigned to a PLC or ID out of range
|
2022-04-22 15:07:59 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-29 02:27:55 +00:00
|
|
|
-- establish a new RTU gateway session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param source_addr integer RTU gateway computer ID
|
2024-06-29 18:10:58 +00:00
|
|
|
---@param i_seq_num integer initial (most recent) sequence number
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param advertisement table RTU capability advertisement
|
|
|
|
---@param version string RTU gateway version
|
2022-05-13 13:45:11 +00:00
|
|
|
---@return integer session_id
|
2024-06-29 02:27:55 +00:00
|
|
|
function svsessions.establish_rtu_session(source_addr, i_seq_num, advertisement, version)
|
2022-05-13 13:45:11 +00:00
|
|
|
---@class rtu_session_struct
|
|
|
|
local rtu_s = {
|
2022-10-06 17:54:52 +00:00
|
|
|
s_type = "rtu",
|
2022-05-13 13:45:11 +00:00
|
|
|
open = true,
|
2022-05-19 14:21:04 +00:00
|
|
|
version = version,
|
2023-12-29 18:58:28 +00:00
|
|
|
r_chan = self.config.RTU_Channel,
|
2023-06-05 21:24:00 +00:00
|
|
|
s_addr = source_addr,
|
2022-05-13 13:45:11 +00:00
|
|
|
in_queue = mqueue.new(),
|
|
|
|
out_queue = mqueue.new(),
|
2022-10-20 16:22:03 +00:00
|
|
|
instance = nil ---@type rtu_session
|
2022-05-13 13:45:11 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local id = self.next_ids.rtu
|
|
|
|
|
2024-06-29 02:27:55 +00:00
|
|
|
rtu_s.instance = rtu.new_session(id, source_addr, i_seq_num, rtu_s.in_queue, rtu_s.out_queue, self.config.RTU_Timeout, advertisement, self.facility, self.fp_ok)
|
2023-04-18 17:49:59 +00:00
|
|
|
table.insert(self.sessions.rtu, rtu_s)
|
2022-05-13 13:45:11 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local mt = {
|
|
|
|
---@param s rtu_session_struct
|
2023-06-06 23:45:04 +00:00
|
|
|
__tostring = function (s) return util.c("RTU [", s.instance.get_id(), "] (@", s.s_addr, ")") end
|
2023-06-05 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(rtu_s, mt)
|
2022-05-13 13:45:11 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
databus.tx_rtu_connected(id, version, source_addr)
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: established new session: ", rtu_s))
|
2023-06-03 18:33:08 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
self.next_ids.rtu = id + 1
|
2022-05-13 13:45:11 +00:00
|
|
|
|
|
|
|
-- success
|
2023-06-05 21:24:00 +00:00
|
|
|
return id
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
|
|
|
|
2022-07-07 04:34:42 +00:00
|
|
|
-- establish a new coordinator session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param source_addr integer coordinator computer ID
|
2024-06-29 18:10:58 +00:00
|
|
|
---@param i_seq_num integer initial (most recent) sequence number
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param version string coordinator version
|
2022-07-07 04:34:42 +00:00
|
|
|
---@return integer|false session_id
|
2024-06-29 02:27:55 +00:00
|
|
|
function svsessions.establish_crd_session(source_addr, i_seq_num, version)
|
2023-06-05 21:24:00 +00:00
|
|
|
if svsessions.get_crd_session() == nil then
|
|
|
|
---@class crd_session_struct
|
|
|
|
local crd_s = {
|
2022-10-20 17:53:39 +00:00
|
|
|
s_type = "crd",
|
|
|
|
open = true,
|
|
|
|
version = version,
|
2023-12-29 18:58:28 +00:00
|
|
|
r_chan = self.config.CRD_Channel,
|
2023-06-05 21:24:00 +00:00
|
|
|
s_addr = source_addr,
|
2022-10-20 17:53:39 +00:00
|
|
|
in_queue = mqueue.new(),
|
|
|
|
out_queue = mqueue.new(),
|
2023-06-05 21:24:00 +00:00
|
|
|
instance = nil ---@type crd_session
|
2022-10-20 17:53:39 +00:00
|
|
|
}
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local id = self.next_ids.crd
|
|
|
|
|
2024-06-29 02:27:55 +00:00
|
|
|
crd_s.instance = coordinator.new_session(id, source_addr, i_seq_num, crd_s.in_queue, crd_s.out_queue, self.config.CRD_Timeout, self.facility, self.fp_ok)
|
2023-06-05 21:24:00 +00:00
|
|
|
table.insert(self.sessions.crd, crd_s)
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local mt = {
|
|
|
|
---@param s crd_session_struct
|
2023-06-06 23:45:04 +00:00
|
|
|
__tostring = function (s) return util.c("CRD [", s.instance.get_id(), "] (@", s.s_addr, ")") end
|
2023-06-05 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(crd_s, mt)
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
databus.tx_crd_connected(version, source_addr)
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: established new session: ", crd_s))
|
2023-05-23 23:22:22 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
self.next_ids.crd = id + 1
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2022-10-20 17:53:39 +00:00
|
|
|
-- success
|
2023-06-05 21:24:00 +00:00
|
|
|
return id
|
2022-10-20 17:53:39 +00:00
|
|
|
else
|
|
|
|
-- we already have a coordinator linked
|
|
|
|
return false
|
|
|
|
end
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
2023-04-18 17:49:59 +00:00
|
|
|
-- establish a new pocket diagnostics session
|
|
|
|
---@nodiscard
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param source_addr integer pocket computer ID
|
2024-06-29 18:10:58 +00:00
|
|
|
---@param i_seq_num integer initial (most recent) sequence number
|
2024-06-29 02:27:55 +00:00
|
|
|
---@param version string pocket version
|
2023-04-18 17:49:59 +00:00
|
|
|
---@return integer|false session_id
|
2024-06-29 02:27:55 +00:00
|
|
|
function svsessions.establish_pdg_session(source_addr, i_seq_num, version)
|
2023-06-03 18:33:08 +00:00
|
|
|
---@class pdg_session_struct
|
|
|
|
local pdg_s = {
|
2023-04-18 17:49:59 +00:00
|
|
|
s_type = "pkt",
|
|
|
|
open = true,
|
|
|
|
version = version,
|
2023-12-29 18:58:28 +00:00
|
|
|
r_chan = self.config.PKT_Channel,
|
2023-06-05 21:24:00 +00:00
|
|
|
s_addr = source_addr,
|
2023-04-18 17:49:59 +00:00
|
|
|
in_queue = mqueue.new(),
|
|
|
|
out_queue = mqueue.new(),
|
2023-06-03 18:33:08 +00:00
|
|
|
instance = nil ---@type pdg_session
|
2023-04-18 17:49:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local id = self.next_ids.pdg
|
|
|
|
|
2024-06-29 02:27:55 +00:00
|
|
|
pdg_s.instance = pocket.new_session(id, source_addr, i_seq_num, pdg_s.in_queue, pdg_s.out_queue, self.config.PKT_Timeout, self.facility, self.fp_ok)
|
2023-06-03 18:33:08 +00:00
|
|
|
table.insert(self.sessions.pdg, pdg_s)
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
local mt = {
|
|
|
|
---@param s pdg_session_struct
|
2023-06-06 23:45:04 +00:00
|
|
|
__tostring = function (s) return util.c("PDG [", s.instance.get_id(), "] (@", s.s_addr, ")") end
|
2023-06-05 21:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(pdg_s, mt)
|
2023-04-18 17:49:59 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
databus.tx_pdg_connected(id, version, source_addr)
|
2023-08-26 15:54:58 +00:00
|
|
|
log.debug(util.c("SVS: established new session: ", pdg_s))
|
2023-04-18 17:49:59 +00:00
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
self.next_ids.pdg = id + 1
|
2023-04-18 17:49:59 +00:00
|
|
|
|
|
|
|
-- success
|
2023-06-05 21:24:00 +00:00
|
|
|
return id
|
2023-04-18 17:49:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- attempt to identify which session's watchdog timer fired
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param timer_event number
|
2022-05-31 19:36:17 +00:00
|
|
|
function svsessions.check_all_watchdogs(timer_event)
|
2023-04-18 17:49:59 +00:00
|
|
|
for _, list in pairs(self.sessions) do _check_watchdogs(list, timer_event) end
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
|
2023-04-18 17:49:59 +00:00
|
|
|
-- iterate all sessions, and update facility/unit data & process control logic
|
2022-05-31 19:36:17 +00:00
|
|
|
function svsessions.iterate_all()
|
2023-04-18 17:49:59 +00:00
|
|
|
-- iterate sessions
|
|
|
|
for _, list in pairs(self.sessions) do _iterate(list) end
|
2022-08-28 16:12:30 +00:00
|
|
|
|
2023-02-16 00:52:28 +00:00
|
|
|
-- report RTU sessions to facility
|
2023-04-18 17:49:59 +00:00
|
|
|
self.facility.report_rtus(self.sessions.rtu)
|
2023-02-16 00:52:28 +00:00
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
-- iterate facility
|
|
|
|
self.facility.update()
|
|
|
|
|
2022-08-28 16:12:30 +00:00
|
|
|
-- iterate units
|
2022-12-18 18:56:04 +00:00
|
|
|
self.facility.update_units()
|
2024-08-16 18:08:53 +00:00
|
|
|
|
|
|
|
_update_dev_dbg()
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- delete all closed sessions
|
2022-05-31 19:36:17 +00:00
|
|
|
function svsessions.free_all_closed()
|
2023-04-18 17:49:59 +00:00
|
|
|
for _, list in pairs(self.sessions) do _free_closed(list) end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2022-05-02 15:42:24 +00:00
|
|
|
|
|
|
|
-- close all open connections
|
2022-05-31 19:36:17 +00:00
|
|
|
function svsessions.close_all()
|
2022-05-02 15:42:24 +00:00
|
|
|
-- close sessions
|
2023-06-05 21:24:00 +00:00
|
|
|
for _, list in pairs(self.sessions) do _close(list) end
|
2022-05-02 15:42:24 +00:00
|
|
|
|
|
|
|
-- free sessions
|
2022-05-04 17:37:01 +00:00
|
|
|
svsessions.free_all_closed()
|
2022-05-02 15:42:24 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2024-08-21 01:32:54 +00:00
|
|
|
--#endregion
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return svsessions
|