From c78db71b1494d63303b99ca3fa17dc262994f8de Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 13 Jan 2022 10:11:42 -0500 Subject: [PATCH] comms and util files --- scada-common/comms.lua | 157 +++++++++++++++++++++++++++++++++++++++++ scada-common/util.lua | 29 ++++++++ 2 files changed, 186 insertions(+) create mode 100644 scada-common/comms.lua create mode 100644 scada-common/util.lua diff --git a/scada-common/comms.lua b/scada-common/comms.lua new file mode 100644 index 0000000..f1ed07f --- /dev/null +++ b/scada-common/comms.lua @@ -0,0 +1,157 @@ + +function server_comms() + local self = { + reactor_struct_cache = nil + } + + local record_struct = function (id, mek_data) + end + + -- send the structure data by request to pocket computers + local send_struct = function () + end + + local command_waste = function () + end +end + +function rplc_comms(id, modem, local_port, server_port, reactor) + local self = { + _id = id, + _modem = modem, + _server = server_port, + _local = local_port, + _reactor = reactor, + _status_cache = nil, + + _send = function (msg) + self._modem.transmit(self._server, self._local, msg) + end + } + + local _send = function (msg) + self._modem.transmit(self._server, self._local, msg) + end + + -- variable reactor status information, excluding heating rate + local _reactor_status = function () + return { + status = self._reactor.getStatus(), + burn_rate = self._reactor.getBurnRate(), + act_burn_r = self._reactor.getActualBurnRate(), + temp = self._reactor.getTemperature(), + damage = self._reactor.getDamagePercent(), + boil_eff = self._reactor.getBoilEfficiency(), + env_loss = self._reactor.getEnvironmentalLoss(), + + fuel = self._reactor.getFuel(), + fuel_need = self._reactor.getFuelNeeded(), + fuel_fill = self._reactor.getFuelFilledPercentage(), + waste = self._reactor.getWaste(), + waste_need = self._reactor.getWasteNeeded(), + waste_fill = self._reactor.getWasteFilledPercentage(), + cool_type = self._reactor.getCoolant()['name'], + cool_amnt = self._reactor.getCoolant()['amount'], + cool_need = self._reactor.getCoolantNeeded(), + cool_fill = self._reactor.getCoolantFilledPercentage(), + hcool_type = self._reactor.getHeatedCoolant()['name'], + hcool_amnt = self._reactor.getHeatedCoolant()['amount'], + hcool_need = self._reactor.getHeatedCoolantNeeded(), + hcool_fill = self._reactor.getHeatedCoolantFilledPercentage() + } + end + + local _status_changed = function () + local status = self._reactor_status() + local changed = false + + for key, value in pairs() do + if value ~= _status_cache[key] then + changed = true + break + end + end + + return changed + end + + -- attempt to establish link with + local send_link_req = function () + local linking_data = { + id = self._id, + type = "link_req" + } + + _send(linking_data) + end + + -- send structure properties (these should not change) + -- server will cache these + local send_struct = function () + local mek_data = { + heat_cap = self._reactor.getHeatCapacity(), + fuel_asm = self._reactor.getFuelAssemblies(), + fuel_sa = self._reactor.getFuelSurfaceArea(), + fuel_cap = self._reactor.getFuelCapacity(), + waste_cap = self._reactor.getWasteCapacity(), + cool_cap = self._reactor.getCoolantCapacity(), + hcool_cap = self._reactor.getHeatedCoolantCapacity(), + max_burn = self._reactor.getMaxBurnRate() + } + + local struct_packet = { + id = self._id, + type = "struct_data", + mek_data = mek_data + } + + _send(struct_packet) + end + + -- send live status information + local send_status = function () + local mek_data = self._reactor_status() + + local sys_data = { + timestamp = os.time(), + control_state = false, + overridden = false, + faults = {}, + waste_production = "antimatter" -- "plutonium", "polonium", "antimatter" + } + end + + local send_keep_alive = function () + -- heating rate is volatile, so it is skipped in status + -- send it with keep alive packets + local mek_data = { + heating_rate = self._reactor.getHeatingRate() + } + + -- basic keep alive packet to server + local keep_alive_packet = { + id = self._id, + type = "keep_alive", + timestamp = os.time(), + mek_data = mek_data + } + + _send(keep_alive_packet) + end + + local handle_link = function (packet) + if packet.type == "link_response" then + return packet.accepted + else + return "wrong_type" + end + end + + return { + send_link_req = send_link_req, + send_struct = send_struct, + send_status = send_status, + send_keep_alive = send_keep_alive, + handle_link = handle_link + } +end \ No newline at end of file diff --git a/scada-common/util.lua b/scada-common/util.lua new file mode 100644 index 0000000..f6fd611 --- /dev/null +++ b/scada-common/util.lua @@ -0,0 +1,29 @@ +-- timestamped print +function print_ts(message) + term.write(os.date("[%H:%M:%S] ") .. message) +end + +-- ComputerCraft OS Timer based Watchdog +-- triggers a timer event if not fed within 'timeout' seconds +function new_watchdog(timeout) + local self = { + _timeout = timeout, + _wd_timer = os.startTimer(_timeout) + } + + local get_timer = function () + return self._wd_timer + end + + local feed = function () + if self._wd_timer ~= nil then + os.cancelTimer(self._wd_timer) + end + self._wd_timer = os.startTimer(self._timeout) + end + + return { + get_timer = get_timer, + feed = feed + } +end