2022-05-04 17:37:01 +00:00
|
|
|
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")
|
2022-05-04 17:37:01 +00:00
|
|
|
local types = require("scada-common.types")
|
2022-05-31 18:14:17 +00:00
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local plc = {}
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
local rps_status_t = types.rps_status_t
|
2022-05-03 14:44:18 +00:00
|
|
|
|
2022-04-21 14:26:02 +00:00
|
|
|
local PROTOCOLS = comms.PROTOCOLS
|
|
|
|
local RPLC_TYPES = comms.RPLC_TYPES
|
|
|
|
local RPLC_LINKING = comms.RPLC_LINKING
|
2022-05-02 21:40:00 +00:00
|
|
|
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
|
2022-04-21 14:26:02 +00:00
|
|
|
|
2022-04-26 01:00:50 +00:00
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
|
|
|
local print_ts = util.print_ts
|
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
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
|
2022-05-31 18:14:17 +00:00
|
|
|
function plc.rps_init(reactor)
|
2022-05-05 17:14:14 +00:00
|
|
|
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-01-14 21:33:09 +00:00
|
|
|
local self = {
|
2022-01-22 19:26:25 +00:00
|
|
|
reactor = reactor,
|
2022-05-05 17:14:14 +00:00
|
|
|
state = { false, false, false, false, false, false, false, false, false },
|
|
|
|
reactor_enabled = false,
|
2022-01-22 19:26:25 +00:00
|
|
|
tripped = false,
|
2022-09-03 17:10:09 +00:00
|
|
|
trip_cause = "" ---@type rps_trip_cause
|
2022-01-14 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
---@class rps
|
|
|
|
local public = {}
|
|
|
|
|
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()
|
2022-05-19 13:19:51 +00:00
|
|
|
if self.reactor.__p_last_fault() ~= "Terminated" then
|
|
|
|
self.state[state_keys.fault] = true
|
|
|
|
end
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- clear reactor access fault flag
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _clear_fault()
|
2022-05-05 17:14:14 +00:00
|
|
|
self.state[state_keys.fault] = false
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check for critical damage
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _damage_critical()
|
2022-04-07 15:12:41 +00:00
|
|
|
local damage_percent = self.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 15:55:04 +00:00
|
|
|
log.error("RPS: failed to check reactor damage")
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
|
|
|
self.state[state_keys.dmg_crit] = false
|
2022-04-07 15:12:41 +00:00
|
|
|
else
|
2022-05-05 17:14:14 +00:00
|
|
|
self.state[state_keys.dmg_crit] = damage_percent >= 100
|
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
|
|
|
|
local temp = self.reactor.getTemperature()
|
|
|
|
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
|
|
|
log.error("RPS: failed to check reactor temperature")
|
|
|
|
_set_fault()
|
|
|
|
self.state[state_keys.high_temp] = false
|
2022-04-07 15:12:41 +00:00
|
|
|
else
|
2022-05-05 17:14:14 +00:00
|
|
|
self.state[state_keys.high_temp] = temp >= 1200
|
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 there is no coolant (<2% filled)
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _no_coolant()
|
2022-05-05 17:14:14 +00:00
|
|
|
local coolant_filled = self.reactor.getCoolantFilledPercentage()
|
|
|
|
if coolant_filled == ppm.ACCESS_FAULT then
|
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
log.error("RPS: failed to check reactor coolant level")
|
|
|
|
_set_fault()
|
|
|
|
self.state[state_keys.no_coolant] = false
|
|
|
|
else
|
|
|
|
self.state[state_keys.no_coolant] = coolant_filled < 0.02
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check for excess waste (>80% filled)
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _excess_waste()
|
2022-05-05 17:14:14 +00:00
|
|
|
local w_filled = self.reactor.getWasteFilledPercentage()
|
|
|
|
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 15:55:04 +00:00
|
|
|
log.error("RPS: failed to check reactor waste level")
|
2022-05-05 17:14:14 +00:00
|
|
|
_set_fault()
|
|
|
|
self.state[state_keys.ex_waste] = false
|
2022-04-07 15:12:41 +00:00
|
|
|
else
|
2022-05-05 17:14:14 +00:00
|
|
|
self.state[state_keys.ex_waste] = w_filled > 0.8
|
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()
|
2022-05-05 17:14:14 +00:00
|
|
|
local hc_filled = self.reactor.getHeatedCoolantFilledPercentage()
|
|
|
|
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
|
|
|
log.error("RPS: failed to check reactor heated coolant level")
|
|
|
|
_set_fault()
|
2022-05-06 13:10:50 +00:00
|
|
|
self.state[state_keys.ex_hcoolant] = false
|
2022-04-07 15:12:41 +00:00
|
|
|
else
|
2022-05-06 13:10:50 +00:00
|
|
|
self.state[state_keys.ex_hcoolant] = hc_filled > 0.95
|
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()
|
2022-04-07 15:12:41 +00:00
|
|
|
local fuel = self.reactor.getFuel()
|
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
|
|
|
log.error("RPS: failed to check reactor fuel")
|
|
|
|
_set_fault()
|
2022-05-06 13:10:50 +00:00
|
|
|
self.state[state_keys.no_fuel] = false
|
2022-04-07 15:12:41 +00:00
|
|
|
else
|
2022-05-06 13:10:50 +00:00
|
|
|
self.state[state_keys.no_fuel] = fuel == 0
|
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 --
|
|
|
|
|
|
|
|
-- re-link a reactor after a peripheral re-connect
|
2022-05-11 16:01:18 +00:00
|
|
|
---@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()
|
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-05-06 13:10:50 +00:00
|
|
|
self.state[state_keys.manual] = true
|
2022-05-05 17:14:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- SCRAM the reactor now
|
2022-05-11 01:49:14 +00:00
|
|
|
---@return boolean success
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.scram()
|
2022-05-05 17:14:14 +00:00
|
|
|
log.info("RPS: reactor SCRAM")
|
|
|
|
|
|
|
|
self.reactor.scram()
|
|
|
|
if self.reactor.__p_is_faulted() then
|
|
|
|
log.error("RPS: failed reactor SCRAM")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
self.reactor_enabled = false
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- start the reactor
|
2022-05-11 01:49:14 +00:00
|
|
|
---@return boolean success
|
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")
|
|
|
|
|
|
|
|
self.reactor.activate()
|
|
|
|
if self.reactor.__p_is_faulted() then
|
|
|
|
log.error("RPS: failed reactor start")
|
|
|
|
else
|
|
|
|
self.reactor_enabled = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- 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
|
2022-05-05 17:14:14 +00:00
|
|
|
local first_trip = false
|
|
|
|
|
|
|
|
-- update state
|
|
|
|
parallel.waitForAll(
|
|
|
|
_damage_critical,
|
|
|
|
_high_temp,
|
|
|
|
_no_coolant,
|
|
|
|
_excess_waste,
|
|
|
|
_excess_heated_coolant,
|
|
|
|
_insufficient_fuel
|
|
|
|
)
|
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-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif self.state[state_keys.manual] then
|
|
|
|
log.warning("RPS: manual SCRAM requested")
|
|
|
|
status = rps_status_t.manual
|
2022-01-02 00:45:33 +00:00
|
|
|
else
|
2022-01-22 19:26:25 +00:00
|
|
|
self.tripped = false
|
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...
|
|
|
|
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
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
public.scram()
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
2022-05-05 17:14:14 +00:00
|
|
|
|
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
|
|
|
|
function public.is_tripped() return self.tripped end
|
|
|
|
function public.is_active() return self.reactor_enabled end
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- reset the RPS
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.reset()
|
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
|
2022-01-22 19:26:25 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
---@param id integer
|
2022-05-19 14:21:04 +00:00
|
|
|
---@param version string
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param modem table
|
|
|
|
---@param local_port integer
|
|
|
|
---@param server_port integer
|
|
|
|
---@param reactor table
|
|
|
|
---@param rps rps
|
|
|
|
---@param conn_watchdog watchdog
|
2022-05-31 18:14:17 +00:00
|
|
|
function plc.comms(id, version, modem, local_port, server_port, reactor, rps, conn_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
|
|
|
id = id,
|
2022-05-19 14:21:04 +00:00
|
|
|
version = version,
|
2022-03-25 16:17:46 +00:00
|
|
|
seq_num = 0,
|
2022-04-24 01:10:25 +00:00
|
|
|
r_seq_num = nil,
|
2022-03-25 16:17:46 +00:00
|
|
|
modem = modem,
|
|
|
|
s_port = server_port,
|
|
|
|
l_port = local_port,
|
|
|
|
reactor = reactor,
|
2022-05-05 15:55:04 +00:00
|
|
|
rps = rps,
|
2022-05-09 19:00:16 +00:00
|
|
|
conn_watchdog = conn_watchdog,
|
2022-04-02 15:46:14 +00:00
|
|
|
scrammed = false,
|
2022-04-21 16:40:21 +00:00
|
|
|
linked = false,
|
|
|
|
status_cache = nil,
|
|
|
|
max_burn_rate = nil
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
---@class plc_comms
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-03 16:08:22 +00:00
|
|
|
-- open modem
|
|
|
|
if not self.modem.isOpen(self.l_port) then
|
|
|
|
self.modem.open(self.l_port)
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-05-11 01:49:14 +00:00
|
|
|
-- send an RPLC packet
|
|
|
|
---@param msg_type RPLC_TYPES
|
|
|
|
---@param msg string
|
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
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
r_pkt.make(self.id, msg_type, msg)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.RPLC, r_pkt.raw_sendable())
|
2022-05-02 16:06:04 +00:00
|
|
|
|
2022-05-02 21:40:00 +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
|
|
|
|
---@param msg_type SCADA_MGMT_TYPES
|
|
|
|
---@param msg string
|
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)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-04-23 01:39:03 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
|
|
|
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
|
|
|
|
0, -- getFuel
|
|
|
|
0, -- getFuelFilledPercentage
|
|
|
|
0, -- getWaste
|
|
|
|
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,
|
2022-05-02 21:40:00 +00:00
|
|
|
function () data_table[9] = self.reactor.getFuelFilledPercentage() end,
|
2022-07-15 13:58:04 +00:00
|
|
|
function () waste = self.reactor.getWaste() end,
|
2022-05-02 21:40:00 +00:00
|
|
|
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
|
|
|
|
}
|
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-07-15 13:58:04 +00:00
|
|
|
if type(fuel) == "table" then
|
|
|
|
data_table[8] = fuel.amount
|
|
|
|
elseif type(fuel) == "number" then
|
|
|
|
data_table[8] = fuel
|
|
|
|
end
|
|
|
|
|
|
|
|
if type(waste) == "table" then
|
|
|
|
data_table[10] = waste.amount
|
|
|
|
elseif type(waste) == "number" then
|
|
|
|
data_table[10] = waste
|
|
|
|
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
|
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
return data_table, self.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)
|
2022-05-26 21:49:43 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.KEEP_ALIVE, { srv_time, util.time() })
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- general ack
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param msg_type RPLC_TYPES
|
|
|
|
---@param succeeded boolean
|
2022-05-31 18:14:17 +00:00
|
|
|
local function _send_ack(msg_type, succeeded)
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(msg_type, { succeeded })
|
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()
|
2022-05-02 21:40:00 +00:00
|
|
|
local mek_data = { 0, 0, 0, 0, 0, 0, 0, 0 }
|
|
|
|
|
|
|
|
local tasks = {
|
|
|
|
function () mek_data[1] = self.reactor.getHeatCapacity() end,
|
|
|
|
function () mek_data[2] = self.reactor.getFuelAssemblies() end,
|
|
|
|
function () mek_data[3] = self.reactor.getFuelSurfaceArea() end,
|
|
|
|
function () mek_data[4] = self.reactor.getFuelCapacity() end,
|
|
|
|
function () mek_data[5] = self.reactor.getWasteCapacity() end,
|
|
|
|
function () mek_data[6] = self.reactor.getCoolantCapacity() end,
|
|
|
|
function () mek_data[7] = self.reactor.getHeatedCoolantCapacity() end,
|
|
|
|
function () mek_data[8] = self.reactor.getMaxBurnRate() end
|
2022-04-02 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
parallel.waitForAll(table.unpack(tasks))
|
|
|
|
|
2022-04-26 01:00:50 +00:00
|
|
|
if not self.reactor.__p_is_faulted() then
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(RPLC_TYPES.MEK_STRUCT, mek_data)
|
2022-04-18 01:12:25 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("failed to send structure: PPM fault")
|
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 --
|
|
|
|
|
2022-04-03 16:08:22 +00:00
|
|
|
-- reconnect a newly connected modem
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param modem table
|
2022-05-11 16:01:18 +00:00
|
|
|
---@diagnostic disable-next-line: redefined-local
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.reconnect_modem(modem)
|
2022-04-03 16:08:22 +00:00
|
|
|
self.modem = modem
|
|
|
|
|
|
|
|
-- open modem
|
|
|
|
if not self.modem.isOpen(self.l_port) then
|
|
|
|
self.modem.open(self.l_port)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- reconnect a newly connected reactor
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param reactor table
|
2022-05-11 16:01:18 +00:00
|
|
|
---@diagnostic disable-next-line: redefined-local
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.reconnect_reactor(reactor)
|
2022-04-03 16:08:22 +00:00
|
|
|
self.reactor = reactor
|
2022-05-03 21:10:42 +00:00
|
|
|
self.status_cache = 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()
|
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
|
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-05-09 19:00:16 +00:00
|
|
|
self.conn_watchdog.cancel()
|
2022-05-11 01:49:14 +00:00
|
|
|
public.unlink()
|
2022-05-02 21:40:00 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.CLOSE, {})
|
|
|
|
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()
|
2022-05-19 14:21:04 +00:00
|
|
|
_send(RPLC_TYPES.LINK_REQ, { self.id, self.version })
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- send live status information
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param degraded boolean
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.send_status(degraded)
|
2022-05-02 21:40:00 +00:00
|
|
|
if self.linked then
|
|
|
|
local mek_data = nil
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
if _update_status_cache() then
|
|
|
|
mek_data = self.status_cache
|
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-02 21:40:00 +00:00
|
|
|
local sys_status = {
|
|
|
|
util.time(), -- timestamp
|
2022-05-09 19:00:16 +00:00
|
|
|
(not self.scrammed), -- requested control state
|
2022-09-08 18:49:01 +00:00
|
|
|
rps.is_tripped(), -- rps_tripped
|
2022-05-02 21:40:00 +00:00
|
|
|
degraded, -- degraded
|
|
|
|
self.reactor.getHeatingRate(), -- heating rate
|
|
|
|
mek_data -- mekanism status data
|
|
|
|
}
|
|
|
|
|
|
|
|
if not self.reactor.__p_is_faulted() then
|
|
|
|
_send(RPLC_TYPES.STATUS, sys_status)
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("failed to send status: PPM fault")
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
|
|
|
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
|
2022-05-05 15:55:04 +00:00
|
|
|
_send(RPLC_TYPES.RPS_STATUS, 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
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param cause rps_status_t
|
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
|
2022-05-05 15:55:04 +00:00
|
|
|
local rps_alarm = {
|
2022-05-02 21:40:00 +00:00
|
|
|
cause,
|
2022-05-05 15:55:04 +00:00
|
|
|
table.unpack(rps.status())
|
2022-05-02 21:40:00 +00:00
|
|
|
}
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
_send(RPLC_TYPES.RPS_ALARM, rps_alarm)
|
2022-05-02 21:40:00 +00:00
|
|
|
end
|
2022-04-26 01:00:50 +00:00
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- 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)
|
2022-03-25 16:17:46 +00:00
|
|
|
local pkt = nil
|
2022-04-26 01:00:50 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
2022-03-25 16:17:46 +00:00
|
|
|
|
|
|
|
-- parse packet as generic SCADA packet
|
2022-04-25 19:49:04 +00:00
|
|
|
s_pkt.receive(side, sender, reply_to, message, distance)
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-04-02 12:28:43 +00:00
|
|
|
if s_pkt.is_valid() then
|
|
|
|
-- get as RPLC packet
|
|
|
|
if s_pkt.protocol() == PROTOCOLS.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
|
|
|
|
elseif s_pkt.protocol() == PROTOCOLS.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
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("illegal packet type " .. s_pkt.protocol(), true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return pkt
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle an RPLC packet
|
2022-05-11 01:49:14 +00:00
|
|
|
---@param packet rplc_frame|mgmt_frame
|
|
|
|
---@param plc_state plc_state
|
|
|
|
---@param setpoints setpoints
|
2022-05-31 18:14:17 +00:00
|
|
|
function public.handle_packet(packet, plc_state, setpoints)
|
2022-04-02 15:22:44 +00:00
|
|
|
if packet ~= nil 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()
|
2022-04-26 01:00:50 +00:00
|
|
|
elseif self.linked and self.r_seq_num >= 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
|
|
|
|
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 :)
|
|
|
|
self.conn_watchdog.feed()
|
2022-04-29 02:36:45 +00:00
|
|
|
|
2022-05-10 16:01:56 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
|
|
|
|
2022-04-24 01:10:25 +00:00
|
|
|
-- handle packet
|
2022-05-10 16:01:56 +00:00
|
|
|
if protocol == PROTOCOLS.RPLC then
|
2022-04-02 15:22:44 +00:00
|
|
|
if self.linked then
|
2022-05-09 19:00:16 +00:00
|
|
|
if packet.type == RPLC_TYPES.LINK_REQ 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-05-04 17:37:01 +00:00
|
|
|
log.debug("received unsolicited link request response")
|
2022-04-29 13:07:29 +00:00
|
|
|
|
|
|
|
local link_ack = packet.data[1]
|
|
|
|
|
|
|
|
if link_ack == RPLC_LINKING.ALLOW then
|
2022-05-03 21:10:42 +00:00
|
|
|
self.status_cache = nil
|
2022-04-29 13:07:29 +00:00
|
|
|
_send_struct()
|
2022-05-11 01:49:14 +00:00
|
|
|
public.send_status(plc_state.degraded)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("re-sent initial status data")
|
2022-04-29 13:07:29 +00:00
|
|
|
elseif link_ack == RPLC_LINKING.DENY then
|
|
|
|
println_ts("received unsolicited link denial, unlinking")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("unsolicited RPLC link request denied")
|
2022-04-29 13:07:29 +00:00
|
|
|
elseif link_ack == RPLC_LINKING.COLLISION then
|
|
|
|
println_ts("received unsolicited link collision, unlinking")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("unsolicited RPLC link request collision")
|
2022-04-29 13:07:29 +00:00
|
|
|
else
|
|
|
|
println_ts("invalid unsolicited link response")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("unsolicited unknown RPLC link request response")
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
2022-04-02 18:43:36 +00:00
|
|
|
|
2022-04-29 13:07:29 +00:00
|
|
|
self.linked = link_ack == RPLC_LINKING.ALLOW
|
2022-04-02 18:43:36 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("RPLC link req packet length mismatch")
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
2022-05-03 21:10:42 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.STATUS then
|
|
|
|
-- request of full status, clear cache first
|
|
|
|
self.status_cache = nil
|
2022-05-11 01:49:14 +00:00
|
|
|
public.send_status(plc_state.degraded)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("sent out status cache again, did supervisor miss it?")
|
2022-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.MEK_STRUCT then
|
|
|
|
-- 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?")
|
2022-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.MEK_BURN_RATE then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- set the burn rate
|
2022-05-05 20:00:49 +00:00
|
|
|
if packet.length == 2 then
|
2022-04-29 13:07:29 +00:00
|
|
|
local success = false
|
|
|
|
local burn_rate = packet.data[1]
|
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-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
|
|
|
|
self.reactor.setBurnRate(burn_rate)
|
|
|
|
success = not self.reactor.__p_is_faulted()
|
|
|
|
end
|
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
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("RPLC set burn rate packet length mismatch")
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.RPS_ENABLE then
|
|
|
|
-- enable the reactor
|
|
|
|
self.scrammed = false
|
|
|
|
_send_ack(packet.type, self.rps.activate())
|
|
|
|
elseif packet.type == RPLC_TYPES.RPS_SCRAM then
|
|
|
|
-- disable the reactor
|
|
|
|
self.scrammed = true
|
|
|
|
self.rps.trip_manual()
|
|
|
|
_send_ack(packet.type, true)
|
2022-05-05 15:55:04 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.RPS_RESET then
|
|
|
|
-- reset the RPS status
|
|
|
|
rps.reset()
|
2022-04-02 18:43:36 +00:00
|
|
|
_send_ack(packet.type, true)
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("received unknown RPLC packet type " .. packet.type)
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
|
|
|
elseif packet.type == RPLC_TYPES.LINK_REQ then
|
|
|
|
-- link request confirmation
|
2022-04-29 13:07:29 +00:00
|
|
|
if packet.length == 1 then
|
|
|
|
local link_ack = packet.data[1]
|
|
|
|
|
|
|
|
if link_ack == RPLC_LINKING.ALLOW then
|
|
|
|
println_ts("linked!")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("RPLC link request approved")
|
2022-04-26 01:00:50 +00:00
|
|
|
|
2022-05-03 21:10:42 +00:00
|
|
|
-- reset remote sequence number and cache
|
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-04-29 13:07:29 +00:00
|
|
|
_send_struct()
|
2022-05-11 01:49:14 +00:00
|
|
|
public.send_status(plc_state.degraded)
|
2022-04-29 13:07:29 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("sent initial status data")
|
2022-04-29 13:07:29 +00:00
|
|
|
elseif link_ack == RPLC_LINKING.DENY then
|
|
|
|
println_ts("link request denied, retrying...")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("RPLC link request denied")
|
2022-04-29 13:07:29 +00:00
|
|
|
elseif link_ack == RPLC_LINKING.COLLISION then
|
|
|
|
println_ts("reactor PLC ID collision (check config), retrying...")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("RPLC link request collision")
|
2022-04-29 13:07:29 +00:00
|
|
|
else
|
|
|
|
println_ts("invalid link response, bad channel? retrying...")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("unknown RPLC link request response")
|
2022-04-29 13:07:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.linked = link_ack == RPLC_LINKING.ALLOW
|
2022-04-02 15:22:44 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("RPLC link req packet length mismatch")
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("discarding non-link packet before linked")
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
2022-05-10 16:01:56 +00:00
|
|
|
elseif protocol == PROTOCOLS.SCADA_MGMT then
|
2022-05-09 19:00:16 +00:00
|
|
|
if packet.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
|
|
|
-- keep alive request received, echo back
|
|
|
|
if packet.length == 1 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
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
-- log.debug("RPLC RTT = " .. trip_time .. "ms")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
|
|
|
_send_keep_alive_ack(timestamp)
|
|
|
|
else
|
|
|
|
log.debug("SCADA keep alive packet length mismatch")
|
|
|
|
end
|
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.CLOSE then
|
|
|
|
-- handle session close
|
|
|
|
self.conn_watchdog.cancel()
|
2022-05-11 01:49:14 +00:00
|
|
|
public.unlink()
|
2022-05-02 21:40:00 +00:00
|
|
|
println_ts("server connection closed by remote host")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("server connection closed by remote host")
|
2022-05-02 16:06:04 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("received unknown SCADA_MGMT packet type " .. packet.type)
|
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
|
2022-03-25 16:17:46 +00:00
|
|
|
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
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return plc
|