2023-04-08 20:49:54 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local const = require("scada-common.constants")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local ppm = require("scada-common.ppm")
|
2023-04-09 01:51:34 +00:00
|
|
|
local rsio = require("scada-common.rsio")
|
2023-04-08 20:49:54 +00:00
|
|
|
local types = require("scada-common.types")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2023-04-21 00:47:14 +00:00
|
|
|
local databus = require("reactor-plc.databus")
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local plc = {}
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2023-02-21 16:29:04 +00:00
|
|
|
local RPS_TRIP_CAUSE = types.RPS_TRIP_CAUSE
|
2022-05-03 14:44:18 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
local PROTOCOL = comms.PROTOCOL
|
|
|
|
local DEVICE_TYPE = comms.DEVICE_TYPE
|
2022-11-13 19:13:30 +00:00
|
|
|
local ESTABLISH_ACK = comms.ESTABLISH_ACK
|
2023-02-21 16:05:57 +00:00
|
|
|
local RPLC_TYPE = comms.RPLC_TYPE
|
2023-08-30 20:45:48 +00:00
|
|
|
local MGMT_TYPE = comms.MGMT_TYPE
|
2023-01-26 23:26:26 +00:00
|
|
|
local AUTO_ACK = comms.PLC_AUTO_ACK
|
2022-04-21 14:26:02 +00:00
|
|
|
|
2023-02-25 07:25:35 +00:00
|
|
|
local RPS_LIMITS = const.RPS_LIMITS
|
|
|
|
|
2022-09-29 15:02:03 +00:00
|
|
|
-- I sure hope the devs don't change this error message, not that it would have safety implications
|
2022-10-07 14:29:25 +00:00
|
|
|
-- I wish they didn't change it to be like this
|
2022-09-29 15:02:03 +00:00
|
|
|
local PCALL_SCRAM_MSG = "pcall: Scram requires the reactor to be active."
|
2022-10-07 14:29:25 +00:00
|
|
|
local PCALL_START_MSG = "pcall: Reactor is already active."
|
2022-09-29 15:02:03 +00:00
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
---@type plc_config
|
|
|
|
local config = {}
|
|
|
|
|
|
|
|
plc.config = config
|
|
|
|
|
|
|
|
-- load the PLC configuration
|
|
|
|
function plc.load_config()
|
2023-10-04 02:52:13 +00:00
|
|
|
if not settings.load("/reactor-plc.settings") then return false end
|
2023-10-01 21:12:59 +00:00
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
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)
|
2023-10-14 14:07:56 +00:00
|
|
|
|
|
|
|
if config.Networked == true then
|
|
|
|
cfv.assert_channel(config.SVR_Channel)
|
|
|
|
cfv.assert_channel(config.PLC_Channel)
|
2023-12-31 01:25:57 +00:00
|
|
|
cfv.assert_type_num(config.ConnTimeout)
|
2023-10-14 14:07:56 +00:00
|
|
|
cfv.assert_min(config.ConnTimeout, 2)
|
|
|
|
cfv.assert_type_num(config.TrustedRange)
|
|
|
|
cfv.assert_min(config.TrustedRange, 0)
|
|
|
|
cfv.assert_type_str(config.AuthKey)
|
2023-10-14 16:02:25 +00:00
|
|
|
|
|
|
|
if type(config.AuthKey) == "string" then
|
|
|
|
local len = string.len(config.AuthKey)
|
|
|
|
cfv.assert_eq(len == 0 or len >= 8, true)
|
|
|
|
end
|
2023-10-14 14:07:56 +00:00
|
|
|
end
|
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
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
|
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
-- RPS: Reactor Protection System<br>
|
|
|
|
-- identifies dangerous states and SCRAMs reactor if warranted<br>
|
|
|
|
-- autonomous from main SCADA supervisor/coordinator control
|
|
|
|
---@nodiscard
|
2022-10-25 17:29:57 +00:00
|
|
|
---@param reactor table
|
|
|
|
---@param is_formed boolean
|
2023-10-01 21:10:16 +00:00
|
|
|
function plc.rps_init(reactor, is_formed)
|
2022-05-05 17:14:14 +00:00
|
|
|
local state_keys = {
|
2023-03-05 03:32:13 +00:00
|
|
|
high_dmg = 1,
|
2022-05-05 17:14:14 +00:00
|
|
|
high_temp = 2,
|
2023-03-05 03:19:53 +00:00
|
|
|
low_coolant = 3,
|
2022-05-05 17:14:14 +00:00
|
|
|
ex_waste = 4,
|
|
|
|
ex_hcoolant = 5,
|
|
|
|
no_fuel = 6,
|
|
|
|
fault = 7,
|
|
|
|
timeout = 8,
|
2022-10-25 17:29:57 +00:00
|
|
|
manual = 9,
|
2022-11-11 20:45:46 +00:00
|
|
|
automatic = 10,
|
|
|
|
sys_fail = 11,
|
|
|
|
force_disabled = 12
|
2022-05-05 17:14:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
local self = {
|
2022-11-11 20:45:46 +00:00
|
|
|
state = { false, false, false, false, false, false, false, false, false, false, false, false },
|
2022-05-05 17:14:14 +00:00
|
|
|
reactor_enabled = false,
|
2023-01-26 23:26:26 +00:00
|
|
|
enabled_at = 0,
|
2023-04-07 12:05:14 +00:00
|
|
|
emer_cool_active = nil, ---@type boolean
|
2022-10-25 17:29:57 +00:00
|
|
|
formed = is_formed,
|
2022-11-11 20:45:46 +00:00
|
|
|
force_disabled = false,
|
2022-01-22 19:26:25 +00:00
|
|
|
tripped = false,
|
2023-02-22 04:50:43 +00:00
|
|
|
trip_cause = "ok" ---@type rps_trip_cause
|
2022-01-14 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 14:34:41 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
2022-04-03 16:08:22 +00:00
|
|
|
|
2022-05-05 17:14:14 +00:00
|
|
|
-- set reactor access fault flag
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _set_fault()
|
2023-02-22 04:50:43 +00:00
|
|
|
if reactor.__p_last_fault() ~= "Terminated" then
|
2022-05-19 13:19:51 +00:00
|
|
|
self.state[state_keys.fault] = true
|
|
|
|
end
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
2023-04-07 12:05:14 +00:00
|
|
|
-- set emergency coolant control (if configured)
|
|
|
|
---@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.
|
2023-10-01 21:10:16 +00:00
|
|
|
if config.EmerCoolEnable then
|
2023-04-07 12:05:14 +00:00
|
|
|
local level = rsio.digital_write_active(rsio.IO.U_EMER_COOL, state)
|
|
|
|
|
|
|
|
if level ~= false then
|
2023-10-01 21:10:16 +00:00
|
|
|
if rsio.is_color(config.EmerCoolColor) then
|
|
|
|
local output = rs.getBundledOutput(config.EmerCoolSide)
|
2023-04-07 12:05:14 +00:00
|
|
|
|
|
|
|
if rsio.digital_write(level) then
|
2023-10-01 21:10:16 +00:00
|
|
|
output = colors.combine(output, config.EmerCoolColor)
|
2023-04-07 12:05:14 +00:00
|
|
|
else
|
2023-10-01 21:10:16 +00:00
|
|
|
output = colors.subtract(output, config.EmerCoolColor)
|
2023-04-07 12:05:14 +00:00
|
|
|
end
|
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
rs.setBundledOutput(config.EmerCoolSide, output)
|
2023-04-07 12:05:14 +00:00
|
|
|
else
|
2023-10-01 21:10:16 +00:00
|
|
|
rs.setOutput(config.EmerCoolSide, rsio.digital_write(level))
|
2023-04-07 12:05:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if state ~= self.emer_cool_active then
|
|
|
|
if state then
|
|
|
|
log.info("RPS: emergency coolant valve OPENED")
|
|
|
|
else
|
|
|
|
log.info("RPS: emergency coolant valve CLOSED")
|
|
|
|
end
|
|
|
|
|
|
|
|
self.emer_cool_active = state
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- check if the reactor is formed
|
|
|
|
local function _is_formed()
|
2023-02-22 04:50:43 +00:00
|
|
|
local formed = reactor.isFormed()
|
2022-11-05 16:44:40 +00:00
|
|
|
if formed == ppm.ACCESS_FAULT then
|
2022-10-25 17:29:57 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
_set_fault()
|
2022-11-05 16:44:40 +00:00
|
|
|
else
|
|
|
|
self.formed = formed
|
|
|
|
|
|
|
|
if not self.state[state_keys.sys_fail] then
|
|
|
|
self.state[state_keys.sys_fail] = not formed
|
|
|
|
end
|
2022-10-25 17:29:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 20:45:46 +00:00
|
|
|
-- check if the reactor is force disabled
|
|
|
|
local function _is_force_disabled()
|
2023-02-22 04:50:43 +00:00
|
|
|
local disabled = reactor.isForceDisabled()
|
2022-11-11 20:45:46 +00:00
|
|
|
if disabled == ppm.ACCESS_FAULT then
|
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
_set_fault()
|
|
|
|
else
|
|
|
|
self.force_disabled = disabled
|
|
|
|
|
|
|
|
if not self.state[state_keys.force_disabled] then
|
|
|
|
self.state[state_keys.force_disabled] = disabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-05 02:55:40 +00:00
|
|
|
-- check for high damage
|
2023-03-05 03:32:13 +00:00
|
|
|
local function _high_damage()
|
2023-02-22 04:50:43 +00:00
|
|
|
local damage_percent = reactor.getDamagePercent()
|
2022-04-18 01:12:25 +00:00
|
|
|
if damage_percent == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
2023-03-05 03:32:13 +00:00
|
|
|
elseif not self.state[state_keys.high_dmg] then
|
|
|
|
self.state[state_keys.high_dmg] = damage_percent >= RPS_LIMITS.MAX_DAMAGE_PERCENT
|
2022-04-07 15:12:41 +00:00
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 17:14:14 +00:00
|
|
|
-- check if the reactor is at a critically high temperature
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _high_temp()
|
2022-05-05 17:14:14 +00:00
|
|
|
-- mekanism: MAX_DAMAGE_TEMPERATURE = 1_200
|
2023-02-22 04:50:43 +00:00
|
|
|
local temp = reactor.getTemperature()
|
2022-05-05 17:14:14 +00:00
|
|
|
if temp == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
2022-10-25 17:29:57 +00:00
|
|
|
elseif not self.state[state_keys.high_temp] then
|
2023-02-25 07:25:35 +00:00
|
|
|
self.state[state_keys.high_temp] = temp >= RPS_LIMITS.MAX_DAMAGE_TEMPERATURE
|
2022-04-07 15:12:41 +00:00
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2023-03-05 03:19:53 +00:00
|
|
|
-- check if there is very low coolant
|
|
|
|
local function _low_coolant()
|
2023-02-22 04:50:43 +00:00
|
|
|
local coolant_filled = reactor.getCoolantFilledPercentage()
|
2022-05-05 17:14:14 +00:00
|
|
|
if coolant_filled == ppm.ACCESS_FAULT then
|
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
_set_fault()
|
2023-03-05 03:19:53 +00:00
|
|
|
elseif not self.state[state_keys.low_coolant] then
|
|
|
|
self.state[state_keys.low_coolant] = coolant_filled < RPS_LIMITS.MIN_COOLANT_FILL
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check for excess waste (>80% filled)
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _excess_waste()
|
2023-02-22 04:50:43 +00:00
|
|
|
local w_filled = reactor.getWasteFilledPercentage()
|
2022-05-05 17:14:14 +00:00
|
|
|
if w_filled == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
2022-10-25 17:29:57 +00:00
|
|
|
elseif not self.state[state_keys.ex_waste] then
|
2023-02-25 07:25:35 +00:00
|
|
|
self.state[state_keys.ex_waste] = w_filled > RPS_LIMITS.MAX_WASTE_FILL
|
2022-04-07 15:12:41 +00:00
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 17:14:14 +00:00
|
|
|
-- check for heated coolant backup (>95% filled)
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _excess_heated_coolant()
|
2023-02-22 04:50:43 +00:00
|
|
|
local hc_filled = reactor.getHeatedCoolantFilledPercentage()
|
2022-05-05 17:14:14 +00:00
|
|
|
if hc_filled == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
2022-10-25 17:29:57 +00:00
|
|
|
elseif not self.state[state_keys.ex_hcoolant] then
|
2023-02-25 07:25:35 +00:00
|
|
|
self.state[state_keys.ex_hcoolant] = hc_filled > RPS_LIMITS.MAX_HEATED_COLLANT_FILL
|
2022-04-07 15:12:41 +00:00
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check if there is no fuel
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _insufficient_fuel()
|
2023-02-25 17:07:25 +00:00
|
|
|
local fuel = reactor.getFuelFilledPercentage()
|
2022-04-18 01:12:25 +00:00
|
|
|
if fuel == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
2022-10-25 17:29:57 +00:00
|
|
|
elseif not self.state[state_keys.no_fuel] then
|
2023-02-25 07:25:35 +00:00
|
|
|
self.state[state_keys.no_fuel] = fuel <= RPS_LIMITS.NO_FUEL_FILL
|
2022-04-07 15:12:41 +00:00
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-25 14:34:41 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
---@class rps
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-25 14:34:41 +00:00
|
|
|
-- re-link a reactor after a peripheral re-connect
|
2023-02-22 04:50:43 +00:00
|
|
|
---@param new_reactor table reconnected reactor
|
|
|
|
function public.reconnect_reactor(new_reactor)
|
|
|
|
reactor = new_reactor
|
2022-04-25 14:34:41 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- trip for lost peripheral
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.trip_fault()
|
2022-05-11 01:49:14 +00:00
|
|
|
_set_fault()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- trip for a PLC comms timeout
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.trip_timeout()
|
2022-05-22 21:57:24 +00:00
|
|
|
self.state[state_keys.timeout] = true
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- manually SCRAM the reactor
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.trip_manual()
|
2022-10-25 17:29:57 +00:00
|
|
|
self.state[state_keys.manual] = true
|
|
|
|
end
|
|
|
|
|
2023-02-20 17:08:51 +00:00
|
|
|
-- automatic SCRAM commanded by supervisor
|
2022-11-11 20:45:46 +00:00
|
|
|
function public.trip_auto()
|
|
|
|
self.state[state_keys.automatic] = true
|
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- trip for unformed reactor
|
|
|
|
function public.trip_sys_fail()
|
|
|
|
self.state[state_keys.fault] = true
|
|
|
|
self.state[state_keys.sys_fail] = true
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
2023-09-19 20:37:15 +00:00
|
|
|
-- SCRAM the reactor now<br>
|
2022-05-11 01:49:14 +00:00
|
|
|
---@return boolean success
|
2023-09-19 20:37:15 +00:00
|
|
|
--- EVENT_CONSUMER: this function consumes events
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.scram()
|
2022-05-05 17:14:14 +00:00
|
|
|
log.info("RPS: reactor SCRAM")
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
reactor.scram()
|
|
|
|
if reactor.__p_is_faulted() and (reactor.__p_last_fault() ~= PCALL_SCRAM_MSG) then
|
2022-05-05 17:14:14 +00:00
|
|
|
log.error("RPS: failed reactor SCRAM")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
self.reactor_enabled = false
|
2023-01-26 23:26:26 +00:00
|
|
|
self.last_runtime = util.time_ms() - self.enabled_at
|
2022-05-05 17:14:14 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-19 20:37:15 +00:00
|
|
|
-- start the reactor now<br>
|
2022-05-11 01:49:14 +00:00
|
|
|
---@return boolean success
|
2023-09-19 20:37:15 +00:00
|
|
|
--- EVENT_CONSUMER: this function consumes events
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.activate()
|
2022-05-05 17:14:14 +00:00
|
|
|
if not self.tripped then
|
|
|
|
log.info("RPS: reactor start")
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
reactor.activate()
|
|
|
|
if reactor.__p_is_faulted() and (reactor.__p_last_fault() ~= PCALL_START_MSG) then
|
2022-05-05 17:14:14 +00:00
|
|
|
log.error("RPS: failed reactor start")
|
|
|
|
else
|
|
|
|
self.reactor_enabled = true
|
2023-01-26 23:26:26 +00:00
|
|
|
self.enabled_at = util.time_ms()
|
2022-05-05 17:14:14 +00:00
|
|
|
return true
|
|
|
|
end
|
2022-10-20 17:27:33 +00:00
|
|
|
else
|
|
|
|
log.debug(util.c("RPS: failed start, RPS tripped: ", self.trip_cause))
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- automatic control activate/re-activate
|
|
|
|
---@return boolean success
|
|
|
|
function public.auto_activate()
|
|
|
|
-- clear automatic SCRAM if it was the cause
|
|
|
|
if self.tripped and self.trip_cause == "automatic" then
|
|
|
|
self.state[state_keys.automatic] = true
|
2023-02-21 16:29:04 +00:00
|
|
|
self.trip_cause = RPS_TRIP_CAUSE.OK
|
2023-01-26 23:26:26 +00:00
|
|
|
self.tripped = false
|
|
|
|
|
|
|
|
log.debug("RPS: cleared automatic SCRAM for re-activation")
|
|
|
|
end
|
|
|
|
|
|
|
|
return public.activate()
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check all safety conditions
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2023-02-21 16:29:04 +00:00
|
|
|
---@return boolean tripped, rps_trip_cause trip_status, boolean first_trip
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.check()
|
2023-02-21 16:29:04 +00:00
|
|
|
local status = RPS_TRIP_CAUSE.OK
|
2022-01-22 19:26:25 +00:00
|
|
|
local was_tripped = self.tripped
|
2022-05-05 17:14:14 +00:00
|
|
|
local first_trip = false
|
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
if self.formed then
|
|
|
|
-- update state
|
|
|
|
parallel.waitForAll(
|
|
|
|
_is_formed,
|
2022-11-11 20:45:46 +00:00
|
|
|
_is_force_disabled,
|
2023-03-05 03:32:13 +00:00
|
|
|
_high_damage,
|
2022-10-26 03:40:36 +00:00
|
|
|
_high_temp,
|
2023-03-05 03:19:53 +00:00
|
|
|
_low_coolant,
|
2022-10-26 03:40:36 +00:00
|
|
|
_excess_waste,
|
|
|
|
_excess_heated_coolant,
|
|
|
|
_insufficient_fuel
|
|
|
|
)
|
|
|
|
else
|
|
|
|
-- check to see if its now formed
|
|
|
|
_is_formed()
|
|
|
|
end
|
2022-04-25 14:34:41 +00:00
|
|
|
|
2022-01-14 21:33:09 +00:00
|
|
|
-- check system states in order of severity
|
2022-04-25 15:40:53 +00:00
|
|
|
if self.tripped then
|
|
|
|
status = self.trip_cause
|
2022-10-25 17:29:57 +00:00
|
|
|
elseif self.state[state_keys.sys_fail] then
|
|
|
|
log.warning("RPS: system failure, reactor not formed")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.SYS_FAIL
|
2022-11-11 20:45:46 +00:00
|
|
|
elseif self.state[state_keys.force_disabled] then
|
|
|
|
log.warning("RPS: reactor was force disabled")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.FORCE_DISABLED
|
2023-03-05 03:32:13 +00:00
|
|
|
elseif self.state[state_keys.high_dmg] then
|
|
|
|
log.warning("RPS: high damage")
|
|
|
|
status = RPS_TRIP_CAUSE.HIGH_DMG
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.high_temp] then
|
|
|
|
log.warning("RPS: high temperature")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.HIGH_TEMP
|
2023-03-05 03:19:53 +00:00
|
|
|
elseif self.state[state_keys.low_coolant] then
|
|
|
|
log.warning("RPS: low coolant")
|
|
|
|
status = RPS_TRIP_CAUSE.LOW_COOLANT
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.ex_waste] then
|
|
|
|
log.warning("RPS: full waste")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.EX_WASTE
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.ex_hcoolant] then
|
|
|
|
log.warning("RPS: heated coolant backup")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.EX_HCOOLANT
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.no_fuel] then
|
|
|
|
log.warning("RPS: no fuel")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.NO_FUEL
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.fault] then
|
|
|
|
log.warning("RPS: reactor access fault")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.FAULT
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.timeout] then
|
|
|
|
log.warning("RPS: supervisor connection timeout")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.TIMEOUT
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.manual] then
|
|
|
|
log.warning("RPS: manual SCRAM requested")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.MANUAL
|
2022-11-11 20:45:46 +00:00
|
|
|
elseif self.state[state_keys.automatic] then
|
|
|
|
log.warning("RPS: automatic SCRAM requested")
|
2023-02-21 16:29:04 +00:00
|
|
|
status = RPS_TRIP_CAUSE.AUTOMATIC
|
2022-01-02 00:45:33 +00:00
|
|
|
else
|
2022-01-22 19:26:25 +00:00
|
|
|
self.tripped = false
|
2023-02-21 16:29:04 +00:00
|
|
|
self.trip_cause = RPS_TRIP_CAUSE.OK
|
2022-01-02 00:45:33 +00:00
|
|
|
end
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2022-05-05 17:14:14 +00:00
|
|
|
-- if a new trip occured...
|
2023-02-21 16:29:04 +00:00
|
|
|
if (not was_tripped) and (status ~= RPS_TRIP_CAUSE.OK) then
|
2022-04-25 15:40:53 +00:00
|
|
|
first_trip = true
|
2022-01-22 19:26:25 +00:00
|
|
|
self.tripped = true
|
|
|
|
self.trip_cause = status
|
2022-04-25 15:40:53 +00:00
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- in the case that the reactor is detected to be active,
|
|
|
|
-- it will be scrammed shortly after this in the main RPS loop if we don't here
|
2022-10-26 03:40:36 +00:00
|
|
|
if self.formed then
|
2022-11-11 20:45:46 +00:00
|
|
|
if not self.force_disabled then
|
|
|
|
public.scram()
|
|
|
|
else
|
|
|
|
log.warning("RPS: skipping SCRAM due to reactor being force disabled")
|
|
|
|
end
|
2022-10-26 03:40:36 +00:00
|
|
|
else
|
|
|
|
log.warning("RPS: skipping SCRAM due to not being formed")
|
|
|
|
end
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
2022-05-05 17:14:14 +00:00
|
|
|
|
2023-04-07 12:05:14 +00:00
|
|
|
-- update emergency coolant control if configured
|
|
|
|
_set_emer_cool(self.state[state_keys.low_coolant])
|
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
-- report RPS status
|
2023-04-18 15:28:46 +00:00
|
|
|
databus.tx_rps(self.tripped, self.state, self.emer_cool_active)
|
2023-04-08 20:49:54 +00:00
|
|
|
|
2022-01-22 19:26:25 +00:00
|
|
|
return self.tripped, status, first_trip
|
|
|
|
end
|
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.status() return self.state end
|
2022-12-07 17:59:21 +00:00
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.is_tripped() return self.tripped end
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-12-07 17:59:21 +00:00
|
|
|
function public.get_trip_cause() return self.trip_cause end
|
2023-04-07 12:05:14 +00:00
|
|
|
---@nodiscard
|
|
|
|
function public.is_low_coolant() return self.states[state_keys.low_coolant] end
|
2022-12-07 17:59:21 +00:00
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.is_active() return self.reactor_enabled end
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-10-25 17:29:57 +00:00
|
|
|
function public.is_formed() return self.formed end
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-11-11 20:45:46 +00:00
|
|
|
function public.is_force_disabled() return self.force_disabled end
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- get the runtime of the reactor if active, or the last runtime if disabled
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2023-01-26 23:26:26 +00:00
|
|
|
---@return integer runtime time since last enable
|
|
|
|
function public.get_runtime() return util.trinary(self.reactor_enabled, util.time_ms() - self.enabled_at, self.last_runtime) end
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- reset the RPS
|
2022-10-25 17:29:57 +00:00
|
|
|
---@param quiet? boolean true to suppress the info log message
|
|
|
|
function public.reset(quiet)
|
2022-01-22 19:26:25 +00:00
|
|
|
self.tripped = false
|
2023-02-21 16:29:04 +00:00
|
|
|
self.trip_cause = RPS_TRIP_CAUSE.OK
|
2022-05-11 01:49:14 +00:00
|
|
|
|
|
|
|
for i = 1, #self.state do
|
|
|
|
self.state[i] = false
|
|
|
|
end
|
2022-10-20 17:27:33 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if not quiet then log.info("RPS: reset") end
|
2022-01-22 19:26:25 +00:00
|
|
|
end
|
|
|
|
|
2023-02-07 05:32:50 +00:00
|
|
|
-- reset the automatic and timeout trip flags, then clear trip if that was the trip cause
|
|
|
|
function public.auto_reset()
|
|
|
|
self.state[state_keys.automatic] = false
|
|
|
|
self.state[state_keys.timeout] = false
|
|
|
|
|
2023-02-21 16:29:04 +00:00
|
|
|
if self.trip_cause == RPS_TRIP_CAUSE.AUTOMATIC or self.trip_cause == RPS_TRIP_CAUSE.TIMEOUT then
|
|
|
|
self.trip_cause = RPS_TRIP_CAUSE.OK
|
2023-02-07 05:32:50 +00:00
|
|
|
self.tripped = false
|
|
|
|
|
2023-02-10 03:52:10 +00:00
|
|
|
log.info("RPS: auto reset")
|
|
|
|
end
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
|
|
|
|
2023-04-09 01:35:44 +00:00
|
|
|
-- link functions with databus
|
|
|
|
databus.link_rps(public.trip_manual, public.reset)
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
return public
|
2022-01-02 00:45:33 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- Reactor PLC Communications
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param version string PLC version
|
2023-06-23 17:52:24 +00:00
|
|
|
---@param nic nic network interface device
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param reactor table reactor device
|
|
|
|
---@param rps rps RPS reference
|
|
|
|
---@param conn_watchdog watchdog watchdog reference
|
2023-10-01 21:10:16 +00:00
|
|
|
function plc.comms(version, nic, reactor, rps, conn_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
2023-06-05 05:13:22 +00:00
|
|
|
sv_addr = comms.BROADCAST,
|
2022-03-25 16:17:46 +00:00
|
|
|
seq_num = 0,
|
2022-04-24 01:10:25 +00:00
|
|
|
r_seq_num = nil,
|
2022-04-02 15:46:14 +00:00
|
|
|
scrammed = false,
|
2022-04-21 16:40:21 +00:00
|
|
|
linked = false,
|
2023-02-16 00:59:58 +00:00
|
|
|
last_est_ack = ESTABLISH_ACK.ALLOW,
|
2022-10-26 03:40:36 +00:00
|
|
|
resend_build = false,
|
2023-02-04 18:47:00 +00:00
|
|
|
auto_ack_token = 0,
|
2023-01-26 23:26:26 +00:00
|
|
|
status_cache = nil,
|
2022-04-21 16:40:21 +00:00
|
|
|
max_burn_rate = nil
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
comms.set_trusted_range(config.TrustedRange)
|
2023-02-07 22:31:22 +00:00
|
|
|
|
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2023-06-23 17:52:24 +00:00
|
|
|
-- configure network channels
|
|
|
|
nic.closeAll()
|
2023-10-01 21:10:16 +00:00
|
|
|
nic.open(config.PLC_Channel)
|
2022-09-23 00:42:06 +00:00
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- send an RPLC packet
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param msg_type RPLC_TYPE
|
2022-09-21 19:53:51 +00:00
|
|
|
---@param msg table
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _send(msg_type, msg)
|
2022-05-02 21:40:00 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local r_pkt = comms.rplc_packet()
|
2022-05-02 16:06:04 +00:00
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
r_pkt.make(config.UnitID, msg_type, msg)
|
2023-06-05 05:13:22 +00:00
|
|
|
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.RPLC, r_pkt.raw_sendable())
|
2022-05-02 16:06:04 +00:00
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
nic.transmit(config.SVR_Channel, config.PLC_Channel, s_pkt)
|
2022-05-02 21:40:00 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
2022-05-02 16:06:04 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- send a SCADA management packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param msg_type MGMT_TYPE
|
2022-09-21 19:53:51 +00:00
|
|
|
---@param msg table
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _send_mgmt(msg_type, msg)
|
2022-05-02 21:40:00 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
2022-04-23 01:39:03 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
m_pkt.make(msg_type, msg)
|
2023-06-05 05:13:22 +00:00
|
|
|
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-04-23 01:39:03 +00:00
|
|
|
|
2023-10-01 21:10:16 +00:00
|
|
|
nic.transmit(config.SVR_Channel, config.PLC_Channel, s_pkt)
|
2022-05-02 21:40:00 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- variable reactor status information, excluding heating rate
|
2022-05-11 01:49:14 +00:00
|
|
|
---@return table data_table, boolean faulted
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _reactor_status()
|
2022-07-15 13:58:04 +00:00
|
|
|
local fuel = nil
|
|
|
|
local waste = nil
|
2022-05-02 21:40:00 +00:00
|
|
|
local coolant = nil
|
|
|
|
local hcoolant = nil
|
|
|
|
|
|
|
|
local data_table = {
|
|
|
|
false, -- getStatus
|
|
|
|
0, -- getBurnRate
|
|
|
|
0, -- getActualBurnRate
|
|
|
|
0, -- getTemperature
|
|
|
|
0, -- getDamagePercent
|
|
|
|
0, -- getBoilEfficiency
|
|
|
|
0, -- getEnvironmentalLoss
|
2022-09-30 21:33:35 +00:00
|
|
|
0, -- fuel_amnt
|
2022-05-02 21:40:00 +00:00
|
|
|
0, -- getFuelFilledPercentage
|
2022-09-30 21:33:35 +00:00
|
|
|
0, -- waste_amnt
|
2022-05-02 21:40:00 +00:00
|
|
|
0, -- getWasteFilledPercentage
|
|
|
|
"", -- coolant_name
|
|
|
|
0, -- coolant_amnt
|
|
|
|
0, -- getCoolantFilledPercentage
|
|
|
|
"", -- hcoolant_name
|
|
|
|
0, -- hcoolant_amnt
|
|
|
|
0 -- getHeatedCoolantFilledPercentage
|
|
|
|
}
|
|
|
|
|
|
|
|
local tasks = {
|
2023-02-22 04:50:43 +00:00
|
|
|
function () data_table[1] = reactor.getStatus() end,
|
|
|
|
function () data_table[2] = reactor.getBurnRate() end,
|
|
|
|
function () data_table[3] = reactor.getActualBurnRate() end,
|
|
|
|
function () data_table[4] = reactor.getTemperature() end,
|
|
|
|
function () data_table[5] = reactor.getDamagePercent() end,
|
|
|
|
function () data_table[6] = reactor.getBoilEfficiency() end,
|
|
|
|
function () data_table[7] = reactor.getEnvironmentalLoss() end,
|
|
|
|
function () fuel = reactor.getFuel() end,
|
|
|
|
function () data_table[9] = reactor.getFuelFilledPercentage() end,
|
|
|
|
function () waste = reactor.getWaste() end,
|
|
|
|
function () data_table[11] = reactor.getWasteFilledPercentage() end,
|
|
|
|
function () coolant = reactor.getCoolant() end,
|
|
|
|
function () data_table[14] = reactor.getCoolantFilledPercentage() end,
|
|
|
|
function () hcoolant = reactor.getHeatedCoolant() end,
|
|
|
|
function () data_table[17] = reactor.getHeatedCoolantFilledPercentage() end
|
2022-05-02 21:40:00 +00:00
|
|
|
}
|
2022-04-27 22:49:54 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
parallel.waitForAll(table.unpack(tasks))
|
2022-04-27 22:49:54 +00:00
|
|
|
|
2022-09-30 21:33:35 +00:00
|
|
|
if fuel ~= nil then
|
2022-07-15 13:58:04 +00:00
|
|
|
data_table[8] = fuel.amount
|
|
|
|
end
|
|
|
|
|
2022-09-30 21:33:35 +00:00
|
|
|
if waste ~= nil then
|
2022-07-15 13:58:04 +00:00
|
|
|
data_table[10] = waste.amount
|
|
|
|
end
|
|
|
|
|
2022-04-27 22:49:54 +00:00
|
|
|
if coolant ~= nil then
|
2022-05-02 21:40:00 +00:00
|
|
|
data_table[12] = coolant.name
|
|
|
|
data_table[13] = coolant.amount
|
2022-04-27 22:49:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if hcoolant ~= nil then
|
2022-05-02 21:40:00 +00:00
|
|
|
data_table[15] = hcoolant.name
|
|
|
|
data_table[16] = hcoolant.amount
|
2022-04-27 22:49:54 +00:00
|
|
|
end
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
return data_table, reactor.__p_is_faulted()
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- update the status cache if changed
|
|
|
|
---@return boolean changed
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _update_status_cache()
|
2022-04-18 01:12:25 +00:00
|
|
|
local status, faulted = _reactor_status()
|
2022-03-25 16:17:46 +00:00
|
|
|
local changed = false
|
|
|
|
|
2022-04-26 01:00:50 +00:00
|
|
|
if self.status_cache ~= nil then
|
|
|
|
if not faulted then
|
|
|
|
for i = 1, #status do
|
|
|
|
if status[i] ~= self.status_cache[i] then
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
end
|
2022-04-18 01:12:25 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
else
|
|
|
|
changed = true
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
2022-04-27 22:49:54 +00:00
|
|
|
if changed and not faulted then
|
2022-03-25 16:17:46 +00:00
|
|
|
self.status_cache = status
|
|
|
|
end
|
|
|
|
|
|
|
|
return changed
|
|
|
|
end
|
|
|
|
|
2022-04-02 18:43:36 +00:00
|
|
|
-- keep alive ack
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param srv_time integer
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _send_keep_alive_ack(srv_time)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_mgmt(MGMT_TYPE.KEEP_ALIVE, { srv_time, util.time() })
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- general ack
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param msg_type RPLC_TYPE
|
2023-01-26 23:26:26 +00:00
|
|
|
---@param status boolean|integer
|
|
|
|
local function _send_ack(msg_type, status)
|
|
|
|
_send(msg_type, { status })
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
-- send structure properties (these should not change, server will cache these)
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _send_struct()
|
2023-09-19 20:37:15 +00:00
|
|
|
local mek_data = { false, 0, 0, 0, types.new_zero_coordinate(), types.new_zero_coordinate(), 0, 0, 0, 0, 0, 0, 0, 0 }
|
2022-05-02 21:40:00 +00:00
|
|
|
|
|
|
|
local tasks = {
|
2023-02-22 04:50:43 +00:00
|
|
|
function () mek_data[1] = reactor.getLength() end,
|
|
|
|
function () mek_data[2] = reactor.getWidth() end,
|
|
|
|
function () mek_data[3] = reactor.getHeight() end,
|
|
|
|
function () mek_data[4] = reactor.getMinPos() end,
|
|
|
|
function () mek_data[5] = reactor.getMaxPos() end,
|
|
|
|
function () mek_data[6] = reactor.getHeatCapacity() end,
|
|
|
|
function () mek_data[7] = reactor.getFuelAssemblies() end,
|
|
|
|
function () mek_data[8] = reactor.getFuelSurfaceArea() end,
|
|
|
|
function () mek_data[9] = reactor.getFuelCapacity() end,
|
|
|
|
function () mek_data[10] = reactor.getWasteCapacity() end,
|
|
|
|
function () mek_data[11] = reactor.getCoolantCapacity() end,
|
|
|
|
function () mek_data[12] = reactor.getHeatedCoolantCapacity() end,
|
|
|
|
function () mek_data[13] = reactor.getMaxBurnRate() end
|
2022-04-02 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
parallel.waitForAll(table.unpack(tasks))
|
|
|
|
|
2023-06-22 19:46:17 +00:00
|
|
|
if reactor.__p_is_ok() then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_STRUCT, mek_data)
|
2022-10-26 03:40:36 +00:00
|
|
|
self.resend_build = false
|
2022-04-18 01:12:25 +00:00
|
|
|
end
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
---@class plc_comms
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-03 16:08:22 +00:00
|
|
|
-- reconnect a newly connected reactor
|
2023-02-22 04:50:43 +00:00
|
|
|
---@param new_reactor table
|
|
|
|
function public.reconnect_reactor(new_reactor)
|
|
|
|
reactor = new_reactor
|
2022-05-03 21:10:42 +00:00
|
|
|
self.status_cache = nil
|
2022-10-26 03:40:36 +00:00
|
|
|
self.resend_build = true
|
2023-01-26 23:26:26 +00:00
|
|
|
self.max_burn_rate = nil
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
-- unlink from the server
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.unlink()
|
2023-06-05 05:13:22 +00:00
|
|
|
self.sv_addr = comms.BROADCAST
|
2022-05-02 21:40:00 +00:00
|
|
|
self.linked = false
|
|
|
|
self.r_seq_num = nil
|
2022-05-03 21:10:42 +00:00
|
|
|
self.status_cache = nil
|
2023-06-05 21:47:43 +00:00
|
|
|
databus.tx_link_state(types.PANEL_LINK_STATE.DISCONNECTED)
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection to the server
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.close()
|
2022-10-26 03:40:36 +00:00
|
|
|
conn_watchdog.cancel()
|
2022-05-11 01:49:14 +00:00
|
|
|
public.unlink()
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_mgmt(MGMT_TYPE.CLOSE, {})
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
|
|
|
|
2022-04-26 01:00:50 +00:00
|
|
|
-- attempt to establish link with supervisor
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.send_link_req()
|
2023-10-01 21:10:16 +00:00
|
|
|
_send_mgmt(MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.PLC, config.UnitID })
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- send live status information
|
2022-10-25 17:29:57 +00:00
|
|
|
---@param no_reactor boolean PLC lost reactor connection
|
2022-10-26 03:40:36 +00:00
|
|
|
---@param formed boolean reactor formed (from PLC state)
|
2022-10-25 17:29:57 +00:00
|
|
|
function public.send_status(no_reactor, formed)
|
2022-05-02 21:40:00 +00:00
|
|
|
if self.linked then
|
2022-10-25 17:29:57 +00:00
|
|
|
local mek_data = nil ---@type table
|
2022-10-26 03:40:36 +00:00
|
|
|
local heating_rate = 0.0 ---@type number
|
2022-10-25 17:29:57 +00:00
|
|
|
|
2022-10-26 03:40:36 +00:00
|
|
|
if (not no_reactor) and rps.is_formed() then
|
2023-09-19 20:37:15 +00:00
|
|
|
if _update_status_cache() then mek_data = self.status_cache end
|
2023-02-22 04:50:43 +00:00
|
|
|
heating_rate = reactor.getHeatingRate()
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
local sys_status = {
|
2023-09-19 20:37:15 +00:00
|
|
|
util.time(), -- timestamp
|
|
|
|
(not self.scrammed), -- requested control state
|
|
|
|
no_reactor, -- no reactor peripheral connected
|
|
|
|
formed, -- reactor formed
|
|
|
|
self.auto_ack_token, -- indicate auto command received prior to this status update
|
|
|
|
heating_rate, -- heating rate
|
|
|
|
mek_data -- mekanism status data
|
2022-05-02 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.STATUS, sys_status)
|
2022-10-26 03:40:36 +00:00
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
if self.resend_build then _send_struct() end
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- send reactor protection system status
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.send_rps_status()
|
2022-05-02 21:40:00 +00:00
|
|
|
if self.linked then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_STATUS, { rps.is_tripped(), rps.get_trip_cause(), table.unpack(rps.status()) })
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- send reactor protection system alarm
|
2023-02-21 16:29:04 +00:00
|
|
|
---@param cause rps_trip_cause reactor protection system status
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.send_rps_alarm(cause)
|
2022-05-02 21:40:00 +00:00
|
|
|
if self.linked then
|
2023-02-21 21:57:33 +00:00
|
|
|
_send(RPLC_TYPE.RPS_ALARM, { cause, table.unpack(rps.status()) })
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- parse a packet
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param side string
|
|
|
|
---@param sender integer
|
|
|
|
---@param reply_to integer
|
|
|
|
---@param message any
|
|
|
|
---@param distance integer
|
|
|
|
---@return rplc_frame|mgmt_frame|nil packet
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.parse_packet(side, sender, reply_to, message, distance)
|
2023-06-23 17:52:24 +00:00
|
|
|
local s_pkt = nic.receive(side, sender, reply_to, message, distance)
|
2023-06-23 18:12:41 +00:00
|
|
|
local pkt = nil
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2023-06-23 18:12:41 +00:00
|
|
|
if s_pkt then
|
2022-04-02 12:28:43 +00:00
|
|
|
-- get as RPLC packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if s_pkt.protocol() == PROTOCOL.RPLC then
|
2022-04-21 14:26:02 +00:00
|
|
|
local rplc_pkt = comms.rplc_packet()
|
2022-04-02 12:28:43 +00:00
|
|
|
if rplc_pkt.decode(s_pkt) then
|
|
|
|
pkt = rplc_pkt.get()
|
|
|
|
end
|
|
|
|
-- get as SCADA management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
|
2022-04-21 14:26:02 +00:00
|
|
|
local mgmt_pkt = comms.mgmt_packet()
|
2022-04-02 12:28:43 +00:00
|
|
|
if mgmt_pkt.decode(s_pkt) then
|
2022-05-02 21:40:00 +00:00
|
|
|
pkt = mgmt_pkt.get()
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
|
|
|
else
|
2023-06-05 05:13:22 +00:00
|
|
|
log.debug("unsupported packet type " .. s_pkt.protocol(), true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return pkt
|
|
|
|
end
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- handle RPLC and MGMT packets
|
2022-10-25 17:29:57 +00:00
|
|
|
---@param packet rplc_frame|mgmt_frame packet frame
|
|
|
|
---@param plc_state plc_state PLC state
|
|
|
|
---@param setpoints setpoints setpoint control table
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.handle_packet(packet, plc_state, setpoints)
|
2023-04-08 20:49:54 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println_ts(message) if not plc_state.fp_ok then util.println_ts(message) end end
|
|
|
|
|
2023-06-05 23:12:43 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
2023-06-05 05:13:22 +00:00
|
|
|
local l_chan = packet.scada_frame.local_channel()
|
|
|
|
local src_addr = packet.scada_frame.src_addr()
|
2023-04-19 13:30:17 +00:00
|
|
|
|
2023-04-08 20:49:54 +00:00
|
|
|
-- handle packets now that we have prints setup
|
2023-10-01 21:10:16 +00:00
|
|
|
if l_chan == config.PLC_Channel then
|
2022-04-24 01:10:25 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.r_seq_num == nil then
|
|
|
|
self.r_seq_num = packet.scada_frame.seq_num()
|
2023-04-21 21:10:15 +00:00
|
|
|
elseif self.linked and ((self.r_seq_num + 1) ~= packet.scada_frame.seq_num()) then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. packet.scada_frame.seq_num())
|
2022-04-24 01:10:25 +00:00
|
|
|
return
|
2023-06-07 16:48:43 +00:00
|
|
|
elseif self.linked and (src_addr ~= self.sv_addr) then
|
|
|
|
log.debug("received packet from unknown computer " .. src_addr .. " while linked (expected " .. self.sv_addr ..
|
|
|
|
"); channel in use by another system?")
|
2023-06-05 23:12:43 +00:00
|
|
|
return
|
2022-04-24 01:10:25 +00:00
|
|
|
else
|
|
|
|
self.r_seq_num = packet.scada_frame.seq_num()
|
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- feed the watchdog first so it doesn't uhh...eat our packets :)
|
2022-10-26 03:40:36 +00:00
|
|
|
conn_watchdog.feed()
|
2022-04-29 02:36:45 +00:00
|
|
|
|
2022-04-24 01:10:25 +00:00
|
|
|
-- handle packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if protocol == PROTOCOL.RPLC then
|
2023-02-22 04:50:43 +00:00
|
|
|
---@cast packet rplc_frame
|
2023-06-05 05:13:22 +00:00
|
|
|
-- if linked, only accept packets from configured supervisor
|
2023-06-07 16:48:43 +00:00
|
|
|
if self.linked then
|
2023-02-21 16:05:57 +00:00
|
|
|
if packet.type == RPLC_TYPE.STATUS then
|
2022-05-03 21:10:42 +00:00
|
|
|
-- request of full status, clear cache first
|
|
|
|
self.status_cache = nil
|
2022-10-25 17:29:57 +00:00
|
|
|
public.send_status(plc_state.no_reactor, plc_state.reactor_formed)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("sent out status cache again, did supervisor miss it?")
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.MEK_STRUCT then
|
2022-04-02 15:22:44 +00:00
|
|
|
-- request for physical structure
|
2022-04-02 18:43:36 +00:00
|
|
|
_send_struct()
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("sent out structure again, did supervisor miss it?")
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.MEK_BURN_RATE then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- set the burn rate
|
2023-01-26 23:26:26 +00:00
|
|
|
if (packet.length == 2) and (type(packet.data[1]) == "number") then
|
2022-04-29 13:07:29 +00:00
|
|
|
local success = false
|
2022-10-20 17:27:33 +00:00
|
|
|
local burn_rate = math.floor(packet.data[1] * 10) / 10
|
2022-05-05 20:00:49 +00:00
|
|
|
local ramp = packet.data[2]
|
2022-04-29 13:07:29 +00:00
|
|
|
|
|
|
|
-- if no known max burn rate, check again
|
2022-05-05 20:00:49 +00:00
|
|
|
if self.max_burn_rate == nil then
|
2023-02-22 04:50:43 +00:00
|
|
|
self.max_burn_rate = reactor.getMaxBurnRate()
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
2022-04-21 16:40:21 +00:00
|
|
|
|
2022-04-30 02:27:54 +00:00
|
|
|
-- if we know our max burn rate, update current burn rate setpoint if in range
|
2022-05-05 20:00:49 +00:00
|
|
|
if self.max_burn_rate ~= ppm.ACCESS_FAULT then
|
|
|
|
if burn_rate > 0 and burn_rate <= self.max_burn_rate then
|
|
|
|
if ramp then
|
|
|
|
setpoints.burn_rate_en = true
|
|
|
|
setpoints.burn_rate = burn_rate
|
|
|
|
success = true
|
|
|
|
else
|
2023-02-22 04:50:43 +00:00
|
|
|
reactor.setBurnRate(burn_rate)
|
2023-06-22 19:46:17 +00:00
|
|
|
success = reactor.__p_is_ok()
|
2022-05-05 20:00:49 +00:00
|
|
|
end
|
2022-10-20 17:27:33 +00:00
|
|
|
else
|
|
|
|
log.debug(burn_rate .. " rate outside of 0 < x <= " .. self.max_burn_rate)
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
2022-04-29 13:07:29 +00:00
|
|
|
_send_ack(packet.type, success)
|
|
|
|
else
|
2023-01-26 23:26:26 +00:00
|
|
|
log.debug("RPLC set burn rate packet length mismatch or non-numeric burn rate")
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_ENABLE then
|
2022-05-05 17:14:14 +00:00
|
|
|
-- enable the reactor
|
|
|
|
self.scrammed = false
|
2022-10-26 03:40:36 +00:00
|
|
|
_send_ack(packet.type, rps.activate())
|
2023-09-19 20:37:15 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_DISABLE then
|
|
|
|
-- disable the reactor, but do not trip
|
|
|
|
self.scrammed = true
|
|
|
|
_send_ack(packet.type, rps.scram())
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_SCRAM then
|
2022-11-11 21:15:44 +00:00
|
|
|
-- disable the reactor per manual request
|
2022-05-05 17:14:14 +00:00
|
|
|
self.scrammed = true
|
2022-10-26 03:40:36 +00:00
|
|
|
rps.trip_manual()
|
2022-05-05 17:14:14 +00:00
|
|
|
_send_ack(packet.type, true)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_ASCRAM then
|
2022-11-11 21:15:44 +00:00
|
|
|
-- disable the reactor per automatic request
|
|
|
|
self.scrammed = true
|
|
|
|
rps.trip_auto()
|
|
|
|
_send_ack(packet.type, true)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_RESET then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- reset the RPS status
|
|
|
|
rps.reset()
|
2022-04-02 18:43:36 +00:00
|
|
|
_send_ack(packet.type, true)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.RPS_AUTO_RESET then
|
2023-02-07 05:32:50 +00:00
|
|
|
-- reset automatic SCRAM and timeout trips
|
|
|
|
rps.auto_reset()
|
|
|
|
_send_ack(packet.type, true)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif packet.type == RPLC_TYPE.AUTO_BURN_RATE then
|
2023-01-26 23:26:26 +00:00
|
|
|
-- automatic control requested a new burn rate
|
2023-02-04 18:47:00 +00:00
|
|
|
if (packet.length == 3) and (type(packet.data[1]) == "number") and (type(packet.data[3]) == "number") then
|
2023-01-26 23:26:26 +00:00
|
|
|
local ack = AUTO_ACK.FAIL
|
2023-02-09 01:26:13 +00:00
|
|
|
local burn_rate = math.floor(packet.data[1] * 100) / 100
|
2023-01-26 23:26:26 +00:00
|
|
|
local ramp = packet.data[2]
|
2023-02-04 18:47:00 +00:00
|
|
|
self.auto_ack_token = packet.data[3]
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
-- if no known max burn rate, check again
|
|
|
|
if self.max_burn_rate == nil then
|
2023-02-22 04:50:43 +00:00
|
|
|
self.max_burn_rate = reactor.getMaxBurnRate()
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- if we know our max burn rate, update current burn rate setpoint if in range
|
|
|
|
if self.max_burn_rate ~= ppm.ACCESS_FAULT then
|
2023-02-09 01:26:13 +00:00
|
|
|
if burn_rate < 0.01 then
|
2023-01-26 23:26:26 +00:00
|
|
|
if rps.is_active() then
|
2023-02-09 01:26:13 +00:00
|
|
|
-- auto scram to disable
|
|
|
|
log.debug("AUTO: stopping the reactor to meet 0.0 burn rate")
|
|
|
|
if rps.scram() then
|
|
|
|
ack = AUTO_ACK.ZERO_DIS_OK
|
2023-01-26 23:26:26 +00:00
|
|
|
else
|
2023-02-21 21:57:33 +00:00
|
|
|
log.warning("AUTO: automatic reactor stop failed")
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
ack = AUTO_ACK.ZERO_DIS_OK
|
|
|
|
end
|
|
|
|
elseif burn_rate <= self.max_burn_rate then
|
|
|
|
if not rps.is_active() then
|
|
|
|
-- activate the reactor
|
2023-02-03 01:17:23 +00:00
|
|
|
log.debug("AUTO: activating the reactor")
|
2023-02-09 01:26:13 +00:00
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
reactor.setBurnRate(0.01)
|
|
|
|
if reactor.__p_is_faulted() then
|
2023-02-21 21:57:33 +00:00
|
|
|
log.warning("AUTO: failed to reset burn rate for auto activation")
|
2023-02-03 01:17:23 +00:00
|
|
|
else
|
2023-02-09 01:26:13 +00:00
|
|
|
if not rps.auto_activate() then
|
2023-02-21 21:57:33 +00:00
|
|
|
log.warning("AUTO: automatic reactor activation failed")
|
2023-02-09 01:26:13 +00:00
|
|
|
end
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if active, set/ramp burn rate
|
|
|
|
if rps.is_active() then
|
|
|
|
if ramp then
|
2023-02-03 01:17:23 +00:00
|
|
|
log.debug(util.c("AUTO: setting burn rate ramp to ", burn_rate))
|
2023-01-26 23:26:26 +00:00
|
|
|
setpoints.burn_rate_en = true
|
|
|
|
setpoints.burn_rate = burn_rate
|
|
|
|
ack = AUTO_ACK.RAMP_SET_OK
|
|
|
|
else
|
2023-02-03 01:17:23 +00:00
|
|
|
log.debug(util.c("AUTO: setting burn rate directly to ", burn_rate))
|
2023-02-22 04:50:43 +00:00
|
|
|
reactor.setBurnRate(burn_rate)
|
|
|
|
ack = util.trinary(reactor.__p_is_faulted(), AUTO_ACK.FAIL, AUTO_ACK.DIRECT_SET_OK)
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2023-02-03 01:17:23 +00:00
|
|
|
log.debug(util.c(burn_rate, " rate outside of 0 < x <= ", self.max_burn_rate))
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
_send_ack(packet.type, ack)
|
|
|
|
else
|
|
|
|
log.debug("RPLC set automatic burn rate packet length mismatch or non-numeric burn rate")
|
|
|
|
end
|
2022-04-02 18:43:36 +00:00
|
|
|
else
|
2023-04-18 22:01:35 +00:00
|
|
|
log.debug("received unknown RPLC packet type " .. packet.type)
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
2023-06-05 05:13:22 +00:00
|
|
|
else
|
2023-06-07 16:48:43 +00:00
|
|
|
log.debug("discarding RPLC packet before linked")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
2023-02-22 04:50:43 +00:00
|
|
|
---@cast packet mgmt_frame
|
2023-06-05 05:13:22 +00:00
|
|
|
-- if linked, only accept packets from configured supervisor
|
2023-06-07 16:48:43 +00:00
|
|
|
if self.linked then
|
2023-08-30 20:45:48 +00:00
|
|
|
if packet.type == MGMT_TYPE.KEEP_ALIVE then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- keep alive request received, echo back
|
|
|
|
if packet.length == 1 and type(packet.data[1]) == "number" then
|
|
|
|
local timestamp = packet.data[1]
|
|
|
|
local trip_time = util.time() - timestamp
|
|
|
|
|
2023-02-26 19:49:16 +00:00
|
|
|
if trip_time > 750 then
|
|
|
|
log.warning("PLC KEEP_ALIVE trip time > 750ms (" .. trip_time .. "ms)")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- log.debug("PLC RTT = " .. trip_time .. "ms")
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
_send_keep_alive_ack(timestamp)
|
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT keep alive packet length/type mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.CLOSE then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- handle session close
|
|
|
|
conn_watchdog.cancel()
|
|
|
|
public.unlink()
|
|
|
|
println_ts("server connection closed by remote host")
|
|
|
|
log.warning("server connection closed by remote host")
|
|
|
|
else
|
2023-04-18 22:01:35 +00:00
|
|
|
log.debug("received unsupported SCADA_MGMT packet type " .. packet.type)
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2022-04-02 15:22:44 +00:00
|
|
|
-- link request confirmation
|
2022-04-29 13:07:29 +00:00
|
|
|
if packet.length == 1 then
|
2022-11-13 19:13:30 +00:00
|
|
|
local est_ack = packet.data[1]
|
2022-04-29 13:07:29 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
if est_ack == ESTABLISH_ACK.ALLOW then
|
2022-04-29 13:07:29 +00:00
|
|
|
println_ts("linked!")
|
2023-06-05 05:13:22 +00:00
|
|
|
log.info("supervisor establish request approved, linked to SV (CID#" .. src_addr .. ")")
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- link + reset remote sequence number and cache
|
|
|
|
self.sv_addr = src_addr
|
|
|
|
self.linked = true
|
2022-04-29 13:07:29 +00:00
|
|
|
self.r_seq_num = nil
|
2022-05-03 21:10:42 +00:00
|
|
|
self.status_cache = nil
|
2022-04-02 15:22:44 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
if plc_state.reactor_formed then _send_struct() end
|
2022-10-25 17:29:57 +00:00
|
|
|
public.send_status(plc_state.no_reactor, plc_state.reactor_formed)
|
2022-04-29 13:07:29 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("sent initial status data")
|
2023-06-05 05:13:22 +00:00
|
|
|
else
|
|
|
|
if self.last_est_ack ~= est_ack then
|
|
|
|
if est_ack == ESTABLISH_ACK.DENY then
|
|
|
|
println_ts("link request denied, retrying...")
|
|
|
|
log.info("supervisor establish request denied, retrying")
|
|
|
|
elseif est_ack == ESTABLISH_ACK.COLLISION then
|
|
|
|
println_ts("reactor PLC ID collision (check config), retrying...")
|
|
|
|
log.warning("establish request collision, retrying")
|
|
|
|
elseif est_ack == ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
println_ts("supervisor version mismatch (try updating), retrying...")
|
|
|
|
log.warning("establish request version mismatch, retrying")
|
|
|
|
else
|
|
|
|
println_ts("invalid link response, bad channel? retrying...")
|
|
|
|
log.error("unknown establish request response, retrying")
|
|
|
|
end
|
2023-02-16 00:59:58 +00:00
|
|
|
end
|
2023-06-05 05:13:22 +00:00
|
|
|
|
|
|
|
-- unlink
|
|
|
|
self.sv_addr = comms.BROADCAST
|
|
|
|
self.linked = false
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
2023-02-16 00:59:58 +00:00
|
|
|
self.last_est_ack = est_ack
|
2023-04-08 20:49:54 +00:00
|
|
|
|
|
|
|
-- report link state
|
|
|
|
databus.tx_link_state(est_ack + 1)
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
log.debug("SCADA_MGMT establish packet length mismatch")
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
2023-06-07 16:35:17 +00:00
|
|
|
else
|
2023-06-07 16:48:43 +00:00
|
|
|
log.debug("discarding non-link SCADA_MGMT packet before linked")
|
2022-05-02 16:06:04 +00:00
|
|
|
end
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
|
|
|
-- should be unreachable assuming packet is from parse_packet()
|
|
|
|
log.error("illegal packet type " .. protocol, true)
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
2023-04-19 13:30:17 +00:00
|
|
|
else
|
2023-06-05 05:13:22 +00:00
|
|
|
log.debug("received packet on unconfigured channel " .. l_chan, true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.is_scrammed() return self.scrammed end
|
2023-02-21 21:57:33 +00:00
|
|
|
---@nodiscard
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.is_linked() return self.linked end
|
2022-05-11 01:49:14 +00:00
|
|
|
|
|
|
|
return public
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return plc
|