2022-03-25 16:17:46 +00:00
|
|
|
-- #REQUIRES comms.lua
|
2022-04-18 01:12:25 +00:00
|
|
|
-- #REQUIRES ppm.lua
|
2022-03-25 16:17:46 +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-01-14 21:33:09 +00:00
|
|
|
-- Internal Safety System
|
|
|
|
-- identifies dangerous states and SCRAMs reactor if warranted
|
2022-04-07 15:12:41 +00:00
|
|
|
-- autonomous from main SCADA supervisor/coordinator control
|
2022-01-14 21:33:09 +00:00
|
|
|
function iss_init(reactor)
|
|
|
|
local self = {
|
2022-01-22 19:26:25 +00:00
|
|
|
reactor = reactor,
|
|
|
|
timed_out = false,
|
|
|
|
tripped = false,
|
|
|
|
trip_cause = ""
|
2022-01-14 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- re-link a reactor after a peripheral re-connect
|
2022-04-03 16:08:22 +00:00
|
|
|
local reconnect_reactor = function (reactor)
|
|
|
|
self.reactor = reactor
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check for critical damage
|
2022-04-05 21:25:56 +00:00
|
|
|
local damage_critical = function ()
|
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
|
|
|
|
log._error("ISS: failed to check reactor damage")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return damage_percent >= 100
|
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check for heated coolant backup
|
2022-04-05 21:25:56 +00:00
|
|
|
local excess_heated_coolant = function ()
|
2022-04-07 15:12:41 +00:00
|
|
|
local hc_needed = self.reactor.getHeatedCoolantNeeded()
|
2022-04-18 01:12:25 +00:00
|
|
|
if hc_needed == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
log._error("ISS: failed to check reactor heated coolant level")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return hc_needed == 0
|
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check for excess waste
|
2022-04-05 21:25:56 +00:00
|
|
|
local excess_waste = function ()
|
2022-04-07 15:12:41 +00:00
|
|
|
local w_needed = self.reactor.getWasteNeeded()
|
2022-04-18 01:12:25 +00:00
|
|
|
if w_needed == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
log._error("ISS: failed to check reactor waste level")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return w_needed == 0
|
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check if the reactor is at a critically high temperature
|
2022-04-05 21:25:56 +00:00
|
|
|
local high_temp = function ()
|
|
|
|
-- mekanism: MAX_DAMAGE_TEMPERATURE = 1_200
|
2022-04-07 15:12:41 +00:00
|
|
|
local temp = self.reactor.getTemperature()
|
2022-04-18 01:12:25 +00:00
|
|
|
if temp == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
log._error("ISS: failed to check reactor temperature")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return temp >= 1200
|
|
|
|
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-04-05 21:25:56 +00:00
|
|
|
local insufficient_fuel = function ()
|
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
|
|
|
|
log._error("ISS: failed to check reactor fuel level")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return fuel == 0
|
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check if there is no coolant
|
2022-04-05 21:25:56 +00:00
|
|
|
local no_coolant = function ()
|
2022-04-07 15:12:41 +00:00
|
|
|
local coolant_filled = self.reactor.getCoolantFilledPercentage()
|
2022-04-18 01:12:25 +00:00
|
|
|
if coolant_filled == ppm.ACCESS_FAULT then
|
2022-04-07 15:12:41 +00:00
|
|
|
-- lost the peripheral or terminated, handled later
|
|
|
|
log._error("ISS: failed to check reactor coolant level")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return coolant_filled < 2
|
|
|
|
end
|
2022-04-05 21:25:56 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- if PLC timed out
|
2022-04-05 21:25:56 +00:00
|
|
|
local timed_out = function ()
|
|
|
|
return self.timed_out
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- check all safety conditions
|
2022-01-14 21:33:09 +00:00
|
|
|
local check = function ()
|
|
|
|
local status = "ok"
|
2022-01-22 19:26:25 +00:00
|
|
|
local was_tripped = self.tripped
|
2022-01-14 21:33:09 +00:00
|
|
|
|
|
|
|
-- check system states in order of severity
|
2022-04-05 21:25:56 +00:00
|
|
|
if damage_critical() then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: damage critical!")
|
2022-01-14 21:33:09 +00:00
|
|
|
status = "dmg_crit"
|
2022-04-05 21:25:56 +00:00
|
|
|
elseif high_temp() then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: high temperature!")
|
2022-01-14 21:33:09 +00:00
|
|
|
status = "high_temp"
|
2022-04-05 21:25:56 +00:00
|
|
|
elseif excess_heated_coolant() then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: heated coolant backup!")
|
2022-01-14 21:33:09 +00:00
|
|
|
status = "heated_coolant_backup"
|
2022-04-05 21:25:56 +00:00
|
|
|
elseif excess_waste() then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: full waste!")
|
2022-01-14 21:33:09 +00:00
|
|
|
status = "full_waste"
|
2022-04-05 21:25:56 +00:00
|
|
|
elseif insufficient_fuel() then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: no fuel!")
|
2022-01-14 21:33:09 +00:00
|
|
|
status = "no_fuel"
|
2022-01-22 19:26:25 +00:00
|
|
|
elseif self.tripped then
|
|
|
|
status = self.trip_cause
|
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-01-14 21:33:09 +00:00
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- if a new trip occured...
|
2022-01-14 21:33:09 +00:00
|
|
|
if status ~= "ok" then
|
2022-04-05 21:58:23 +00:00
|
|
|
log._warning("ISS: reactor SCRAM")
|
2022-01-22 19:26:25 +00:00
|
|
|
self.tripped = true
|
|
|
|
self.trip_cause = status
|
2022-04-18 01:12:25 +00:00
|
|
|
if self.reactor.scram() == ppm.ACCESS_FAULT then
|
|
|
|
log._error("ISS: failed reactor SCRAM")
|
|
|
|
end
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
2022-01-22 19:26:25 +00:00
|
|
|
|
2022-04-05 20:09:29 +00:00
|
|
|
local first_trip = not was_tripped and self.tripped
|
2022-01-14 21:33:09 +00:00
|
|
|
|
2022-01-22 19:26:25 +00:00
|
|
|
return self.tripped, status, first_trip
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- report a PLC comms timeout
|
2022-01-22 19:26:25 +00:00
|
|
|
local trip_timeout = function ()
|
|
|
|
self.tripped = false
|
|
|
|
self.trip_cause = "timeout"
|
|
|
|
self.timed_out = true
|
|
|
|
self.reactor.scram()
|
2022-01-02 00:45:33 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- reset the ISS
|
2022-01-14 21:33:09 +00:00
|
|
|
local reset = function ()
|
2022-01-22 19:26:25 +00:00
|
|
|
self.timed_out = false
|
|
|
|
self.tripped = false
|
|
|
|
self.trip_cause = ""
|
|
|
|
end
|
|
|
|
|
2022-04-07 15:12:41 +00:00
|
|
|
-- get the ISS status
|
2022-01-22 19:26:25 +00:00
|
|
|
local status = function (named)
|
|
|
|
if named then
|
|
|
|
return {
|
|
|
|
damage_critical = damage_critical(),
|
|
|
|
excess_heated_coolant = excess_heated_coolant(),
|
|
|
|
excess_waste = excess_waste(),
|
|
|
|
high_temp = high_temp(),
|
|
|
|
insufficient_fuel = insufficient_fuel(),
|
|
|
|
no_coolant = no_coolant(),
|
|
|
|
timed_out = timed_out()
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {
|
|
|
|
damage_critical(),
|
|
|
|
excess_heated_coolant(),
|
|
|
|
excess_waste(),
|
|
|
|
high_temp(),
|
|
|
|
insufficient_fuel(),
|
|
|
|
no_coolant(),
|
|
|
|
timed_out()
|
|
|
|
}
|
|
|
|
end
|
2022-01-14 21:33:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2022-04-03 16:08:22 +00:00
|
|
|
reconnect_reactor = reconnect_reactor,
|
2022-01-14 21:33:09 +00:00
|
|
|
check = check,
|
2022-01-22 19:26:25 +00:00
|
|
|
trip_timeout = trip_timeout,
|
2022-01-14 21:33:09 +00:00
|
|
|
reset = reset,
|
2022-01-22 19:26:25 +00:00
|
|
|
status = status,
|
2022-01-14 21:33:09 +00:00
|
|
|
damage_critical = damage_critical,
|
|
|
|
excess_heated_coolant = excess_heated_coolant,
|
|
|
|
excess_waste = excess_waste,
|
|
|
|
high_temp = high_temp,
|
|
|
|
insufficient_fuel = insufficient_fuel,
|
2022-01-22 19:26:25 +00:00
|
|
|
no_coolant = no_coolant,
|
|
|
|
timed_out = timed_out
|
2022-01-14 21:33:09 +00:00
|
|
|
}
|
2022-01-02 00:45:33 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
|
|
|
|
-- reactor PLC communications
|
2022-04-03 16:08:22 +00:00
|
|
|
function comms_init(id, modem, local_port, server_port, reactor, iss)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
|
|
|
id = id,
|
|
|
|
seq_num = 0,
|
|
|
|
modem = modem,
|
|
|
|
s_port = server_port,
|
|
|
|
l_port = local_port,
|
|
|
|
reactor = reactor,
|
2022-04-02 18:43:36 +00:00
|
|
|
iss = iss,
|
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-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-04-23 01:39:03 +00:00
|
|
|
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.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
2022-03-25 16:17:46 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- variable reactor status information, excluding heating rate
|
|
|
|
local _reactor_status = function ()
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-03-25 16:17:46 +00:00
|
|
|
return {
|
2022-04-23 01:39:03 +00:00
|
|
|
self.reactor.getStatus(),
|
|
|
|
self.reactor.getBurnRate(),
|
|
|
|
self.reactor.getActualBurnRate(),
|
|
|
|
self.reactor.getTemperature(),
|
|
|
|
self.reactor.getDamagePercent(),
|
|
|
|
self.reactor.getBoilEfficiency(),
|
|
|
|
self.reactor.getEnvironmentalLoss(),
|
|
|
|
|
|
|
|
self.reactor.getFuel(),
|
|
|
|
self.reactor.getFuelNeeded(),
|
|
|
|
self.reactor.getFuelFilledPercentage(),
|
|
|
|
self.reactor.getWaste(),
|
|
|
|
self.reactor.getWasteNeeded(),
|
|
|
|
self.reactor.getWasteFilledPercentage(),
|
|
|
|
self.reactor.getCoolant()['name'],
|
|
|
|
self.reactor.getCoolant()['amount'],
|
|
|
|
self.reactor.getCoolantNeeded(),
|
|
|
|
self.reactor.getCoolantFilledPercentage(),
|
|
|
|
self.reactor.getHeatedCoolant()['name'],
|
|
|
|
self.reactor.getHeatedCoolant()['amount'],
|
|
|
|
self.reactor.getHeatedCoolantNeeded(),
|
|
|
|
self.reactor.getHeatedCoolantFilledPercentage()
|
2022-04-18 01:12:25 +00:00
|
|
|
}, ppm.faulted()
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local _update_status_cache = function ()
|
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-18 01:12:25 +00:00
|
|
|
if not faulted then
|
2022-04-23 01:39:03 +00:00
|
|
|
for i = 1, #status do
|
|
|
|
if status[i] ~= self.status_cache[i] then
|
2022-04-18 01:12:25 +00:00
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
self.status_cache = status
|
|
|
|
end
|
|
|
|
|
|
|
|
return changed
|
|
|
|
end
|
|
|
|
|
2022-04-02 18:43:36 +00:00
|
|
|
-- keep alive ack
|
2022-04-23 01:39:03 +00:00
|
|
|
local _send_keep_alive_ack = function (srv_time)
|
2022-04-23 15:54:09 +00:00
|
|
|
_send(RPLC_TYPES.KEEP_ALIVE, { srv_time, os.epoch() })
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- general ack
|
2022-04-23 01:39:03 +00:00
|
|
|
local _send_ack = function (msg_type, succeeded)
|
|
|
|
_send(msg_type, { succeeded })
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- send structure properties (these should not change)
|
|
|
|
-- (server will cache these)
|
|
|
|
local _send_struct = function ()
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-04-23 01:39:03 +00:00
|
|
|
|
2022-04-02 18:43:36 +00:00
|
|
|
local mek_data = {
|
2022-04-23 01:39:03 +00:00
|
|
|
self.reactor.getHeatCapacity(),
|
|
|
|
self.reactor.getFuelAssemblies(),
|
|
|
|
self.reactor.getFuelSurfaceArea(),
|
|
|
|
self.reactor.getFuelCapacity(),
|
|
|
|
self.reactor.getWasteCapacity(),
|
|
|
|
self.reactor.getCoolantCapacity(),
|
|
|
|
self.reactor.getHeatedCoolantCapacity(),
|
|
|
|
self.reactor.getMaxBurnRate()
|
2022-04-02 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:39:03 +00:00
|
|
|
if not ppm.is_faulted() then
|
|
|
|
_send(RPLC_TYPES.MEK_STRUCT, mek_data)
|
2022-04-18 01:12:25 +00:00
|
|
|
else
|
|
|
|
log._error("failed to send structure: PPM fault")
|
|
|
|
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
|
|
|
|
local reconnect_modem = function (modem)
|
|
|
|
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
|
|
|
|
local reconnect_reactor = function (reactor)
|
|
|
|
self.reactor = reactor
|
2022-04-18 01:12:25 +00:00
|
|
|
_update_status_cache()
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- parse an RPLC packet
|
|
|
|
local parse_packet = function(side, sender, reply_to, message, distance)
|
|
|
|
local pkt = nil
|
|
|
|
local s_pkt = scada_packet()
|
|
|
|
|
|
|
|
-- parse packet as generic SCADA packet
|
|
|
|
s_pkt.recieve(side, sender, reply_to, message, distance)
|
|
|
|
|
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
|
|
|
|
pkt = mgmt_packet.get()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
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-04-05 21:58:23 +00:00
|
|
|
local handle_packet = function (packet, plc_state)
|
2022-04-02 15:22:44 +00:00
|
|
|
if packet ~= nil then
|
|
|
|
if packet.scada_frame.protocol() == PROTOCOLS.RPLC then
|
|
|
|
if self.linked then
|
|
|
|
if packet.type == RPLC_TYPES.KEEP_ALIVE then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- keep alive request received, echo back
|
|
|
|
local timestamp = packet.data[1]
|
2022-04-23 15:54:09 +00:00
|
|
|
local trip_time = os.epoch() - timestamp
|
2022-04-02 18:43:36 +00:00
|
|
|
|
|
|
|
if trip_time < 0 then
|
|
|
|
log._warning("PLC KEEP_ALIVE trip time less than 0 (" .. trip_time .. ")")
|
|
|
|
elseif trip_time > 1 then
|
|
|
|
log._warning("PLC KEEP_ALIVE trip time > 1s (" .. trip_time .. ")")
|
|
|
|
end
|
|
|
|
|
2022-04-23 01:39:03 +00:00
|
|
|
_send_keep_alive_ack(timestamp)
|
2022-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.LINK_REQ then
|
|
|
|
-- link request confirmation
|
2022-04-02 18:43:36 +00:00
|
|
|
log._debug("received unsolicited link request response")
|
|
|
|
|
|
|
|
local link_ack = packet.data[1]
|
|
|
|
|
|
|
|
if link_ack == RPLC_LINKING.ALLOW then
|
|
|
|
_send_struct()
|
|
|
|
send_status()
|
|
|
|
log._debug("re-sent initial status data")
|
|
|
|
elseif link_ack == RPLC_LINKING.DENY then
|
2022-04-21 14:26:02 +00:00
|
|
|
-- @todo: make sure this doesn't become a MITM security risk
|
2022-04-02 18:43:36 +00:00
|
|
|
print_ts("received unsolicited link denial, unlinking\n")
|
|
|
|
log._debug("unsolicited rplc link request denied")
|
|
|
|
elseif link_ack == RPLC_LINKING.COLLISION then
|
2022-04-21 14:26:02 +00:00
|
|
|
-- @todo: make sure this doesn't become a MITM security risk
|
2022-04-02 18:43:36 +00:00
|
|
|
print_ts("received unsolicited link collision, unlinking\n")
|
|
|
|
log._warning("unsolicited rplc link request collision")
|
|
|
|
else
|
|
|
|
print_ts("invalid unsolicited link response\n")
|
|
|
|
log._error("unsolicited unknown rplc link request response")
|
|
|
|
end
|
|
|
|
|
|
|
|
self.linked = link_ack == RPLC_LINKING.ALLOW
|
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-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.MEK_SCRAM then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- disable the reactor
|
2022-04-02 15:46:14 +00:00
|
|
|
self.scrammed = true
|
2022-04-05 21:58:23 +00:00
|
|
|
plc_state.scram = true
|
2022-04-18 01:12:25 +00:00
|
|
|
_send_ack(packet.type, self.reactor.scram() == ppm.ACCESS_OK)
|
2022-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.MEK_ENABLE then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- enable the reactor
|
2022-04-02 15:46:14 +00:00
|
|
|
self.scrammed = false
|
2022-04-05 21:58:23 +00:00
|
|
|
plc_state.scram = false
|
2022-04-18 01:12:25 +00:00
|
|
|
_send_ack(packet.type, self.reactor.activate() == ppm.ACCESS_OK)
|
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
|
|
|
|
local success = false
|
2022-04-21 16:40:21 +00:00
|
|
|
local burn_rate = packet.data[1]
|
|
|
|
local max_burn_rate = self.max_burn_rate
|
2022-04-02 18:43:36 +00:00
|
|
|
|
2022-04-21 16:40:21 +00:00
|
|
|
-- if no known max burn rate, check again
|
|
|
|
if max_burn_rate == nil then
|
|
|
|
max_burn_rate = self.reactor.getMaxBurnRate()
|
|
|
|
self.max_burn_rate = max_burn_rate
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if we know our max burn rate, update current burn rate if in range
|
2022-04-18 01:12:25 +00:00
|
|
|
if max_burn_rate ~= ppm.ACCESS_FAULT then
|
2022-04-02 18:43:36 +00:00
|
|
|
if burn_rate > 0 and burn_rate <= max_burn_rate then
|
|
|
|
success = self.reactor.setBurnRate(burn_rate)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
_send_ack(packet.type, success == ppm.ACCESS_OK)
|
2022-04-02 15:22:44 +00:00
|
|
|
elseif packet.type == RPLC_TYPES.ISS_CLEAR then
|
2022-04-02 18:43:36 +00:00
|
|
|
-- clear the ISS status
|
|
|
|
iss.reset()
|
|
|
|
_send_ack(packet.type, true)
|
|
|
|
else
|
|
|
|
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
|
|
|
|
local link_ack = packet.data[1]
|
|
|
|
|
|
|
|
if link_ack == RPLC_LINKING.ALLOW then
|
|
|
|
print_ts("...linked!\n")
|
|
|
|
log._debug("rplc link request approved")
|
|
|
|
|
2022-04-02 18:43:36 +00:00
|
|
|
_send_struct()
|
|
|
|
send_status()
|
2022-04-02 15:22:44 +00:00
|
|
|
|
|
|
|
log._debug("sent initial status data")
|
|
|
|
elseif link_ack == RPLC_LINKING.DENY then
|
|
|
|
print_ts("...denied, retrying...\n")
|
|
|
|
log._debug("rplc link request denied")
|
|
|
|
elseif link_ack == RPLC_LINKING.COLLISION then
|
|
|
|
print_ts("reactor PLC ID collision (check config), retrying...\n")
|
|
|
|
log._warning("rplc link request collision")
|
|
|
|
else
|
|
|
|
print_ts("invalid link response, bad channel? retrying...\n")
|
|
|
|
log._error("unknown rplc link request response")
|
|
|
|
end
|
|
|
|
|
|
|
|
self.linked = link_ack == RPLC_LINKING.ALLOW
|
|
|
|
else
|
2022-04-18 01:12:25 +00:00
|
|
|
log._debug("discarding non-link packet before linked")
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
|
|
|
elseif packet.scada_frame.protocol() == PROTOCOLS.SCADA_MGMT then
|
2022-04-21 14:44:43 +00:00
|
|
|
-- @todo
|
2022-04-02 15:22:44 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- attempt to establish link with supervisor
|
|
|
|
local send_link_req = function ()
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(RPLC_TYPES.LINK_REQ, {})
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- send live status information
|
2022-04-05 13:41:06 +00:00
|
|
|
-- overridden : if ISS force disabled reactor
|
2022-04-18 01:12:25 +00:00
|
|
|
-- degraded : if PLC status is degraded
|
|
|
|
local send_status = function (overridden, degraded)
|
2022-03-25 16:17:46 +00:00
|
|
|
local mek_data = nil
|
|
|
|
|
|
|
|
if _update_status_cache() then
|
|
|
|
mek_data = self.status_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
local sys_status = {
|
2022-04-23 15:54:09 +00:00
|
|
|
os.epoch(),
|
2022-04-23 01:39:03 +00:00
|
|
|
(not self.scrammed),
|
|
|
|
overridden,
|
|
|
|
degraded,
|
|
|
|
self.reactor.getHeatingRate(),
|
|
|
|
mek_data
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(RPLC_TYPES.STATUS, sys_status)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
|
2022-04-21 16:40:21 +00:00
|
|
|
local send_iss_status = function ()
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(RPLC_TYPES.ISS_STATUS, iss.status())
|
2022-04-21 16:40:21 +00:00
|
|
|
end
|
|
|
|
|
2022-04-02 18:43:36 +00:00
|
|
|
local send_iss_alarm = function (cause)
|
|
|
|
local iss_alarm = {
|
2022-04-23 01:39:03 +00:00
|
|
|
cause,
|
|
|
|
iss.status()
|
2022-04-02 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-23 01:39:03 +00:00
|
|
|
_send(RPLC_TYPES.ISS_ALARM, iss_alarm)
|
2022-04-02 18:43:36 +00:00
|
|
|
end
|
|
|
|
|
2022-04-02 15:46:14 +00:00
|
|
|
local is_scrammed = function () return self.scrammed end
|
|
|
|
local is_linked = function () return self.linked end
|
2022-04-02 15:22:44 +00:00
|
|
|
local unlink = function () self.linked = false end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
return {
|
2022-04-03 16:08:22 +00:00
|
|
|
reconnect_modem = reconnect_modem,
|
|
|
|
reconnect_reactor = reconnect_reactor,
|
2022-03-25 16:17:46 +00:00
|
|
|
parse_packet = parse_packet,
|
|
|
|
handle_packet = handle_packet,
|
|
|
|
send_link_req = send_link_req,
|
2022-04-02 15:22:44 +00:00
|
|
|
send_status = send_status,
|
2022-04-21 16:40:21 +00:00
|
|
|
send_iss_status = send_iss_status,
|
2022-04-02 18:43:36 +00:00
|
|
|
send_iss_alarm = send_iss_alarm,
|
2022-04-02 15:46:14 +00:00
|
|
|
is_scrammed = is_scrammed,
|
|
|
|
is_linked = is_linked,
|
2022-04-02 15:22:44 +00:00
|
|
|
unlink = unlink
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
end
|