2022-10-03 01:17:13 +00:00
|
|
|
local comms = require("scada-common.comms")
|
2024-04-30 02:03:54 +00:00
|
|
|
local const = require("scada-common.constants")
|
2022-10-03 01:17:13 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
2023-02-14 20:15:34 +00:00
|
|
|
local types = require("scada-common.types")
|
2022-10-03 01:17:13 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2023-05-23 23:22:22 +00:00
|
|
|
local databus = require("supervisor.databus")
|
|
|
|
|
2022-10-03 01:17:13 +00:00
|
|
|
local svqtypes = require("supervisor.session.svqtypes")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local plc = {}
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
local PROTOCOL = comms.PROTOCOL
|
|
|
|
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 PLC_AUTO_ACK = comms.PLC_AUTO_ACK
|
2023-02-21 16:05:57 +00:00
|
|
|
local UNIT_COMMAND = comms.UNIT_COMMAND
|
2022-10-20 16:22:03 +00:00
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- retry time constants in ms
|
2023-01-26 23:26:26 +00:00
|
|
|
local INITIAL_WAIT = 1500
|
|
|
|
local INITIAL_AUTO_WAIT = 1000
|
|
|
|
local RETRY_PERIOD = 1000
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local PLC_S_CMDS = {
|
2022-10-03 01:17:13 +00:00
|
|
|
SCRAM = 1,
|
2022-11-11 21:15:44 +00:00
|
|
|
ASCRAM = 2,
|
|
|
|
ENABLE = 3,
|
2023-09-19 20:37:15 +00:00
|
|
|
DISABLE = 4,
|
|
|
|
RPS_RESET = 5,
|
|
|
|
RPS_AUTO_RESET = 6
|
2022-05-05 20:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local PLC_S_DATA = {
|
|
|
|
BURN_RATE = 1,
|
2023-01-03 21:50:31 +00:00
|
|
|
RAMP_BURN_RATE = 2,
|
|
|
|
AUTO_BURN_RATE = 3
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
plc.PLC_S_CMDS = PLC_S_CMDS
|
2022-05-05 20:00:49 +00:00
|
|
|
plc.PLC_S_DATA = PLC_S_DATA
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2022-04-23 16:12:33 +00:00
|
|
|
local PERIODICS = {
|
2022-07-07 17:18:10 +00:00
|
|
|
KEEP_ALIVE = 2000
|
2022-04-23 16:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
-- PLC supervisor session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-02-13 17:27:22 +00:00
|
|
|
---@param id integer session ID
|
2023-06-05 21:24:00 +00:00
|
|
|
---@param s_addr integer device source address
|
2023-02-25 04:36:16 +00:00
|
|
|
---@param reactor_id integer reactor ID
|
2023-02-13 17:27:22 +00:00
|
|
|
---@param in_queue mqueue in message queue
|
|
|
|
---@param out_queue mqueue out message queue
|
|
|
|
---@param timeout number communications timeout
|
2023-06-03 19:45:48 +00:00
|
|
|
---@param fp_ok boolean if the front panel UI is running
|
2023-06-05 21:24:00 +00:00
|
|
|
function plc.new_session(id, s_addr, reactor_id, in_queue, out_queue, timeout, fp_ok)
|
2023-06-03 19:45:48 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println(message) if not fp_ok then util.println_ts(message) end end
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
local log_header = "plc_session(" .. id .. "): "
|
|
|
|
|
|
|
|
local self = {
|
|
|
|
commanded_state = false,
|
2022-05-01 21:04:38 +00:00
|
|
|
commanded_burn_rate = 0.0,
|
2023-02-04 18:47:00 +00:00
|
|
|
auto_cmd_token = 0,
|
2022-05-05 20:00:49 +00:00
|
|
|
ramping_rate = false,
|
2023-01-03 21:50:31 +00:00
|
|
|
auto_lock = false,
|
2022-04-22 15:07:59 +00:00
|
|
|
-- connection properties
|
|
|
|
seq_num = 0,
|
2022-04-23 16:12:33 +00:00
|
|
|
r_seq_num = nil,
|
2022-04-22 15:07:59 +00:00
|
|
|
connected = true,
|
|
|
|
received_struct = false,
|
2022-05-03 21:25:31 +00:00
|
|
|
received_status_cache = false,
|
2023-04-18 17:49:59 +00:00
|
|
|
conn_watchdog = util.new_watchdog(timeout),
|
2022-04-23 16:12:33 +00:00
|
|
|
last_rtt = 0,
|
2022-05-01 21:04:38 +00:00
|
|
|
-- periodic messages
|
2022-04-23 16:12:33 +00:00
|
|
|
periodics = {
|
2022-04-24 00:46:01 +00:00
|
|
|
last_update = 0,
|
2022-04-23 16:12:33 +00:00
|
|
|
keep_alive = 0
|
|
|
|
},
|
2022-05-01 21:04:38 +00:00
|
|
|
-- when to next retry one of these requests
|
2022-04-22 15:07:59 +00:00
|
|
|
retry_times = {
|
2022-05-02 21:43:23 +00:00
|
|
|
struct_req = (util.time() + 500),
|
2022-05-03 21:25:31 +00:00
|
|
|
status_req = (util.time() + 500),
|
2023-09-19 20:37:15 +00:00
|
|
|
disable_req = 0,
|
2022-04-22 15:07:59 +00:00
|
|
|
scram_req = 0,
|
2023-02-13 23:08:32 +00:00
|
|
|
ascram_req = 0,
|
2022-05-01 21:04:38 +00:00
|
|
|
burn_rate_req = 0,
|
2022-05-05 15:55:04 +00:00
|
|
|
rps_reset_req = 0
|
2022-05-01 21:04:38 +00:00
|
|
|
},
|
|
|
|
-- command acknowledgements
|
|
|
|
acks = {
|
2023-09-19 20:37:15 +00:00
|
|
|
disable = true,
|
2022-05-01 21:04:38 +00:00
|
|
|
scram = true,
|
2023-02-13 23:08:32 +00:00
|
|
|
ascram = true,
|
2022-05-01 21:04:38 +00:00
|
|
|
burn_rate = true,
|
2022-05-05 15:55:04 +00:00
|
|
|
rps_reset = true
|
2022-04-22 15:07:59 +00:00
|
|
|
},
|
2022-04-23 16:12:33 +00:00
|
|
|
-- session database
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class reactor_db
|
2022-04-22 15:07:59 +00:00
|
|
|
sDB = {
|
2023-02-04 18:47:00 +00:00
|
|
|
auto_ack_token = 0,
|
2022-04-24 00:46:01 +00:00
|
|
|
last_status_update = 0,
|
2022-04-22 15:07:59 +00:00
|
|
|
control_state = false,
|
2022-10-25 17:29:57 +00:00
|
|
|
no_reactor = false,
|
|
|
|
formed = false,
|
2022-05-05 15:55:04 +00:00
|
|
|
rps_tripped = false,
|
2022-09-03 17:10:09 +00:00
|
|
|
rps_trip_cause = "ok", ---@type rps_trip_cause
|
2024-04-30 02:03:54 +00:00
|
|
|
max_op_temp_H2O = 1200,
|
|
|
|
max_op_temp_Na = 1200,
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class rps_status
|
2022-05-05 15:55:04 +00:00
|
|
|
rps_status = {
|
2023-03-05 03:32:13 +00:00
|
|
|
high_dmg = false,
|
2022-04-22 15:07:59 +00:00
|
|
|
high_temp = false,
|
2023-03-05 03:19:53 +00:00
|
|
|
low_cool = false,
|
2022-10-25 17:29:57 +00:00
|
|
|
ex_waste = false,
|
|
|
|
ex_hcool = false,
|
|
|
|
no_fuel = false,
|
2022-07-16 16:54:02 +00:00
|
|
|
fault = false,
|
|
|
|
timeout = false,
|
2022-10-25 17:29:57 +00:00
|
|
|
manual = false,
|
2022-11-11 20:45:46 +00:00
|
|
|
automatic = false,
|
|
|
|
sys_fail = false,
|
|
|
|
force_dis = false
|
2022-04-22 15:07:59 +00:00
|
|
|
},
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class mek_status
|
2022-04-22 15:07:59 +00:00
|
|
|
mek_status = {
|
2022-05-11 16:31:19 +00:00
|
|
|
heating_rate = 0.0,
|
2022-04-22 15:07:59 +00:00
|
|
|
|
|
|
|
status = false,
|
2022-05-11 16:31:19 +00:00
|
|
|
burn_rate = 0.0,
|
|
|
|
act_burn_rate = 0.0,
|
|
|
|
temp = 0.0,
|
|
|
|
damage = 0.0,
|
|
|
|
boil_eff = 0.0,
|
|
|
|
env_loss = 0.0,
|
|
|
|
|
2022-05-11 17:05:20 +00:00
|
|
|
fuel = 0,
|
|
|
|
fuel_need = 0,
|
2022-05-11 16:31:19 +00:00
|
|
|
fuel_fill = 0.0,
|
2022-05-11 17:05:20 +00:00
|
|
|
waste = 0,
|
|
|
|
waste_need = 0,
|
2022-05-11 16:31:19 +00:00
|
|
|
waste_fill = 0.0,
|
2024-04-30 02:03:54 +00:00
|
|
|
ccool_type = types.FLUID.EMPTY_GAS, ---@type fluid
|
2022-05-11 17:05:20 +00:00
|
|
|
ccool_amnt = 0,
|
|
|
|
ccool_need = 0,
|
|
|
|
ccool_fill = 0.0,
|
2024-04-30 02:03:54 +00:00
|
|
|
hcool_type = types.FLUID.EMPTY_GAS, ---@type fluid
|
2022-05-11 17:05:20 +00:00
|
|
|
hcool_amnt = 0,
|
|
|
|
hcool_need = 0,
|
2022-05-11 16:31:19 +00:00
|
|
|
hcool_fill = 0.0
|
2022-04-22 15:07:59 +00:00
|
|
|
},
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class mek_struct
|
2022-04-22 15:07:59 +00:00
|
|
|
mek_struct = {
|
2022-09-30 21:33:35 +00:00
|
|
|
length = 0,
|
|
|
|
width = 0,
|
|
|
|
height = 0,
|
2023-02-14 20:15:34 +00:00
|
|
|
min_pos = types.new_zero_coordinate(),
|
|
|
|
max_pos = types.new_zero_coordinate(),
|
2022-05-11 17:05:20 +00:00
|
|
|
heat_cap = 0,
|
|
|
|
fuel_asm = 0,
|
|
|
|
fuel_sa = 0,
|
|
|
|
fuel_cap = 0,
|
|
|
|
waste_cap = 0,
|
|
|
|
ccool_cap = 0,
|
|
|
|
hcool_cap = 0,
|
2022-05-11 16:31:19 +00:00
|
|
|
max_burn = 0.0
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
---@class plc_session
|
|
|
|
local public = {}
|
|
|
|
|
2024-04-30 02:03:54 +00:00
|
|
|
-- compute maximum expected operational temperatures for high temp warnings
|
|
|
|
local function _compute_op_temps()
|
|
|
|
local JOULES_PER_MB = const.mek.JOULES_PER_MB
|
|
|
|
local BASE_BOIL_TEMP = const.mek.BASE_BOIL_TEMP
|
|
|
|
|
|
|
|
local heat_cap = self.sDB.mek_struct.heat_cap
|
|
|
|
local max_burn = self.sDB.mek_struct.max_burn
|
|
|
|
|
|
|
|
self.sDB.max_op_temp_H2O = max_burn * 2 * (JOULES_PER_MB * heat_cap ^ -1) + BASE_BOIL_TEMP
|
|
|
|
self.sDB.max_op_temp_Na = max_burn * (JOULES_PER_MB * heat_cap ^ -1) + BASE_BOIL_TEMP
|
|
|
|
|
2024-04-30 22:54:01 +00:00
|
|
|
log.info(util.sprintf(log_header .. "computed maximum operational temperatures %.3fK (H2O) and %.3fK (Na)",
|
2024-04-30 02:03:54 +00:00
|
|
|
self.sDB.max_op_temp_H2O, self.sDB.max_op_temp_Na))
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- copy in the RPS status
|
|
|
|
---@param rps_status table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _copy_rps_status(rps_status)
|
2022-12-07 17:59:21 +00:00
|
|
|
self.sDB.rps_tripped = rps_status[1]
|
|
|
|
self.sDB.rps_trip_cause = rps_status[2]
|
2023-03-05 03:32:13 +00:00
|
|
|
self.sDB.rps_status.high_dmg = rps_status[3]
|
2022-12-07 17:59:21 +00:00
|
|
|
self.sDB.rps_status.high_temp = rps_status[4]
|
2023-03-05 03:19:53 +00:00
|
|
|
self.sDB.rps_status.low_cool = rps_status[5]
|
2022-12-07 17:59:21 +00:00
|
|
|
self.sDB.rps_status.ex_waste = rps_status[6]
|
|
|
|
self.sDB.rps_status.ex_hcool = rps_status[7]
|
|
|
|
self.sDB.rps_status.no_fuel = rps_status[8]
|
|
|
|
self.sDB.rps_status.fault = rps_status[9]
|
|
|
|
self.sDB.rps_status.timeout = rps_status[10]
|
|
|
|
self.sDB.rps_status.manual = rps_status[11]
|
|
|
|
self.sDB.rps_status.automatic = rps_status[12]
|
|
|
|
self.sDB.rps_status.sys_fail = rps_status[13]
|
|
|
|
self.sDB.rps_status.force_dis = rps_status[14]
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- copy in the reactor status
|
|
|
|
---@param mek_data table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _copy_status(mek_data)
|
2022-05-01 21:04:38 +00:00
|
|
|
-- copy status information
|
2022-04-23 01:55:26 +00:00
|
|
|
self.sDB.mek_status.status = mek_data[1]
|
|
|
|
self.sDB.mek_status.burn_rate = mek_data[2]
|
|
|
|
self.sDB.mek_status.act_burn_rate = mek_data[3]
|
|
|
|
self.sDB.mek_status.temp = mek_data[4]
|
|
|
|
self.sDB.mek_status.damage = mek_data[5]
|
|
|
|
self.sDB.mek_status.boil_eff = mek_data[6]
|
|
|
|
self.sDB.mek_status.env_loss = mek_data[7]
|
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- copy container information
|
2022-04-23 01:55:26 +00:00
|
|
|
self.sDB.mek_status.fuel = mek_data[8]
|
2022-04-27 22:52:06 +00:00
|
|
|
self.sDB.mek_status.fuel_fill = mek_data[9]
|
|
|
|
self.sDB.mek_status.waste = mek_data[10]
|
|
|
|
self.sDB.mek_status.waste_fill = mek_data[11]
|
2022-05-11 17:05:20 +00:00
|
|
|
self.sDB.mek_status.ccool_type = mek_data[12]
|
|
|
|
self.sDB.mek_status.ccool_amnt = mek_data[13]
|
|
|
|
self.sDB.mek_status.ccool_fill = mek_data[14]
|
2022-04-27 22:52:06 +00:00
|
|
|
self.sDB.mek_status.hcool_type = mek_data[15]
|
|
|
|
self.sDB.mek_status.hcool_amnt = mek_data[16]
|
|
|
|
self.sDB.mek_status.hcool_fill = mek_data[17]
|
2022-05-01 21:04:38 +00:00
|
|
|
|
|
|
|
-- update computable fields if we have our structure
|
|
|
|
if self.received_struct then
|
|
|
|
self.sDB.mek_status.fuel_need = self.sDB.mek_struct.fuel_cap - self.sDB.mek_status.fuel_fill
|
|
|
|
self.sDB.mek_status.waste_need = self.sDB.mek_struct.waste_cap - self.sDB.mek_status.waste_fill
|
2022-10-03 01:17:13 +00:00
|
|
|
self.sDB.mek_status.cool_need = self.sDB.mek_struct.ccool_cap - self.sDB.mek_status.ccool_fill
|
2022-05-01 21:04:38 +00:00
|
|
|
self.sDB.mek_status.hcool_need = self.sDB.mek_struct.hcool_cap - self.sDB.mek_status.hcool_fill
|
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- copy in the reactor structure
|
|
|
|
---@param mek_data table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _copy_struct(mek_data)
|
2022-10-25 17:29:57 +00:00
|
|
|
self.sDB.mek_struct.length = mek_data[1]
|
|
|
|
self.sDB.mek_struct.width = mek_data[2]
|
|
|
|
self.sDB.mek_struct.height = mek_data[3]
|
|
|
|
self.sDB.mek_struct.min_pos = mek_data[4]
|
|
|
|
self.sDB.mek_struct.max_pos = mek_data[5]
|
|
|
|
self.sDB.mek_struct.heat_cap = mek_data[6]
|
|
|
|
self.sDB.mek_struct.fuel_asm = mek_data[7]
|
|
|
|
self.sDB.mek_struct.fuel_sa = mek_data[8]
|
|
|
|
self.sDB.mek_struct.fuel_cap = mek_data[9]
|
|
|
|
self.sDB.mek_struct.waste_cap = mek_data[10]
|
|
|
|
self.sDB.mek_struct.ccool_cap = mek_data[11]
|
|
|
|
self.sDB.mek_struct.hcool_cap = mek_data[12]
|
|
|
|
self.sDB.mek_struct.max_burn = mek_data[13]
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-16 21:13:54 +00:00
|
|
|
-- mark this PLC session as closed, stop watchdog
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _close()
|
2023-04-18 17:49:59 +00:00
|
|
|
self.conn_watchdog.cancel()
|
2022-05-16 21:13:54 +00:00
|
|
|
self.connected = false
|
2023-05-23 23:22:22 +00:00
|
|
|
databus.tx_plc_disconnected(reactor_id)
|
2022-05-16 21:13:54 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 21:43:23 +00:00
|
|
|
-- send an RPLC packet
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param msg_type RPLC_TYPE
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param msg table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _send(msg_type, msg)
|
2022-05-02 21:43:23 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local r_pkt = comms.rplc_packet()
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
r_pkt.make(reactor_id, msg_type, msg)
|
2023-06-05 21:24:00 +00:00
|
|
|
s_pkt.make(s_addr, self.seq_num, PROTOCOL.RPLC, r_pkt.raw_sendable())
|
2022-05-02 21:43:23 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_packet(s_pkt)
|
2022-05-02 21:43:23 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- send a SCADA management packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param msg_type MGMT_TYPE
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param msg table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _send_mgmt(msg_type, msg)
|
2022-05-02 21:43:23 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
|
|
|
m_pkt.make(msg_type, msg)
|
2023-06-05 21:24:00 +00:00
|
|
|
s_pkt.make(s_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-05-02 21:43:23 +00:00
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_packet(s_pkt)
|
2022-05-02 21:43:23 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get an ACK status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param pkt rplc_frame
|
|
|
|
---@return boolean|nil ack
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _get_ack(pkt)
|
2022-04-23 01:55:26 +00:00
|
|
|
if pkt.length == 1 then
|
|
|
|
return pkt.data[1]
|
2022-04-22 15:07:59 +00:00
|
|
|
else
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug(log_header .. "RPLC ACK length mismatch")
|
2022-04-22 15:07:59 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- handle a packet
|
2023-04-17 23:48:03 +00:00
|
|
|
---@param pkt mgmt_frame|rplc_frame
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _handle_packet(pkt)
|
2022-04-23 16:12:33 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.r_seq_num == nil then
|
2022-05-02 15:42:24 +00:00
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
2023-04-21 21:10:15 +00:00
|
|
|
elseif (self.r_seq_num + 1) ~= pkt.scada_frame.seq_num() then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(log_header .. "sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. pkt.scada_frame.seq_num())
|
2022-05-02 15:42:24 +00:00
|
|
|
return
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
2022-05-02 15:42:24 +00:00
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- process packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if pkt.scada_frame.protocol() == PROTOCOL.RPLC then
|
2023-04-17 23:48:03 +00:00
|
|
|
---@cast pkt rplc_frame
|
2022-05-02 15:42:24 +00:00
|
|
|
-- check reactor ID
|
2023-02-25 04:36:16 +00:00
|
|
|
if pkt.id ~= reactor_id then
|
2023-04-19 13:30:17 +00:00
|
|
|
log.warning(log_header .. "discarding RPLC packet with ID not matching reactor ID: reactor " .. reactor_id .. " != " .. pkt.id)
|
2022-05-02 15:42:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-04-23 16:12:33 +00:00
|
|
|
-- feed watchdog
|
2023-04-18 17:49:59 +00:00
|
|
|
self.conn_watchdog.feed()
|
2022-04-23 16:12:33 +00:00
|
|
|
|
|
|
|
-- handle packet by type
|
2023-02-21 16:05:57 +00:00
|
|
|
if pkt.type == RPLC_TYPE.STATUS then
|
2022-04-23 16:12:33 +00:00
|
|
|
-- status packet received, update data
|
2023-02-04 18:47:00 +00:00
|
|
|
if pkt.length >= 5 then
|
2023-07-08 22:07:40 +00:00
|
|
|
if (type(pkt.data[1]) == "number") and (type(pkt.data[2]) == "boolean") and (type(pkt.data[3]) == "boolean") and
|
|
|
|
(type(pkt.data[4]) == "boolean") and (type(pkt.data[5]) == "number") then
|
|
|
|
self.sDB.last_status_update = pkt.data[1]
|
|
|
|
self.sDB.control_state = pkt.data[2]
|
|
|
|
self.sDB.no_reactor = pkt.data[3]
|
|
|
|
self.sDB.formed = pkt.data[4]
|
|
|
|
self.sDB.auto_ack_token = pkt.data[5]
|
|
|
|
|
|
|
|
if (not self.sDB.no_reactor) and self.sDB.formed and (type(pkt.data[6]) == "number") then
|
|
|
|
self.sDB.mek_status.heating_rate = pkt.data[6] or 0.0
|
|
|
|
|
|
|
|
-- attempt to read mek_data table
|
|
|
|
if type(pkt.data[7]) == "table" then
|
|
|
|
local status = pcall(_copy_status, pkt.data[7])
|
|
|
|
if status then
|
|
|
|
-- copied in status data OK
|
|
|
|
self.received_status_cache = true
|
|
|
|
else
|
|
|
|
-- error copying status data
|
|
|
|
log.error(log_header .. "failed to parse status packet data")
|
|
|
|
end
|
2022-10-25 17:29:57 +00:00
|
|
|
end
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2023-07-08 22:07:40 +00:00
|
|
|
else
|
|
|
|
log.debug(log_header .. "RPLC status packet invalid")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug(log_header .. "RPLC status packet length mismatch")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.MEK_STRUCT then
|
2022-04-23 16:12:33 +00:00
|
|
|
-- received reactor structure, record it
|
2022-09-30 21:33:35 +00:00
|
|
|
if pkt.length == 14 then
|
2022-05-02 15:42:24 +00:00
|
|
|
local status = pcall(_copy_struct, pkt.data)
|
2022-04-23 16:12:33 +00:00
|
|
|
if status then
|
|
|
|
-- copied in structure data OK
|
2024-04-30 02:03:54 +00:00
|
|
|
_compute_op_temps()
|
2022-05-02 21:43:23 +00:00
|
|
|
self.received_struct = true
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.PLC_BUILD_CHANGED, reactor_id)
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
|
|
|
-- error copying structure data
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error(log_header .. "failed to parse struct packet data")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug(log_header .. "RPLC struct packet length mismatch")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.MEK_BURN_RATE then
|
2022-05-05 17:14:14 +00:00
|
|
|
-- burn rate acknowledgement
|
2022-05-02 15:42:24 +00:00
|
|
|
local ack = _get_ack(pkt)
|
2022-04-23 16:12:33 +00:00
|
|
|
if ack then
|
2022-05-05 17:14:14 +00:00
|
|
|
self.acks.burn_rate = true
|
2022-04-23 16:12:33 +00:00
|
|
|
elseif ack == false then
|
2022-05-05 17:14:14 +00:00
|
|
|
log.debug(log_header .. "burn rate update failed!")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2022-10-20 16:22:03 +00:00
|
|
|
|
|
|
|
-- send acknowledgement to coordinator
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.CRDN_ACK, {
|
|
|
|
unit = reactor_id,
|
2023-02-21 16:05:57 +00:00
|
|
|
cmd = UNIT_COMMAND.SET_BURN,
|
2022-10-20 16:22:03 +00:00
|
|
|
ack = ack
|
|
|
|
})
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_ENABLE then
|
2022-04-23 16:12:33 +00:00
|
|
|
-- enable acknowledgement
|
2022-05-02 15:42:24 +00:00
|
|
|
local ack = _get_ack(pkt)
|
2022-04-23 16:12:33 +00:00
|
|
|
if ack then
|
|
|
|
self.sDB.control_state = true
|
|
|
|
elseif ack == false then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug(log_header .. "enable failed!")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2022-10-20 16:22:03 +00:00
|
|
|
|
|
|
|
-- send acknowledgement to coordinator
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.CRDN_ACK, {
|
|
|
|
unit = reactor_id,
|
2023-02-21 16:05:57 +00:00
|
|
|
cmd = UNIT_COMMAND.START,
|
2022-10-20 16:22:03 +00:00
|
|
|
ack = ack
|
|
|
|
})
|
2023-10-04 21:28:14 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_DISABLE then
|
|
|
|
-- disable acknowledgement
|
|
|
|
local ack = _get_ack(pkt)
|
|
|
|
if ack then
|
|
|
|
self.acks.disable = true
|
|
|
|
self.sDB.control_state = false
|
|
|
|
elseif ack == false then
|
|
|
|
log.debug(log_header .. "disable failed!")
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_SCRAM then
|
2022-11-11 21:15:44 +00:00
|
|
|
-- manual SCRAM acknowledgement
|
2022-05-02 15:42:24 +00:00
|
|
|
local ack = _get_ack(pkt)
|
|
|
|
if ack then
|
2022-05-05 17:14:14 +00:00
|
|
|
self.acks.scram = true
|
|
|
|
self.sDB.control_state = false
|
2022-05-02 15:42:24 +00:00
|
|
|
elseif ack == false then
|
2022-11-11 21:15:44 +00:00
|
|
|
log.debug(log_header .. "manual SCRAM failed!")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2022-10-20 16:22:03 +00:00
|
|
|
|
|
|
|
-- send acknowledgement to coordinator
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.CRDN_ACK, {
|
|
|
|
unit = reactor_id,
|
2023-02-21 16:05:57 +00:00
|
|
|
cmd = UNIT_COMMAND.SCRAM,
|
2022-10-20 16:22:03 +00:00
|
|
|
ack = ack
|
|
|
|
})
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_ASCRAM then
|
2022-11-11 21:15:44 +00:00
|
|
|
-- automatic SCRAM acknowledgement
|
|
|
|
local ack = _get_ack(pkt)
|
|
|
|
if ack then
|
2023-02-13 23:08:32 +00:00
|
|
|
self.acks.ascram = true
|
2022-11-11 21:15:44 +00:00
|
|
|
self.sDB.control_state = false
|
|
|
|
elseif ack == false then
|
|
|
|
log.debug(log_header .. " automatic SCRAM failed!")
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_STATUS then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS status packet received, copy data
|
2022-12-07 17:59:21 +00:00
|
|
|
if pkt.length == 14 then
|
2022-05-05 15:55:04 +00:00
|
|
|
local status = pcall(_copy_rps_status, pkt.data)
|
2022-04-23 16:12:33 +00:00
|
|
|
if status then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- copied in RPS status data OK
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
2022-05-05 15:55:04 +00:00
|
|
|
-- error copying RPS status data
|
|
|
|
log.error(log_header .. "failed to parse RPS status packet data")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-05 15:55:04 +00:00
|
|
|
log.debug(log_header .. "RPLC RPS status packet length mismatch")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_ALARM then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS alarm
|
2022-11-11 21:46:38 +00:00
|
|
|
if pkt.length == 13 then
|
2022-12-07 17:59:21 +00:00
|
|
|
local status = pcall(_copy_rps_status, { true, table.unpack(pkt.data) })
|
2022-04-23 16:12:33 +00:00
|
|
|
if status then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- copied in RPS status data OK
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
2022-05-05 15:55:04 +00:00
|
|
|
-- error copying RPS status data
|
|
|
|
log.error(log_header .. "failed to parse RPS alarm status data")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-05 15:55:04 +00:00
|
|
|
log.debug(log_header .. "RPLC RPS alarm packet length mismatch")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_RESET then
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS reset acknowledgement
|
2022-05-02 15:42:24 +00:00
|
|
|
local ack = _get_ack(pkt)
|
|
|
|
if ack then
|
2022-10-07 15:43:18 +00:00
|
|
|
self.acks.rps_reset = true
|
2022-05-05 15:55:04 +00:00
|
|
|
self.sDB.rps_tripped = false
|
|
|
|
self.sDB.rps_trip_cause = "ok"
|
2022-05-02 15:42:24 +00:00
|
|
|
elseif ack == false then
|
2022-05-05 15:55:04 +00:00
|
|
|
log.debug(log_header .. "RPS reset failed")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2022-10-20 16:22:03 +00:00
|
|
|
|
|
|
|
-- send acknowledgement to coordinator
|
2023-02-25 04:36:16 +00:00
|
|
|
out_queue.push_data(svqtypes.SV_Q_DATA.CRDN_ACK, {
|
|
|
|
unit = reactor_id,
|
2023-02-21 16:05:57 +00:00
|
|
|
cmd = UNIT_COMMAND.RESET_RPS,
|
2022-10-20 16:22:03 +00:00
|
|
|
ack = ack
|
|
|
|
})
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.RPS_AUTO_RESET then
|
2023-02-07 05:32:50 +00:00
|
|
|
-- RPS auto control reset acknowledgement
|
|
|
|
local ack = _get_ack(pkt)
|
2023-02-13 23:08:32 +00:00
|
|
|
if not ack then
|
2023-02-07 05:32:50 +00:00
|
|
|
log.debug(log_header .. "RPS auto reset failed")
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.type == RPLC_TYPE.AUTO_BURN_RATE then
|
2023-01-26 23:26:26 +00:00
|
|
|
if pkt.length == 1 then
|
|
|
|
local ack = pkt.data[1]
|
|
|
|
|
|
|
|
if ack == PLC_AUTO_ACK.FAIL then
|
2023-02-07 05:32:50 +00:00
|
|
|
self.acks.burn_rate = false
|
|
|
|
log.debug(log_header .. "RPLC automatic burn rate set fail")
|
|
|
|
elseif ack == PLC_AUTO_ACK.DIRECT_SET_OK or ack == PLC_AUTO_ACK.RAMP_SET_OK or ack == PLC_AUTO_ACK.ZERO_DIS_OK then
|
|
|
|
self.acks.burn_rate = true
|
|
|
|
else
|
|
|
|
self.acks.burn_rate = false
|
|
|
|
log.debug(log_header .. "RPLC automatic burn rate ack unknown")
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug(log_header .. "RPLC automatic burn rate ack packet length mismatch")
|
|
|
|
end
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug(log_header .. "handler received unsupported RPLC packet type " .. pkt.type)
|
2022-05-02 15:42:24 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif pkt.scada_frame.protocol() == PROTOCOL.SCADA_MGMT then
|
2023-04-17 23:48:03 +00:00
|
|
|
---@cast pkt mgmt_frame
|
2023-08-30 20:45:48 +00:00
|
|
|
if pkt.type == MGMT_TYPE.KEEP_ALIVE then
|
2022-05-09 19:00:16 +00:00
|
|
|
-- keep alive reply
|
|
|
|
if pkt.length == 2 then
|
|
|
|
local srv_start = pkt.data[1]
|
2023-04-12 20:02:29 +00:00
|
|
|
-- local plc_send = pkt.data[2]
|
2022-05-09 19:00:16 +00:00
|
|
|
local srv_now = util.time()
|
|
|
|
self.last_rtt = srv_now - srv_start
|
|
|
|
|
2023-02-26 19:49:16 +00:00
|
|
|
if self.last_rtt > 750 then
|
|
|
|
log.warning(log_header .. "PLC KEEP_ALIVE round trip time > 750ms (" .. self.last_rtt .. "ms)")
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 15:16:25 +00:00
|
|
|
-- log.debug(log_header .. "PLC RTT = " .. self.last_rtt .. "ms")
|
|
|
|
-- log.debug(log_header .. "PLC TT = " .. (srv_now - plc_send) .. "ms")
|
2023-05-23 23:22:22 +00:00
|
|
|
|
|
|
|
databus.tx_plc_rtt(reactor_id, self.last_rtt)
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
|
|
|
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif pkt.type == MGMT_TYPE.CLOSE then
|
2022-05-02 15:42:24 +00:00
|
|
|
-- close the session
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2022-05-02 15:42:24 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type)
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get the session ID
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-02-13 17:27:22 +00:00
|
|
|
function public.get_id() return id end
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get the session database
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_db() return self.sDB end
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2023-02-04 18:47:00 +00:00
|
|
|
-- check if ramping is completed by first verifying auto command token ack
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-02-04 18:47:00 +00:00
|
|
|
function public.is_ramp_complete()
|
|
|
|
return (self.sDB.auto_ack_token == self.auto_cmd_token) and (self.commanded_burn_rate == self.sDB.mek_status.act_burn_rate)
|
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get the reactor structure
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@return mek_struct|table struct struct or empty table
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_struct()
|
2022-04-22 15:07:59 +00:00
|
|
|
if self.received_struct then
|
|
|
|
return self.sDB.mek_struct
|
|
|
|
else
|
2022-11-02 17:45:52 +00:00
|
|
|
return {}
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- get the reactor status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
|
|
|
---@return mek_status|table struct status or empty table
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_status()
|
2022-05-03 21:25:31 +00:00
|
|
|
if self.received_status_cache then
|
|
|
|
return self.sDB.mek_status
|
|
|
|
else
|
2022-11-02 17:45:52 +00:00
|
|
|
return {}
|
2022-05-03 21:25:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-21 16:24:43 +00:00
|
|
|
-- get the reactor RPS status
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_rps()
|
2022-05-21 16:24:43 +00:00
|
|
|
return self.sDB.rps_status
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get the general status information
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_general_status()
|
2022-05-21 16:24:43 +00:00
|
|
|
return {
|
2022-09-03 14:50:14 +00:00
|
|
|
self.sDB.last_status_update,
|
|
|
|
self.sDB.control_state,
|
|
|
|
self.sDB.rps_tripped,
|
2022-09-08 18:49:01 +00:00
|
|
|
self.sDB.rps_trip_cause,
|
2022-10-25 17:29:57 +00:00
|
|
|
self.sDB.no_reactor,
|
|
|
|
self.sDB.formed
|
2022-05-21 16:24:43 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
-- lock out some manual operator actions during automatic control
|
|
|
|
---@param engage boolean true to engage the lockout
|
|
|
|
function public.auto_lock(engage)
|
|
|
|
self.auto_lock = engage
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
-- stop retrying a burn rate command
|
|
|
|
if engage then
|
|
|
|
self.acks.burn_rate = true
|
|
|
|
end
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
2023-10-14 22:49:51 +00:00
|
|
|
-- check if the manual lockout for automatic control is active
|
2023-10-14 21:57:50 +00:00
|
|
|
function public.is_auto_locked() return self.auto_lock end
|
|
|
|
|
2023-01-03 21:50:31 +00:00
|
|
|
-- set the burn rate on behalf of automatic control
|
|
|
|
---@param rate number burn rate
|
|
|
|
---@param ramp boolean true to ramp, false to not
|
|
|
|
function public.auto_set_burn(rate, ramp)
|
|
|
|
self.ramping_rate = ramp
|
2023-02-25 04:36:16 +00:00
|
|
|
in_queue.push_data(PLC_S_DATA.AUTO_BURN_RATE, rate)
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
|
2022-05-03 21:25:31 +00:00
|
|
|
-- check if a timer matches this session's watchdog
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.check_wd(timer)
|
2023-04-18 17:49:59 +00:00
|
|
|
return self.conn_watchdog.is_timer(timer) and self.connected
|
2022-05-03 21:25:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.close()
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_mgmt(MGMT_TYPE.CLOSE, {})
|
2023-02-25 04:36:16 +00:00
|
|
|
println("connection to reactor " .. reactor_id .. " PLC closed by server")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info(log_header .. "session closed by server")
|
2022-05-03 21:25:31 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- iterate the session
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-11 16:31:19 +00:00
|
|
|
---@return boolean connected
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.iterate()
|
2022-04-23 16:12:33 +00:00
|
|
|
if self.connected then
|
|
|
|
------------------
|
|
|
|
-- handle queue --
|
|
|
|
------------------
|
|
|
|
|
2022-04-27 22:52:06 +00:00
|
|
|
local handle_start = util.time()
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
while in_queue.ready() and self.connected do
|
2022-04-23 16:12:33 +00:00
|
|
|
-- get a new message to process
|
2023-02-25 04:36:16 +00:00
|
|
|
local message = in_queue.pop()
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
if message ~= nil then
|
|
|
|
if message.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- handle a packet
|
|
|
|
_handle_packet(message.message)
|
|
|
|
elseif message.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction
|
|
|
|
local cmd = message.message
|
|
|
|
if cmd == PLC_S_CMDS.ENABLE then
|
|
|
|
-- enable reactor
|
2024-05-15 00:10:21 +00:00
|
|
|
self.acks.disable = true
|
2023-01-03 21:50:31 +00:00
|
|
|
if not self.auto_lock then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_ENABLE, {})
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2023-09-19 20:37:15 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.DISABLE then
|
|
|
|
-- disable the reactor
|
|
|
|
self.acks.disable = false
|
|
|
|
self.retry_times.disable_req = util.time() + INITIAL_WAIT
|
|
|
|
_send(RPLC_TYPE.RPS_DISABLE, {})
|
2022-05-11 16:31:19 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.SCRAM then
|
|
|
|
-- SCRAM reactor
|
|
|
|
self.acks.scram = false
|
|
|
|
self.retry_times.scram_req = util.time() + INITIAL_WAIT
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_SCRAM, {})
|
2022-11-11 21:15:44 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.ASCRAM then
|
|
|
|
-- SCRAM reactor
|
2023-02-13 23:08:32 +00:00
|
|
|
self.acks.ascram = false
|
|
|
|
self.retry_times.ascram_req = util.time() + INITIAL_WAIT
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_ASCRAM, {})
|
2022-05-11 16:31:19 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.RPS_RESET then
|
|
|
|
-- reset RPS
|
2023-02-13 23:08:32 +00:00
|
|
|
self.acks.ascram = true
|
2022-05-11 16:31:19 +00:00
|
|
|
self.acks.rps_reset = false
|
|
|
|
self.retry_times.rps_reset_req = util.time() + INITIAL_WAIT
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_RESET, {})
|
2023-02-07 05:32:50 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.RPS_AUTO_RESET then
|
2023-02-13 23:08:32 +00:00
|
|
|
if self.sDB.rps_status.automatic or self.sDB.rps_status.timeout then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_AUTO_RESET, {})
|
2023-02-07 05:32:50 +00:00
|
|
|
end
|
2023-01-26 23:26:26 +00:00
|
|
|
else
|
2023-04-19 13:30:17 +00:00
|
|
|
log.error(log_header .. "unsupported command received in in_queue (this is a bug)", true)
|
2022-05-11 16:31:19 +00:00
|
|
|
end
|
|
|
|
elseif message.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction with body
|
2022-10-06 17:54:52 +00:00
|
|
|
local cmd = message.message ---@type queue_data
|
2022-05-11 16:31:19 +00:00
|
|
|
if cmd.key == PLC_S_DATA.BURN_RATE then
|
|
|
|
-- update burn rate
|
2023-01-03 21:50:31 +00:00
|
|
|
if not self.auto_lock then
|
|
|
|
cmd.val = math.floor(cmd.val * 10) / 10 -- round to 10ths place
|
|
|
|
if cmd.val > 0 and cmd.val <= self.sDB.mek_struct.max_burn then
|
|
|
|
self.commanded_burn_rate = cmd.val
|
2023-02-04 18:47:00 +00:00
|
|
|
self.auto_cmd_token = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
self.ramping_rate = false
|
|
|
|
self.acks.burn_rate = false
|
|
|
|
self.retry_times.burn_rate_req = util.time() + INITIAL_WAIT
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
2022-10-07 15:28:56 +00:00
|
|
|
end
|
2022-05-11 16:31:19 +00:00
|
|
|
elseif cmd.key == PLC_S_DATA.RAMP_BURN_RATE then
|
|
|
|
-- ramp to burn rate
|
2023-01-03 21:50:31 +00:00
|
|
|
if not self.auto_lock then
|
|
|
|
cmd.val = math.floor(cmd.val * 10) / 10 -- round to 10ths place
|
|
|
|
if cmd.val > 0 and cmd.val <= self.sDB.mek_struct.max_burn then
|
|
|
|
self.commanded_burn_rate = cmd.val
|
2023-02-04 18:47:00 +00:00
|
|
|
self.auto_cmd_token = 0
|
2023-01-03 21:50:31 +00:00
|
|
|
self.ramping_rate = true
|
|
|
|
self.acks.burn_rate = false
|
2024-05-15 00:10:21 +00:00
|
|
|
self.acks.disable = true
|
2023-01-03 21:50:31 +00:00
|
|
|
self.retry_times.burn_rate_req = util.time() + INITIAL_WAIT
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
2023-01-03 21:50:31 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif cmd.key == PLC_S_DATA.AUTO_BURN_RATE then
|
|
|
|
-- set automatic burn rate
|
2023-01-26 23:26:26 +00:00
|
|
|
if self.auto_lock then
|
2024-05-15 00:10:21 +00:00
|
|
|
cmd.val = math.floor(cmd.val * 100) / 100 -- round to 100ths place
|
2023-02-04 18:47:00 +00:00
|
|
|
if cmd.val >= 0 and cmd.val <= self.sDB.mek_struct.max_burn then
|
|
|
|
self.auto_cmd_token = util.time_ms()
|
2023-01-26 23:26:26 +00:00
|
|
|
self.commanded_burn_rate = cmd.val
|
|
|
|
|
|
|
|
-- this is only for manual control, only retry auto ramps
|
|
|
|
self.acks.burn_rate = not self.ramping_rate
|
2024-05-15 00:10:21 +00:00
|
|
|
self.acks.disable = true
|
2023-01-26 23:26:26 +00:00
|
|
|
self.retry_times.burn_rate_req = util.time() + INITIAL_AUTO_WAIT
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.AUTO_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate, self.auto_cmd_token })
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
2022-10-07 15:28:56 +00:00
|
|
|
end
|
2023-01-26 23:26:26 +00:00
|
|
|
else
|
2023-04-19 13:30:17 +00:00
|
|
|
log.error(log_header .. "unsupported data command received in in_queue (this is a bug)", true)
|
2022-05-11 16:31:19 +00:00
|
|
|
end
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2022-04-27 22:52:06 +00:00
|
|
|
|
|
|
|
-- max 100ms spent processing queue
|
|
|
|
if util.time() - handle_start > 100 then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(log_header .. "exceeded 100ms queue process limit")
|
2022-04-27 22:52:06 +00:00
|
|
|
break
|
|
|
|
end
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- exit if connection was closed
|
|
|
|
if not self.connected then
|
2023-02-25 04:36:16 +00:00
|
|
|
println("connection to reactor " .. reactor_id .. " PLC closed by remote host")
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info(log_header .. "session closed by remote host")
|
2022-05-02 21:43:23 +00:00
|
|
|
return self.connected
|
2022-05-02 15:42:24 +00:00
|
|
|
end
|
|
|
|
|
2022-04-23 16:12:33 +00:00
|
|
|
----------------------
|
|
|
|
-- update periodics --
|
|
|
|
----------------------
|
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
local elapsed = util.time() - self.periodics.last_update
|
|
|
|
|
|
|
|
local periodics = self.periodics
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- keep alive
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
periodics.keep_alive = periodics.keep_alive + elapsed
|
|
|
|
if periodics.keep_alive >= PERIODICS.KEEP_ALIVE then
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_mgmt(MGMT_TYPE.KEEP_ALIVE, { util.time() })
|
2022-05-01 21:04:38 +00:00
|
|
|
periodics.keep_alive = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
self.periodics.last_update = util.time()
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
-- attempt retries --
|
|
|
|
---------------------
|
|
|
|
|
2022-05-02 21:43:23 +00:00
|
|
|
local rtimes = self.retry_times
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if (not self.sDB.no_reactor) and self.sDB.formed then
|
|
|
|
-- struct request retry
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if not self.received_struct then
|
|
|
|
if rtimes.struct_req - util.time() <= 0 then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_STRUCT, {})
|
2022-10-25 17:29:57 +00:00
|
|
|
rtimes.struct_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- status cache request retry
|
2022-05-03 21:25:31 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if not self.received_status_cache then
|
|
|
|
if rtimes.status_req - util.time() <= 0 then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_STATUS, {})
|
2022-10-25 17:29:57 +00:00
|
|
|
rtimes.status_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
2022-05-03 21:25:31 +00:00
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- burn rate request retry
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if not self.acks.burn_rate then
|
|
|
|
if rtimes.burn_rate_req - util.time() <= 0 then
|
2023-02-04 18:47:00 +00:00
|
|
|
if self.auto_cmd_token > 0 then
|
|
|
|
if self.auto_lock then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.AUTO_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate, self.auto_cmd_token })
|
2023-02-04 18:47:00 +00:00
|
|
|
else
|
|
|
|
-- would have been an auto command, but disengaged, so stop retrying
|
|
|
|
self.acks.burn_rate = true
|
|
|
|
end
|
|
|
|
elseif not self.auto_lock then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
2023-02-04 18:47:00 +00:00
|
|
|
else
|
|
|
|
-- shouldn't be in this state, just pretend it was acknowledged
|
|
|
|
self.acks.burn_rate = true
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
rtimes.burn_rate_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2023-09-19 20:37:15 +00:00
|
|
|
-- reactor disable request retry
|
|
|
|
|
|
|
|
if not self.acks.disable then
|
2023-10-04 21:28:14 +00:00
|
|
|
if rtimes.disable_req - util.time() <= 0 then
|
2023-09-19 20:37:15 +00:00
|
|
|
_send(RPLC_TYPE.RPS_DISABLE, {})
|
|
|
|
rtimes.disable_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
-- SCRAM request retry
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-10-25 17:29:57 +00:00
|
|
|
if not self.acks.scram then
|
2023-10-04 21:28:14 +00:00
|
|
|
if rtimes.scram_req - util.time() <= 0 then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_SCRAM, {})
|
2022-10-25 17:29:57 +00:00
|
|
|
rtimes.scram_req = util.time() + RETRY_PERIOD
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 23:08:32 +00:00
|
|
|
-- automatic SCRAM request retry
|
|
|
|
|
|
|
|
if not self.acks.ascram then
|
|
|
|
if rtimes.ascram_req - util.time() <= 0 then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_ASCRAM, {})
|
2023-02-13 23:08:32 +00:00
|
|
|
rtimes.ascram_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
-- RPS reset request retry
|
2022-05-01 21:04:38 +00:00
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
if not self.acks.rps_reset then
|
|
|
|
if rtimes.rps_reset_req - util.time() <= 0 then
|
2023-02-21 16:05:57 +00:00
|
|
|
_send(RPLC_TYPE.RPS_RESET, {})
|
2022-05-05 15:55:04 +00:00
|
|
|
rtimes.rps_reset_req = util.time() + RETRY_PERIOD
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return self.connected
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
return public
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return plc
|