#64 supervisor code cleanup

This commit is contained in:
Mikayla Fischler
2022-05-31 15:36:17 -04:00
parent 4ec07ca053
commit 43d5c0f8ad
12 changed files with 140 additions and 140 deletions

View File

@@ -1,5 +1,5 @@
local comms = require("scada-common.comms")
local log = require("scada-common.log")
local log = require("scada-common.log")
local types = require("scada-common.types")
local unit_session = require("supervisor.session.rtu.unit_session")
@@ -32,7 +32,7 @@ local PERIODICS = {
---@param unit_id integer
---@param advert rtu_advertisement
---@param out_queue mqueue
boiler.new = function (session_id, unit_id, advert, out_queue)
function boiler.new(session_id, unit_id, advert, out_queue)
-- type check
if advert.type ~= RTU_UNIT_TYPES.BOILER then
log.error("attempt to instantiate boiler RTU for type '" .. advert.type .. "'. this is a bug.")
@@ -86,19 +86,19 @@ boiler.new = function (session_id, unit_id, advert, out_queue)
-- PRIVATE FUNCTIONS --
-- query the build of the device
local _request_build = function ()
local function _request_build()
-- read input registers 1 through 7 (start = 1, count = 7)
self.session.send_request(TXN_TYPES.BUILD, MODBUS_FCODE.READ_INPUT_REGS, { 1, 7 })
end
-- query the state of the device
local _request_state = function ()
local function _request_state()
-- read input registers 8 through 9 (start = 8, count = 2)
self.session.send_request(TXN_TYPES.STATE, MODBUS_FCODE.READ_INPUT_REGS, { 8, 2 })
end
-- query the tanks of the device
local _request_tanks = function ()
local function _request_tanks()
-- read input registers 10 through 21 (start = 10, count = 12)
self.session.send_request(TXN_TYPES.TANKS, MODBUS_FCODE.READ_INPUT_REGS, { 10, 12 })
end
@@ -107,7 +107,7 @@ boiler.new = function (session_id, unit_id, advert, out_queue)
-- handle a packet
---@param m_pkt modbus_frame
public.handle_packet = function (m_pkt)
function public.handle_packet(m_pkt)
local txn_type = self.session.try_resolve(m_pkt.txn_id)
if txn_type == false then
-- nothing to do
@@ -162,7 +162,7 @@ boiler.new = function (session_id, unit_id, advert, out_queue)
-- update this runner
---@param time_now integer milliseconds
public.update = function (time_now)
function public.update(time_now)
if not self.periodics.has_build and self.periodics.next_build_req <= time_now then
_request_build()
self.periodics.next_build_req = time_now + PERIODICS.BUILD
@@ -180,7 +180,7 @@ boiler.new = function (session_id, unit_id, advert, out_queue)
end
-- get the unit session database
public.get_db = function () return self.db end
function public.get_db() return self.db end
return public
end

View File

@@ -1,5 +1,5 @@
local comms = require("scada-common.comms")
local log = require("scada-common.log")
local log = require("scada-common.log")
local types = require("scada-common.types")
local unit_session = require("supervisor.session.rtu.unit_session")
@@ -29,7 +29,7 @@ local PERIODICS = {
---@param unit_id integer
---@param advert rtu_advertisement
---@param out_queue mqueue
emachine.new = function (session_id, unit_id, advert, out_queue)
function emachine.new(session_id, unit_id, advert, out_queue)
-- type check
if advert.type ~= RTU_UNIT_TYPES.EMACHINE then
log.error("attempt to instantiate emachine RTU for type '" .. advert.type .. "'. this is a bug.")
@@ -63,13 +63,13 @@ emachine.new = function (session_id, unit_id, advert, out_queue)
-- PRIVATE FUNCTIONS --
-- query the build of the device
local _request_build = function ()
local function _request_build()
-- read input register 1 (start = 1, count = 1)
self.session.send_request(TXN_TYPES.BUILD, MODBUS_FCODE.READ_INPUT_REGS, { 1, 1 })
end
-- query the state of the energy storage
local _request_storage = function ()
local function _request_storage()
-- read input registers 2 through 4 (start = 2, count = 3)
self.session.send_request(TXN_TYPES.STORAGE, MODBUS_FCODE.READ_INPUT_REGS, { 2, 3 })
end
@@ -78,7 +78,7 @@ emachine.new = function (session_id, unit_id, advert, out_queue)
-- handle a packet
---@param m_pkt modbus_frame
public.handle_packet = function (m_pkt)
function public.handle_packet(m_pkt)
local txn_type = self.session.try_resolve(m_pkt.txn_id)
if txn_type == false then
-- nothing to do
@@ -107,7 +107,7 @@ emachine.new = function (session_id, unit_id, advert, out_queue)
-- update this runner
---@param time_now integer milliseconds
public.update = function (time_now)
function public.update(time_now)
if not self.has_build and self.periodics.next_build_req <= time_now then
_request_build()
self.periodics.next_build_req = time_now + PERIODICS.BUILD
@@ -120,7 +120,7 @@ emachine.new = function (session_id, unit_id, advert, out_queue)
end
-- get the unit session database
public.get_db = function () return self.db end
function public.get_db() return self.db end
return public
end

View File

@@ -1,9 +1,9 @@
local comms = require("scada-common.comms")
local log = require("scada-common.log")
local mqueue= require("scada-common.mqueue")
local rsio = require("scada-common.rsio")
local types = require("scada-common.types")
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 rsio = require("scada-common.rsio")
local types = require("scada-common.types")
local util = require("scada-common.util")
local unit_session = require("supervisor.session.rtu.unit_session")
@@ -50,7 +50,7 @@ local PERIODICS = {
---@param unit_id integer
---@param advert rtu_advertisement
---@param out_queue mqueue
redstone.new = function (session_id, unit_id, advert, out_queue)
function redstone.new(session_id, unit_id, advert, out_queue)
-- type check
if advert.type ~= RTU_UNIT_TYPES.REDSTONE then
log.error("attempt to instantiate redstone RTU for type '" .. advert.type .. "'. this is a bug.")
@@ -113,22 +113,22 @@ redstone.new = function (session_id, unit_id, advert, out_queue)
-- PRIVATE FUNCTIONS --
-- query discrete inputs
local _request_discrete_inputs = function ()
local function _request_discrete_inputs()
self.session.send_request(TXN_TYPES.DI_READ, MODBUS_FCODE.READ_DISCRETE_INPUTS, { 1, #self.io_list.digital_in })
end
-- query input registers
local _request_input_registers = function ()
local function _request_input_registers()
self.session.send_request(TXN_TYPES.INPUT_REG_READ, MODBUS_FCODE.READ_INPUT_REGS, { 1, #self.io_list.analog_in })
end
-- write coil output
local _write_coil = function (coil, value)
local function _write_coil(coil, value)
self.session.send_request(TXN_TYPES.COIL_WRITE, MODBUS_FCODE.WRITE_MUL_COILS, { coil, value })
end
-- write holding register output
local _write_holding_register = function (reg, value)
local function _write_holding_register(reg, value)
self.session.send_request(TXN_TYPES.HOLD_REG_WRITE, MODBUS_FCODE.WRITE_MUL_HOLD_REGS, { reg, value })
end
@@ -136,7 +136,7 @@ redstone.new = function (session_id, unit_id, advert, out_queue)
-- handle a packet
---@param m_pkt modbus_frame
public.handle_packet = function (m_pkt)
function public.handle_packet(m_pkt)
local txn_type = self.session.try_resolve(m_pkt.txn_id)
if txn_type == false then
-- nothing to do
@@ -173,7 +173,7 @@ redstone.new = function (session_id, unit_id, advert, out_queue)
-- update this runner
---@param time_now integer milliseconds
public.update = function (time_now)
function public.update(time_now)
-- check command queue
while self.in_q.ready() do
-- get a new message to process
@@ -246,7 +246,7 @@ redstone.new = function (session_id, unit_id, advert, out_queue)
end
-- get the unit session database
public.get_db = function () return self.db end
function public.get_db() return self.db end
return public, self.in_q
end

View File

@@ -1,5 +1,5 @@
local comms = require("scada-common.comms")
local log = require("scada-common.log")
local log = require("scada-common.log")
local types = require("scada-common.types")
local unit_session = require("supervisor.session.rtu.unit_session")
@@ -33,7 +33,7 @@ local PERIODICS = {
---@param unit_id integer
---@param advert rtu_advertisement
---@param out_queue mqueue
turbine.new = function (session_id, unit_id, advert, out_queue)
function turbine.new(session_id, unit_id, advert, out_queue)
-- type check
if advert.type ~= RTU_UNIT_TYPES.TURBINE then
log.error("attempt to instantiate turbine RTU for type '" .. advert.type .. "'. this is a bug.")
@@ -82,19 +82,19 @@ turbine.new = function (session_id, unit_id, advert, out_queue)
-- PRIVATE FUNCTIONS --
-- query the build of the device
local _request_build = function ()
local function _request_build()
-- read input registers 1 through 9 (start = 1, count = 9)
self.session.send_request(TXN_TYPES.BUILD, MODBUS_FCODE.READ_INPUT_REGS, { 1, 9 })
end
-- query the state of the device
local _request_state = function ()
local function _request_state()
-- read input registers 10 through 13 (start = 10, count = 4)
self.session.send_request(TXN_TYPES.STATE, MODBUS_FCODE.READ_INPUT_REGS, { 10, 4 })
end
-- query the tanks of the device
local _request_tanks = function ()
local function _request_tanks()
-- read input registers 14 through 16 (start = 14, count = 3)
self.session.send_request(TXN_TYPES.TANKS, MODBUS_FCODE.READ_INPUT_REGS, { 14, 3 })
end
@@ -103,7 +103,7 @@ turbine.new = function (session_id, unit_id, advert, out_queue)
-- handle a packet
---@param m_pkt modbus_frame
public.handle_packet = function (m_pkt)
function public.handle_packet(m_pkt)
local txn_type = self.session.try_resolve(m_pkt.txn_id)
if txn_type == false then
-- nothing to do
@@ -150,7 +150,7 @@ turbine.new = function (session_id, unit_id, advert, out_queue)
-- update this runner
---@param time_now integer milliseconds
public.update = function (time_now)
function public.update(time_now)
if not self.has_build and self.periodics.next_build_req <= time_now then
_request_build()
self.periodics.next_build_req = time_now + PERIODICS.BUILD
@@ -168,7 +168,7 @@ turbine.new = function (session_id, unit_id, advert, out_queue)
end
-- get the unit session database
public.get_db = function () return self.db end
function public.get_db() return self.db end
return public
end

View File

@@ -9,7 +9,7 @@ local txnctrl = {}
local TIMEOUT = 2000 -- 2000ms max wait
-- create a new transaction controller
txnctrl.new = function ()
function txnctrl.new()
local self = {
list = {},
next_id = 0
@@ -21,19 +21,19 @@ txnctrl.new = function ()
local insert = table.insert
-- get the length of the transaction list
public.length = function ()
function public.length()
return #self.list
end
-- check if there are no active transactions
public.empty = function ()
function public.empty()
return #self.list == 0
end
-- create a new transaction of the given type
---@param txn_type integer
---@return integer txn_id
public.create = function (txn_type)
function public.create(txn_type)
local txn_id = self.next_id
insert(self.list, {
@@ -50,7 +50,7 @@ txnctrl.new = function ()
-- mark a transaction as resolved to get its transaction type
---@param txn_id integer
---@return integer txn_type
public.resolve = function (txn_id)
function public.resolve(txn_id)
local txn_type = nil
for i = 1, public.length() do
@@ -66,7 +66,7 @@ txnctrl.new = function ()
-- renew a transaction by re-inserting it with its ID and type
---@param txn_id integer
---@param txn_type integer
public.renew = function (txn_id, txn_type)
function public.renew(txn_id, txn_type)
insert(self.list, {
txn_id = txn_id,
txn_type = txn_type,
@@ -75,13 +75,13 @@ txnctrl.new = function ()
end
-- close timed-out transactions
public.cleanup = function ()
function public.cleanup()
local now = util.time()
util.filter_table(self.list, function (txn) return txn.expiry > now end)
end
-- clear the transaction list
public.clear = function ()
function public.clear()
self.list = {}
end

View File

@@ -1,5 +1,5 @@
local comms = require("scada-common.comms")
local log = require("scada-common.log")
local log = require("scada-common.log")
local types = require("scada-common.types")
local txnctrl = require("supervisor.session.rtu.txnctrl")
@@ -16,7 +16,7 @@ local MODBUS_EXCODE = types.MODBUS_EXCODE
---@param out_queue mqueue send queue
---@param log_tag string logging tag
---@param txn_tags table transaction log tags
unit_session.new = function (unit_id, advert, out_queue, log_tag, txn_tags)
function unit_session.new(unit_id, advert, out_queue, log_tag, txn_tags)
local self = {
log_tag = log_tag,
txn_tags = txn_tags,
@@ -41,7 +41,7 @@ unit_session.new = function (unit_id, advert, out_queue, log_tag, txn_tags)
---@param txn_type integer transaction type
---@param f_code MODBUS_FCODE function code
---@param register_param table register range or register and values
protected.send_request = function (txn_type, f_code, register_param)
function protected.send_request(txn_type, f_code, register_param)
local m_pkt = comms.modbus_packet()
local txn_id = self.transaction_controller.create(txn_type)
@@ -53,7 +53,7 @@ unit_session.new = function (unit_id, advert, out_queue, log_tag, txn_tags)
-- try to resolve a MODBUS transaction
---@param m_pkt modbus_frame MODBUS packet
---@return integer|false txn_type transaction type or false on error/busy
protected.try_resolve = function (m_pkt)
function protected.try_resolve(m_pkt)
if m_pkt.scada_frame.protocol() == PROTOCOLS.MODBUS_TCP then
if m_pkt.unit_id == self.unit_id then
local txn_type = self.transaction_controller.resolve(m_pkt.txn_id)
@@ -112,42 +112,42 @@ unit_session.new = function (unit_id, advert, out_queue, log_tag, txn_tags)
end
-- get the public interface
protected.get = function () return public end
function protected.get() return public end
-- PUBLIC FUNCTIONS --
-- get the unit ID
public.get_unit_id = function () return self.unit_id end
function public.get_unit_id() return self.unit_id end
-- get the device index
public.get_device_idx = function () return self.device_index end
function public.get_device_idx() return self.device_index end
-- get the reactor ID
public.get_reactor = function () return self.reactor end
function public.get_reactor() return self.reactor end
-- close this unit
public.close = function () self.connected = false end
function public.close() self.connected = false end
-- check if this unit is connected
public.is_connected = function () return self.connected end
function public.is_connected() return self.connected end
-- check if this unit is faulted
public.is_faulted = function () return self.device_fail end
function public.is_faulted() return self.device_fail end
-- PUBLIC TEMPLATE FUNCTIONS --
-- handle a packet
---@param m_pkt modbus_frame
---@diagnostic disable-next-line: unused-local
public.handle_packet = function (m_pkt)
function public.handle_packet(m_pkt)
log.debug("template unit_session.handle_packet() called", true)
end
-- update this runner
---@param time_now integer milliseconds
---@diagnostic disable-next-line: unused-local
public.update = function (time_now)
function public.update(time_now)
log.debug("template unit_session.update() called", true)
end
-- get the unit session database
public.get_db = function () return {} end
function public.get_db() return {} end
return protected
end