From 43d5c0f8addaa2b37e4e24693e86a3e80a227b2a Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Tue, 31 May 2022 15:36:17 -0400 Subject: [PATCH] #64 supervisor code cleanup --- supervisor/session/plc.lua | 42 ++++++++++++------------- supervisor/session/rtu.lua | 30 +++++++++--------- supervisor/session/rtu/boiler.lua | 16 +++++----- supervisor/session/rtu/emachine.lua | 14 ++++----- supervisor/session/rtu/redstone.lua | 28 ++++++++--------- supervisor/session/rtu/turbine.lua | 16 +++++----- supervisor/session/rtu/txnctrl.lua | 16 +++++----- supervisor/session/rtu/unit_session.lua | 28 ++++++++--------- supervisor/session/svsessions.lua | 32 +++++++++---------- supervisor/startup.lua | 8 ++--- supervisor/supervisor.lua | 18 +++++------ supervisor/unit.lua | 32 +++++++++---------- 12 files changed, 140 insertions(+), 140 deletions(-) diff --git a/supervisor/session/plc.lua b/supervisor/session/plc.lua index 5279208..b4e7291 100644 --- a/supervisor/session/plc.lua +++ b/supervisor/session/plc.lua @@ -1,7 +1,7 @@ -local comms = require("scada-common.comms") -local log = require("scada-common.log") +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 util = require("scada-common.util") local plc = {} @@ -41,7 +41,7 @@ local PERIODICS = { ---@param for_reactor integer ---@param in_queue mqueue ---@param out_queue mqueue -plc.new_session = function (id, for_reactor, in_queue, out_queue) +function plc.new_session(id, for_reactor, in_queue, out_queue) local log_header = "plc_session(" .. id .. "): " local self = { @@ -146,7 +146,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- copy in the RPS status ---@param rps_status table - local _copy_rps_status = function (rps_status) + local function _copy_rps_status(rps_status) self.sDB.rps_status.dmg_crit = rps_status[1] self.sDB.rps_status.ex_hcool = rps_status[2] self.sDB.rps_status.ex_waste = rps_status[3] @@ -158,7 +158,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- copy in the reactor status ---@param mek_data table - local _copy_status = function (mek_data) + local function _copy_status(mek_data) -- copy status information self.sDB.mek_status.status = mek_data[1] self.sDB.mek_status.burn_rate = mek_data[2] @@ -191,7 +191,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- copy in the reactor structure ---@param mek_data table - local _copy_struct = function (mek_data) + local function _copy_struct(mek_data) self.sDB.mek_struct.heat_cap = mek_data[1] self.sDB.mek_struct.fuel_asm = mek_data[2] self.sDB.mek_struct.fuel_sa = mek_data[3] @@ -203,7 +203,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) end -- mark this PLC session as closed, stop watchdog - local _close = function () + local function _close() self.plc_conn_watchdog.cancel() self.connected = false end @@ -211,7 +211,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- send an RPLC packet ---@param msg_type RPLC_TYPES ---@param msg table - local _send = function (msg_type, msg) + local function _send(msg_type, msg) local s_pkt = comms.scada_packet() local r_pkt = comms.rplc_packet() @@ -225,7 +225,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- send a SCADA management packet ---@param msg_type SCADA_MGMT_TYPES ---@param msg table - local _send_mgmt = function (msg_type, msg) + local function _send_mgmt(msg_type, msg) local s_pkt = comms.scada_packet() local m_pkt = comms.mgmt_packet() @@ -239,7 +239,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- get an ACK status ---@param pkt rplc_frame ---@return boolean|nil ack - local _get_ack = function (pkt) + local function _get_ack(pkt) if pkt.length == 1 then return pkt.data[1] else @@ -250,7 +250,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- handle a packet ---@param pkt rplc_frame - local _handle_packet = function (pkt) + local function _handle_packet(pkt) -- check sequence number if self.r_seq_num == nil then self.r_seq_num = pkt.scada_frame.seq_num() @@ -408,13 +408,13 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- PUBLIC FUNCTIONS -- -- get the session ID - public.get_id = function () return self.id end + function public.get_id() return self.id end -- get the session database - public.get_db = function () return self.sDB end + function public.get_db() return self.sDB end -- get the reactor structure - public.get_struct = function () + function public.get_struct() if self.received_struct then return self.sDB.mek_struct else @@ -423,7 +423,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) end -- get the reactor status - public.get_status = function () + function public.get_status() if self.received_status_cache then return self.sDB.mek_status else @@ -432,12 +432,12 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) end -- get the reactor RPS status - public.get_rps = function () + function public.get_rps() return self.sDB.rps_status end -- get the general status information - public.get_general_status = function () + function public.get_general_status() return { last_status_update = self.sDB.last_status_update, control_state = self.sDB.control_state, @@ -449,12 +449,12 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) end -- check if a timer matches this session's watchdog - public.check_wd = function (timer) + function public.check_wd(timer) return self.plc_conn_watchdog.is_timer(timer) and self.connected end -- close the connection - public.close = function () + function public.close() _close() _send_mgmt(SCADA_MGMT_TYPES.CLOSE, {}) println("connection to reactor " .. self.for_reactor .. " PLC closed by server") @@ -463,7 +463,7 @@ plc.new_session = function (id, for_reactor, in_queue, out_queue) -- iterate the session ---@return boolean connected - public.iterate = function () + function public.iterate() if self.connected then ------------------ -- handle queue -- diff --git a/supervisor/session/rtu.lua b/supervisor/session/rtu.lua index ff95087..5454917 100644 --- a/supervisor/session/rtu.lua +++ b/supervisor/session/rtu.lua @@ -1,14 +1,14 @@ -local comms = require("scada-common.comms") -local log = require("scada-common.log") +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 util = require("scada-common.util") +local rsio = require("scada-common.rsio") +local util = require("scada-common.util") -- supervisor rtu sessions (svrs) -local svrs_boiler = require("supervisor.session.rtu.boiler") +local svrs_boiler = require("supervisor.session.rtu.boiler") local svrs_emachine = require("supervisor.session.rtu.emachine") local svrs_redstone = require("supervisor.session.rtu.redstone") -local svrs_turbine = require("supervisor.session.rtu.turbine") +local svrs_turbine = require("supervisor.session.rtu.turbine") local rtu = {} @@ -46,7 +46,7 @@ local PERIODICS = { ---@param in_queue mqueue ---@param out_queue mqueue ---@param advertisement table -rtu.new_session = function (id, in_queue, out_queue, advertisement) +function rtu.new_session(id, in_queue, out_queue, advertisement) local log_header = "rtu_session(" .. id .. "): " local self = { @@ -68,7 +68,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) local public = {} -- parse the recorded advertisement and create unit sub-sessions - local _handle_advertisement = function () + local function _handle_advertisement() self.units = {} self.rs_io_q = {} @@ -130,7 +130,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) end -- mark this RTU session as closed, stop watchdog - local _close = function () + local function _close() self.rtu_conn_watchdog.cancel() self.connected = false @@ -143,7 +143,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- send a SCADA management packet ---@param msg_type SCADA_MGMT_TYPES ---@param msg table - local _send_mgmt = function (msg_type, msg) + local function _send_mgmt(msg_type, msg) local s_pkt = comms.scada_packet() local m_pkt = comms.mgmt_packet() @@ -156,7 +156,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- handle a packet ---@param pkt modbus_frame|mgmt_frame - local _handle_packet = function (pkt) + local function _handle_packet(pkt) -- check sequence number if self.r_seq_num == nil then self.r_seq_num = pkt.scada_frame.seq_num() @@ -212,16 +212,16 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- PUBLIC FUNCTIONS -- -- get the session ID - public.get_id = function () return self.id end + function public.get_id() return self.id end -- check if a timer matches this session's watchdog ---@param timer number - public.check_wd = function (timer) + function public.check_wd(timer) return self.rtu_conn_watchdog.is_timer(timer) and self.connected end -- close the connection - public.close = function () + function public.close() _close() _send_mgmt(SCADA_MGMT_TYPES.CLOSE, {}) println(log_header .. "connection to RTU closed by server") @@ -230,7 +230,7 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- iterate the session ---@return boolean connected - public.iterate = function () + function public.iterate() if self.connected then ------------------ -- handle queue -- diff --git a/supervisor/session/rtu/boiler.lua b/supervisor/session/rtu/boiler.lua index c3fa28e..552b3e2 100644 --- a/supervisor/session/rtu/boiler.lua +++ b/supervisor/session/rtu/boiler.lua @@ -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 diff --git a/supervisor/session/rtu/emachine.lua b/supervisor/session/rtu/emachine.lua index e47293a..525fa07 100644 --- a/supervisor/session/rtu/emachine.lua +++ b/supervisor/session/rtu/emachine.lua @@ -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 diff --git a/supervisor/session/rtu/redstone.lua b/supervisor/session/rtu/redstone.lua index b41e223..e870201 100644 --- a/supervisor/session/rtu/redstone.lua +++ b/supervisor/session/rtu/redstone.lua @@ -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 diff --git a/supervisor/session/rtu/turbine.lua b/supervisor/session/rtu/turbine.lua index d62626e..913f007 100644 --- a/supervisor/session/rtu/turbine.lua +++ b/supervisor/session/rtu/turbine.lua @@ -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 diff --git a/supervisor/session/rtu/txnctrl.lua b/supervisor/session/rtu/txnctrl.lua index a19dee6..2ebb526 100644 --- a/supervisor/session/rtu/txnctrl.lua +++ b/supervisor/session/rtu/txnctrl.lua @@ -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 diff --git a/supervisor/session/rtu/unit_session.lua b/supervisor/session/rtu/unit_session.lua index 83a0766..ee7a217 100644 --- a/supervisor/session/rtu/unit_session.lua +++ b/supervisor/session/rtu/unit_session.lua @@ -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 diff --git a/supervisor/session/svsessions.lua b/supervisor/session/svsessions.lua index 02bf70b..1077661 100644 --- a/supervisor/session/svsessions.lua +++ b/supervisor/session/svsessions.lua @@ -1,10 +1,10 @@ -local log = require("scada-common.log") +local log = require("scada-common.log") local mqueue = require("scada-common.mqueue") -local util = require("scada-common.util") +local util = require("scada-common.util") local coordinator = require("supervisor.session.coordinator") -local plc = require("supervisor.session.plc") -local rtu = require("supervisor.session.rtu") +local plc = require("supervisor.session.plc") +local rtu = require("supervisor.session.rtu") -- Supervisor Sessions Handler @@ -121,14 +121,14 @@ end -- link the modem ---@param modem table -svsessions.link_modem = function (modem) +function svsessions.link_modem(modem) self.modem = modem end -- find an RTU session by the remote port ---@param remote_port integer ---@return rtu_session_struct|nil -svsessions.find_rtu_session = function (remote_port) +function svsessions.find_rtu_session(remote_port) -- check RTU sessions return _find_session(self.rtu_sessions, remote_port) end @@ -136,7 +136,7 @@ end -- find a PLC session by the remote port ---@param remote_port integer ---@return plc_session_struct|nil -svsessions.find_plc_session = function (remote_port) +function svsessions.find_plc_session(remote_port) -- check PLC sessions return _find_session(self.plc_sessions, remote_port) end @@ -144,7 +144,7 @@ end -- find a PLC/RTU session by the remote port ---@param remote_port integer ---@return plc_session_struct|rtu_session_struct|nil -svsessions.find_device_session = function (remote_port) +function svsessions.find_device_session(remote_port) -- check RTU sessions local s = _find_session(self.rtu_sessions, remote_port) @@ -157,7 +157,7 @@ end -- find a coordinator session by the remote port ---@param remote_port integer ---@return nil -svsessions.find_coord_session = function (remote_port) +function svsessions.find_coord_session(remote_port) -- check coordinator sessions return _find_session(self.coord_sessions, remote_port) end @@ -165,7 +165,7 @@ end -- get a session by reactor ID ---@param reactor integer ---@return plc_session_struct|nil session -svsessions.get_reactor_session = function (reactor) +function svsessions.get_reactor_session(reactor) local session = nil for i = 1, #self.plc_sessions do @@ -183,7 +183,7 @@ end ---@param for_reactor integer ---@param version string ---@return integer|false session_id -svsessions.establish_plc_session = function (local_port, remote_port, for_reactor, version) +function svsessions.establish_plc_session(local_port, remote_port, for_reactor, version) if svsessions.get_reactor_session(for_reactor) == nil then ---@class plc_session_struct local plc_s = { @@ -217,7 +217,7 @@ end ---@param remote_port integer ---@param advertisement table ---@return integer session_id -svsessions.establish_rtu_session = function (local_port, remote_port, advertisement) +function svsessions.establish_rtu_session(local_port, remote_port, advertisement) -- pull version from advertisement local version = table.remove(advertisement, 1) @@ -245,7 +245,7 @@ end -- attempt to identify which session's watchdog timer fired ---@param timer_event number -svsessions.check_all_watchdogs = function (timer_event) +function svsessions.check_all_watchdogs(timer_event) -- check RTU session watchdogs _check_watchdogs(self.rtu_sessions, timer_event) @@ -257,7 +257,7 @@ svsessions.check_all_watchdogs = function (timer_event) end -- iterate all sessions -svsessions.iterate_all = function () +function svsessions.iterate_all() -- iterate RTU sessions _iterate(self.rtu_sessions) @@ -269,7 +269,7 @@ svsessions.iterate_all = function () end -- delete all closed sessions -svsessions.free_all_closed = function () +function svsessions.free_all_closed() -- free closed RTU sessions _free_closed(self.rtu_sessions) @@ -281,7 +281,7 @@ svsessions.free_all_closed = function () end -- close all open connections -svsessions.close_all = function () +function svsessions.close_all() -- close sessions _close(self.rtu_sessions) _close(self.plc_sessions) diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 1dcf7a7..8bffd4f 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -4,16 +4,16 @@ require("/initenv").init_env() -local log = require("scada-common.log") -local ppm = require("scada-common.ppm") +local log = require("scada-common.log") +local ppm = require("scada-common.ppm") local util = require("scada-common.util") local svsessions = require("supervisor.session.svsessions") -local config = require("supervisor.config") +local config = require("supervisor.config") local supervisor = require("supervisor.supervisor") -local SUPERVISOR_VERSION = "beta-v0.4.1" +local SUPERVISOR_VERSION = "beta-v0.4.2" local print = util.print local println = util.println diff --git a/supervisor/supervisor.lua b/supervisor/supervisor.lua index ba07d31..23c4a9e 100644 --- a/supervisor/supervisor.lua +++ b/supervisor/supervisor.lua @@ -1,6 +1,6 @@ local comms = require("scada-common.comms") -local log = require("scada-common.log") -local util = require("scada-common.util") +local log = require("scada-common.log") +local util = require("scada-common.util") local svsessions = require("supervisor.session.svsessions") @@ -25,7 +25,7 @@ local println_ts = util.println_ts ---@param modem table ---@param dev_listen integer ---@param coord_listen integer -supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_listen) +function supervisor.comms(version, num_reactors, modem, dev_listen, coord_listen) local self = { version = version, num_reactors = num_reactors, @@ -41,7 +41,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis -- PRIVATE FUNCTIONS -- -- open all channels - local _open_channels = function () + local function _open_channels() if not self.modem.isOpen(self.dev_listen) then self.modem.open(self.dev_listen) end @@ -60,7 +60,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis -- send PLC link request responses ---@param dest integer ---@param msg table - local _send_plc_linking = function (seq_id, dest, msg) + local function _send_plc_linking(seq_id, dest, msg) local s_pkt = comms.scada_packet() local r_pkt = comms.rplc_packet() @@ -73,7 +73,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis -- send RTU advertisement responses ---@param seq_id integer ---@param dest integer - local _send_remote_linked = function (seq_id, dest) + local function _send_remote_linked(seq_id, dest) local s_pkt = comms.scada_packet() local m_pkt = comms.mgmt_packet() @@ -88,7 +88,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis -- reconnect a newly connected modem ---@param modem table ---@diagnostic disable-next-line: redefined-local - public.reconnect_modem = function (modem) + function public.reconnect_modem(modem) self.modem = modem svsessions.link_modem(self.modem) _open_channels() @@ -101,7 +101,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis ---@param message any ---@param distance integer ---@return modbus_frame|rplc_frame|mgmt_frame|coord_frame|nil packet - public.parse_packet = function(side, sender, reply_to, message, distance) + function public.parse_packet(side, sender, reply_to, message, distance) local pkt = nil local s_pkt = comms.scada_packet() @@ -143,7 +143,7 @@ supervisor.comms = function (version, num_reactors, modem, dev_listen, coord_lis -- handle a packet ---@param packet modbus_frame|rplc_frame|mgmt_frame|coord_frame - public.handle_packet = function(packet) + function public.handle_packet(packet) if packet ~= nil then local l_port = packet.scada_frame.local_port() local r_port = packet.scada_frame.remote_port() diff --git a/supervisor/unit.lua b/supervisor/unit.lua index 628de67..33f5853 100644 --- a/supervisor/unit.lua +++ b/supervisor/unit.lua @@ -23,7 +23,7 @@ local DT_KEYS = { ---@param for_reactor integer reactor unit number ---@param num_boilers integer number of boilers expected ---@param num_turbines integer number of turbines expected -unit.new = function (for_reactor, num_boilers, num_turbines) +function unit.new(for_reactor, num_boilers, num_turbines) local self = { r_id = for_reactor, plc_s = nil, ---@class plc_session @@ -82,7 +82,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- compute a change with respect to time of the given value ---@param key string value key ---@param value number value - local _compute_dt = function (key, value) + local function _compute_dt(key, value) if self.deltas[key] then local data = self.deltas[key] @@ -101,14 +101,14 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- clear a delta ---@param key string value key - local _reset_dt = function (key) + local function _reset_dt(key) self.deltas[key] = nil end -- get the delta t of a value ---@param key string value key ---@return number - local _get_dt = function (key) + local function _get_dt(key) if self.deltas[key] then return self.deltas[key].dt else @@ -117,7 +117,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- update all delta computations - local _dt__compute_all = function () + local function _dt__compute_all() if self.plc_s ~= nil then local plc_db = self.plc_s.get_db() @@ -151,7 +151,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- update the annunciator - local _update_annunciator = function () + local function _update_annunciator() -- update deltas _dt__compute_all() @@ -312,7 +312,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- unlink disconnected units ---@param sessions table - local _unlink_disconnected_units = function (sessions) + local function _unlink_disconnected_units(sessions) util.filter_table(sessions, function (u) return u.is_connected() end) end @@ -320,7 +320,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- link the PLC ---@param plc_session plc_session_struct - public.link_plc_session = function (plc_session) + function public.link_plc_session(plc_session) self.plc_s = plc_session -- reset deltas @@ -333,7 +333,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- link a turbine RTU session ---@param turbine unit_session - public.add_turbine = function (turbine) + function public.add_turbine(turbine) if #self.turbines < self.num_turbines and turbine.get_device_idx() <= self.num_turbines then table.insert(self.turbines, turbine) @@ -349,7 +349,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) -- link a boiler RTU session ---@param boiler unit_session - public.add_boiler = function (boiler) + function public.add_boiler(boiler) if #self.boilers < self.num_boilers and boiler.get_device_idx() <= self.num_boilers then table.insert(self.boilers, boiler) @@ -366,7 +366,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- link a redstone RTU capability - public.add_redstone = function (field, accessor) + function public.add_redstone(field, accessor) -- ensure field exists if self.redstone[field] == nil then self.redstone[field] = {} @@ -377,7 +377,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- update (iterate) this unit - public.update = function () + function public.update() -- unlink PLC if session was closed if not self.plc_s.open then self.plc_s = nil @@ -392,7 +392,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- get build properties of all machines - public.get_build = function () + function public.get_build() local build = {} if self.plc_s ~= nil then @@ -413,7 +413,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- get reactor status - public.get_reactor_status = function () + function public.get_reactor_status() local status = {} if self.plc_s ~= nil then @@ -427,7 +427,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- get RTU statuses - public.get_rtu_statuses = function () + function public.get_rtu_statuses() local status = {} -- status of boilers (including tanks) @@ -452,7 +452,7 @@ unit.new = function (for_reactor, num_boilers, num_turbines) end -- get the annunciator status - public.get_annunciator = function () return self.db.annunciator end + function public.get_annunciator() return self.db.annunciator end return public end