bobmarley12345

mepowermonitor

Oct 30th, 2020 (edited)
781
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MonitorSide = "top"
  2. GlassesSide = "left"
  3. BackupGeneratorSide = "top"
  4. Monitor1 = peripheral.wrap("monitor_82")
  5. Monitor2 = peripheral.wrap("monitor_83")
  6. Glasses = peripheral.wrap(GlassesSide)
  7. EmergencyActivationPercentage = 50
  8. cell1 = peripheral.wrap("cofh_thermalexpansion_energycell_118")
  9. cell2 = peripheral.wrap("cofh_thermalexpansion_energycell_119")
  10. cell3 = peripheral.wrap("cofh_thermalexpansion_energycell_120")
  11. cell4 = peripheral.wrap("cofh_thermalexpansion_energycell_121")
  12. cell5 = peripheral.wrap("cofh_thermalexpansion_energycell_122")
  13. cell6 = peripheral.wrap("cofh_thermalexpansion_energycell_123")
  14. cell7 = peripheral.wrap("cofh_thermalexpansion_energycell_124")
  15. cell8 = peripheral.wrap("cofh_thermalexpansion_energycell_125")
  16.  
  17. function GetHexFromColour(clr)
  18.     local colourOut = 0xCC4C4C --red
  19.     if (clr == colours.white)  then colourOut = 0xFFFFFF end
  20.     if (clr == colours.orange) then colourOut = 0xF2B233 end
  21.     if (clr == colours.yellow) then colourOut = 0xDEDE6C end
  22.     if (clr == colours.green)  then colourOut = 0x57A64E end
  23.     if (clr == colours.cyan)   then colourOut = 0x4C99B2 end
  24.     return colourOut;
  25. end
  26.  
  27. function GetColourFromPercent(percentage)
  28.     local colourOut = colours.red
  29.     if (percentage > 15) then colourOut = colours.orange end
  30.     if (percentage > 45) then colourOut = colours.yellow end
  31.     if (percentage > 70) then colourOut = colours.green end
  32.     if (percentage > 90) then colourOut = colours.cyan end
  33.     return colourOut
  34. end
  35.  
  36. function ClearScreens()
  37.     Monitor1.setTextColor(colours.black)
  38.     Monitor1.setBackgroundColor(colours.black)
  39.     Monitor1.clear()
  40.     Monitor1.setCursorPos(1,1)
  41.     Monitor2.setTextColor(colours.black)
  42.     Monitor2.setBackgroundColor(colours.black)
  43.     Monitor2.clear()
  44.     Monitor2.setCursorPos(1,1)
  45.     Glasses.clear()
  46. end
  47.  
  48. function DrawText(xPos, yPos, text, textColour, backgroundColour)
  49.     Monitor1.setBackgroundColor(backgroundColour)
  50.     Monitor1.setTextColor(textColour)
  51.     Monitor1.setCursorPos(xPos,yPos)
  52.     Monitor1.write(text)
  53.     Monitor2.setBackgroundColor(backgroundColour)
  54.     Monitor2.setTextColor(textColour)
  55.     Monitor2.setCursorPos(xPos,yPos)
  56.     Monitor2.write(text)
  57.     Glasses.addText(xPos * 5, yPos * 8, text, GetHexFromColour(textColour))
  58. end
  59.  
  60. function DrawLine(xPos, yPos, lineLength, colour)
  61.     Monitor1.setBackgroundColor(colour)
  62.     Monitor1.setTextColor(colour)
  63.     Monitor1.setCursorPos(xPos,yPos)
  64.     Monitor1.write(string.rep(" ", lineLength))
  65.     Monitor2.setBackgroundColor(colour)
  66.     Monitor2.setTextColor(colour)
  67.     Monitor2.setCursorPos(xPos,yPos)
  68.     Monitor2.write(string.rep(" ", lineLength))
  69.     Glasses.addBox(xPos * 5, yPos * 5, lineLength * 5, 5, GetHexFromColour(colour), 1)
  70. end
  71.  
  72. function ProgressBar(xPos, yPos, barLength, value, maxValue, backgroundColour, progressColour)
  73.     DrawLine(xPos, yPos, barLength, backgroundColour) --backgoround bar
  74.     local barSize = math.floor((value/maxValue) * barLength)
  75.     DrawLine(xPos, yPos, barSize, progressColour) --progress so far
  76. end
  77.  
  78. function DrawPowerBar(monitorX, yPos, cellName, cellEnergy, cellMaxEnergy, cellPercent, barColour)
  79.     DrawText(3, yPos, cellName, colours.white, colours.black)
  80.     ProgressBar(10, yPos, monitorX - 15, cellEnergy, cellMaxEnergy, barColour, GetColourFromPercent(cellPercent))
  81.     DrawText(monitorX - 4, yPos, cellPercent .. "%", colours.white, colours.black)
  82. end
  83.  
  84. function FetchAndRedraw()
  85.     Monitor1.setTextScale(1)
  86.     Monitor2.setTextScale(1)
  87.     local monX, monY = Monitor1.getSize()
  88.  
  89.     local cell1Energy = cell1.getEnergyStored("unknown")
  90.     local cell2Energy = cell2.getEnergyStored("unknown")
  91.     local cell3Energy = cell3.getEnergyStored("unknown")
  92.     local cell4Energy = cell4.getEnergyStored("unknown")
  93.     local cell5Energy = cell5.getEnergyStored("unknown")
  94.     local cell6Energy = cell6.getEnergyStored("unknown")
  95.     local cell7Energy = cell7.getEnergyStored("unknown")
  96.     local cell8Energy = cell8.getEnergyStored("unknown")
  97.  
  98.     local cell1MaxEnergy = cell1.getMaxEnergyStored("unknown")
  99.     local cell2MaxEnergy = cell2.getMaxEnergyStored("unknown")
  100.     local cell3MaxEnergy = cell3.getMaxEnergyStored("unknown")
  101.     local cell4MaxEnergy = cell4.getMaxEnergyStored("unknown")
  102.     local cell5MaxEnergy = cell5.getMaxEnergyStored("unknown")
  103.     local cell6MaxEnergy = cell6.getMaxEnergyStored("unknown")
  104.     local cell7MaxEnergy = cell7.getMaxEnergyStored("unknown")
  105.     local cell8MaxEnergy = cell8.getMaxEnergyStored("unknown")
  106.  
  107.     local cell1Percent = math.floor((cell1Energy / cell1MaxEnergy) * 100)
  108.     local cell2Percent = math.floor((cell2Energy / cell2MaxEnergy) * 100)
  109.     local cell3Percent = math.floor((cell3Energy / cell3MaxEnergy) * 100)
  110.     local cell4Percent = math.floor((cell4Energy / cell4MaxEnergy) * 100)
  111.     local cell5Percent = math.floor((cell5Energy / cell5MaxEnergy) * 100)
  112.     local cell6Percent = math.floor((cell6Energy / cell6MaxEnergy) * 100)
  113.     local cell7Percent = math.floor((cell7Energy / cell7MaxEnergy) * 100)
  114.     local cell8Percent = math.floor((cell8Energy / cell8MaxEnergy) * 100)
  115.  
  116.     local c1c
  117.     local c2c
  118.     local c3c
  119.     local c4c
  120.     local c5c
  121.     local c6c
  122.     local c7c
  123.     local c8c
  124.  
  125.     if (cell1Percent == 0) then c1c = colours.red else c1c = colours.grey end
  126.     if (cell2Percent == 0) then c2c = colours.red else c2c = colours.grey end
  127.     if (cell3Percent == 0) then c3c = colours.red else c3c = colours.grey end
  128.     if (cell4Percent == 0) then c4c = colours.red else c4c = colours.grey end
  129.     if (cell5Percent == 0) then c5c = colours.red else c5c = colours.grey end
  130.     if (cell6Percent == 0) then c6c = colours.red else c6c = colours.grey end
  131.     if (cell7Percent == 0) then c7c = colours.red else c7c = colours.grey end
  132.     if (cell8Percent == 0) then c8c = colours.red else c8c = colours.grey end
  133.  
  134.     local totalEnergyPercentage = cell1Percent + cell2Percent + cell3Percent + cell4Percent + cell5Percent + cell6Percent + cell7Percent + cell8Percent
  135.     local shouldEmergencyPowerBeOn = totalEnergyPercentage < EmergencyActivationPercentage
  136.  
  137.     ClearScreens()
  138.  
  139.     DrawPowerBar(monX, 2,  "Cell 1", cell1Energy, cell1MaxEnergy, cell1Percent, c1c)
  140.     DrawPowerBar(monX, 4,  "Cell 2", cell2Energy, cell2MaxEnergy, cell2Percent, c2c)
  141.     DrawPowerBar(monX, 6,  "Cell 3", cell3Energy, cell3MaxEnergy, cell3Percent, c3c)
  142.     DrawPowerBar(monX, 8,  "Cell 4", cell4Energy, cell4MaxEnergy, cell4Percent, c4c)
  143.     DrawPowerBar(monX, 10, "Cell 5", cell5Energy, cell5MaxEnergy, cell5Percent, c5c)
  144.     DrawPowerBar(monX, 12, "Cell 6", cell6Energy, cell6MaxEnergy, cell6Percent, c6c)
  145.     DrawPowerBar(monX, 14, "Cell 7", cell7Energy, cell7MaxEnergy, cell7Percent, c7c)
  146.     DrawPowerBar(monX, 16, "Cell 8", cell8Energy, cell8MaxEnergy, cell8Percent, c8c)
  147.  
  148.     DrawText(3, 18, "Emergency Power", colours.white, colours.black)
  149.  
  150.     if (shouldEmergencyPowerBeOn) then
  151.         redstone.setOutput(BackupGeneratorSide, true)
  152.         DrawText(3, 19, "ONLINE", colours.white, colours.green)
  153.  
  154.     else
  155.         redstone.setOutput(BackupGeneratorSide, false)
  156.         DrawText(3, 19, "OFFLINE", colours.white, colours.red)
  157.     end
  158. end
  159.  
  160. while true do
  161.     FetchAndRedraw()
  162.     os.sleep(2)
  163. end
RAW Paste Data