cc-mek-scada/reactor-plc/plc.lua

1016 lines
40 KiB
Lua
Raw Normal View History

local comms = require("scada-common.comms")
2022-05-31 18:14:17 +00:00
local log = require("scada-common.log")
local ppm = require("scada-common.ppm")
local types = require("scada-common.types")
2022-05-31 18:14:17 +00:00
local util = require("scada-common.util")
local plc = {}
2022-05-05 15:55:04 +00:00
local rps_status_t = types.rps_status_t
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
local ESTABLISH_ACK = comms.ESTABLISH_ACK
2023-02-21 16:05:57 +00:00
local RPLC_TYPE = comms.RPLC_TYPE
local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
local AUTO_ACK = comms.PLC_AUTO_ACK
local print = util.print
local println = util.println
local print_ts = util.print_ts
local println_ts = util.println_ts
-- I sure hope the devs don't change this error message, not that it would have safety implications
-- I wish they didn't change it to be like this
local PCALL_SCRAM_MSG = "pcall: Scram requires the reactor to be active."
local PCALL_START_MSG = "pcall: Reactor is already active."
-- RPS SAFETY CONSTANTS
local MAX_DAMAGE_PERCENT = 90
local MAX_DAMAGE_TEMPERATURE = 1200
local MIN_COOLANT_FILL = 0.10
local MAX_WASTE_FILL = 0.8
local MAX_HEATED_COLLANT_FILL = 0.95
-- END RPS SAFETY CONSTANTS
2022-05-11 01:49:14 +00:00
--- RPS: Reactor Protection System
---
--- identifies dangerous states and SCRAMs reactor if warranted
---
--- autonomous from main SCADA supervisor/coordinator control
---@param reactor table
---@param is_formed boolean
function plc.rps_init(reactor, is_formed)
local state_keys = {
dmg_crit = 1,
high_temp = 2,
no_coolant = 3,
ex_waste = 4,
ex_hcoolant = 5,
no_fuel = 6,
fault = 7,
timeout = 8,
manual = 9,
2022-11-11 20:45:46 +00:00
automatic = 10,
sys_fail = 11,
force_disabled = 12
}
local self = {
2022-01-22 19:26:25 +00:00
reactor = reactor,
2022-11-11 20:45:46 +00:00
state = { false, false, false, false, false, false, false, false, false, false, false, false },
reactor_enabled = false,
enabled_at = 0,
formed = is_formed,
2022-11-11 20:45:46 +00:00
force_disabled = false,
2022-01-22 19:26:25 +00:00
tripped = false,
trip_cause = "ok" ---@type rps_trip_cause
}
2022-05-11 01:49:14 +00:00
---@class rps
local public = {}
2022-04-25 14:34:41 +00:00
-- PRIVATE FUNCTIONS --
-- set reactor access fault flag
2022-05-31 18:14:17 +00:00
local function _set_fault()
if self.reactor.__p_last_fault() ~= "Terminated" then
self.state[state_keys.fault] = true
end
end
-- clear reactor access fault flag
2022-05-31 18:14:17 +00:00
local function _clear_fault()
self.state[state_keys.fault] = false
end
-- check if the reactor is formed
local function _is_formed()
local formed = self.reactor.isFormed()
if formed == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
else
self.formed = formed
if not self.state[state_keys.sys_fail] then
self.state[state_keys.sys_fail] = not formed
end
end
end
2022-11-11 20:45:46 +00:00
-- check if the reactor is force disabled
local function _is_force_disabled()
local disabled = self.reactor.isForceDisabled()
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
-- check for critical damage
2022-05-31 18:14:17 +00:00
local function _damage_critical()
local damage_percent = self.reactor.getDamagePercent()
if damage_percent == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.dmg_crit] then
self.state[state_keys.dmg_crit] = damage_percent >= MAX_DAMAGE_PERCENT
end
2022-04-05 21:25:56 +00:00
end
-- check if the reactor is at a critically high temperature
2022-05-31 18:14:17 +00:00
local function _high_temp()
-- mekanism: MAX_DAMAGE_TEMPERATURE = 1_200
local temp = self.reactor.getTemperature()
if temp == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.high_temp] then
self.state[state_keys.high_temp] = temp >= MAX_DAMAGE_TEMPERATURE
end
2022-04-05 21:25:56 +00:00
end
-- check if there is no coolant (<2% filled)
2022-05-31 18:14:17 +00:00
local function _no_coolant()
local coolant_filled = self.reactor.getCoolantFilledPercentage()
if coolant_filled == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.no_coolant] then
self.state[state_keys.no_coolant] = coolant_filled < MIN_COOLANT_FILL
end
end
-- check for excess waste (>80% filled)
2022-05-31 18:14:17 +00:00
local function _excess_waste()
local w_filled = self.reactor.getWasteFilledPercentage()
if w_filled == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.ex_waste] then
self.state[state_keys.ex_waste] = w_filled > MAX_WASTE_FILL
end
2022-04-05 21:25:56 +00:00
end
-- check for heated coolant backup (>95% filled)
2022-05-31 18:14:17 +00:00
local function _excess_heated_coolant()
local hc_filled = self.reactor.getHeatedCoolantFilledPercentage()
if hc_filled == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.ex_hcoolant] then
self.state[state_keys.ex_hcoolant] = hc_filled > MAX_HEATED_COLLANT_FILL
end
2022-04-05 21:25:56 +00:00
end
-- check if there is no fuel
2022-05-31 18:14:17 +00:00
local function _insufficient_fuel()
local fuel = self.reactor.getFuel()
if fuel == ppm.ACCESS_FAULT then
-- lost the peripheral or terminated, handled later
_set_fault()
elseif not self.state[state_keys.no_fuel] then
2022-05-06 13:10:50 +00:00
self.state[state_keys.no_fuel] = fuel == 0
end
2022-04-05 21:25:56 +00:00
end
2022-04-25 14:34:41 +00:00
-- PUBLIC FUNCTIONS --
-- re-link a reactor after a peripheral re-connect
---@diagnostic disable-next-line: redefined-local
2022-05-31 18:14:17 +00:00
function public.reconnect_reactor(reactor)
2022-04-25 14:34:41 +00:00
self.reactor = reactor
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()
self.state[state_keys.timeout] = true
end
-- manually SCRAM the reactor
2022-05-31 18:14:17 +00:00
function public.trip_manual()
self.state[state_keys.manual] = true
end
-- automatic SCRAM commanded by supervisor
2022-11-11 20:45:46 +00:00
function public.trip_auto()
self.state[state_keys.automatic] = true
end
-- trip for unformed reactor
function public.trip_sys_fail()
self.state[state_keys.fault] = true
self.state[state_keys.sys_fail] = true
end
-- SCRAM the reactor now (blocks waiting for server tick)
2022-05-11 01:49:14 +00:00
---@return boolean success
2022-05-31 18:14:17 +00:00
function public.scram()
log.info("RPS: reactor SCRAM")
self.reactor.scram()
if self.reactor.__p_is_faulted() and (self.reactor.__p_last_fault() ~= PCALL_SCRAM_MSG) then
log.error("RPS: failed reactor SCRAM")
return false
else
self.reactor_enabled = false
self.last_runtime = util.time_ms() - self.enabled_at
return true
end
end
-- start the reactor now (blocks waiting for server tick)
2022-05-11 01:49:14 +00:00
---@return boolean success
2022-05-31 18:14:17 +00:00
function public.activate()
if not self.tripped then
log.info("RPS: reactor start")
self.reactor.activate()
if self.reactor.__p_is_faulted() and (self.reactor.__p_last_fault() ~= PCALL_START_MSG) then
log.error("RPS: failed reactor start")
else
self.reactor_enabled = true
self.enabled_at = util.time_ms()
return true
end
else
log.debug(util.c("RPS: failed start, RPS tripped: ", self.trip_cause))
end
return false
2022-04-05 21:25:56 +00:00
end
-- 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
self.trip_cause = rps_status_t.ok
self.tripped = false
log.debug("RPS: cleared automatic SCRAM for re-activation")
end
return public.activate()
end
-- check all safety conditions
2022-05-11 01:49:14 +00:00
---@return boolean tripped, rps_status_t trip_status, boolean first_trip
2022-05-31 18:14:17 +00:00
function public.check()
2022-05-05 15:55:04 +00:00
local status = rps_status_t.ok
2022-01-22 19:26:25 +00:00
local was_tripped = self.tripped
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,
2022-10-26 03:40:36 +00:00
_damage_critical,
_high_temp,
_no_coolant,
_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
-- check system states in order of severity
2022-04-25 15:40:53 +00:00
if self.tripped then
status = self.trip_cause
elseif self.state[state_keys.sys_fail] then
log.warning("RPS: system failure, reactor not formed")
status = rps_status_t.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")
status = rps_status_t.force_disabled
elseif self.state[state_keys.dmg_crit] then
log.warning("RPS: damage critical")
2022-05-05 15:55:04 +00:00
status = rps_status_t.dmg_crit
elseif self.state[state_keys.high_temp] then
log.warning("RPS: high temperature")
2022-05-05 15:55:04 +00:00
status = rps_status_t.high_temp
elseif self.state[state_keys.no_coolant] then
log.warning("RPS: no coolant")
2022-05-05 15:55:04 +00:00
status = rps_status_t.no_coolant
elseif self.state[state_keys.ex_waste] then
log.warning("RPS: full waste")
2022-05-05 15:55:04 +00:00
status = rps_status_t.ex_waste
elseif self.state[state_keys.ex_hcoolant] then
log.warning("RPS: heated coolant backup")
status = rps_status_t.ex_hcoolant
elseif self.state[state_keys.no_fuel] then
log.warning("RPS: no fuel")
2022-05-05 15:55:04 +00:00
status = rps_status_t.no_fuel
elseif self.state[state_keys.fault] then
log.warning("RPS: reactor access fault")
status = rps_status_t.fault
elseif self.state[state_keys.timeout] then
log.warning("RPS: supervisor connection timeout")
2022-05-05 15:55:04 +00:00
status = rps_status_t.timeout
elseif self.state[state_keys.manual] then
log.warning("RPS: manual SCRAM requested")
status = rps_status_t.manual
2022-11-11 20:45:46 +00:00
elseif self.state[state_keys.automatic] then
log.warning("RPS: automatic SCRAM requested")
status = rps_status_t.automatic
else
2022-01-22 19:26:25 +00:00
self.tripped = false
self.trip_cause = rps_status_t.ok
end
2022-04-25 15:40:53 +00:00
-- if a new trip occured...
if (not was_tripped) and (status ~= rps_status_t.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
-- 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
end
2022-01-22 19:26:25 +00:00
return self.tripped, status, first_trip
end
2022-05-31 18:14:17 +00:00
function public.status() return self.state end
2022-05-31 18:14:17 +00:00
function public.is_tripped() return self.tripped end
function public.get_trip_cause() return self.trip_cause end
2022-05-31 18:14:17 +00:00
function public.is_active() return self.reactor_enabled end
function public.is_formed() return self.formed end
2022-11-11 20:45:46 +00:00
function public.is_force_disabled() return self.force_disabled end
-- get the runtime of the reactor if active, or the last runtime if disabled
---@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
---@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
2022-05-05 15:55:04 +00:00
self.trip_cause = rps_status_t.ok
2022-05-11 01:49:14 +00:00
for i = 1, #self.state do
self.state[i] = false
end
if not quiet then log.info("RPS: reset") end
2022-01-22 19:26:25 +00:00
end
-- 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
if self.trip_cause == rps_status_t.automatic or self.trip_cause == rps_status_t.timeout then
self.trip_cause = rps_status_t.ok
self.tripped = false
log.info("RPS: auto reset")
end
end
2022-05-11 01:49:14 +00:00
return public
end
2022-05-11 01:49:14 +00:00
-- Reactor PLC Communications
---@param id integer reactor ID
---@param version string PLC version
---@param modem table modem device
---@param local_port integer local listening port
---@param server_port integer remote server port
---@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, modem, local_port, server_port, range, reactor, rps, conn_watchdog)
local self = {
seq_num = 0,
r_seq_num = nil,
modem = modem,
s_port = server_port,
l_port = local_port,
reactor = reactor,
scrammed = false,
linked = false,
last_est_ack = ESTABLISH_ACK.ALLOW,
2022-10-26 03:40:36 +00:00
resend_build = false,
auto_ack_token = 0,
status_cache = nil,
max_burn_rate = nil
}
---@class plc_comms
local public = {}
comms.set_trusted_range(range)
-- PRIVATE FUNCTIONS --
-- configure modem channels
local function _conf_channels()
self.modem.closeAll()
self.modem.open(self.l_port)
end
_conf_channels()
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
---@param msg table
2022-05-31 18:14:17 +00:00
local function _send(msg_type, msg)
local s_pkt = comms.scada_packet()
local r_pkt = comms.rplc_packet()
2022-05-02 16:06:04 +00:00
2022-10-26 03:40:36 +00:00
r_pkt.make(id, msg_type, msg)
2023-02-21 16:05:57 +00:00
s_pkt.make(self.seq_num, PROTOCOL.RPLC, r_pkt.raw_sendable())
2022-05-02 16:06:04 +00:00
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
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-02-21 16:05:57 +00:00
---@param msg_type SCADA_MGMT_TYPE
---@param msg table
2022-05-31 18:14:17 +00:00
local function _send_mgmt(msg_type, msg)
local s_pkt = comms.scada_packet()
local m_pkt = comms.mgmt_packet()
m_pkt.make(msg_type, msg)
2023-02-21 16:05:57 +00:00
s_pkt.make(self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
self.seq_num = self.seq_num + 1
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
local coolant = nil
local hcoolant = nil
local data_table = {
false, -- getStatus
0, -- getBurnRate
0, -- getActualBurnRate
0, -- getTemperature
0, -- getDamagePercent
0, -- getBoilEfficiency
0, -- getEnvironmentalLoss
0, -- fuel_amnt
0, -- getFuelFilledPercentage
0, -- waste_amnt
0, -- getWasteFilledPercentage
"", -- coolant_name
0, -- coolant_amnt
0, -- getCoolantFilledPercentage
"", -- hcoolant_name
0, -- hcoolant_amnt
0 -- getHeatedCoolantFilledPercentage
}
local tasks = {
function () data_table[1] = self.reactor.getStatus() end,
function () data_table[2] = self.reactor.getBurnRate() end,
function () data_table[3] = self.reactor.getActualBurnRate() end,
function () data_table[4] = self.reactor.getTemperature() end,
function () data_table[5] = self.reactor.getDamagePercent() end,
function () data_table[6] = self.reactor.getBoilEfficiency() end,
function () data_table[7] = self.reactor.getEnvironmentalLoss() end,
2022-07-15 13:58:04 +00:00
function () fuel = self.reactor.getFuel() end,
function () data_table[9] = self.reactor.getFuelFilledPercentage() end,
2022-07-15 13:58:04 +00:00
function () waste = self.reactor.getWaste() end,
function () data_table[11] = self.reactor.getWasteFilledPercentage() end,
function () coolant = self.reactor.getCoolant() end,
function () data_table[14] = self.reactor.getCoolantFilledPercentage() end,
function () hcoolant = self.reactor.getHeatedCoolant() end,
function () data_table[17] = self.reactor.getHeatedCoolantFilledPercentage() end
}
parallel.waitForAll(table.unpack(tasks))
if fuel ~= nil then
2022-07-15 13:58:04 +00:00
data_table[8] = fuel.amount
end
if waste ~= nil then
2022-07-15 13:58:04 +00:00
data_table[10] = waste.amount
end
if coolant ~= nil then
data_table[12] = coolant.name
data_table[13] = coolant.amount
end
if hcoolant ~= nil then
data_table[15] = hcoolant.name
data_table[16] = hcoolant.amount
end
return data_table, self.reactor.__p_is_faulted()
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()
local status, faulted = _reactor_status()
local changed = false
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
end
end
else
changed = true
end
if changed and not faulted then
self.status_cache = status
end
return changed
end
-- 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-02-21 16:05:57 +00:00
_send_mgmt(SCADA_MGMT_TYPE.KEEP_ALIVE, { srv_time, util.time() })
end
-- general ack
2023-02-21 16:05:57 +00:00
---@param msg_type RPLC_TYPE
---@param status boolean|integer
local function _send_ack(msg_type, status)
_send(msg_type, { status })
end
-- send structure properties (these should not change, server will cache these)
2022-05-31 18:14:17 +00:00
local function _send_struct()
local min_pos = { x = 0, y = 0, z = 0 }
local max_pos = { x = 0, y = 0, z = 0 }
local mek_data = { false, 0, 0, 0, min_pos, max_pos, 0, 0, 0, 0, 0, 0, 0, 0 }
local tasks = {
function () mek_data[1] = self.reactor.getLength() end,
function () mek_data[2] = self.reactor.getWidth() end,
function () mek_data[3] = self.reactor.getHeight() end,
function () mek_data[4] = self.reactor.getMinPos() end,
function () mek_data[5] = self.reactor.getMaxPos() end,
function () mek_data[6] = self.reactor.getHeatCapacity() end,
function () mek_data[7] = self.reactor.getFuelAssemblies() end,
function () mek_data[8] = self.reactor.getFuelSurfaceArea() end,
function () mek_data[9] = self.reactor.getFuelCapacity() end,
function () mek_data[10] = self.reactor.getWasteCapacity() end,
function () mek_data[11] = self.reactor.getCoolantCapacity() end,
function () mek_data[12] = self.reactor.getHeatedCoolantCapacity() end,
function () mek_data[13] = self.reactor.getMaxBurnRate() end
}
parallel.waitForAll(table.unpack(tasks))
if not self.reactor.__p_is_faulted() 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
else
log.error("failed to send structure: PPM fault")
end
end
-- PUBLIC FUNCTIONS --
-- reconnect a newly connected modem
2022-05-11 01:49:14 +00:00
---@param modem table
---@diagnostic disable-next-line: redefined-local
2022-05-31 18:14:17 +00:00
function public.reconnect_modem(modem)
self.modem = modem
_conf_channels()
end
-- reconnect a newly connected reactor
2022-05-11 01:49:14 +00:00
---@param reactor table
---@diagnostic disable-next-line: redefined-local
2022-05-31 18:14:17 +00:00
function public.reconnect_reactor(reactor)
self.reactor = reactor
self.status_cache = nil
2022-10-26 03:40:36 +00:00
self.resend_build = true
self.max_burn_rate = nil
end
-- unlink from the server
2022-05-31 18:14:17 +00:00
function public.unlink()
self.linked = false
self.r_seq_num = nil
self.status_cache = nil
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-02-21 16:05:57 +00:00
_send_mgmt(SCADA_MGMT_TYPE.CLOSE, {})
end
-- attempt to establish link with supervisor
2022-05-31 18:14:17 +00:00
function public.send_link_req()
2023-02-21 16:05:57 +00:00
_send_mgmt(SCADA_MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.PLC, id })
end
-- send live status information
---@param no_reactor boolean PLC lost reactor connection
2022-10-26 03:40:36 +00:00
---@param formed boolean reactor formed (from PLC state)
function public.send_status(no_reactor, formed)
if self.linked then
local mek_data = nil ---@type table
2022-10-26 03:40:36 +00:00
local heating_rate = 0.0 ---@type number
2022-10-26 03:40:36 +00:00
if (not no_reactor) and rps.is_formed() then
if _update_status_cache() then
mek_data = self.status_cache
end
heating_rate = self.reactor.getHeatingRate()
end
local sys_status = {
util.time(), -- timestamp
(not self.scrammed), -- requested control state
no_reactor, -- no reactor peripheral connected
formed, -- reactor formed
self.auto_ack_token, -- token to indicate auto command has been received before this status update
heating_rate, -- heating rate
mek_data -- mekanism status data
}
2023-02-21 16:05:57 +00:00
_send(RPLC_TYPE.STATUS, sys_status)
2022-10-26 03:40:36 +00:00
if self.resend_build then
_send_struct()
end
end
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()
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()) })
end
end
2022-05-05 15:55:04 +00:00
-- send reactor protection system alarm
---@param cause rps_status_t reactor protection system status
2022-05-31 18:14:17 +00:00
function public.send_rps_alarm(cause)
if self.linked then
2022-05-05 15:55:04 +00:00
local rps_alarm = {
cause,
2022-05-05 15:55:04 +00:00
table.unpack(rps.status())
}
2023-02-21 16:05:57 +00:00
_send(RPLC_TYPE.RPS_ALARM, rps_alarm)
end
end
-- parse an RPLC packet
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)
local pkt = nil
local s_pkt = comms.scada_packet()
-- parse packet as generic SCADA packet
s_pkt.receive(side, sender, reply_to, message, distance)
if s_pkt.is_valid() then
-- get as RPLC packet
2023-02-21 16:05:57 +00:00
if s_pkt.protocol() == PROTOCOL.RPLC then
local rplc_pkt = comms.rplc_packet()
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
local mgmt_pkt = comms.mgmt_packet()
if mgmt_pkt.decode(s_pkt) then
pkt = mgmt_pkt.get()
end
else
log.error("illegal packet type " .. s_pkt.protocol(), true)
end
end
return pkt
end
-- handle an RPLC packet
---@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)
if packet ~= nil and packet.scada_frame.local_port() == self.l_port then
-- check sequence number
if self.r_seq_num == nil then
self.r_seq_num = packet.scada_frame.seq_num()
elseif self.linked and self.r_seq_num >= packet.scada_frame.seq_num() then
log.warning("sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. packet.scada_frame.seq_num())
return
else
self.r_seq_num = packet.scada_frame.seq_num()
end
-- feed the watchdog first so it doesn't uhh...eat our packets :)
2022-10-26 03:40:36 +00:00
conn_watchdog.feed()
2022-05-10 16:01:56 +00:00
local protocol = packet.scada_frame.protocol()
-- handle packet
2023-02-21 16:05:57 +00:00
if protocol == PROTOCOL.RPLC then
if self.linked then
2023-02-21 16:05:57 +00:00
if packet.type == RPLC_TYPE.STATUS then
-- request of full status, clear cache first
self.status_cache = nil
public.send_status(plc_state.no_reactor, plc_state.reactor_formed)
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
-- request for physical structure
_send_struct()
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
-- set the burn rate
if (packet.length == 2) and (type(packet.data[1]) == "number") then
2022-04-29 13:07:29 +00:00
local success = false
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
self.max_burn_rate = self.reactor.getMaxBurnRate()
2022-04-29 13:07:29 +00:00
end
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
self.reactor.setBurnRate(burn_rate)
success = not self.reactor.__p_is_faulted()
end
else
log.debug(burn_rate .. " rate outside of 0 < x <= " .. self.max_burn_rate)
2022-04-29 13:07:29 +00:00
end
end
2022-04-29 13:07:29 +00:00
_send_ack(packet.type, success)
else
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
-- enable the reactor
self.scrammed = false
2022-10-26 03:40:36 +00:00
_send_ack(packet.type, rps.activate())
2023-02-21 16:05:57 +00:00
elseif packet.type == RPLC_TYPE.RPS_SCRAM then
-- disable the reactor per manual request
self.scrammed = true
2022-10-26 03:40:36 +00:00
rps.trip_manual()
_send_ack(packet.type, true)
2023-02-21 16:05:57 +00:00
elseif packet.type == RPLC_TYPE.RPS_ASCRAM then
-- 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()
_send_ack(packet.type, true)
2023-02-21 16:05:57 +00:00
elseif packet.type == RPLC_TYPE.RPS_AUTO_RESET then
-- 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
-- automatic control requested a new burn rate
if (packet.length == 3) and (type(packet.data[1]) == "number") and (type(packet.data[3]) == "number") then
local ack = AUTO_ACK.FAIL
local burn_rate = math.floor(packet.data[1] * 100) / 100
local ramp = packet.data[2]
self.auto_ack_token = packet.data[3]
-- if no known max burn rate, check again
if self.max_burn_rate == nil then
self.max_burn_rate = self.reactor.getMaxBurnRate()
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
if burn_rate < 0.01 then
if rps.is_active() then
-- 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
self.auto_last_disable = util.time_ms()
else
log.debug("AUTO: automatic reactor stop failed")
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
log.debug("AUTO: activating the reactor")
self.reactor.setBurnRate(0.01)
if self.reactor.__p_is_faulted() then
log.debug("AUTO: failed to reset burn rate for auto activation")
else
if not rps.auto_activate() then
log.debug("AUTO: automatic reactor activation failed")
end
end
end
-- if active, set/ramp burn rate
if rps.is_active() then
if ramp then
log.debug(util.c("AUTO: setting burn rate ramp to ", burn_rate))
setpoints.burn_rate_en = true
setpoints.burn_rate = burn_rate
ack = AUTO_ACK.RAMP_SET_OK
else
log.debug(util.c("AUTO: setting burn rate directly to ", burn_rate))
self.reactor.setBurnRate(burn_rate)
ack = util.trinary(self.reactor.__p_is_faulted(), AUTO_ACK.FAIL, AUTO_ACK.DIRECT_SET_OK)
end
end
else
log.debug(util.c(burn_rate, " rate outside of 0 < x <= ", self.max_burn_rate))
end
end
_send_ack(packet.type, ack)
else
log.debug("RPLC set automatic burn rate packet length mismatch or non-numeric burn rate")
end
else
log.warning("received unknown RPLC packet type " .. packet.type)
end
else
log.debug("discarding RPLC packet before linked")
end
2023-02-21 16:05:57 +00:00
elseif protocol == PROTOCOL.SCADA_MGMT then
if self.linked then
2023-02-21 16:05:57 +00:00
if packet.type == SCADA_MGMT_TYPE.ESTABLISH then
-- link request confirmation
if packet.length == 1 then
log.debug("received unsolicited establish response")
local est_ack = packet.data[1]
if est_ack == ESTABLISH_ACK.ALLOW then
self.status_cache = nil
_send_struct()
public.send_status(plc_state.no_reactor, plc_state.reactor_formed)
log.debug("re-sent initial status data")
elseif est_ack == ESTABLISH_ACK.DENY then
println_ts("received unsolicited link denial, unlinking")
log.info("unsolicited establish request denied")
elseif est_ack == ESTABLISH_ACK.COLLISION then
println_ts("received unsolicited link collision, unlinking")
log.warning("unsolicited establish request collision")
elseif est_ack == ESTABLISH_ACK.BAD_VERSION then
println_ts("received unsolicited link version mismatch, unlinking")
log.warning("unsolicited establish request version mismatch")
else
println_ts("invalid unsolicited link response")
log.error("unsolicited unknown establish request response")
end
self.linked = est_ack == ESTABLISH_ACK.ALLOW
-- clear this since this is for something that was unsolicited
self.last_est_ack = ESTABLISH_ACK.ALLOW
else
log.debug("SCADA_MGMT establish packet length mismatch")
end
2023-02-21 16:05:57 +00:00
elseif packet.type == SCADA_MGMT_TYPE.KEEP_ALIVE then
-- 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
if trip_time > 500 then
log.warning("PLC KEEP_ALIVE trip time > 500ms (" .. trip_time .. "ms)")
end
-- log.debug("RPLC RTT = " .. trip_time .. "ms")
_send_keep_alive_ack(timestamp)
else
log.debug("SCADA_MGMT keep alive packet length/type mismatch")
end
2023-02-21 16:05:57 +00:00
elseif packet.type == SCADA_MGMT_TYPE.CLOSE then
-- 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
log.warning("received unsupported SCADA_MGMT packet type " .. packet.type)
end
2023-02-21 16:05:57 +00:00
elseif packet.type == SCADA_MGMT_TYPE.ESTABLISH then
-- link request confirmation
2022-04-29 13:07:29 +00:00
if packet.length == 1 then
local est_ack = packet.data[1]
2022-04-29 13:07:29 +00:00
if est_ack == ESTABLISH_ACK.ALLOW then
2022-04-29 13:07:29 +00:00
println_ts("linked!")
log.debug("supervisor establish request approved")
-- reset remote sequence number and cache
2022-04-29 13:07:29 +00:00
self.r_seq_num = nil
self.status_cache = nil
if plc_state.reactor_formed then _send_struct() end
public.send_status(plc_state.no_reactor, plc_state.reactor_formed)
2022-04-29 13:07:29 +00:00
log.debug("sent initial status data")
elseif self.last_est_ack ~= est_ack then
if est_ack == ESTABLISH_ACK.DENY then
println_ts("link request denied, retrying...")
log.debug("establish request denied")
elseif est_ack == ESTABLISH_ACK.COLLISION then
println_ts("reactor PLC ID collision (check config), retrying...")
log.warning("establish request collision")
elseif est_ack == ESTABLISH_ACK.BAD_VERSION then
println_ts("supervisor version mismatch (try updating), retrying...")
log.warning("establish request version mismatch")
else
println_ts("invalid link response, bad channel? retrying...")
log.error("unknown establish request response")
end
end
self.linked = est_ack == ESTABLISH_ACK.ALLOW
self.last_est_ack = est_ack
else
log.debug("SCADA_MGMT establish packet length mismatch")
end
2022-05-02 16:06:04 +00:00
else
log.debug("discarding non-link SCADA_MGMT packet before linked")
2022-05-02 16:06:04 +00:00
end
else
-- should be unreachable assuming packet is from parse_packet()
log.error("illegal packet type " .. protocol, true)
end
end
end
2022-05-31 18:14:17 +00:00
function public.is_scrammed() return self.scrammed end
function public.is_linked() return self.linked end
2022-05-11 01:49:14 +00:00
return public
end
return plc