vvenaya

Untitled

Sep 22nd, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. local function iif(c,y,n)
  3.    if c then return y end
  4.    return n
  5. end
  6.  
  7. local function nz(c,r,pfix)
  8.    if c then
  9.      return (pfix or "")..c
  10.    else
  11.      return r  
  12.    end
  13. end
  14.  
  15. local function findPeripheral(name)
  16.    local names=peripheral.getNames()
  17.    for i,n in ipairs(name) do
  18.       if n:find(name)==1 then
  19.          return peripheral.wrap(n)
  20.       end
  21.    end
  22.    error("could not find a peripheral of type '"..name..")
  23.   return nil
  24. end
  25.  
  26. local mon=findPeripheral("monitor_")
  27. local ae=findPeripheral("appeng_me_tilecontroller_")
  28. local mfe1=findPeripheral("mfe_")
  29.  
  30. local mon_h,mon_v=mon.getSize()
  31. local fExit=false
  32. local TIMERINTERVAL=4
  33. local timer=os.startTimer(TIMERINTERVAL)
  34. local prevEUStored = 0
  35.  
  36. local function setColors(foreground,background)
  37.   if tonumber(foreground) then
  38.      mon.setTextColor(foreground)
  39.   end
  40.   if tonumber(background) then
  41.      mon.setBackgroundColor(background)
  42.   end
  43. end
  44.  
  45. local function printAt(x,y,txt,foreground,background)
  46.   setColors(foreground,background)
  47.   mon.setCursorPos(x,y)
  48.   mon.write(txt)
  49. end
  50.  
  51. local function printCenterAt(y,txt,foreground,background)
  52.   printAt(1,y,string.rep(" ",mon_h),colours.black,background)
  53.   printAt(math.floor((mon_h-string.len(txt))/2),y,txt,foreground,background)
  54. end
  55.  
  56. local function drawHorizontalBar(x,y,w,h,percent)
  57.  for j=1,h do
  58.   for i=1,w do
  59.      local percentOfBar= math.floor((i/w)*100)
  60.      local colorIndex=iif(percentOfBar>percent, "gray",
  61.            iif(percent>70,"white",
  62.            iif(percent>50,"yellow",
  63.            iif(percent>15,"orange",
  64.                 "red"))))
  65.      printAt(x+i-1,y+j-1," ",colours.black,colors[colorIndex])
  66.    end
  67.  end
  68. end
  69.  
  70. local function setupScreen()
  71.   setColors(colours.white,colours.black)
  72.   mon.clear()
  73.   printCenterAt(1,"Vvenaya's Dashboard",colours.black,colours.green)
  74.   printAt(1,3,"EU Power storage:",colours.white,colours.black)
  75.   printAt(1,mon_v,string.rep(" ",mon_h),colours.black,colours.white)
  76. end
  77.  
  78. local function getTotalMEBytes()
  79.   return 0+ae.getTotalBytes()
  80. end
  81.  
  82. local function getFreeMEBytes()
  83. --   return 0
  84.   return 0+ae.getFreeBytes()
  85. end
  86.  
  87. local function getFreeTypes()
  88.   return 0+ae.getRemainingItemTypes()
  89. end
  90.  
  91. local function getStoredTypes()
  92.   return 0+ae.getStoredItemTypes()
  93. end
  94.  
  95. local function isMEOnline()
  96.   return getTotalMEBytes()~=0
  97. end
  98.  
  99.  
  100.  
  101. local function updateME()
  102.   local freeBytes = getFreeMEBytes()
  103.   local totalBytes = getTotalMEBytes()
  104.   local freeTypes = getFreeTypes()
  105.   local totalTypes = getStoredTypes()+getFreeTypes()
  106.  
  107.   printAt(1,mon_v," ME ",colours.black,iif(isMEOnline(),colours.green,colours.red))
  108.  
  109.   if isMEOnline() then
  110.      local txtColor = iif(freeBytes>totalBytes/2,colors.black,iif(freeBytes<1000,colors.red,colours.green))
  111.  
  112.      setColors(colours.black)
  113.      printAt(6,,mon_v," M:",colours.black,colours.white)
  114.      setColors(iif(freeBytes>totalBytes/2,colors.black,iif(freeBytes<1000,colors.red,colours.green)))
  115.      write(ae.getFreeBytes().."/")
  116.      setColors(colours.black)
  117.      write(ae.getTotalBytes().." T:")
  118.      setColors(freeTypes>totalTypes/2,colors.black,iif(freeTypes<10,colors.red,colours.green))
  119.      write(freeTypes)
  120.      setColors(colours.black)
  121.      write("/"..totalTypes.." I:")
  122.      mon.write(" I:"..ae.getStoredItemCount().." Items")
  123.   else
  124.      printAt(6,mon_v,"Offline"..string.rep(" ",mon_h-10),colours.red,colours.white)
  125.   end
  126. end
  127.  
  128. local function updateMFE()
  129.   if not mfe1 then return end
  130.   local capacity=mfe1.getEUCapacity()
  131.   local EUStored=mfe1.getEUStored()
  132.   local percentStored=math.floor((EUStored/capacity)*100)
  133.   local deltaEUStored = math.floor(((EUStored - prevEUStored)/TIMERINTERVAL) / 20)
  134.  
  135.   local xPos=2
  136.   local yPos=5
  137.  
  138.   printAt(xPos,yPos,"MFE 1 ",colours.black,colours.red)
  139.   printAt(xPos+6,yPos,"|",colours.white,colours.black)
  140.   printAt(mon_h-6,yPos,"| "..percentStored.."% ")
  141.  
  142.   drawHorizontalBar(xPos+7,yPos,mon_h-xPos-13,1, percentStored)
  143.   printAt(3,yPos+2,"Total : " .. EUStored.." EU",colours.white, colours.black)
  144.  
  145.   local deltaAsString = ""..deltaEUStored
  146.  
  147.   printAt(3,yPos+3,"Delta : ")
  148.   printAt(11,yPos+3,deltaAsString, iif(deltaEUStored<0,colours.red,colours.white))
  149.   printAt(11+deltaAsString:len(),yPos+3," EU/t               ",colours.white, colours.black)
  150.  
  151.   prevEUStored = EUStored
  152. end
  153.  
  154. local function updateDashboard()
  155.   updateME()
  156.   updateMFE()
  157. end
  158.  
  159. setupScreen()
  160. updateDashboard()
  161.  
  162. while not fExit do
  163.   local e,p1,p2,p3 = os.pullEvent()
  164.   if e=="timer" and p1==timer then  
  165.      updateDashboard()
  166.      timer=os.startTimer(TIMERINTERVAL)
  167.   elseif e=="char" and p1=="x" then
  168.      fExit=true
  169.   else
  170.      print(e.." "..nz(p1,"",":")..nz(p2,"",":")..nz(p3,"",":"))      
  171.   end
  172. end
  173.  
  174. term.clear()
  175. term.setCursorPos(1,1)
  176. print("Monitoring application terminated.")
Advertisement
Add Comment
Please, Sign In to add comment