#307 PLC integration with new config storage

This commit is contained in:
Mikayla Fischler
2023-10-01 17:10:16 -04:00
parent b1446637ad
commit bfa24b3665
3 changed files with 105 additions and 78 deletions

View File

@@ -26,14 +26,58 @@ local RPS_LIMITS = const.RPS_LIMITS
local PCALL_SCRAM_MSG = "pcall: Scram requires the reactor to be active."
local PCALL_START_MSG = "pcall: Reactor is already active."
---@type plc_config
local config = {}
plc.config = config
-- load the PLC configuration
function plc.load_config()
config.Networked = settings.get("Networked")
config.UnitID = settings.get("UnitID")
config.EmerCoolEnable = settings.get("EmerCoolEnable")
config.EmerCoolSide = settings.get("EmerCoolSide")
config.EmerCoolColor = settings.get("EmerCoolColor")
config.SVR_Channel = settings.get("SVR_Channel")
config.PLC_Channel = settings.get("PLC_Channel")
config.ConnTimeout = settings.get("ConnTimeout")
config.TrustedRange = settings.get("TrustedRange")
config.AuthKey = settings.get("AuthKey")
config.LogMode = settings.get("LogMode")
config.LogPath = settings.get("LogPath")
config.LogDebug = settings.get("LogDebug")
local cfv = util.new_validator()
cfv.assert_type_bool(config.Networked)
cfv.assert_type_int(config.UnitID)
cfv.assert_type_bool(config.EmerCoolEnable)
cfv.assert_channel(config.SVR_Channel)
cfv.assert_channel(config.PLC_Channel)
cfv.assert_type_int(config.ConnTimeout)
cfv.assert_min(config.ConnTimeout, 2)
cfv.assert_type_num(config.TrustedRange)
cfv.assert_type_str(config.AuthKey)
cfv.assert_type_int(config.LogMode)
cfv.assert_type_str(config.LogPath)
cfv.assert_type_bool(config.LogDebug)
-- check emergency coolant configuration if enabled
if config.EmerCoolEnable then
cfv.assert_eq(rsio.is_valid_side(config.EmerCoolSide), true)
cfv.assert_eq(config.EmerCoolColor == nil or rsio.is_color(config.EmerCoolColor), true)
end
return cfv.valid()
end
-- RPS: Reactor Protection System<br>
-- identifies dangerous states and SCRAMs reactor if warranted<br>
-- autonomous from main SCADA supervisor/coordinator control
---@nodiscard
---@param reactor table
---@param is_formed boolean
---@param emer_cool nil|table emergency coolant configuration
function plc.rps_init(reactor, is_formed, emer_cool)
function plc.rps_init(reactor, is_formed)
local state_keys = {
high_dmg = 1,
high_temp = 2,
@@ -73,22 +117,22 @@ function plc.rps_init(reactor, is_formed, emer_cool)
---@param state boolean true to enable emergency coolant, false to disable
local function _set_emer_cool(state)
-- check if this was configured: if it's a table, fields have already been validated.
if type(emer_cool) == "table" then
if config.EmerCoolEnable then
local level = rsio.digital_write_active(rsio.IO.U_EMER_COOL, state)
if level ~= false then
if rsio.is_color(emer_cool.color) then
local output = rs.getBundledOutput(emer_cool.side)
if rsio.is_color(config.EmerCoolColor) then
local output = rs.getBundledOutput(config.EmerCoolSide)
if rsio.digital_write(level) then
output = colors.combine(output, emer_cool.color)
output = colors.combine(output, config.EmerCoolColor)
else
output = colors.subtract(output, emer_cool.color)
output = colors.subtract(output, config.EmerCoolColor)
end
rs.setBundledOutput(emer_cool.side, output)
rs.setBundledOutput(config.EmerCoolSide, output)
else
rs.setOutput(emer_cool.side, rsio.digital_write(level))
rs.setOutput(config.EmerCoolSide, rsio.digital_write(level))
end
if state ~= self.emer_cool_active then
@@ -443,16 +487,12 @@ end
-- Reactor PLC Communications
---@nodiscard
---@param id integer reactor ID
---@param version string PLC version
---@param nic nic network interface device
---@param plc_channel integer PLC comms channel
---@param svr_channel integer supervisor server channel
---@param range integer trusted device connection range
---@param reactor table reactor device
---@param rps rps RPS reference
---@param conn_watchdog watchdog watchdog reference
function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, rps, conn_watchdog)
function plc.comms(version, nic, reactor, rps, conn_watchdog)
local self = {
sv_addr = comms.BROADCAST,
seq_num = 0,
@@ -466,13 +506,13 @@ function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, r
max_burn_rate = nil
}
comms.set_trusted_range(range)
comms.set_trusted_range(config.TrustedRange)
-- PRIVATE FUNCTIONS --
-- configure network channels
nic.closeAll()
nic.open(plc_channel)
nic.open(config.PLC_Channel)
-- send an RPLC packet
---@param msg_type RPLC_TYPE
@@ -481,10 +521,10 @@ function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, r
local s_pkt = comms.scada_packet()
local r_pkt = comms.rplc_packet()
r_pkt.make(id, msg_type, msg)
r_pkt.make(config.UnitID, msg_type, msg)
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.RPLC, r_pkt.raw_sendable())
nic.transmit(svr_channel, plc_channel, s_pkt)
nic.transmit(config.SVR_Channel, config.PLC_Channel, s_pkt)
self.seq_num = self.seq_num + 1
end
@@ -498,7 +538,7 @@ function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, r
m_pkt.make(msg_type, msg)
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
nic.transmit(svr_channel, plc_channel, s_pkt)
nic.transmit(config.SVR_Channel, config.PLC_Channel, s_pkt)
self.seq_num = self.seq_num + 1
end
@@ -673,7 +713,7 @@ function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, r
-- attempt to establish link with supervisor
function public.send_link_req()
_send_mgmt(MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.PLC, id })
_send_mgmt(MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.PLC, config.UnitID })
end
-- send live status information
@@ -769,7 +809,7 @@ function plc.comms(id, version, nic, plc_channel, svr_channel, range, reactor, r
local src_addr = packet.scada_frame.src_addr()
-- handle packets now that we have prints setup
if l_chan == plc_channel then
if l_chan == config.PLC_Channel then
-- check sequence number
if self.r_seq_num == nil then
self.r_seq_num = packet.scada_frame.seq_num()