Taw_

ME I/O Rate Monitor

Aug 24th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. chest = peripheral.find("chest")
  2. me = peripheral.find("aemultipart")
  3. mon = peripheral.find("monitor")
  4. dataMax = 300
  5. readsPerSecond = 2
  6. items = {}
  7.  
  8. function linReg(data)
  9.     if #data == 0 then return 0,0 end
  10.     local i
  11.     local x,y,x2,y2,xy = 0,0,0,0,0
  12.     for i=1,#data do
  13.         x = x + i
  14.         y = y + data[i]
  15.         x2 = x2 + i*i
  16.         y2 = y2 + data[i]*data[i]
  17.         xy = xy + i*data[i]
  18.     end
  19.     local a,b = (x*x2-x*xy)/(#data*x2-x*x),(#data*xy-x*y)/(#data*x2-x*x)
  20.     if a ~= a then a = 0 end
  21.     if b ~= b then b = 0 end
  22.     return a,b
  23. end
  24.  
  25. function buildItemList()
  26.     chest = peripheral.find("chest")
  27.     me = peripheral.find("aemultipart")
  28.     if not chest or not me then
  29.         return
  30.     end
  31.     local list = {}
  32.     local stacks = chest.getAllStacks()
  33.     for k,v in pairs(stacks) do
  34.         local a = v.all()
  35.         local data = {
  36.             current = -1,
  37.             slope = 0,
  38.             name = a.display_name,
  39.             id = a.id,
  40.             dmg = a.dmg,
  41.             count = {}
  42.         }
  43.         if items and items[a.dmg.."~"..a.id] then
  44.             data.count = items[a.dmg.."~"..a.id].count
  45.         end
  46.         if #data.count >= dataMax then table.remove(data.count,1) end
  47.         local value = nil
  48.         local ok = pcall(function() value = me.getItemDetail(a) end)
  49.         if ok then
  50.             data.current = value.basic().qty
  51.         end
  52.         table.insert(data.count,data.current)
  53.         local b,m = linReg(data.count)
  54.         data.slope = m
  55.         list[a.dmg.."~"..a.id] = data
  56.     end
  57.     items = list
  58. end
  59.  
  60. function round(x)
  61.     if x%2 ~= 0.5 then
  62.         return math.floor(x+0.5)
  63.     end
  64.     return x-0.5
  65. end
  66.  
  67. function getScaledSlope(slope)
  68.     return round(slope*readsPerSecond*getScale()*100)/100
  69. end
  70.  
  71. function getScale()
  72.     return 60
  73. end
  74.  
  75. function getScaledUnits()
  76.     return "min"
  77. end
  78.  
  79. function showStats()
  80.     term.redirect(mon)
  81.     local r=1
  82.     local w,h = term.getSize()
  83.     for k,v in pairs(items) do
  84.         term.setCursorPos(1,r)
  85.         if #v.count < dataMax/3 then
  86.             term.setTextColor(colors.gray)
  87.         elseif #v.count < dataMax*2/3 then
  88.             term.setTextColor(colors.lightGray)
  89.         else
  90.             term.setTextColor(colors.white)
  91.         end
  92.         term.clearLine()
  93.         term.write(#v.count.." ")
  94.         term.setCursorPos(5,r)
  95.         term.setTextColor(colors.white)
  96.         term.write(v.name..": ")
  97.         term.write(v.current.." ")
  98.         term.write("("..getScaledSlope(v.slope).." / "..getScaledUnits()..")")
  99.         r = r + 1
  100.     end
  101.     while r <= h do
  102.         term.setCursorPos(1,r)
  103.         term.clearLine()
  104.         r = r + 1
  105.     end
  106. end
  107.  
  108. while true do
  109.     buildItemList()
  110.     showStats()
  111.     sleep(1/readsPerSecond)
  112. end
Add Comment
Please, Sign In to add comment