2022-08-28 16:12:30 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
2023-02-21 17:27:16 +00:00
|
|
|
local types = require("scada-common.types")
|
2022-08-28 16:12:30 +00:00
|
|
|
local util = require("scada-common.util")
|
2022-05-09 13:35:39 +00:00
|
|
|
|
2023-06-03 18:33:08 +00:00
|
|
|
local databus = require("supervisor.databus")
|
|
|
|
|
2022-10-03 01:17:13 +00:00
|
|
|
local svqtypes = require("supervisor.session.svqtypes")
|
|
|
|
|
2022-05-11 17:02:21 +00:00
|
|
|
-- supervisor rtu sessions (svrs)
|
2022-11-15 02:43:02 +00:00
|
|
|
local unit_session = require("supervisor.session.rtu.unit_session")
|
2022-06-04 21:59:24 +00:00
|
|
|
local svrs_boilerv = require("supervisor.session.rtu.boilerv")
|
2022-06-04 15:11:35 +00:00
|
|
|
local svrs_envd = require("supervisor.session.rtu.envd")
|
2022-06-04 21:45:52 +00:00
|
|
|
local svrs_imatrix = require("supervisor.session.rtu.imatrix")
|
2022-05-15 00:27:06 +00:00
|
|
|
local svrs_redstone = require("supervisor.session.rtu.redstone")
|
2022-06-04 20:25:23 +00:00
|
|
|
local svrs_sna = require("supervisor.session.rtu.sna")
|
2022-06-04 19:33:04 +00:00
|
|
|
local svrs_sps = require("supervisor.session.rtu.sps")
|
2022-06-05 13:30:56 +00:00
|
|
|
local svrs_turbinev = require("supervisor.session.rtu.turbinev")
|
2022-05-11 17:02:21 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local rtu = {}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
local PROTOCOL = comms.PROTOCOL
|
|
|
|
local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
|
2023-02-21 17:27:16 +00:00
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
2022-05-09 13:35:39 +00:00
|
|
|
|
2022-05-10 16:01:56 +00:00
|
|
|
local PERIODICS = {
|
2022-07-07 17:18:10 +00:00
|
|
|
KEEP_ALIVE = 2000
|
2022-05-10 16:01:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
-- create a new RTU session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-02-13 17:27:22 +00:00
|
|
|
---@param id integer session ID
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param s_addr integer device source address
|
2023-02-13 17:27:22 +00:00
|
|
|
---@param in_queue mqueue in message queue
|
|
|
|
---@param out_queue mqueue out message queue
|
|
|
|
---@param timeout number communications timeout
|
|
|
|
---@param advertisement table RTU device advertisement
|
|
|
|
---@param facility facility facility data table
|
2023-06-03 19:45:48 +00:00
|
|
|
---@param fp_ok boolean if the front panel UI is running
|
2023-06-05 21:24:00 +00:00
|
|
|
function rtu.new_session(id, s_addr, in_queue, out_queue, timeout, advertisement, facility, fp_ok)
|
2023-06-03 19:45:48 +00:00
|
|
|
-- 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
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
local log_header = "rtu_session(" .. id .. "): "
|
|
|
|
|
|
|
|
local self = {
|
2022-09-19 02:25:59 +00:00
|
|
|
modbus_q = mqueue.new(),
|
2022-05-13 13:45:11 +00:00
|
|
|
advert = advertisement,
|
2022-12-18 18:56:04 +00:00
|
|
|
fac_units = facility.get_units(),
|
2022-05-09 13:35:39 +00:00
|
|
|
-- connection properties
|
|
|
|
seq_num = 0,
|
|
|
|
r_seq_num = nil,
|
|
|
|
connected = true,
|
2023-04-18 17:49:59 +00:00
|
|
|
conn_watchdog = util.new_watchdog(timeout),
|
2022-05-13 13:45:11 +00:00
|
|
|
last_rtt = 0,
|
2022-08-28 16:12:30 +00:00
|
|
|
-- periodic messages
|
|
|
|
periodics = {
|
|
|
|
last_update = 0,
|
|
|
|
keep_alive = 0
|
|
|
|
},
|
2022-05-13 13:45:11 +00:00
|
|
|
units = {}
|
2022-05-09 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
---@class rtu_session
|
|
|
|
local public = {}
|
|
|
|
|
2022-06-05 13:30:56 +00:00
|
|
|
local function _reset_config()
|
|
|
|
self.units = {}
|
|
|
|
end
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
-- parse the recorded advertisement and create unit sub-sessions
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _handle_advertisement()
|
2023-06-03 18:33:08 +00:00
|
|
|
local unit_count = 0
|
|
|
|
|
2022-11-12 06:35:31 +00:00
|
|
|
_reset_config()
|
|
|
|
|
2022-12-18 18:56:04 +00:00
|
|
|
for i = 1, #self.fac_units do
|
|
|
|
local unit = self.fac_units[i] ---@type reactor_unit
|
2022-12-10 18:58:17 +00:00
|
|
|
unit.purge_rtu_devices(id)
|
|
|
|
facility.purge_rtu_devices(id)
|
2022-11-12 06:35:31 +00:00
|
|
|
end
|
2022-05-16 15:54:34 +00:00
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
for i = 1, #self.advert do
|
2023-04-12 20:02:29 +00:00
|
|
|
local unit = nil ---@type unit_session|nil
|
2022-05-13 13:45:11 +00:00
|
|
|
|
|
|
|
---@type rtu_advertisement
|
|
|
|
local unit_advert = {
|
2022-05-15 00:27:06 +00:00
|
|
|
type = self.advert[i][1],
|
|
|
|
index = self.advert[i][2],
|
|
|
|
reactor = self.advert[i][3],
|
|
|
|
rsio = self.advert[i][4]
|
2022-05-13 13:45:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
local u_type = unit_advert.type ---@type integer|boolean
|
2022-05-13 13:45:11 +00:00
|
|
|
|
2022-06-05 20:53:36 +00:00
|
|
|
-- validate unit advertisement
|
|
|
|
|
|
|
|
local advert_validator = util.new_validator()
|
|
|
|
advert_validator.assert_type_int(unit_advert.index)
|
|
|
|
advert_validator.assert_type_int(unit_advert.reactor)
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if u_type == RTU_UNIT_TYPE.REDSTONE then
|
2022-06-05 20:53:36 +00:00
|
|
|
advert_validator.assert_type_table(unit_advert.rsio)
|
|
|
|
end
|
|
|
|
|
|
|
|
if advert_validator.valid() then
|
|
|
|
advert_validator.assert_min(unit_advert.index, 1)
|
2022-12-10 18:58:17 +00:00
|
|
|
advert_validator.assert_min(unit_advert.reactor, 0)
|
2022-12-18 18:56:04 +00:00
|
|
|
advert_validator.assert_max(unit_advert.reactor, #self.fac_units)
|
2022-06-05 20:53:36 +00:00
|
|
|
if not advert_validator.valid() then u_type = false end
|
|
|
|
else
|
|
|
|
u_type = false
|
|
|
|
end
|
|
|
|
|
2022-12-10 18:58:17 +00:00
|
|
|
local type_string = util.strval(u_type)
|
2023-02-21 17:27:16 +00:00
|
|
|
if type(u_type) == "number" then type_string = types.rtu_type_to_string(u_type) end
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2022-06-05 20:53:36 +00:00
|
|
|
-- create unit by type
|
|
|
|
|
|
|
|
if u_type == false then
|
|
|
|
-- validation fail
|
2022-11-12 06:35:31 +00:00
|
|
|
log.debug(log_header .. "advertisement unit validation failure")
|
2022-05-15 00:27:06 +00:00
|
|
|
else
|
2022-12-10 18:58:17 +00:00
|
|
|
if unit_advert.reactor > 0 then
|
2022-12-18 18:56:04 +00:00
|
|
|
local target_unit = self.fac_units[unit_advert.reactor] ---@type reactor_unit
|
2022-12-10 18:58:17 +00:00
|
|
|
|
2023-05-10 20:40:52 +00:00
|
|
|
-- unit RTUs
|
2023-02-21 16:05:57 +00:00
|
|
|
if u_type == RTU_UNIT_TYPE.REDSTONE then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- redstone
|
|
|
|
unit = svrs_redstone.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then target_unit.add_redstone(unit) end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.BOILER_VALVE then
|
2023-05-10 20:40:52 +00:00
|
|
|
-- boiler
|
2022-12-10 18:58:17 +00:00
|
|
|
unit = svrs_boilerv.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then target_unit.add_boiler(unit) end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.TURBINE_VALVE then
|
2023-05-10 20:40:52 +00:00
|
|
|
-- turbine
|
2022-12-10 18:58:17 +00:00
|
|
|
unit = svrs_turbinev.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then target_unit.add_turbine(unit) end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.ENV_DETECTOR then
|
2023-02-14 03:11:31 +00:00
|
|
|
-- environment detector
|
|
|
|
unit = svrs_envd.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then target_unit.add_envd(unit) end
|
2023-05-10 20:40:52 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.VIRTUAL then
|
|
|
|
-- skip virtual units
|
|
|
|
log.debug(util.c(log_header, "skipping virtual RTU unit #", i))
|
2022-12-10 18:58:17 +00:00
|
|
|
else
|
|
|
|
log.error(util.c(log_header, "bad advertisement: encountered unsupported reactor-specific RTU type ", type_string))
|
|
|
|
end
|
2022-10-06 17:54:52 +00:00
|
|
|
else
|
2023-05-10 20:40:52 +00:00
|
|
|
-- facility RTUs
|
2023-02-21 16:05:57 +00:00
|
|
|
if u_type == RTU_UNIT_TYPE.REDSTONE then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- redstone
|
|
|
|
unit = svrs_redstone.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then facility.add_redstone(unit) end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.IMATRIX then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- induction matrix
|
|
|
|
unit = svrs_imatrix.new(id, i, unit_advert, self.modbus_q)
|
|
|
|
if type(unit) ~= "nil" then facility.add_imatrix(unit) end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.SPS then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- super-critical phase shifter
|
|
|
|
unit = svrs_sps.new(id, i, unit_advert, self.modbus_q)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.SNA then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- solar neutron activator
|
|
|
|
unit = svrs_sna.new(id, i, unit_advert, self.modbus_q)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.ENV_DETECTOR then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- environment detector
|
|
|
|
unit = svrs_envd.new(id, i, unit_advert, self.modbus_q)
|
2023-02-14 03:11:31 +00:00
|
|
|
if type(unit) ~= "nil" then facility.add_envd(unit) end
|
2023-05-10 20:40:52 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPE.VIRTUAL then
|
|
|
|
-- skip virtual units
|
|
|
|
log.debug(util.c(log_header, "skipping virtual RTU unit #", i))
|
2022-12-10 18:58:17 +00:00
|
|
|
else
|
|
|
|
log.error(util.c(log_header, "bad advertisement: encountered unsupported reactor-independent RTU type ", type_string))
|
|
|
|
end
|
2022-10-06 17:54:52 +00:00
|
|
|
end
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if unit ~= nil then
|
2023-05-12 00:54:43 +00:00
|
|
|
self.units[i] = unit
|
2023-06-03 18:33:08 +00:00
|
|
|
unit_count = unit_count + 1
|
2023-05-10 20:40:52 +00:00
|
|
|
elseif u_type ~= RTU_UNIT_TYPE.VIRTUAL then
|
2022-06-05 13:30:56 +00:00
|
|
|
_reset_config()
|
2022-12-10 18:58:17 +00:00
|
|
|
log.error(util.c(log_header, "bad advertisement: error occured while creating a unit (type is ", type_string, ")"))
|
2022-05-13 13:45:11 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-06-03 18:33:08 +00:00
|
|
|
|
|
|
|
databus.tx_rtu_units(id, unit_count)
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
|
|
|
|
2022-05-16 21:13:54 +00:00
|
|
|
-- mark this RTU session as closed, stop watchdog
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _close()
|
2023-04-18 17:49:59 +00:00
|
|
|
self.conn_watchdog.cancel()
|
2022-05-16 21:13:54 +00:00
|
|
|
self.connected = false
|
2023-06-03 18:33:08 +00:00
|
|
|
databus.tx_rtu_disconnected(id)
|
2022-05-16 21:13:54 +00:00
|
|
|
|
|
|
|
-- mark all RTU unit sessions as closed so the reactor unit knows
|
2023-05-12 00:54:43 +00:00
|
|
|
for _, unit in pairs(self.units) do unit.close() end
|
2022-05-16 21:13:54 +00:00
|
|
|
end
|
|
|
|
|
2022-09-19 02:25:59 +00:00
|
|
|
-- send a MODBUS packet
|
|
|
|
---@param m_pkt modbus_packet MODBUS packet
|
|
|
|
local function _send_modbus(m_pkt)
|
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
|
2023-06-05 21:24:00 +00:00
|
|
|
s_pkt.make(s_addr, self.seq_num, PROTOCOL.MODBUS_TCP, m_pkt.raw_sendable())
|
2022-09-19 02:25:59 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_packet(s_pkt)
|
2022-09-19 02:25:59 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
-- send a SCADA management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param msg_type SCADA_MGMT_TYPE
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param msg table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _send_mgmt(msg_type, msg)
|
2022-05-09 13:35:39 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
|
|
|
m_pkt.make(msg_type, msg)
|
2023-06-05 21:24:00 +00:00
|
|
|
s_pkt.make(s_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-05-09 13:35:39 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_packet(s_pkt)
|
2022-05-09 13:35:39 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle a packet
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param pkt modbus_frame|mgmt_frame
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _handle_packet(pkt)
|
2022-05-09 13:35:39 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.r_seq_num == nil then
|
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
2023-04-21 21:10:15 +00:00
|
|
|
elseif (self.r_seq_num + 1) ~= pkt.scada_frame.seq_num() then
|
2022-05-09 13:35:39 +00:00
|
|
|
log.warning(log_header .. "sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. pkt.scada_frame.seq_num())
|
|
|
|
return
|
|
|
|
else
|
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- feed watchdog
|
2023-04-18 17:49:59 +00:00
|
|
|
self.conn_watchdog.feed()
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
-- process packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if pkt.scada_frame.protocol() == PROTOCOL.MODBUS_TCP then
|
2023-04-17 23:48:03 +00:00
|
|
|
---@cast pkt modbus_frame
|
2022-05-13 13:45:11 +00:00
|
|
|
if self.units[pkt.unit_id] ~= nil then
|
2022-05-13 13:49:24 +00:00
|
|
|
local unit = self.units[pkt.unit_id] ---@type unit_session
|
2022-05-13 13:45:11 +00:00
|
|
|
unit.handle_packet(pkt)
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.scada_frame.protocol() == PROTOCOL.SCADA_MGMT then
|
2023-04-17 23:48:03 +00:00
|
|
|
---@cast pkt mgmt_frame
|
2022-05-15 00:27:06 +00:00
|
|
|
-- handle management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if pkt.type == SCADA_MGMT_TYPE.KEEP_ALIVE then
|
2022-05-09 19:00:16 +00:00
|
|
|
-- keep alive reply
|
|
|
|
if pkt.length == 2 then
|
|
|
|
local srv_start = pkt.data[1]
|
2023-04-12 20:02:29 +00:00
|
|
|
-- local rtu_send = pkt.data[2]
|
2022-05-09 19:00:16 +00:00
|
|
|
local srv_now = util.time()
|
|
|
|
self.last_rtt = srv_now - srv_start
|
|
|
|
|
2023-02-26 19:49:16 +00:00
|
|
|
if self.last_rtt > 750 then
|
|
|
|
log.warning(log_header .. "RTU KEEP_ALIVE round trip time > 750ms (" .. self.last_rtt .. "ms)")
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
-- log.debug(log_header .. "RTU RTT = " .. self.last_rtt .. "ms")
|
|
|
|
-- log.debug(log_header .. "RTU TT = " .. (srv_now - rtu_send) .. "ms")
|
2023-06-03 18:33:08 +00:00
|
|
|
|
|
|
|
databus.tx_rtu_rtt(id, self.last_rtt)
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
|
|
|
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == SCADA_MGMT_TYPE.CLOSE then
|
2022-05-09 13:35:39 +00:00
|
|
|
-- close the session
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == SCADA_MGMT_TYPE.RTU_ADVERT then
|
2022-05-09 13:35:39 +00:00
|
|
|
-- RTU unit advertisement
|
2022-11-12 06:35:31 +00:00
|
|
|
log.debug(log_header .. "received updated advertisement")
|
2022-05-13 13:45:11 +00:00
|
|
|
self.advert = pkt.data
|
2022-11-12 06:35:31 +00:00
|
|
|
|
|
|
|
-- handle advertisement; this will re-create all unit sub-sessions
|
2022-05-15 00:27:06 +00:00
|
|
|
_handle_advertisement()
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == SCADA_MGMT_TYPE.RTU_DEV_REMOUNT then
|
2022-11-11 19:59:53 +00:00
|
|
|
if pkt.length == 1 then
|
2023-02-12 18:06:44 +00:00
|
|
|
local unit_id = pkt.data[1]
|
2022-11-11 19:59:53 +00:00
|
|
|
if self.units[unit_id] ~= nil then
|
|
|
|
local unit = self.units[unit_id] ---@type unit_session
|
|
|
|
unit.invalidate_cache()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug(log_header .. "SCADA RTU device re-mount packet length mismatch")
|
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
else
|
|
|
|
log.debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
|
|
|
-- get the session ID
|
2022-12-10 18:58:17 +00:00
|
|
|
function public.get_id() return id end
|
2022-05-09 13:35:39 +00:00
|
|
|
|
|
|
|
-- check if a timer matches this session's watchdog
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param timer number
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.check_wd(timer)
|
2023-04-18 17:49:59 +00:00
|
|
|
return self.conn_watchdog.is_timer(timer) and self.connected
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.close()
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2023-02-21 16:05:57 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPE.CLOSE, {})
|
2022-05-09 13:35:39 +00:00
|
|
|
println(log_header .. "connection to RTU closed by server")
|
|
|
|
log.info(log_header .. "session closed by server")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- iterate the session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-13 13:45:11 +00:00
|
|
|
---@return boolean connected
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.iterate()
|
2022-05-09 13:35:39 +00:00
|
|
|
if self.connected then
|
|
|
|
------------------
|
|
|
|
-- handle queue --
|
|
|
|
------------------
|
|
|
|
|
|
|
|
local handle_start = util.time()
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
while in_queue.ready() and self.connected do
|
2022-05-09 13:35:39 +00:00
|
|
|
-- get a new message to process
|
2023-02-25 04:36:16 +00:00
|
|
|
local msg = in_queue.pop()
|
2022-05-13 13:45:11 +00:00
|
|
|
|
|
|
|
if msg ~= nil then
|
|
|
|
if msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- handle a packet
|
|
|
|
_handle_packet(msg.message)
|
|
|
|
elseif msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction with body
|
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
-- exit if connection was closed
|
|
|
|
if not self.connected then
|
2023-02-07 22:51:55 +00:00
|
|
|
println("RTU connection " .. id .. " closed by remote host")
|
2022-05-09 13:35:39 +00:00
|
|
|
log.info(log_header .. "session closed by remote host")
|
|
|
|
return self.connected
|
|
|
|
end
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
------------------
|
|
|
|
-- update units --
|
|
|
|
------------------
|
|
|
|
|
2022-05-16 15:54:34 +00:00
|
|
|
local time_now = util.time()
|
|
|
|
|
2023-05-12 00:54:43 +00:00
|
|
|
for _, unit in pairs(self.units) do unit.update(time_now) end
|
2022-05-15 00:27:06 +00:00
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
----------------------
|
|
|
|
-- update periodics --
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
local elapsed = util.time() - self.periodics.last_update
|
|
|
|
|
|
|
|
local periodics = self.periodics
|
|
|
|
|
|
|
|
-- keep alive
|
|
|
|
|
|
|
|
periodics.keep_alive = periodics.keep_alive + elapsed
|
|
|
|
if periodics.keep_alive >= PERIODICS.KEEP_ALIVE then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPE.KEEP_ALIVE, { util.time() })
|
2022-05-09 13:35:39 +00:00
|
|
|
periodics.keep_alive = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
self.periodics.last_update = util.time()
|
2022-09-19 02:25:59 +00:00
|
|
|
|
2022-11-15 02:43:02 +00:00
|
|
|
--------------------------------------------
|
|
|
|
-- process RTU session handler out queues --
|
|
|
|
--------------------------------------------
|
2022-09-19 02:25:59 +00:00
|
|
|
|
|
|
|
for _ = 1, self.modbus_q.length() do
|
|
|
|
-- get the next message
|
|
|
|
local msg = self.modbus_q.pop()
|
|
|
|
|
|
|
|
if msg ~= nil then
|
|
|
|
if msg.qtype == mqueue.TYPE.PACKET then
|
2022-11-15 02:43:02 +00:00
|
|
|
-- handle a packet
|
2022-09-19 02:25:59 +00:00
|
|
|
_send_modbus(msg.message)
|
2022-11-15 02:43:02 +00:00
|
|
|
elseif msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction with body
|
2023-02-20 05:49:37 +00:00
|
|
|
local cmd = msg.message ---@type queue_data
|
|
|
|
if cmd.key == unit_session.RTU_US_DATA.BUILD_CHANGED then
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.RTU_BUILD_CHANGED, cmd.val)
|
2023-02-20 05:49:37 +00:00
|
|
|
end
|
2022-09-19 02:25:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return self.connected
|
|
|
|
end
|
|
|
|
|
2022-09-19 02:25:59 +00:00
|
|
|
-- handle initial advertisement
|
|
|
|
_handle_advertisement()
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
return public
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return rtu
|