Guest User

startup

a guest
Sep 22nd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. local mon=peripheral.wrap("monitor_2")
  2. local ae=peripheral.wrap("appeng_me_tilecontroller_1")
  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 getTotalMEBytes()
  67.    return 0+ae.getTotalBytes()
  68. end
  69.  
  70. local function getFreeMEBytes()
  71. --   return 0
  72.    return 0+ae.getFreeBytes()
  73. end
  74.  
  75. local function isMEOnline()
  76.    return getTotalMEBytes()~=0
  77. end
  78.  
  79. local function updateME()
  80.    local freeBytes = getFreeMEBytes()
  81.    local totalBytes = getTotalMEBytes()
  82.  
  83.    printAt(1,mon_v," ME ",colours.black,iif(isMEOnline(),colours.green,colours.red))
  84.  
  85.    if isMEOnline() then
  86.       local txtColor = iif(freeBytes>totalBytes/2,colors.black,iif(freeBytes<1000,colors.red,colours.green))
  87.       printAt(6,mon_v,ae.getFreeBytes(),txtColor,colours.white)
  88.       setColors(colours.black)
  89.       mon.write("/"..ae.getTotalBytes().." Bytes ")
  90.       mon.write(" "..ae.getStoredItemCount().." Items")
  91.    else
  92.       printAt(6,mon_v,"Offline"..string.rep(" ",mon_h-10),colours.red,colours.white)
  93.    end
  94. end
  95.  
  96. local function updateMFE()
  97.    if not mfe1 then return end
  98.    local capacity=mfe1.getEUCapacity()
  99.    local EUStored=mfe1.getEUStored()
  100.    local percentStored=math.floor((EUStored/capacity)*100)
  101.    local deltaEUStored = math.floor(((EUStored - prevEUStored)/TIMERINTERVAL) / 20)
  102.  
  103.    local xPos=2
  104.    local yPos=5
  105.  
  106.    printAt(xPos,yPos,"MFE 1 ",colours.black,colours.red)
  107.    printAt(xPos+6,yPos,"|",colours.white,colours.black)
  108.    printAt(mon_h-6,yPos,"| "..percentStored.."% ")
  109.  
  110.    drawHorizontalBar(xPos+7,yPos,mon_h-xPos-13,1, percentStored)
  111.    printAt(3,yPos+2,"Total : " .. EUStored.." EU",colours.white, colours.black)
  112.  
  113.    local deltaAsString = ""..deltaEUStored
  114.  
  115.    printAt(3,yPos+3,"Delta : ")
  116.    printAt(11,yPos+3,deltaAsString, iif(deltaEUStored<0,colours.red,colours.white))
  117.    printAt(11+deltaAsString:len(),yPos+3," EU/t               ",colours.white, colours.black)
  118.  
  119.    prevEUStored = EUStored
  120. end
  121.  
  122. local function updateDashboard()
  123.    updateME()
  124.    updateMFE()
  125. end
  126.  
  127. setupScreen()
  128. updateDashboard()
  129.  
  130. while not fExit do
  131.    local e,p1,p2,p3 = os.pullEvent()
  132.    if e=="timer" and p1==timer then  
  133.       updateDashboard()
  134.       timer=os.startTimer(TIMERINTERVAL)
  135.    elseif e=="char" and p1=="x" then
  136.       fExit=true
  137.    else
  138.       print(e.." "..nz(p1,"",":")..nz(p2,"",":")..nz(p3,"",":"))      
  139.    end
  140. end
  141.  
  142. term.clear()
  143. term.setCursorPos(1,1)
  144. print("Monitoring application terminated.")
Advertisement
Add Comment
Please, Sign In to add comment