vvenaya

AEmonitor

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