#580 RTU gateway backplane
This commit is contained in:
75
rtu/rtu.lua
75
rtu/rtu.lua
@@ -36,7 +36,9 @@ function rtu.load_config()
|
||||
config.SVR_Channel = settings.get("SVR_Channel")
|
||||
config.RTU_Channel = settings.get("RTU_Channel")
|
||||
config.ConnTimeout = settings.get("ConnTimeout")
|
||||
config.WirelessModem = settings.get("WirelessModem")
|
||||
config.WiredModem = settings.get("WiredModem")
|
||||
config.PreferWireless = settings.get("PreferWireless")
|
||||
config.TrustedRange = settings.get("TrustedRange")
|
||||
config.AuthKey = settings.get("AuthKey")
|
||||
|
||||
@@ -62,7 +64,9 @@ function rtu.validate_config(cfg)
|
||||
cfv.assert_channel(cfg.RTU_Channel)
|
||||
cfv.assert_type_num(cfg.ConnTimeout)
|
||||
cfv.assert_min(cfg.ConnTimeout, 2)
|
||||
cfv.assert_type_bool(cfg.WirelessModem)
|
||||
cfv.assert((cfg.WiredModem == false) or (type(cfg.WiredModem) == "string"))
|
||||
cfv.assert_type_bool(cfg.PreferWireless)
|
||||
cfv.assert_type_num(cfg.TrustedRange)
|
||||
cfv.assert_min(cfg.TrustedRange, 0)
|
||||
cfv.assert_type_str(cfg.AuthKey)
|
||||
@@ -288,7 +292,7 @@ end
|
||||
-- RTU Communications
|
||||
---@nodiscard
|
||||
---@param version string RTU version
|
||||
---@param nic nic network interface device
|
||||
---@param nic nic|nil network interface device
|
||||
---@param conn_watchdog watchdog watchdog reference
|
||||
function rtu.comms(version, nic, conn_watchdog)
|
||||
local self = {
|
||||
@@ -301,30 +305,43 @@ function rtu.comms(version, nic, conn_watchdog)
|
||||
|
||||
local insert = table.insert
|
||||
|
||||
if nic.isWireless() then
|
||||
comms.set_trusted_range(config.TrustedRange)
|
||||
end
|
||||
-- CONDITIONAL PRIVATE FUNCTIONS --
|
||||
|
||||
-- PRIVATE FUNCTIONS --
|
||||
|
||||
-- configure modem channels
|
||||
nic.closeAll()
|
||||
nic.open(config.RTU_Channel)
|
||||
-- these don't check for nic to be nil to save execution time on functions called extremely often
|
||||
-- when the nic isn't present, the aliases _send and _send_modbus are cleared
|
||||
|
||||
-- send a scada management packet
|
||||
---@param msg_type MGMT_TYPE
|
||||
---@param msg table
|
||||
local function _send(msg_type, msg)
|
||||
local function _nic_send(msg_type, msg)
|
||||
local s_pkt = comms.scada_packet()
|
||||
local m_pkt = comms.mgmt_packet()
|
||||
|
||||
m_pkt.make(msg_type, msg)
|
||||
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
||||
|
||||
---@diagnostic disable-next-line: need-check-nil
|
||||
nic.transmit(config.SVR_Channel, config.RTU_Channel, s_pkt)
|
||||
self.seq_num = self.seq_num + 1
|
||||
end
|
||||
|
||||
-- send a MODBUS TCP packet
|
||||
---@param m_pkt modbus_packet
|
||||
local function _nic_send_modbus(m_pkt)
|
||||
local s_pkt = comms.scada_packet()
|
||||
|
||||
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.MODBUS_TCP, m_pkt.raw_sendable())
|
||||
|
||||
---@diagnostic disable-next-line: need-check-nil
|
||||
nic.transmit(config.SVR_Channel, config.RTU_Channel, s_pkt)
|
||||
self.seq_num = self.seq_num + 1
|
||||
end
|
||||
|
||||
-- PRIVATE FUNCTIONS --
|
||||
|
||||
-- send a scada management packet
|
||||
local _send = _nic_send
|
||||
|
||||
-- keep alive ack
|
||||
---@param srv_time integer
|
||||
local function _send_keep_alive_ack(srv_time)
|
||||
@@ -355,13 +372,7 @@ function rtu.comms(version, nic, conn_watchdog)
|
||||
local public = {}
|
||||
|
||||
-- send a MODBUS TCP packet
|
||||
---@param m_pkt modbus_packet
|
||||
function public.send_modbus(m_pkt)
|
||||
local s_pkt = comms.scada_packet()
|
||||
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.MODBUS_TCP, m_pkt.raw_sendable())
|
||||
nic.transmit(config.SVR_Channel, config.RTU_Channel, s_pkt)
|
||||
self.seq_num = self.seq_num + 1
|
||||
end
|
||||
public.send_modbus = _nic_send_modbus
|
||||
|
||||
-- unlink from the server
|
||||
---@param rtu_state rtu_state
|
||||
@@ -408,6 +419,8 @@ function rtu.comms(version, nic, conn_watchdog)
|
||||
---@param distance integer
|
||||
---@return modbus_frame|mgmt_frame|nil packet
|
||||
function public.parse_packet(side, sender, reply_to, message, distance)
|
||||
-- unreachable if there isn't a nic
|
||||
---@diagnostic disable-next-line: need-check-nil
|
||||
local s_pkt = nic.receive(side, sender, reply_to, message, distance)
|
||||
local pkt = nil
|
||||
|
||||
@@ -598,6 +611,34 @@ function rtu.comms(version, nic, conn_watchdog)
|
||||
end
|
||||
end
|
||||
|
||||
-- set the current NIC
|
||||
---@param _nic nic
|
||||
function public.assign_nic(_nic)
|
||||
if nic then nic.closeAll() end
|
||||
|
||||
if _nic.isWireless() then
|
||||
comms.set_trusted_range(config.TrustedRange)
|
||||
end
|
||||
|
||||
-- configure receive channels
|
||||
_nic.closeAll()
|
||||
_nic.open(config.RTU_Channel)
|
||||
|
||||
nic = _nic
|
||||
_send = _nic_send
|
||||
public.send_modbus = _nic_send_modbus
|
||||
end
|
||||
|
||||
-- clear the current NIC
|
||||
function public.unassign_nic()
|
||||
_send = function () end
|
||||
public.send_modbus = function () end
|
||||
nic = nil
|
||||
end
|
||||
|
||||
-- set the NIC if one was given
|
||||
if nic then public.assign_nic(nic) else public.unassign_nic() end
|
||||
|
||||
return public
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user