Advertisement
cc-editor

reactor_monitoring

Oct 3rd, 2024 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.73 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 monitor = peripheral.wrap(settings.get("akw.monitor"))
  7.  
  8. local reactors = {}
  9. for _, id in ipairs({"A", "B", "C", "D"}) do
  10.     reactors[id] = {
  11.         active = settings.get("akw.reactor" .. id .. ".active"),
  12.         rods = {},
  13.         x_offset = 0,
  14.         y_offset = 0,
  15.     }
  16.     if reactors[id].active then
  17.         for j = 1, 4 do
  18.             reactors[id].rods[j] = {
  19.                 deactivated = false,
  20.                 boiler = peripheral.wrap(settings.get("akw.reactor" .. id .. ".rod" .. j .. ".boiler")),
  21.                 battery = peripheral.wrap(settings.get("akw.reactor" .. id .. ".rod" .. j .. ".battery")),
  22.             }
  23.         end
  24.     end
  25. end
  26.  
  27. local power_lines = {}
  28. for i = 1, 6 do
  29.     power_lines[i] = peripheral.wrap(settings.get("akw.powerLine" .. i))
  30. end
  31. -- #endregion
  32.  
  33. -- #region Functions
  34. local function init_reactor_display(reactor_id, x_offset, y_offset)
  35.     print("Initializing reactor display for block " .. reactor_id)
  36.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  37.     monitor.write("Block " .. reactor_id)
  38.     for i = 1, 4 do
  39.         monitor.setCursorPos(1 + x_offset + 1, 1 + y_offset + 1 + i)
  40.         monitor.write("Stab " .. i)
  41.     end
  42. end
  43.  
  44. local function init_powerline_display(x_offset, y_offset)
  45.     print("Initializing powerline display")
  46.     monitor.setCursorPos(1+ x_offset, 1 + y_offset)
  47.     monitor.write("Energieabnahme")
  48.     for i = 1, 6 do
  49.         monitor.setCursorPos(1 + x_offset + 1, 1 + y_offset + 1 + i)
  50.         monitor.write("Leitung " .. i)
  51.     end
  52. end
  53.  
  54. local function init_fuel_display(x_offset, y_offset)
  55.     print("Initializing fuel display")
  56.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  57.     monitor.write("Treibstoff")
  58. end
  59.  
  60. local function update_reactor_display(reactor_id, x_offset, y_offset)
  61.     print("Updating reactor display for block " .. reactor_id)
  62.     for i = 1, 4 do
  63.         local message = "          "
  64.         local boiler_state = reactors[reactor_id].rods[i].boiler.getLine(1)
  65.         local generator_state = reactors[reactor_id].rods[i].boiler.getLine(6)
  66.         if string.find(boiler_state, "Max") then
  67.             if string.find(generator_state, "236 RPM") then
  68.                 reactors[reactor_id].rods[i].deactivated = false
  69.                 monitor.setTextColor(colors.green)
  70.                 message = "aktiv   OK"
  71.             else
  72.                 reactors[reactor_id].rods[i].deactivated = true
  73.                 monitor.setTextColor(colors.red)
  74.                 message = "aktiv   ER"
  75.             end
  76.         elseif string.find(boiler_state, "Idle") and reactors[reactor_id].rods[i].deactivated then
  77.             monitor.setTextColor(colors.red)
  78.             message = "deaktiv ER"
  79.         elseif string.find(boiler_state, "Idle") then
  80.             monitor.setTextColor(colors.lightGray)
  81.             message = "inaktiv OK"
  82.         else
  83.             monitor.setTextColor(colors.red)
  84.             monitor.setTextColor(colors.yellow)
  85.             message = "startet   "
  86.         end
  87.  
  88.         monitor.setCursorPos(1 + x_offset + 8, 1 + y_offset + 1 + i)
  89.         monitor.write(message)
  90.         monitor.setTextColor(colors.white)
  91.     end
  92. end
  93.  
  94. local function update_powerline_display(x_offset, y_offset)
  95.     print("Updating powerline display")
  96.     for i = 1, 6 do
  97.         local power = tostring(power_lines[i].getAveragePower())
  98.         if power == "0" then
  99.             monitor.setTextColor(colors.lightGray)
  100.         end
  101.  
  102.         for j = 1, 5 - #power do
  103.             power = " " .. power
  104.         end
  105.  
  106.         monitor.setCursorPos(1 + x_offset + 11, 1 + y_offset + 1 + i)
  107.         monitor.write(power)
  108.         monitor.setTextColor(colors.white)
  109.     end
  110. end
  111.  
  112. local function update_clock_display(x_offset, y_offset)
  113.     print("Updating clock display")
  114.     monitor.setCursorPos(1 + x_offset, 1 + y_offset)
  115.     local time = textutils.formatTime(os.time(), true)
  116.     if #time == 4 then
  117.         time = "0" .. time
  118.     end
  119.     monitor.write(time)
  120. end
  121.  
  122. local function update_fuel_display(x_offset, y_offset)
  123.     print("Updating fuel display")
  124.     local fuelInfo = me_system.getItem({name = fuel_type})
  125.     local amount = "0"
  126.     if fuelInfo ~= nil then
  127.         amount = tostring(fuelInfo.amount)
  128.         for i = 1, 5 - #amount do
  129.             amount = " " .. amount
  130.         end
  131.     end
  132.     if fuelInfo == nil or fuelInfo.amount < 64 then
  133.         monitor.setTextColor(colors.red)
  134.     elseif fuelInfo.amount < 256 then
  135.         monitor.setTextColor(colors.yellow)
  136.     else
  137.         monitor.setTextColor(colors.green)
  138.     end
  139.  
  140.     monitor.setCursorPos(1 + x_offset + 11, 1 + y_offset)
  141.     monitor.write(amount)
  142.     monitor.setTextColor(colors.white)
  143. end
  144. -- #endregion
  145.  
  146. -- #region Init
  147. monitor.setTextScale(1)
  148. monitor.setCursorBlink(false)
  149. monitor.clear()
  150.  
  151. reactors["A"].x_offset = 1
  152. reactors["A"].y_offset = 1
  153. reactors["B"].x_offset = 21
  154. reactors["B"].y_offset = 1
  155. reactors["C"].x_offset = 1
  156. reactors["C"].y_offset = 9
  157. reactors["D"].x_offset = 21
  158. reactors["D"].y_offset = 9
  159.  
  160. local power_lines_x_offset = 43
  161. local power_lines_y_offset = 9
  162. local clock_x_offset = 54
  163. local clock_y_offset = 1
  164. local fuel_x_offset = 43
  165. local fuel_y_offset = 3
  166.  
  167. for _, id in ipairs({"A", "B", "C", "D"}) do
  168.     if reactors[id].active then
  169.         init_reactor_display(id, reactors[id].x_offset, reactors[id].y_offset)
  170.     end
  171. end
  172.  
  173. init_powerline_display(power_lines_x_offset, power_lines_y_offset)
  174. init_fuel_display(fuel_x_offset, fuel_y_offset)
  175. -- #endregion
  176.  
  177. local function update_reactor(reactor_id)
  178.     if not reactors[reactor_id].active then
  179.         return
  180.     end
  181.     while true do
  182.         update_reactor_display(reactor_id, reactors[reactor_id].x_offset, reactors[reactor_id].y_offset)
  183.         sleep(10)
  184.     end
  185. end
  186.  
  187. local function update_reactor_A()
  188.     update_reactor("A")
  189. end
  190.  
  191. local function update_reactor_B()
  192.     update_reactor("B")
  193. end
  194.  
  195. local function update_reactor_C()
  196.     update_reactor("C")
  197. end
  198.  
  199. local function update_reactor_D()
  200.     update_reactor("D")
  201. end
  202.  
  203. local function update_powerline()
  204.     while true do
  205.         update_powerline_display(power_lines_x_offset, power_lines_y_offset)
  206.         sleep(10)
  207.     end
  208. end
  209.  
  210. local function update_clock()
  211.     while true do
  212.         update_clock_display(clock_x_offset, clock_y_offset)
  213.         sleep(5)
  214.     end
  215. end
  216.  
  217. local function update_fuel()
  218.     while true do
  219.         update_fuel_display(fuel_x_offset, fuel_y_offset)
  220.         sleep(10)
  221.     end
  222. end
  223.  
  224. print("Starting reactor monitoring")
  225. parallel.waitForAll(update_reactor_A, update_reactor_B, update_reactor_C, update_reactor_D, update_powerline, update_clock, update_fuel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement