#118 supervisor cleanup

This commit is contained in:
Mikayla Fischler
2023-02-24 23:36:16 -05:00
parent 38ac552613
commit b7895080cb
19 changed files with 241 additions and 156 deletions

View File

@@ -31,6 +31,7 @@ local PERIODICS = {
}
-- create a new boilerv rtu session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer RTU unit ID
---@param advert rtu_advertisement RTU advertisement table
@@ -238,6 +239,7 @@ function boilerv.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -22,6 +22,7 @@ local PERIODICS = {
}
-- create a new environment detector rtu session runner
---@nodiscard
---@param session_id integer
---@param unit_id integer
---@param advert rtu_advertisement
@@ -99,6 +100,7 @@ function envd.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -31,6 +31,7 @@ local PERIODICS = {
}
-- create a new imatrix rtu session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer RTU unit ID
---@param advert rtu_advertisement RTU advertisement table
@@ -212,6 +213,7 @@ function imatrix.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -45,6 +45,7 @@ local PERIODICS = {
---@field req IO_LVL
-- create a new redstone rtu session runner
---@nodiscard
---@param session_id integer
---@param unit_id integer
---@param advert rtu_advertisement
@@ -118,6 +119,7 @@ function redstone.new(session_id, unit_id, advert, out_queue)
---@class rs_db_dig_io
local io_f = {
---@nodiscard
read = function () return rsio.digital_is_active(port, self.phy_io.digital_in[port].phy) end,
---@param active boolean
write = function (active) end
@@ -132,6 +134,7 @@ function redstone.new(session_id, unit_id, advert, out_queue)
---@class rs_db_dig_io
local io_f = {
---@nodiscard
read = function () return rsio.digital_is_active(port, self.phy_io.digital_out[port].phy) end,
---@param active boolean
write = function (active)
@@ -149,6 +152,7 @@ function redstone.new(session_id, unit_id, advert, out_queue)
---@class rs_db_ana_io
local io_f = {
---@nodiscard
---@return integer
read = function () return self.phy_io.analog_in[port].phy end,
---@param value integer
@@ -164,6 +168,7 @@ function redstone.new(session_id, unit_id, advert, out_queue)
---@class rs_db_ana_io
local io_f = {
---@nodiscard
---@return integer
read = function () return self.phy_io.analog_out[port].phy end,
---@param value integer
@@ -378,6 +383,7 @@ function redstone.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -28,6 +28,7 @@ local PERIODICS = {
}
-- create a new sna rtu session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer RTU unit ID
---@param advert rtu_advertisement RTU advertisement table
@@ -175,6 +176,7 @@ function sna.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -31,6 +31,7 @@ local PERIODICS = {
}
-- create a new sps rtu session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer RTU unit ID
---@param advert rtu_advertisement RTU advertisement table
@@ -112,7 +113,7 @@ function sps.new(session_id, unit_id, advert, out_queue)
-- query the tanks of the device
local function _request_tanks()
-- read input registers 11 through 19 (start = 11, count = 9)
self.session.send_request(TXN_TYPES.TANKS, MODBUS_FCODE.READ_INPUT_REGS, { 10, 12 })
self.session.send_request(TXN_TYPES.TANKS, MODBUS_FCODE.READ_INPUT_REGS, { 11, 9 })
end
-- PUBLIC FUNCTIONS --
@@ -222,6 +223,7 @@ function sps.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -43,6 +43,7 @@ local PERIODICS = {
}
-- create a new turbinev rtu session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer RTU unit ID
---@param advert rtu_advertisement RTU advertisement table
@@ -309,6 +310,7 @@ function turbinev.new(session_id, unit_id, advert, out_queue)
end
-- get the unit session database
---@nodiscard
function public.get_db() return self.db end
return public

View File

@@ -6,9 +6,10 @@ local util = require("scada-common.util")
local txnctrl = {}
local TIMEOUT = 2000 -- 2000ms max wait
local TIMEOUT = 2000 -- 2000ms max wait
-- create a new transaction controller
---@nodiscard
function txnctrl.new()
local self = {
list = {},
@@ -22,16 +23,19 @@ function txnctrl.new()
local remove = table.remove
-- get the length of the transaction list
---@nodiscard
function public.length()
return #self.list
end
-- check if there are no active transactions
---@nodiscard
function public.empty()
return #self.list == 0
end
-- create a new transaction of the given type
---@nodiscard
---@param txn_type integer
---@return integer txn_id
function public.create(txn_type)
@@ -49,6 +53,7 @@ function txnctrl.new()
end
-- mark a transaction as resolved to get its transaction type
---@nodiscard
---@param txn_id integer
---@return integer txn_type
function public.resolve(txn_id)

View File

@@ -23,6 +23,7 @@ unit_session.RTU_US_CMDS = RTU_US_CMDS
unit_session.RTU_US_DATA = RTU_US_DATA
-- create a new unit session runner
---@nodiscard
---@param session_id integer RTU session ID
---@param unit_id integer MODBUS unit ID
---@param advert rtu_advertisement RTU advertisement for this unit
@@ -31,12 +32,8 @@ unit_session.RTU_US_DATA = RTU_US_DATA
---@param txn_tags table transaction log tags
function unit_session.new(session_id, unit_id, advert, out_queue, log_tag, txn_tags)
local self = {
log_tag = log_tag,
txn_tags = txn_tags,
unit_id = unit_id,
device_index = advert.index,
reactor = advert.reactor,
out_q = out_queue,
transaction_controller = txnctrl.new(),
connected = true,
device_fail = false
@@ -61,21 +58,22 @@ function unit_session.new(session_id, unit_id, advert, out_queue, log_tag, txn_t
local m_pkt = comms.modbus_packet()
local txn_id = self.transaction_controller.create(txn_type)
m_pkt.make(txn_id, self.unit_id, f_code, register_param)
m_pkt.make(txn_id, unit_id, f_code, register_param)
self.out_q.push_packet(m_pkt)
out_queue.push_packet(m_pkt)
return txn_id
end
-- try to resolve a MODBUS transaction
---@nodiscard
---@param m_pkt modbus_frame MODBUS packet
---@return integer|false txn_type, integer txn_id transaction type or false on error/busy, transaction ID
function protected.try_resolve(m_pkt)
if m_pkt.scada_frame.protocol() == PROTOCOL.MODBUS_TCP then
if m_pkt.unit_id == self.unit_id then
if m_pkt.unit_id == unit_id then
local txn_type = self.transaction_controller.resolve(m_pkt.txn_id)
local txn_tag = " (" .. util.strval(self.txn_tags[txn_type]) .. ")"
local txn_tag = " (" .. util.strval(txn_tags[txn_type]) .. ")"
if bit.band(m_pkt.func_code, MODBUS_FCODE.ERROR_FLAG) ~= 0 then
-- transaction incomplete or failed
@@ -135,26 +133,35 @@ function unit_session.new(session_id, unit_id, advert, out_queue, log_tag, txn_t
end
-- get the public interface
---@nodiscard
function protected.get() return public end
-- PUBLIC FUNCTIONS --
-- get the unit ID
---@nodiscard
function public.get_session_id() return session_id end
-- get the unit ID
function public.get_unit_id() return self.unit_id end
---@nodiscard
function public.get_unit_id() return unit_id end
-- get the device index
---@nodiscard
function public.get_device_idx() return self.device_index end
-- get the reactor ID
---@nodiscard
function public.get_reactor() return self.reactor end
-- get the command queue
---@nodiscard
function public.get_cmd_queue() return protected.in_q end
-- close this unit
---@nodiscard
function public.close() self.connected = false end
-- check if this unit is connected
---@nodiscard
function public.is_connected() return self.connected end
-- check if this unit is faulted
---@nodiscard
function public.is_faulted() return self.device_fail end
-- PUBLIC TEMPLATE FUNCTIONS --
@@ -179,6 +186,7 @@ function unit_session.new(session_id, unit_id, advert, out_queue, log_tag, txn_t
end
-- get the unit session database
---@nodiscard
function public.get_db()
log.debug("template unit_session.get_db() called", true)
return {}