Merge branch 'devel' into 225-consolidate-network-channels
This commit is contained in:
@@ -4,6 +4,8 @@ local mqueue = require("scada-common.mqueue")
|
||||
local types = require("scada-common.types")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local databus = require("supervisor.databus")
|
||||
|
||||
local svqtypes = require("supervisor.session.svqtypes")
|
||||
|
||||
local coordinator = {}
|
||||
@@ -18,8 +20,6 @@ local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
||||
|
||||
local SV_Q_DATA = svqtypes.SV_Q_DATA
|
||||
|
||||
local println = util.println
|
||||
|
||||
-- retry time constants in ms
|
||||
-- local INITIAL_WAIT = 1500
|
||||
local RETRY_PERIOD = 1000
|
||||
@@ -49,7 +49,11 @@ local PERIODICS = {
|
||||
---@param out_queue mqueue out message queue
|
||||
---@param timeout number communications timeout
|
||||
---@param facility facility facility data table
|
||||
function coordinator.new_session(id, in_queue, out_queue, timeout, facility)
|
||||
---@param fp_ok boolean if the front panel UI is running
|
||||
function coordinator.new_session(id, in_queue, out_queue, timeout, facility, fp_ok)
|
||||
-- print a log message to the terminal as long as the UI isn't running
|
||||
local function println(message) if not fp_ok then util.println_ts(message) end end
|
||||
|
||||
local log_header = "crdn_session(" .. id .. "): "
|
||||
|
||||
local self = {
|
||||
@@ -84,6 +88,7 @@ function coordinator.new_session(id, in_queue, out_queue, timeout, facility)
|
||||
local function _close()
|
||||
self.conn_watchdog.cancel()
|
||||
self.connected = false
|
||||
databus.tx_crd_disconnected()
|
||||
end
|
||||
|
||||
-- send a CRDN packet
|
||||
@@ -205,6 +210,8 @@ function coordinator.new_session(id, in_queue, out_queue, timeout, facility)
|
||||
|
||||
-- log.debug(log_header .. "COORD RTT = " .. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "COORD TT = " .. (srv_now - coord_send) .. "ms")
|
||||
|
||||
databus.tx_crd_rtt(self.last_rtt)
|
||||
else
|
||||
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
||||
end
|
||||
|
||||
@@ -4,6 +4,8 @@ local mqueue = require("scada-common.mqueue")
|
||||
local types = require("scada-common.types")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local databus = require("supervisor.databus")
|
||||
|
||||
local svqtypes = require("supervisor.session.svqtypes")
|
||||
|
||||
local plc = {}
|
||||
@@ -14,8 +16,6 @@ local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
|
||||
local PLC_AUTO_ACK = comms.PLC_AUTO_ACK
|
||||
local UNIT_COMMAND = comms.UNIT_COMMAND
|
||||
|
||||
local println = util.println
|
||||
|
||||
-- retry time constants in ms
|
||||
local INITIAL_WAIT = 1500
|
||||
local INITIAL_AUTO_WAIT = 1000
|
||||
@@ -49,7 +49,11 @@ local PERIODICS = {
|
||||
---@param in_queue mqueue in message queue
|
||||
---@param out_queue mqueue out message queue
|
||||
---@param timeout number communications timeout
|
||||
function plc.new_session(id, reactor_id, in_queue, out_queue, timeout)
|
||||
---@param fp_ok boolean if the front panel UI is running
|
||||
function plc.new_session(id, reactor_id, in_queue, out_queue, timeout, fp_ok)
|
||||
-- print a log message to the terminal as long as the UI isn't running
|
||||
local function println(message) if not fp_ok then util.println_ts(message) end end
|
||||
|
||||
local log_header = "plc_session(" .. id .. "): "
|
||||
|
||||
local self = {
|
||||
@@ -235,6 +239,7 @@ function plc.new_session(id, reactor_id, in_queue, out_queue, timeout)
|
||||
local function _close()
|
||||
self.conn_watchdog.cancel()
|
||||
self.connected = false
|
||||
databus.tx_plc_disconnected(reactor_id)
|
||||
end
|
||||
|
||||
-- send an RPLC packet
|
||||
@@ -485,6 +490,8 @@ function plc.new_session(id, reactor_id, in_queue, out_queue, timeout)
|
||||
|
||||
-- log.debug(log_header .. "PLC RTT = " .. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "PLC TT = " .. (srv_now - plc_send) .. "ms")
|
||||
|
||||
databus.tx_plc_rtt(reactor_id, self.last_rtt)
|
||||
else
|
||||
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
||||
end
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
local comms = require("scada-common.comms")
|
||||
local log = require("scada-common.log")
|
||||
local mqueue = require("scada-common.mqueue")
|
||||
local util = require("scada-common.util")
|
||||
local comms = require("scada-common.comms")
|
||||
local log = require("scada-common.log")
|
||||
local mqueue = require("scada-common.mqueue")
|
||||
local util = require("scada-common.util")
|
||||
local databus = require("supervisor.databus")
|
||||
|
||||
local pocket = {}
|
||||
|
||||
local PROTOCOL = comms.PROTOCOL
|
||||
local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
|
||||
|
||||
local println = util.println
|
||||
|
||||
-- retry time constants in ms
|
||||
-- local INITIAL_WAIT = 1500
|
||||
-- local RETRY_PERIOD = 1000
|
||||
@@ -33,8 +32,12 @@ local PERIODICS = {
|
||||
---@param in_queue mqueue in message queue
|
||||
---@param out_queue mqueue out message queue
|
||||
---@param timeout number communications timeout
|
||||
function pocket.new_session(id, in_queue, out_queue, timeout)
|
||||
local log_header = "diag_session(" .. id .. "): "
|
||||
---@param fp_ok boolean if the front panel UI is running
|
||||
function pocket.new_session(id, in_queue, out_queue, timeout, fp_ok)
|
||||
-- print a log message to the terminal as long as the UI isn't running
|
||||
local function println(message) if not fp_ok then util.println_ts(message) end end
|
||||
|
||||
local log_header = "pdg_session(" .. id .. "): "
|
||||
|
||||
local self = {
|
||||
-- connection properties
|
||||
@@ -55,18 +58,19 @@ function pocket.new_session(id, in_queue, out_queue, timeout)
|
||||
acks = {
|
||||
},
|
||||
-- session database
|
||||
---@class diag_db
|
||||
---@class pdg_db
|
||||
sDB = {
|
||||
}
|
||||
}
|
||||
|
||||
---@class diag_session
|
||||
---@class pdg_session
|
||||
local public = {}
|
||||
|
||||
-- mark this diagnostics session as closed, stop watchdog
|
||||
local function _close()
|
||||
self.conn_watchdog.cancel()
|
||||
self.connected = false
|
||||
databus.tx_pdg_disconnected(id)
|
||||
end
|
||||
|
||||
-- send a SCADA management packet
|
||||
@@ -106,16 +110,18 @@ function pocket.new_session(id, in_queue, out_queue, timeout)
|
||||
-- keep alive reply
|
||||
if pkt.length == 2 then
|
||||
local srv_start = pkt.data[1]
|
||||
-- local diag_send = pkt.data[2]
|
||||
-- local pdg_send = pkt.data[2]
|
||||
local srv_now = util.time()
|
||||
self.last_rtt = srv_now - srv_start
|
||||
|
||||
if self.last_rtt > 750 then
|
||||
log.warning(log_header .. "DIAG KEEP_ALIVE round trip time > 750ms (" .. self.last_rtt .. "ms)")
|
||||
log.warning(log_header .. "PDG KEEP_ALIVE round trip time > 750ms (" .. self.last_rtt .. "ms)")
|
||||
end
|
||||
|
||||
-- log.debug(log_header .. "DIAG RTT = " .. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "DIAG TT = " .. (srv_now - diag_send) .. "ms")
|
||||
-- log.debug(log_header .. "PDG RTT = " .. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "PDG TT = " .. (srv_now - pdg_send) .. "ms")
|
||||
|
||||
databus.tx_pdg_rtt(id, self.last_rtt)
|
||||
else
|
||||
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
||||
end
|
||||
|
||||
@@ -4,6 +4,8 @@ local mqueue = require("scada-common.mqueue")
|
||||
local types = require("scada-common.types")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local databus = require("supervisor.databus")
|
||||
|
||||
local svqtypes = require("supervisor.session.svqtypes")
|
||||
|
||||
-- supervisor rtu sessions (svrs)
|
||||
@@ -22,8 +24,6 @@ local PROTOCOL = comms.PROTOCOL
|
||||
local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
|
||||
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
||||
|
||||
local println = util.println
|
||||
|
||||
local PERIODICS = {
|
||||
KEEP_ALIVE = 2000
|
||||
}
|
||||
@@ -36,7 +36,11 @@ local PERIODICS = {
|
||||
---@param timeout number communications timeout
|
||||
---@param advertisement table RTU device advertisement
|
||||
---@param facility facility facility data table
|
||||
function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facility)
|
||||
---@param fp_ok boolean if the front panel UI is running
|
||||
function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facility, fp_ok)
|
||||
-- print a log message to the terminal as long as the UI isn't running
|
||||
local function println(message) if not fp_ok then util.println_ts(message) end end
|
||||
|
||||
local log_header = "rtu_session(" .. id .. "): "
|
||||
|
||||
local self = {
|
||||
@@ -66,6 +70,8 @@ function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facili
|
||||
|
||||
-- parse the recorded advertisement and create unit sub-sessions
|
||||
local function _handle_advertisement()
|
||||
local unit_count = 0
|
||||
|
||||
_reset_config()
|
||||
|
||||
for i = 1, #self.fac_units do
|
||||
@@ -171,24 +177,26 @@ function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facili
|
||||
end
|
||||
|
||||
if unit ~= nil then
|
||||
table.insert(self.units, unit)
|
||||
self.units[i] = unit
|
||||
unit_count = unit_count + 1
|
||||
elseif u_type ~= RTU_UNIT_TYPE.VIRTUAL then
|
||||
_reset_config()
|
||||
log.error(util.c(log_header, "bad advertisement: error occured while creating a unit (type is ", type_string, ")"))
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
databus.tx_rtu_units(id, unit_count)
|
||||
end
|
||||
|
||||
-- mark this RTU session as closed, stop watchdog
|
||||
local function _close()
|
||||
self.conn_watchdog.cancel()
|
||||
self.connected = false
|
||||
databus.tx_rtu_disconnected(id)
|
||||
|
||||
-- mark all RTU unit sessions as closed so the reactor unit knows
|
||||
for i = 1, #self.units do
|
||||
self.units[i].close()
|
||||
end
|
||||
for _, unit in pairs(self.units) do unit.close() end
|
||||
end
|
||||
|
||||
-- send a MODBUS packet
|
||||
@@ -256,6 +264,8 @@ function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facili
|
||||
|
||||
-- log.debug(log_header .. "RTU RTT = " .. self.last_rtt .. "ms")
|
||||
-- log.debug(log_header .. "RTU TT = " .. (srv_now - rtu_send) .. "ms")
|
||||
|
||||
databus.tx_rtu_rtt(id, self.last_rtt)
|
||||
else
|
||||
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
||||
end
|
||||
@@ -351,9 +361,7 @@ function rtu.new_session(id, in_queue, out_queue, timeout, advertisement, facili
|
||||
|
||||
local time_now = util.time()
|
||||
|
||||
for i = 1, #self.units do
|
||||
self.units[i].update(time_now)
|
||||
end
|
||||
for _, unit in pairs(self.units) do unit.update(time_now) end
|
||||
|
||||
----------------------
|
||||
-- update periodics --
|
||||
|
||||
@@ -3,6 +3,7 @@ local mqueue = require("scada-common.mqueue")
|
||||
local util = require("scada-common.util")
|
||||
|
||||
local config = require("supervisor.config")
|
||||
local databus = require("supervisor.databus")
|
||||
local facility = require("supervisor.facility")
|
||||
|
||||
local svqtypes = require("supervisor.session.svqtypes")
|
||||
@@ -22,24 +23,26 @@ local CRD_S_DATA = coordinator.CRD_S_DATA
|
||||
|
||||
local svsessions = {}
|
||||
|
||||
---@enum SESSION_TYPE
|
||||
local SESSION_TYPE = {
|
||||
RTU_SESSION = 0, -- RTU gateway
|
||||
PLC_SESSION = 1, -- reactor PLC
|
||||
COORD_SESSION = 2, -- coordinator
|
||||
DIAG_SESSION = 3 -- pocket diagnostics
|
||||
PDG_SESSION = 3 -- pocket diagnostics
|
||||
}
|
||||
|
||||
svsessions.SESSION_TYPE = SESSION_TYPE
|
||||
|
||||
local self = {
|
||||
modem = nil, ---@type table|nil
|
||||
fp_ok = false,
|
||||
num_reactors = 0,
|
||||
facility = nil, ---@type facility|nil
|
||||
sessions = { rtu = {}, plc = {}, coord = {}, diag = {} },
|
||||
next_ids = { rtu = 0, plc = 0, coord = 0, diag = 0 }
|
||||
sessions = { rtu = {}, plc = {}, coord = {}, pdg = {} },
|
||||
next_ids = { rtu = 0, plc = 0, coord = 0, pdg = 0 }
|
||||
}
|
||||
|
||||
---@alias sv_session_structs plc_session_struct|rtu_session_struct|coord_session_struct|diag_session_struct
|
||||
---@alias sv_session_structs plc_session_struct|rtu_session_struct|coord_session_struct|pdg_session_struct
|
||||
|
||||
-- PRIVATE FUNCTIONS --
|
||||
|
||||
@@ -194,11 +197,13 @@ end
|
||||
-- PUBLIC FUNCTIONS --
|
||||
|
||||
-- initialize svsessions
|
||||
---@param modem table
|
||||
---@param num_reactors integer
|
||||
---@param cooling_conf table
|
||||
function svsessions.init(modem, num_reactors, cooling_conf)
|
||||
---@param modem table modem device
|
||||
---@param fp_ok boolean front panel active
|
||||
---@param num_reactors integer number of reactors
|
||||
---@param cooling_conf table cooling configuration definition
|
||||
function svsessions.init(modem, fp_ok, num_reactors, cooling_conf)
|
||||
self.modem = modem
|
||||
self.fp_ok = fp_ok
|
||||
self.num_reactors = num_reactors
|
||||
self.facility = facility.new(num_reactors, cooling_conf)
|
||||
end
|
||||
@@ -245,12 +250,11 @@ end
|
||||
-- find a pocket diagnostics session by the remote port
|
||||
---@nodiscard
|
||||
---@param remote_port integer
|
||||
---@return diag_session_struct|nil
|
||||
---@return pdg_session_struct|nil
|
||||
function svsessions.find_pdg_session(remote_port)
|
||||
-- check diagnostic sessions
|
||||
local session = _find_session(self.sessions.diag, remote_port)
|
||||
---@cast session diag_session_struct|nil
|
||||
|
||||
---@cast session pdg_session_struct|nil
|
||||
return session
|
||||
end
|
||||
|
||||
@@ -299,13 +303,17 @@ function svsessions.establish_plc_session(local_port, remote_port, for_reactor,
|
||||
instance = nil ---@type plc_session
|
||||
}
|
||||
|
||||
plc_s.instance = plc.new_session(self.next_ids.plc, for_reactor, plc_s.in_queue, plc_s.out_queue, config.PLC_TIMEOUT)
|
||||
plc_s.instance = plc.new_session(self.next_ids.plc, for_reactor, plc_s.in_queue, plc_s.out_queue,
|
||||
config.PLC_TIMEOUT, self.fp_ok)
|
||||
table.insert(self.sessions.plc, plc_s)
|
||||
|
||||
local units = self.facility.get_units()
|
||||
units[for_reactor].link_plc_session(plc_s)
|
||||
|
||||
log.debug(util.c("established new PLC session to ", remote_port, " with ID ", self.next_ids.plc, " for reactor ", for_reactor))
|
||||
log.debug(util.c("established new PLC session to ", remote_port, " with ID ", self.next_ids.plc,
|
||||
" for reactor ", for_reactor))
|
||||
|
||||
databus.tx_plc_connected(for_reactor, version, remote_port)
|
||||
|
||||
self.next_ids.plc = self.next_ids.plc + 1
|
||||
|
||||
@@ -337,11 +345,14 @@ function svsessions.establish_rtu_session(local_port, remote_port, advertisement
|
||||
instance = nil ---@type rtu_session
|
||||
}
|
||||
|
||||
rtu_s.instance = rtu.new_session(self.next_ids.rtu, rtu_s.in_queue, rtu_s.out_queue, config.RTU_TIMEOUT, advertisement, self.facility)
|
||||
rtu_s.instance = rtu.new_session(self.next_ids.rtu, rtu_s.in_queue, rtu_s.out_queue, config.RTU_TIMEOUT, advertisement,
|
||||
self.facility, self.fp_ok)
|
||||
table.insert(self.sessions.rtu, rtu_s)
|
||||
|
||||
log.debug("established new RTU session to " .. remote_port .. " with ID " .. self.next_ids.rtu)
|
||||
|
||||
databus.tx_rtu_connected(self.next_ids.rtu, version, remote_port)
|
||||
|
||||
self.next_ids.rtu = self.next_ids.rtu + 1
|
||||
|
||||
-- success
|
||||
@@ -368,11 +379,14 @@ function svsessions.establish_coord_session(local_port, remote_port, version)
|
||||
instance = nil ---@type coord_session
|
||||
}
|
||||
|
||||
coord_s.instance = coordinator.new_session(self.next_ids.coord, coord_s.in_queue, coord_s.out_queue, config.CRD_TIMEOUT, self.facility)
|
||||
coord_s.instance = coordinator.new_session(self.next_ids.coord, coord_s.in_queue, coord_s.out_queue, config.CRD_TIMEOUT,
|
||||
self.facility, self.fp_ok)
|
||||
table.insert(self.sessions.coord, coord_s)
|
||||
|
||||
log.debug("established new coordinator session to " .. remote_port .. " with ID " .. self.next_ids.coord)
|
||||
|
||||
databus.tx_crd_connected(version, remote_port)
|
||||
|
||||
self.next_ids.coord = self.next_ids.coord + 1
|
||||
|
||||
-- success
|
||||
@@ -389,9 +403,9 @@ end
|
||||
---@param remote_port integer
|
||||
---@param version string
|
||||
---@return integer|false session_id
|
||||
function svsessions.establish_diag_session(local_port, remote_port, version)
|
||||
---@class diag_session_struct
|
||||
local diag_s = {
|
||||
function svsessions.establish_pdg_session(local_port, remote_port, version)
|
||||
---@class pdg_session_struct
|
||||
local pdg_s = {
|
||||
s_type = "pkt",
|
||||
open = true,
|
||||
version = version,
|
||||
@@ -399,18 +413,20 @@ function svsessions.establish_diag_session(local_port, remote_port, version)
|
||||
r_port = remote_port,
|
||||
in_queue = mqueue.new(),
|
||||
out_queue = mqueue.new(),
|
||||
instance = nil ---@type diag_session
|
||||
instance = nil ---@type pdg_session
|
||||
}
|
||||
|
||||
diag_s.instance = pocket.new_session(self.next_ids.diag, diag_s.in_queue, diag_s.out_queue, config.PKT_TIMEOUT)
|
||||
table.insert(self.sessions.diag, diag_s)
|
||||
pdg_s.instance = pocket.new_session(self.next_ids.pdg, pdg_s.in_queue, pdg_s.out_queue, config.PKT_TIMEOUT, self.fp_ok)
|
||||
table.insert(self.sessions.pdg, pdg_s)
|
||||
|
||||
log.debug("established new pocket diagnostics session to " .. remote_port .. " with ID " .. self.next_ids.diag)
|
||||
log.debug("established new pocket diagnostics session to " .. remote_port .. " with ID " .. self.next_ids.pdg)
|
||||
|
||||
self.next_ids.diag = self.next_ids.diag + 1
|
||||
databus.tx_pdg_connected(self.next_ids.pdg, version, remote_port)
|
||||
|
||||
self.next_ids.pdg = self.next_ids.pdg + 1
|
||||
|
||||
-- success
|
||||
return diag_s.instance.get_id()
|
||||
return pdg_s.instance.get_id()
|
||||
end
|
||||
|
||||
-- attempt to identify which session's watchdog timer fired
|
||||
|
||||
Reference in New Issue
Block a user