vvenaya

AEmonitor

Sep 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. local mon=peripheral.wrap("monitor_2")
  2. local ae=peripheral.wrap("appeng_me_tilecontroller_0")
  3. local mfe1=peripheral.wrap("mfe_0")
  4.  
  5. local mon_h,mon_v=mon.getSize()
  6. local fExit=false
  7. local TIMERINTERVAL=4
  8. local timer=os.startTimer(TIMERINTERVAL)
  9.  
  10. local function iif(c,y,n)
  11. if c then return y end
  12. return n
  13. end
  14.  
  15. local function nz(c,r,pfix)
  16. if c then
  17. return (pfix or "")..c
  18. else
  19. return r
  20. end
  21. end
  22.  
  23. local function setColors(foreground,background)
  24. if tonumber(foreground) then
  25. mon.setTextColor(foreground)
  26. end
  27. if tonumber(background) then
  28. mon.setBackgroundColor(background)
  29. end
  30. end
  31.  
  32. local function printAt(x,y,txt,foreground,background)
  33. setColors(foreground,background)
  34. mon.setCursorPos(x,y)
  35. mon.write(txt)
  36. end
  37.  
  38. local function printCenterAt(y,txt,foreground,background)
  39. printAt(1,y,string.rep(" ",mon_h),colours.black,background)
  40. printAt(math.floor((mon_h-string.len(txt))/2),y,txt,foreground,background)
  41. end
  42.  
  43. local function drawHorizontalBar(x,y,w,h,percent)
  44. for j=1,h do
  45. for i=1,w do
  46. local percentOfBar= math.floor((i/w)*100)
  47. local colorIndex=iif(percentOfBar>percent, "gray",
  48. iif(percent>70,"white",
  49. iif(percent>50,"yellow",
  50. iif(percent>15,"orange",
  51. "red"))))
  52. printAt(x+i-1,y+j-1," ",colours.black,colors[colorIndex])
  53. end
  54. end
  55. end
  56.  
  57. local function setupScreen()
  58. setColors(colours.white,colours.black)
  59. mon.clear()
  60. printCenterAt(1,"Vvenaya's Dashboard",colours.black,colours.green)
  61. printAt(1,3,"EU Power storage:",colours.white,colours.black)
  62. printAt(1,mon_v,string.rep(" ",mon_h),colours.black,colours.white)
  63. printAt(1,mon_v,"AE ",nil,colours.green)
  64. end
  65.  
  66. local function updateAE()
  67. local freeBytes = 0 + ae.getFreeBytes()
  68. local totalBytes = 0 + ae.getTotalBytes()
  69. local txtColor = iif(freeBytes>totalBytes/2,colors.black,iif(freeBytes<1000,colors.red,colours.green))
  70.  
  71. printAt(5,mon_v,ae.getFreeBytes(),txtColor,colours.white)
  72. setColors(colours.black)
  73. mon.write("/"..ae.getTotalBytes().." Bytes ")
  74. mon.write(" "..ae.getStoredItemCount().." Items")
  75. end
  76.  
  77. local function updateMFE()
  78. if not mfe1 then return end
  79. local capacity=mfe1.getEUCapacity()
  80. local EUStored=mfe1.getEUStored()
  81. local percentStored=math.floor((capacity/EUStored)*100)
  82.  
  83. local yPos=5
  84.  
  85. printAt(1,yPos,"MFE 1 ",colours.black,colours.red)
  86. printAt(7,yPos,"|",colours.white,colours.black)
  87. printAt(mon_h-7,yPos,"| "..percentStored.."% ")
  88.  
  89. drawHorizontalBar(8,yPos,mon_h-15,1, percentStored)
  90. printAt(3,yPos+2,"Total: " .. EUStored.." EU",colours.white, colours.black)
  91. end
  92.  
  93. local function updateDashboard()
  94. updateAE()
  95. updateMFE()
  96. end
  97.  
  98. setupScreen()
  99. updateDashboard()
  100.  
  101. while not fExit do
  102. local e,p1,p2,p3 = os.pullEvent()
  103. if e=="timer" and p1==timer then
  104. updateDashboard()
  105. timer=os.startTimer(TIMERINTERVAL)
  106. elseif e=="char" and p1=="x" then
  107. fExit=true
  108. else
  109. print(e.." "..nz(p1,"",":")..nz(p2,"",":")..nz(p3,"",":"))
  110. end
  111. end
  112.  
  113. term.clear()
  114. term.write("Monitoring application terminated.")
Advertisement
Add Comment
Please, Sign In to add comment