#502 much needed refresh and cleanup of PLC struct and status packet handling

This commit is contained in:
Mikayla Fischler 2024-06-27 21:03:53 -04:00
parent 2bc20ec312
commit 897a3ed22d
4 changed files with 100 additions and 104 deletions

View File

@ -571,33 +571,17 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog)
self.seq_num = self.seq_num + 1 self.seq_num = self.seq_num + 1
end end
-- variable reactor status information, excluding heating rate -- dynamic reactor status information, excluding heating rate
---@return table data_table, boolean faulted ---@return table data_table, boolean faulted
local function _reactor_status() local function _get_reactor_status()
local fuel = nil local fuel = nil
local waste = nil local waste = nil
local coolant = nil local coolant = nil
local hcoolant = nil local hcoolant = nil
local data_table = { local data_table = {}
false, -- getStatus
0, -- getBurnRate reactor.__p_disable_afc()
0, -- getActualBurnRate
0, -- getTemperature
0, -- getDamagePercent
0, -- getBoilEfficiency
0, -- getEnvironmentalLoss
0, -- fuel_amnt
0, -- getFuelFilledPercentage
0, -- waste_amnt
0, -- getWasteFilledPercentage
"", -- coolant_name
0, -- coolant_amnt
0, -- getCoolantFilledPercentage
"", -- hcoolant_name
0, -- hcoolant_amnt
0 -- getHeatedCoolantFilledPercentage
}
local tasks = { local tasks = {
function () data_table[1] = reactor.getStatus() end, function () data_table[1] = reactor.getStatus() end,
@ -637,30 +621,32 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog)
data_table[16] = hcoolant.amount data_table[16] = hcoolant.amount
end end
reactor.__p_enable_afc()
return data_table, reactor.__p_is_faulted() return data_table, reactor.__p_is_faulted()
end end
-- update the status cache if changed -- update the status cache if changed
---@return boolean changed ---@return boolean changed
local function _update_status_cache() local function _update_status_cache()
local status, faulted = _reactor_status() local status, faulted = _get_reactor_status()
local changed = false local changed = false
if self.status_cache ~= nil then if not faulted then
if not faulted then if self.status_cache ~= nil then
for i = 1, #status do for i = 1, #status do
if status[i] ~= self.status_cache[i] then if status[i] ~= self.status_cache[i] then
changed = true changed = true
break break
end end
end end
else
changed = true
end end
else
changed = true
end
if changed and not faulted then if changed then
self.status_cache = status self.status_cache = status
end
end end
return changed return changed
@ -679,9 +665,11 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog)
_send(msg_type, { status }) _send(msg_type, { status })
end end
-- send structure properties (these should not change, server will cache these) -- send static structure properties, cached by server
local function _send_struct() local function _send_struct()
local mek_data = { false, 0, 0, 0, types.new_zero_coordinate(), types.new_zero_coordinate(), 0, 0, 0, 0, 0, 0, 0, 0 } local mek_data = {}
reactor.__p_disable_afc()
local tasks = { local tasks = {
function () mek_data[1] = reactor.getLength() end, function () mek_data[1] = reactor.getLength() end,
@ -705,6 +693,8 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog)
_send(RPLC_TYPE.MEK_STRUCT, mek_data) _send(RPLC_TYPE.MEK_STRUCT, mek_data)
self.resend_build = false self.resend_build = false
end end
reactor.__p_enable_afc()
end end
-- PUBLIC FUNCTIONS -- -- PUBLIC FUNCTIONS --

View File

@ -18,7 +18,7 @@ local plc = require("reactor-plc.plc")
local renderer = require("reactor-plc.renderer") local renderer = require("reactor-plc.renderer")
local threads = require("reactor-plc.threads") local threads = require("reactor-plc.threads")
local R_PLC_VERSION = "v1.7.11" local R_PLC_VERSION = "v1.7.12"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts

View File

@ -209,52 +209,89 @@ function plc.new_session(id, s_addr, reactor_id, in_queue, out_queue, timeout, f
-- copy in the reactor status -- copy in the reactor status
---@param mek_data table ---@param mek_data table
local function _copy_status(mek_data) local function _copy_status(mek_data)
local stat = self.sDB.mek_status
local struct = self.sDB.mek_struct
-- copy status information -- copy status information
self.sDB.mek_status.status = mek_data[1] stat.status = mek_data[1]
self.sDB.mek_status.burn_rate = mek_data[2] stat.burn_rate = mek_data[2]
self.sDB.mek_status.act_burn_rate = mek_data[3] stat.act_burn_rate = mek_data[3]
self.sDB.mek_status.temp = mek_data[4] stat.temp = mek_data[4]
self.sDB.mek_status.damage = mek_data[5] stat.damage = mek_data[5]
self.sDB.mek_status.boil_eff = mek_data[6] stat.boil_eff = mek_data[6]
self.sDB.mek_status.env_loss = mek_data[7] stat.env_loss = mek_data[7]
-- copy container information -- copy container information
self.sDB.mek_status.fuel = mek_data[8] stat.fuel = mek_data[8]
self.sDB.mek_status.fuel_fill = mek_data[9] stat.fuel_fill = mek_data[9]
self.sDB.mek_status.waste = mek_data[10] stat.waste = mek_data[10]
self.sDB.mek_status.waste_fill = mek_data[11] stat.waste_fill = mek_data[11]
self.sDB.mek_status.ccool_type = mek_data[12] stat.ccool_type = mek_data[12]
self.sDB.mek_status.ccool_amnt = mek_data[13] stat.ccool_amnt = mek_data[13]
self.sDB.mek_status.ccool_fill = mek_data[14] stat.ccool_fill = mek_data[14]
self.sDB.mek_status.hcool_type = mek_data[15] stat.hcool_type = mek_data[15]
self.sDB.mek_status.hcool_amnt = mek_data[16] stat.hcool_amnt = mek_data[16]
self.sDB.mek_status.hcool_fill = mek_data[17] stat.hcool_fill = mek_data[17]
-- update computable fields if we have our structure -- update computable fields if we have our structure
if self.received_struct then if self.received_struct then
self.sDB.mek_status.fuel_need = self.sDB.mek_struct.fuel_cap - self.sDB.mek_status.fuel_fill stat.fuel_need = struct.fuel_cap - stat.fuel_fill
self.sDB.mek_status.waste_need = self.sDB.mek_struct.waste_cap - self.sDB.mek_status.waste_fill stat.waste_need = struct.waste_cap - stat.waste_fill
self.sDB.mek_status.cool_need = self.sDB.mek_struct.ccool_cap - self.sDB.mek_status.ccool_fill stat.cool_need = struct.ccool_cap - stat.ccool_fill
self.sDB.mek_status.hcool_need = self.sDB.mek_struct.hcool_cap - self.sDB.mek_status.hcool_fill stat.hcool_need = struct.hcool_cap - stat.hcool_fill
end end
end end
-- copy in the reactor structure -- copy in the reactor structure
---@param mek_data table ---@param mek_data table
local function _copy_struct(mek_data) local function _copy_struct(mek_data)
self.sDB.mek_struct.length = mek_data[1] local struct = self.sDB.mek_struct
self.sDB.mek_struct.width = mek_data[2]
self.sDB.mek_struct.height = mek_data[3] struct.length = mek_data[1]
self.sDB.mek_struct.min_pos = mek_data[4] struct.width = mek_data[2]
self.sDB.mek_struct.max_pos = mek_data[5] struct.height = mek_data[3]
self.sDB.mek_struct.heat_cap = mek_data[6] struct.min_pos = mek_data[4]
self.sDB.mek_struct.fuel_asm = mek_data[7] struct.max_pos = mek_data[5]
self.sDB.mek_struct.fuel_sa = mek_data[8] struct.heat_cap = mek_data[6]
self.sDB.mek_struct.fuel_cap = mek_data[9] struct.fuel_asm = mek_data[7]
self.sDB.mek_struct.waste_cap = mek_data[10] struct.fuel_sa = mek_data[8]
self.sDB.mek_struct.ccool_cap = mek_data[11] struct.fuel_cap = mek_data[9]
self.sDB.mek_struct.hcool_cap = mek_data[12] struct.waste_cap = mek_data[10]
self.sDB.mek_struct.max_burn = mek_data[13] struct.ccool_cap = mek_data[11]
struct.hcool_cap = mek_data[12]
struct.max_burn = mek_data[13]
end
-- handle a reactor status packet
---@param pkt rplc_frame
local function _handle_status(pkt)
local valid = (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")
if valid 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
if #pkt.data[7] == 17 then
_copy_status(pkt.data[7])
self.received_status_cache = true
else
log.error(log_header .. "RPLC status packet reactor data length mismatch")
end
end
end
else
log.debug(log_header .. "RPLC status packet invalid")
end
end end
-- mark this PLC session as closed, stop watchdog -- mark this PLC session as closed, stop watchdog
@ -334,48 +371,17 @@ function plc.new_session(id, s_addr, reactor_id, in_queue, out_queue, timeout, f
if pkt.type == RPLC_TYPE.STATUS then if pkt.type == RPLC_TYPE.STATUS then
-- status packet received, update data -- status packet received, update data
if pkt.length >= 5 then if pkt.length >= 5 then
if (type(pkt.data[1]) == "number") and (type(pkt.data[2]) == "boolean") and (type(pkt.data[3]) == "boolean") and _handle_status(pkt)
(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
end
end
else
log.debug(log_header .. "RPLC status packet invalid")
end
else else
log.debug(log_header .. "RPLC status packet length mismatch") log.debug(log_header .. "RPLC status packet length mismatch")
end end
elseif pkt.type == RPLC_TYPE.MEK_STRUCT then elseif pkt.type == RPLC_TYPE.MEK_STRUCT then
-- received reactor structure, record it -- received reactor structure, record it
if pkt.length == 14 then if pkt.length == 13 then
local status = pcall(_copy_struct, pkt.data) _copy_struct(pkt.data)
if status then _compute_op_temps()
-- copied in structure data OK self.received_struct = true
_compute_op_temps() out_queue.push_data(svqtypes.SV_Q_DATA.PLC_BUILD_CHANGED, reactor_id)
self.received_struct = true
out_queue.push_data(svqtypes.SV_Q_DATA.PLC_BUILD_CHANGED, reactor_id)
else
-- error copying structure data
log.error(log_header .. "failed to parse struct packet data")
end
else else
log.debug(log_header .. "RPLC struct packet length mismatch") log.debug(log_header .. "RPLC struct packet length mismatch")
end end

View File

@ -21,7 +21,7 @@ local supervisor = require("supervisor.supervisor")
local svsessions = require("supervisor.session.svsessions") local svsessions = require("supervisor.session.svsessions")
local SUPERVISOR_VERSION = "v1.3.12" local SUPERVISOR_VERSION = "v1.3.13"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts