2022-05-04 17:37:01 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local mqueue = require("scada-common.mqueue")
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local plc = {}
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2022-04-23 16:12:33 +00:00
|
|
|
local PROTOCOLS = comms.PROTOCOLS
|
2022-04-22 15:07:59 +00:00
|
|
|
local RPLC_TYPES = comms.RPLC_TYPES
|
2022-05-02 21:43:23 +00:00
|
|
|
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2022-05-03 21:25:31 +00:00
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
|
|
|
local print_ts = util.print_ts
|
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- retry time constants in ms
|
|
|
|
local INITIAL_WAIT = 1500
|
|
|
|
local RETRY_PERIOD = 1000
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local PLC_S_CMDS = {
|
2022-04-22 15:07:59 +00:00
|
|
|
SCRAM = 0,
|
|
|
|
ENABLE = 1,
|
2022-05-05 20:00:49 +00:00
|
|
|
RPS_RESET = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
local PLC_S_DATA = {
|
|
|
|
BURN_RATE = 1,
|
|
|
|
RAMP_BURN_RATE = 2
|
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-04-27 22:52:06 +00:00
|
|
|
KEEP_ALIVE = 2.0
|
2022-04-23 16:12:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
-- PLC supervisor session
|
2022-05-04 17:37:01 +00:00
|
|
|
plc.new_session = function (id, for_reactor, in_queue, out_queue)
|
2022-04-22 15:07:59 +00:00
|
|
|
local log_header = "plc_session(" .. id .. "): "
|
|
|
|
|
|
|
|
local self = {
|
|
|
|
id = id,
|
|
|
|
for_reactor = for_reactor,
|
|
|
|
in_q = in_queue,
|
|
|
|
out_q = out_queue,
|
|
|
|
commanded_state = false,
|
2022-05-01 21:04:38 +00:00
|
|
|
commanded_burn_rate = 0.0,
|
2022-05-05 20:00:49 +00:00
|
|
|
ramping_rate = 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,
|
2022-04-23 16:12:33 +00:00
|
|
|
plc_conn_watchdog = util.new_watchdog(3),
|
|
|
|
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),
|
2022-04-22 15:07:59 +00:00
|
|
|
scram_req = 0,
|
2022-05-01 21:04:38 +00:00
|
|
|
enable_req = 0,
|
|
|
|
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 = {
|
|
|
|
scram = true,
|
|
|
|
enable = true,
|
|
|
|
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-04-22 15:07:59 +00:00
|
|
|
sDB = {
|
2022-04-24 00:46:01 +00:00
|
|
|
last_status_update = 0,
|
2022-04-22 15:07:59 +00:00
|
|
|
control_state = false,
|
|
|
|
overridden = false,
|
|
|
|
degraded = false,
|
2022-05-05 15:55:04 +00:00
|
|
|
rps_tripped = false,
|
|
|
|
rps_trip_cause = "ok",
|
|
|
|
rps_status = {
|
2022-04-22 15:07:59 +00:00
|
|
|
dmg_crit = false,
|
|
|
|
ex_hcool = false,
|
|
|
|
ex_waste = false,
|
|
|
|
high_temp = false,
|
|
|
|
no_fuel = false,
|
|
|
|
no_cool = false,
|
|
|
|
timed_out = false
|
|
|
|
},
|
|
|
|
mek_status = {
|
|
|
|
heating_rate = 0,
|
|
|
|
|
|
|
|
status = false,
|
|
|
|
burn_rate = 0,
|
|
|
|
act_burn_rate = 0,
|
|
|
|
temp = 0,
|
|
|
|
damage = 0,
|
|
|
|
boil_eff = 0,
|
|
|
|
env_loss = 0,
|
|
|
|
|
|
|
|
fuel = 0,
|
|
|
|
fuel_need = 0,
|
|
|
|
fuel_fill = 0,
|
|
|
|
waste = 0,
|
|
|
|
waste_need = 0,
|
|
|
|
waste_fill = 0,
|
|
|
|
cool_type = "?",
|
|
|
|
cool_amnt = 0,
|
|
|
|
cool_need = 0,
|
|
|
|
cool_fill = 0,
|
|
|
|
hcool_type = "?",
|
|
|
|
hcool_amnt = 0,
|
|
|
|
hcool_need = 0,
|
|
|
|
hcool_fill = 0
|
|
|
|
},
|
|
|
|
mek_struct = {
|
|
|
|
heat_cap = 0,
|
|
|
|
fuel_asm = 0,
|
|
|
|
fuel_sa = 0,
|
|
|
|
fuel_cap = 0,
|
|
|
|
waste_cap = 0,
|
|
|
|
cool_cap = 0,
|
|
|
|
hcool_cap = 0,
|
|
|
|
max_burn = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:55:04 +00:00
|
|
|
local _copy_rps_status = function (rps_status)
|
|
|
|
self.sDB.rps_status.dmg_crit = rps_status[1]
|
|
|
|
self.sDB.rps_status.ex_hcool = rps_status[2]
|
|
|
|
self.sDB.rps_status.ex_waste = rps_status[3]
|
|
|
|
self.sDB.rps_status.high_temp = rps_status[4]
|
|
|
|
self.sDB.rps_status.no_fuel = rps_status[5]
|
|
|
|
self.sDB.rps_status.no_cool = rps_status[6]
|
|
|
|
self.sDB.rps_status.timed_out = rps_status[7]
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-04-23 01:55:26 +00:00
|
|
|
local _copy_status = function (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]
|
|
|
|
self.sDB.mek_status.cool_type = mek_data[12]
|
|
|
|
self.sDB.mek_status.cool_amnt = mek_data[13]
|
|
|
|
self.sDB.mek_status.cool_fill = mek_data[14]
|
|
|
|
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
|
|
|
|
self.sDB.mek_status.cool_need = self.sDB.mek_struct.cool_cap - self.sDB.mek_status.cool_fill
|
|
|
|
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
|
|
|
|
|
|
|
|
local _copy_struct = function (mek_data)
|
2022-04-23 01:55:26 +00:00
|
|
|
self.sDB.mek_struct.heat_cap = mek_data[1]
|
|
|
|
self.sDB.mek_struct.fuel_asm = mek_data[2]
|
|
|
|
self.sDB.mek_struct.fuel_sa = mek_data[3]
|
|
|
|
self.sDB.mek_struct.fuel_cap = mek_data[4]
|
|
|
|
self.sDB.mek_struct.waste_cap = mek_data[5]
|
|
|
|
self.sDB.mek_struct.cool_cap = mek_data[6]
|
|
|
|
self.sDB.mek_struct.hcool_cap = mek_data[7]
|
|
|
|
self.sDB.mek_struct.max_burn = mek_data[8]
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
2022-05-02 21:43:23 +00:00
|
|
|
-- send an RPLC packet
|
|
|
|
local _send = function (msg_type, msg)
|
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local r_pkt = comms.rplc_packet()
|
|
|
|
|
|
|
|
r_pkt.make(self.id, msg_type, msg)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.RPLC, r_pkt.raw_sendable())
|
|
|
|
|
|
|
|
self.out_q.push_packet(s_pkt)
|
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- send a SCADA management packet
|
|
|
|
local _send_mgmt = function (msg_type, msg)
|
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
|
|
|
m_pkt.make(msg_type, msg)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.SCADA_MGMT, m_pkt.raw_sendable())
|
|
|
|
|
|
|
|
self.out_q.push_packet(s_pkt)
|
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get an ACK status
|
2022-04-22 15:07:59 +00:00
|
|
|
local _get_ack = function (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
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(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
|
|
|
|
local _handle_packet = function (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()
|
|
|
|
elseif self.r_seq_num >= 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
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.scada_frame.protocol() == PROTOCOLS.RPLC then
|
|
|
|
-- check reactor ID
|
|
|
|
if pkt.id ~= for_reactor then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning(log_header .. "RPLC packet with ID not matching reactor ID: reactor " .. self.for_reactor .. " != " .. pkt.id)
|
2022-05-02 15:42:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-04-23 16:12:33 +00:00
|
|
|
-- feed watchdog
|
|
|
|
self.plc_conn_watchdog.feed()
|
|
|
|
|
|
|
|
-- handle packet by type
|
2022-05-09 19:00:16 +00:00
|
|
|
if pkt.type == RPLC_TYPES.STATUS then
|
2022-04-23 16:12:33 +00:00
|
|
|
-- status packet received, update data
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.length >= 5 then
|
|
|
|
self.sDB.last_status_update = pkt.data[1]
|
|
|
|
self.sDB.control_state = pkt.data[2]
|
|
|
|
self.sDB.overridden = pkt.data[3]
|
|
|
|
self.sDB.degraded = pkt.data[4]
|
|
|
|
self.sDB.mek_status.heating_rate = pkt.data[5]
|
2022-04-23 16:12:33 +00:00
|
|
|
|
|
|
|
-- attempt to read mek_data table
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.data[6] ~= nil then
|
|
|
|
local status = pcall(_copy_status, pkt.data[6])
|
2022-04-23 16:12:33 +00:00
|
|
|
if status then
|
|
|
|
-- copied in status data OK
|
2022-05-03 21:25:31 +00:00
|
|
|
self.received_status_cache = true
|
2022-04-23 16:12:33 +00:00
|
|
|
else
|
|
|
|
-- error copying status data
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error(log_header .. "failed to parse status packet data")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
|
|
|
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
|
2022-05-02 15:42:24 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.MEK_STRUCT then
|
2022-04-23 16:12:33 +00:00
|
|
|
-- received reactor structure, record it
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.length == 8 then
|
|
|
|
local status = pcall(_copy_struct, pkt.data)
|
2022-04-23 16:12:33 +00:00
|
|
|
if status then
|
|
|
|
-- copied in structure data OK
|
2022-05-02 21:43:23 +00:00
|
|
|
self.received_struct = true
|
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
|
2022-05-05 17:14:14 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.MEK_BURN_RATE then
|
|
|
|
-- 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-05-05 17:14:14 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.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
|
2022-05-01 21:04:38 +00:00
|
|
|
self.acks.enable = true
|
2022-04-23 16:12:33 +00:00
|
|
|
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-05-05 17:14:14 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.RPS_SCRAM then
|
|
|
|
-- 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-05-05 17:14:14 +00:00
|
|
|
log.debug(log_header .. "SCRAM failed!")
|
2022-04-23 16:12:33 +00:00
|
|
|
end
|
2022-05-05 15:55:04 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.RPS_STATUS then
|
|
|
|
-- RPS status packet received, copy data
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.length == 7 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
|
2022-05-05 15:55:04 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.RPS_ALARM then
|
|
|
|
-- RPS alarm
|
2022-04-23 16:12:33 +00:00
|
|
|
self.sDB.overridden = true
|
2022-05-02 15:42:24 +00:00
|
|
|
if pkt.length == 8 then
|
2022-05-05 15:55:04 +00:00
|
|
|
self.sDB.rps_tripped = true
|
|
|
|
self.sDB.rps_trip_cause = pkt.data[1]
|
|
|
|
local status = pcall(_copy_rps_status, { table.unpack(pkt.data, 2, #pkt.length) })
|
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
|
2022-05-05 15:55:04 +00:00
|
|
|
elseif pkt.type == RPLC_TYPES.RPS_RESET then
|
|
|
|
-- RPS reset acknowledgement
|
2022-05-02 15:42:24 +00:00
|
|
|
local ack = _get_ack(pkt)
|
|
|
|
if ack then
|
2022-05-05 15:55:04 +00:00
|
|
|
self.acks.rps_tripped = true
|
|
|
|
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
|
|
|
|
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
|
|
|
|
elseif pkt.scada_frame.protocol() == PROTOCOLS.SCADA_MGMT then
|
2022-05-09 19:00:16 +00:00
|
|
|
if pkt.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
|
|
|
-- keep alive reply
|
|
|
|
if pkt.length == 2 then
|
|
|
|
local srv_start = pkt.data[1]
|
|
|
|
local plc_send = pkt.data[2]
|
|
|
|
local srv_now = util.time()
|
|
|
|
self.last_rtt = srv_now - srv_start
|
|
|
|
|
|
|
|
if self.last_rtt > 500 then
|
|
|
|
log.warning(log_header .. "PLC KEEP_ALIVE round trip time > 500ms (" .. self.last_rtt .. "ms)")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- log.debug(log_header .. "PLC RTT = ".. self.last_rtt .. "ms")
|
|
|
|
-- log.debug(log_header .. "PLC TT = ".. (srv_now - plc_send) .. "ms")
|
|
|
|
else
|
|
|
|
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
|
|
|
end
|
|
|
|
elseif pkt.type == SCADA_MGMT_TYPES.CLOSE then
|
2022-05-02 15:42:24 +00:00
|
|
|
-- close the session
|
|
|
|
self.connected = false
|
|
|
|
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
|
2022-04-22 15:07:59 +00:00
|
|
|
local get_id = function () return self.id end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get the session database
|
2022-04-23 16:12:33 +00:00
|
|
|
local get_db = function () return self.sDB end
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
-- get the reactor structure
|
2022-04-22 15:07:59 +00:00
|
|
|
local get_struct = function ()
|
|
|
|
if self.received_struct then
|
|
|
|
return self.sDB.mek_struct
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-03 21:25:31 +00:00
|
|
|
-- get the reactor structure
|
|
|
|
local get_status = function ()
|
|
|
|
if self.received_status_cache then
|
|
|
|
return self.sDB.mek_status
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if a timer matches this session's watchdog
|
|
|
|
local check_wd = function (timer)
|
|
|
|
return timer == self.plc_conn_watchdog.get_timer()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection
|
|
|
|
local close = function ()
|
|
|
|
self.plc_conn_watchdog.cancel()
|
|
|
|
self.connected = false
|
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.CLOSE, {})
|
|
|
|
println("connection to reactor " .. self.for_reactor .. " 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
|
2022-04-22 15:07:59 +00:00
|
|
|
local iterate = function ()
|
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()
|
|
|
|
|
2022-05-02 15:42:24 +00:00
|
|
|
while self.in_q.ready() and self.connected do
|
2022-04-23 16:12:33 +00:00
|
|
|
-- get a new message to process
|
|
|
|
local message = self.in_q.pop()
|
|
|
|
|
|
|
|
if message.qtype == mqueue.TYPE.PACKET then
|
2022-04-27 22:52:06 +00:00
|
|
|
-- handle a packet
|
2022-04-23 16:12:33 +00:00
|
|
|
_handle_packet(message.message)
|
|
|
|
elseif message.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction
|
2022-05-01 21:04:38 +00:00
|
|
|
local cmd = message.message
|
2022-05-05 17:14:14 +00:00
|
|
|
if cmd == PLC_S_CMDS.ENABLE then
|
2022-05-01 21:04:38 +00:00
|
|
|
-- enable reactor
|
|
|
|
self.acks.enable = false
|
|
|
|
self.retry_times.enable_req = util.time() + INITIAL_WAIT
|
2022-05-05 17:14:14 +00:00
|
|
|
_send(RPLC_TYPES.RPS_ENABLE, {})
|
|
|
|
elseif cmd == PLC_S_CMDS.SCRAM then
|
|
|
|
-- SCRAM reactor
|
|
|
|
self.acks.scram = false
|
|
|
|
self.retry_times.scram_req = util.time() + INITIAL_WAIT
|
|
|
|
_send(RPLC_TYPES.RPS_SCRAM, {})
|
2022-05-05 15:55:04 +00:00
|
|
|
elseif cmd == PLC_S_CMDS.RPS_RESET then
|
|
|
|
-- reset RPS
|
|
|
|
self.acks.rps_reset = false
|
|
|
|
self.retry_times.rps_reset_req = util.time() + INITIAL_WAIT
|
|
|
|
_send(RPLC_TYPES.RPS_RESET, {})
|
2022-05-01 21:04:38 +00:00
|
|
|
end
|
|
|
|
elseif message.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction with body
|
|
|
|
local cmd = message.message
|
2022-05-05 20:00:49 +00:00
|
|
|
if cmd.key == PLC_S_DATA.BURN_RATE then
|
2022-05-01 21:04:38 +00:00
|
|
|
-- update burn rate
|
|
|
|
self.commanded_burn_rate = cmd.val
|
2022-05-05 20:00:49 +00:00
|
|
|
self.ramping_rate = false
|
|
|
|
self.acks.burn_rate = false
|
|
|
|
self.retry_times.burn_rate_req = util.time() + INITIAL_WAIT
|
|
|
|
_send(RPLC_TYPES.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
|
|
|
elseif cmd.key == PLC_S_DATA.RAMP_BURN_RATE then
|
|
|
|
-- ramp to burn rate
|
|
|
|
self.commanded_burn_rate = cmd.val
|
|
|
|
self.ramping_rate = true
|
2022-05-01 21:04:38 +00:00
|
|
|
self.acks.burn_rate = false
|
|
|
|
self.retry_times.burn_rate_req = util.time() + INITIAL_WAIT
|
2022-05-05 20:00:49 +00:00
|
|
|
_send(RPLC_TYPES.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
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
|
2022-05-03 21:25:31 +00:00
|
|
|
self.plc_conn_watchdog.cancel()
|
|
|
|
println("connection to reactor " .. self.for_reactor .. " 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
|
2022-05-09 19:00:16 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.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
|
|
|
|
|
|
|
-- struct request retry
|
|
|
|
|
|
|
|
if not self.received_struct then
|
|
|
|
if rtimes.struct_req - util.time() <= 0 then
|
2022-05-02 21:43:23 +00:00
|
|
|
_send(RPLC_TYPES.MEK_STRUCT, {})
|
2022-05-01 21:04:38 +00:00
|
|
|
rtimes.struct_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-03 21:25:31 +00:00
|
|
|
-- status cache request retry
|
|
|
|
|
|
|
|
if not self.received_status_cache then
|
|
|
|
if rtimes.status_req - util.time() <= 0 then
|
|
|
|
_send(RPLC_TYPES.MEK_STATUS, {})
|
|
|
|
rtimes.status_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- SCRAM request retry
|
|
|
|
|
|
|
|
if not self.acks.scram then
|
|
|
|
if rtimes.scram_req - util.time() <= 0 then
|
2022-05-05 17:14:14 +00:00
|
|
|
_send(RPLC_TYPES.RPS_SCRAM, {})
|
2022-05-01 21:04:38 +00:00
|
|
|
rtimes.scram_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- enable request retry
|
|
|
|
|
|
|
|
if not self.acks.enable then
|
|
|
|
if rtimes.enable_req - util.time() <= 0 then
|
2022-05-05 17:14:14 +00:00
|
|
|
_send(RPLC_TYPES.RPS_ENABLE, {})
|
2022-05-01 21:04:38 +00:00
|
|
|
rtimes.enable_req = util.time() + RETRY_PERIOD
|
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2022-05-01 21:04:38 +00:00
|
|
|
-- burn rate request retry
|
|
|
|
|
|
|
|
if not self.acks.burn_rate then
|
|
|
|
if rtimes.burn_rate_req - util.time() <= 0 then
|
2022-05-05 20:00:49 +00:00
|
|
|
_send(RPLC_TYPES.MEK_BURN_RATE, { self.commanded_burn_rate, self.ramping_rate })
|
2022-05-01 21:04:38 +00:00
|
|
|
rtimes.burn_rate_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
|
|
|
|
_send(RPLC_TYPES.RPS_RESET, {})
|
|
|
|
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
|
|
|
|
|
|
|
|
return {
|
|
|
|
get_id = get_id,
|
2022-04-23 16:12:33 +00:00
|
|
|
get_db = get_db,
|
2022-04-22 15:07:59 +00:00
|
|
|
get_struct = get_struct,
|
2022-05-03 21:25:31 +00:00
|
|
|
get_status = get_status,
|
|
|
|
check_wd = check_wd,
|
|
|
|
close = close,
|
2022-04-22 15:07:59 +00:00
|
|
|
iterate = iterate
|
|
|
|
}
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return plc
|