From 790571b6fcb0ba7c43582aa34d5e73797f7ae998 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Wed, 18 May 2022 13:49:04 -0400 Subject: [PATCH] #55 correctly use device IDs vs unit IDs --- supervisor/session/rtu.lua | 8 +++--- supervisor/session/rtu/boiler.lua | 5 ++-- supervisor/session/rtu/emachine.lua | 5 ++-- supervisor/session/rtu/redstone.lua | 8 ++++-- supervisor/session/rtu/turbine.lua | 5 ++-- supervisor/session/rtu/unit_session.lua | 38 +++++++++++++++++++------ supervisor/startup.lua | 2 +- 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/supervisor/session/rtu.lua b/supervisor/session/rtu.lua index 33a66d7..ff95087 100644 --- a/supervisor/session/rtu.lua +++ b/supervisor/session/rtu.lua @@ -88,17 +88,17 @@ rtu.new_session = function (id, in_queue, out_queue, advertisement) -- create unit by type if u_type == RTU_UNIT_TYPES.REDSTONE then - unit, rs_in_q = svrs_redstone.new(self.id, unit_advert, self.out_q) + unit, rs_in_q = svrs_redstone.new(self.id, i, unit_advert, self.out_q) elseif u_type == RTU_UNIT_TYPES.BOILER then - unit = svrs_boiler.new(self.id, unit_advert, self.out_q) + unit = svrs_boiler.new(self.id, i, unit_advert, self.out_q) elseif u_type == RTU_UNIT_TYPES.BOILER_VALVE then -- @todo Mekanism 10.1+ elseif u_type == RTU_UNIT_TYPES.TURBINE then - unit = svrs_turbine.new(self.id, unit_advert, self.out_q) + unit = svrs_turbine.new(self.id, i, unit_advert, self.out_q) elseif u_type == RTU_UNIT_TYPES.TURBINE_VALVE then -- @todo Mekanism 10.1+ elseif u_type == RTU_UNIT_TYPES.EMACHINE then - unit = svrs_emachine.new(self.id, unit_advert, self.out_q) + unit = svrs_emachine.new(self.id, i, unit_advert, self.out_q) elseif u_type == RTU_UNIT_TYPES.IMATRIX then -- @todo Mekanism 10.1+ else diff --git a/supervisor/session/rtu/boiler.lua b/supervisor/session/rtu/boiler.lua index c45e22c..c3fa28e 100644 --- a/supervisor/session/rtu/boiler.lua +++ b/supervisor/session/rtu/boiler.lua @@ -29,9 +29,10 @@ local PERIODICS = { -- create a new boiler rtu session runner ---@param session_id integer +---@param unit_id integer ---@param advert rtu_advertisement ---@param out_queue mqueue -boiler.new = function (session_id, advert, out_queue) +boiler.new = function (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.") @@ -41,7 +42,7 @@ boiler.new = function (session_id, advert, out_queue) local log_tag = "session.rtu(" .. session_id .. ").boiler(" .. advert.index .. "): " local self = { - session = unit_session.new(log_tag, advert, out_queue, TXN_TAGS), + session = unit_session.new(unit_id, advert, out_queue, log_tag, TXN_TAGS), has_build = false, periodics = { next_build_req = 0, diff --git a/supervisor/session/rtu/emachine.lua b/supervisor/session/rtu/emachine.lua index c9c6481..e47293a 100644 --- a/supervisor/session/rtu/emachine.lua +++ b/supervisor/session/rtu/emachine.lua @@ -26,9 +26,10 @@ local PERIODICS = { -- create a new energy machine rtu session runner ---@param session_id integer +---@param unit_id integer ---@param advert rtu_advertisement ---@param out_queue mqueue -emachine.new = function (session_id, advert, out_queue) +emachine.new = function (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.") @@ -38,7 +39,7 @@ emachine.new = function (session_id, advert, out_queue) local log_tag = "session.rtu(" .. session_id .. ").emachine(" .. advert.index .. "): " local self = { - session = unit_session.new(log_tag, advert, out_queue, TXN_TAGS), + session = unit_session.new(unit_id, advert, out_queue, log_tag, TXN_TAGS), has_build = false, periodics = { next_build_req = 0, diff --git a/supervisor/session/rtu/redstone.lua b/supervisor/session/rtu/redstone.lua index 57848d9..b41e223 100644 --- a/supervisor/session/rtu/redstone.lua +++ b/supervisor/session/rtu/redstone.lua @@ -47,19 +47,21 @@ local PERIODICS = { -- create a new redstone rtu session runner ---@param session_id integer +---@param unit_id integer ---@param advert rtu_advertisement ---@param out_queue mqueue -redstone.new = function (session_id, advert, out_queue) +redstone.new = function (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.") return nil end - local log_tag = "session.rtu(" .. session_id .. ").redstone(" .. advert.index .. "): " + -- for redstone, use unit ID not device index + local log_tag = "session.rtu(" .. session_id .. ").redstone(" .. unit_id .. "): " local self = { - session = unit_session.new(log_tag, advert, out_queue, TXN_TAGS), + session = unit_session.new(unit_id, advert, out_queue, log_tag, TXN_TAGS), has_di = false, has_ai = false, periodics = { diff --git a/supervisor/session/rtu/turbine.lua b/supervisor/session/rtu/turbine.lua index d45d244..d62626e 100644 --- a/supervisor/session/rtu/turbine.lua +++ b/supervisor/session/rtu/turbine.lua @@ -30,9 +30,10 @@ local PERIODICS = { -- create a new turbine rtu session runner ---@param session_id integer +---@param unit_id integer ---@param advert rtu_advertisement ---@param out_queue mqueue -turbine.new = function (session_id, advert, out_queue) +turbine.new = function (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.") @@ -42,7 +43,7 @@ turbine.new = function (session_id, advert, out_queue) local log_tag = "session.rtu(" .. session_id .. ").turbine(" .. advert.index .. "): " local self = { - session = unit_session.new(log_tag, advert, out_queue, TXN_TAGS), + session = unit_session.new(unit_id, advert, out_queue, log_tag, TXN_TAGS), has_build = false, periodics = { next_build_req = 0, diff --git a/supervisor/session/rtu/unit_session.lua b/supervisor/session/rtu/unit_session.lua index 67f83c0..4f50120 100644 --- a/supervisor/session/rtu/unit_session.lua +++ b/supervisor/session/rtu/unit_session.lua @@ -11,15 +11,17 @@ local MODBUS_FCODE = types.MODBUS_FCODE local MODBUS_EXCODE = types.MODBUS_EXCODE -- create a new unit session runner ----@param log_tag string ----@param advert rtu_advertisement ----@param out_queue mqueue ----@param txn_tags table -unit_session.new = function (log_tag, advert, out_queue, txn_tags) +---@param unit_id integer MODBUS unit ID +---@param advert rtu_advertisement RTU advertisement for this unit +---@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) local self = { log_tag = log_tag, txn_tags = txn_tags, - uid = advert.index, + unit_id = unit_id, + device_index = advert.index, reactor = advert.reactor, out_q = out_queue, transaction_controller = txnctrl.new(), @@ -43,7 +45,7 @@ unit_session.new = function (log_tag, advert, out_queue, txn_tags) local m_pkt = comms.modbus_packet() local txn_id = self.transaction_controller.create(txn_type) - m_pkt.make(txn_id, self.uid, f_code, register_param) + m_pkt.make(txn_id, self.unit_id, f_code, register_param) self.out_q.push_packet(m_pkt) end @@ -53,7 +55,7 @@ unit_session.new = function (log_tag, advert, out_queue, txn_tags) ---@return integer|false txn_type transaction type or false on error/busy protected.try_resolve = function (m_pkt) if m_pkt.scada_frame.protocol() == PROTOCOLS.MODBUS_TCP then - if m_pkt.unit_id == self.uid then + if m_pkt.unit_id == self.unit_id then local txn_type = self.transaction_controller.resolve(m_pkt.txn_id) local txn_tag = " (" .. self.txn_tags[txn_type] .. ")" @@ -115,7 +117,9 @@ unit_session.new = function (log_tag, advert, out_queue, txn_tags) -- PUBLIC FUNCTIONS -- -- get the unit ID - public.get_uid = function () return self.uid end + public.get_unit_id = function () return self.unit_id end + -- get the device index + public.get_device_idx = function () return self.device_index end -- get the reactor ID public.get_reactor = function () return self.reactor end @@ -126,6 +130,22 @@ unit_session.new = function (log_tag, advert, out_queue, txn_tags) -- check if this unit is faulted public.is_faulted = function () 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) + 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) + log.debug("template unit_session.update() called", true) + end + return protected end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index d647f99..e6e800a 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -13,7 +13,7 @@ local svsessions = require("supervisor.session.svsessions") local config = require("supervisor.config") local supervisor = require("supervisor.supervisor") -local SUPERVISOR_VERSION = "alpha-v0.3.8" +local SUPERVISOR_VERSION = "alpha-v0.3.9" local print = util.print local println = util.println