Advertisement
Guest User

reactor_monitor

a guest
Apr 9th, 2020
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local reactor = peripheral.find("BigReactors-Reactor")
  3.  
  4. local rodSize = 4
  5.  
  6. function clear()
  7.     term.clear()
  8.     term.setBackgroundColor(colors.black)
  9.     term.setCursorPos(1,1)
  10. end
  11.  
  12. function drawRods(sx, sy)
  13.  
  14.     local controlRods = reactor.getNumberOfControlRods()
  15.  
  16.     for i=1,controlRods do
  17.         term.setCursorPos(1,1)
  18.         local percent = reactor.getControlRodLevel(i-1)/100.0
  19.        
  20.         local h = math.floor(percent*rodSize)
  21.        
  22.         if percent == 1 then
  23.             h = rodSize-1
  24.         end
  25.        
  26.         local left = (rodSize-h)-1
  27.         local x = sx+i*2
  28.         local y = sy
  29.        
  30.         if i > 10 then
  31.             y = y + rodSize+1
  32.             x = sx + (i-10)*2
  33.         end
  34.        
  35.         --write(tostring(i)..' '..tostring(h))
  36.         --x,y,width,height
  37.         paintutils.drawFilledBox(x, y, x, y+h, colours.green)
  38.        
  39.         if percent < 1 then
  40.           paintutils.drawFilledBox(x, y+h, x, (y+h)+left, colours.red)
  41.         end
  42.     end
  43.  
  44.     term.setBackgroundColor(colours.black)
  45. end
  46.  
  47. function drawHeader()
  48.     local activeText = "Active"
  49.    
  50.     if not reactor.getActive() then
  51.         activeText = "Disabled"
  52.     end
  53.    
  54.     write("Reactor "..activeText.."\n")
  55. end
  56.  
  57. function discordNotify(botName, message)
  58.     local postData = "{ \"username\" : \""..botName.."\", \"content\" : \""..message.."\"}"
  59.     local headers = {
  60.         ["Content-Type"] = "application/json"
  61.     }
  62.  
  63.  write(postData)
  64.     -- Make a HTTP request
  65.     local request,err = http.post("https://discordapp.com/api/webhooks/696901545643606046/Ki7Un8TIliV9qpMNtOaWdevL_z1YcRYw-kpZsVo18TvueOqI3UesNowY2b5ZlZhYq4Zj", postData, headers)
  66.    
  67.  if not request then error(err) end
  68.  
  69.  request.close()
  70. end
  71.  
  72. function notifyReactorStatus(fuelPercent)
  73.            
  74.     local activeText = "Active"
  75.  
  76.     if not reactor.getActive() then
  77.         activeText = "Disabled"
  78.     end
  79.  
  80.     reactorInfo = ""
  81.     reactorInfo = reactorInfo.."Status: "..activeText.."\\n"
  82.     reactorInfo = reactorInfo.."Fuel: "..reactor.getFuelAmount().." mb "..fuelPercent.."%\\n"
  83.     reactorInfo = reactorInfo.."Energy last Tick: "..reactor.getEnergyProducedLastTick().." RF"
  84.  
  85.     if fuelPercent < 10 then
  86.         reactorInfo = reactorInfo.."\\n\\nAlert: Fuel bellow 10%!"
  87.     end
  88.  
  89.     discordNotify("Reactor Info", "```md\\n#Current Reactor Info\\n=============\\n\\n"..reactorInfo.."```")    
  90. end
  91.  
  92. term.redirect(monitor)
  93. monitor.setTextScale(0.5)
  94.  
  95. local updates = 0
  96. local screenUpdateEvery = 10
  97.  
  98. local notifySent = false
  99.  
  100. while true do
  101.     if updates >= screenUpdateEvery then
  102.         clear()
  103.         drawHeader()
  104.         drawRods(1, 4)
  105.        
  106.         updates = 0
  107.     end
  108.  
  109.     local time = textutils.formatTime(os.time("local"), true)
  110.  
  111.     local fuelPercent = math.floor((reactor.getFuelAmount()*1.0/reactor.getFuelAmountMax())*100.0)
  112.  
  113.     --Send reactor status twice a day
  114.     if not notifySent and (time == "17:00" or time == "10:00") then
  115.         notifySent = true
  116.        
  117.         notifyReactorStatus(fuelPercent)
  118.     end
  119.    
  120.     --Prevent multiple notify per hour
  121.     if notifySent and (time == "18:00" or time == "11:00") then
  122.         notifySent = false
  123.     end
  124.  
  125.  --notifyReactorStatus(fuelPercent)
  126.  
  127.     sleep(1)
  128.     updates = updates + 1
  129. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement