Advertisement
cc-editor

AKW Monitoring

Jan 15th, 2025 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. settings.load()
  2.  
  3. -- #region Setup
  4. local me_system = peripheral.wrap(settings.get("akw.meSystem"))
  5. local fuel_type = settings.get("akw.fuelType")
  6. local recipient_id = settings.get("rednet.recipient")
  7. local protocol = settings.get("rednet.protocol")
  8. local modem_side = settings.get("rednet.modemSide")
  9.  
  10. local update_table = {
  11.     reactors = {},
  12.     power_lines = {},
  13.     fuel = 0
  14. }
  15.  
  16. local reactors = {}
  17. for _, id in ipairs({"A", "B", "C", "D"}) do
  18.     reactors[id] = {
  19.         active = settings.get("akw.reactor" .. id .. ".active"),
  20.         rods = {}
  21.     }
  22.  
  23.     if reactors[id].active then
  24.         update_table.reactors[id] = {
  25.             rods = {},
  26.             name = id
  27.         }
  28.  
  29.         for j = 1, 4 do
  30.             reactors[id].rods[j] = {
  31.                 deactivated = false,
  32.                 boiler = peripheral.wrap(settings.get("akw.reactor" .. id .. ".rod" .. j .. ".boiler")),
  33.                 battery = peripheral.wrap(settings.get("akw.reactor" .. id .. ".rod" .. j .. ".battery")),
  34.             }
  35.  
  36.             update_table.reactors[id].rods[j] = {
  37.                 name = id..j,
  38.                 state = "unknown"
  39.             }
  40.         end
  41.     end
  42. end
  43.  
  44. local power_lines = {}
  45. for i = 1, 6 do
  46.     power_lines[i] = peripheral.wrap(settings.get("akw.powerLine" .. i))
  47.     update_table.power_lines[i] = {
  48.         name = i,
  49.         power = 0
  50.     }
  51. end
  52.  
  53. rednet.open(modem_side)
  54. -- #endregion
  55.  
  56. -- #region Functions
  57. local function update_reactor_monitoring()
  58.     for _, reactor_id in ipairs({"A", "B", "C", "D"}) do
  59.         if reactors[reactor_id].active then
  60.             for i = 1, 4 do
  61.                 local boiler_state = reactors[reactor_id].rods[i].boiler.getLine(1)
  62.                 local generator_state = reactors[reactor_id].rods[i].boiler.getLine(6)
  63.                 if string.find(boiler_state, "Max") then
  64.                     if string.find(generator_state, "236 RPM") then
  65.                         reactors[reactor_id].rods[i].deactivated = false
  66.                         update_table.reactors[reactor_id].rods[i].state = "active OK"
  67.                     else
  68.                         reactors[reactor_id].rods[i].deactivated = true
  69.                         update_table.reactors[reactor_id].rods[i].state = "active ERROR"
  70.                     end
  71.                 elseif string.find(boiler_state, "Idle") and reactors[reactor_id].rods[i].deactivated then
  72.                     update_table.reactors[reactor_id].rods[i].state = "deactived ERROR"
  73.                 elseif string.find(boiler_state, "Idle") then
  74.                     update_table.reactors[reactor_id].rods[i].state = "inactive OK"
  75.                 else
  76.                     update_table.reactors[reactor_id].rods[i].state = "starting"
  77.                 end
  78.             end
  79.         end
  80.     end
  81. end
  82.  
  83. local function update_powerline_monitoring()
  84.     for i = 1, 6 do
  85.         update_table.power_lines[i] = power_lines[i].getAveragePower()
  86.     end
  87. end
  88.  
  89. local function update_fuel_monitoring()
  90.     local fuelInfo = me_system.getItem({name = fuel_type})
  91.     if fuelInfo ~= nil then
  92.         update_table.fuel = fuelInfo.amount
  93.     else
  94.         update_table.fuel = 0
  95.     end
  96. end
  97. -- #endregion
  98.  
  99. local function send_update()
  100.     print("Sending update via [" .. protocol .. "] to PC" .. recipient_id)
  101.     rednet.send(recipient_id, update_table, protocol)
  102.     print("Update sent")
  103. end
  104.  
  105. local function monitoring_reactor()
  106.     while true do
  107.         print("Fetching metrics...")
  108.         update_reactor_monitoring()
  109.         update_powerline_monitoring()
  110.         update_fuel_monitoring()
  111.         send_update()
  112.         sleep(5)
  113.     end
  114. end
  115. monitoring_reactor()
  116.  
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement