PlowmanPlow

ComputerCraft - BigReactor Monitor

Feb 25th, 2016
122
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local statusChannelID = 5
  2. local controlChannelID = 7
  3. local reactorActivateP = 5
  4.  
  5. -- Find Modem
  6. local modem = peripheral.find("modem", function(n, o) return o.isWireless() end)
  7. if modem ~= nil then
  8.    modem.open(controlChannelID)
  9. end
  10.  
  11. -- Find Reactor
  12. local reactor = peripheral.find("BigReactors-Reactor")
  13. if reactor == nil then
  14.    error("Could not bind to reactor. Is one attached?")
  15. end
  16.  
  17. -- Find Tesseract side
  18. local tessSide = nil
  19. for sideId, side in ipairs(redstone.getSides()) do
  20.    if peripheral.getType(side) == "tile_thermalexpansion_ender_tesseract_name" then
  21.       tessSide = side
  22.    end
  23. end
  24.  
  25. term.clear()
  26. term.setCursorPos(1,1)
  27. print("BigReactor Monitor...")
  28. print()
  29. if tessSide ~= nil then
  30.    print("Tesseract is present")
  31. else
  32.    print("Tesseract is absent")
  33. end
  34. print("Reactor Output: Enabled")
  35. print("Reactor Status: Inactive")
  36. print("Reactor Buffer: ")
  37. print("Reactor Fuel: ")
  38. print("Reactor Generation: ")
  39. print()
  40. print("Press 'q' to quit.")
  41. print()
  42.  
  43. if tessSide ~= nil then rs.setOutput(tessSide, false) end
  44. local reactorEnabled = true
  45. reactor.setActive(false)
  46. local rActive = reactor.getActive()
  47. local pollTimer = os.startTimer(0)
  48. local status = {}
  49. while true do
  50.    local event, p1, p2, p3, p4 = os.pullEvent()
  51.    if event == "key" then
  52.       if p1 == 16 then -- 'q'
  53.          term.setCursorPos(1, 6)
  54.          return
  55.       end
  56.    elseif event == "modem_message" and p2 == controlChannelID then
  57.       local message = p4
  58.       if message == "enabled" then
  59.          reactorEnabled = true
  60.          term.setCursorPos(1,4)
  61.          term.write("Reactor output: Enabled ")
  62.          if tessSide ~= nil then rs.setOutput(tessSide, true) end
  63.       elseif message == "disabled" then
  64.          reactorEnabled = false
  65.          term.setCursorPos(1,4)
  66.          term.write("Reactor output: Disabled")
  67.          if tessSide ~= nil then rs.setOutput(tessSide, false) end
  68.       end
  69.    elseif event == "timer" and p1 == pollTimer then
  70.       local rEnergyP = (reactor.getEnergyStored() / 10000000) * 100
  71.       if rActive == false and rEnergyP <= reactorActivateP then
  72.          reactor.setActive(true)
  73.       end
  74.       if rActive == true and rEnergyP >= 95 and reactorEnabled == false then
  75.          reactor.setActive(false)
  76.       end
  77.       rActive = reactor.getActive()
  78.       if rActive == true then
  79.          status["Reactor Status"] = "Active"
  80.       else
  81.          status["Reactor Status"] = "Inactive"
  82.       end
  83.       term.setCursorPos(1,5)
  84.       term.write("Reactor Status: " .. status["Reactor Status"] .. "   ")
  85.       status["Reactor Buffer"] = math.floor(rEnergyP) .. "%"
  86.       term.setCursorPos(1,6)
  87.       term.write("Reactor Buffer: " .. status["Reactor Buffer"] .. "   ")
  88.       status["Reactor Fuel"] = math.floor((reactor.getFuelAmount() / reactor.getFuelAmountMax()) * 100) .. "%"
  89.       term.setCursorPos(1,7)
  90.       term.write("Reactor Fuel: " .. status["Reactor Fuel"] .. "   ")
  91.       status["Reactor Generation"] = math.floor(reactor.getEnergyProducedLastTick()) .. " RF/t"
  92.       term.setCursorPos(1,8)
  93.       term.write("Reactor Generation: " .. status["Reactor Generation"] .. "          ")
  94.       modem.transmit(statusChannelID, 0, textutils.serialize(status))
  95.       pollTimer = os.startTimer(5)
  96.    end
  97. end
RAW Paste Data