osmarks

EnerGraph

Apr 22nd, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TODO: actually make graph?
  2.  
  3. local monitor = peripheral.find "monitor"
  4. local storage = peripheral.find(settings.get "storage_type" or "draconic_rf_storage")
  5. local capacity = (storage.getMaxEnergyStored or storage.getEnergyCapacity)()
  6. local delay = 0.1
  7. local ticks_delay = 0.1 / 0.05
  8.  
  9. local function read_energy()
  10.     return storage.getEnergyStored()
  11. end
  12.  
  13. monitor.setTextScale(1)
  14. monitor.setBackgroundColor(colors.black)
  15. monitor.setTextColor(colors.white)
  16. local data = {}
  17.  
  18. local prefixes = {"", "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"}
  19. local function SI_prefix(value, unit)
  20.     local i = 1
  21.     local x = value
  22.     while x > 1000 or x < -1000 do
  23.         x = x / 1000
  24.         i = i + 1
  25.     end
  26.     return ("%.3f%s%s"):format(x, prefixes[i], unit)
  27. end
  28.  
  29. local function display(data)
  30.     monitor.clear()
  31.     local longest_label = 0
  32.     for _, val in pairs(data) do
  33.         if #val[1] > longest_label then longest_label = #val[1] end
  34.     end
  35.     local i = 1
  36.     for _, val in pairs(data) do
  37.         monitor.setCursorPos(1, i)
  38.         monitor.write(val[1] .. ":" .. (" "):rep(longest_label - #val[1] + 2) .. val[2])
  39.         i = i + 1
  40.     end
  41. end
  42.  
  43. local past_RF_per_tick = {}
  44. local history_length = 1200 / ticks_delay
  45. local function display_stats()
  46.     local previous
  47.     while true do
  48.         local energy = read_energy()
  49.         if previous then
  50.             local diff = energy - previous
  51.             local RF_per_tick = diff / ticks_delay
  52.             table.insert(past_RF_per_tick, RF_per_tick)
  53.             if #past_RF_per_tick > history_length then table.remove(past_RF_per_tick, 1) end
  54.             local total = 0
  55.             for _, h in pairs(past_RF_per_tick) do total = total + h end
  56.             local average = total / #past_RF_per_tick
  57.            
  58.             display {
  59.                 { "Time", ("%s.%03d"):format(os.date "!%X", os.epoch "utc" % 1000) },
  60.                 { "Stored", SI_prefix(energy, "RF") },
  61.                 { "Capacity", SI_prefix(capacity, "RF") },
  62.                 { "% filled", ("%.4f%%"):format(energy / capacity * 100) },
  63.                 { "Inst I/O", SI_prefix(RF_per_tick, "RF/t") },
  64.                 { "60s I/O" , SI_prefix(average, "RF/t") },
  65.             }
  66.         end
  67.         previous = energy
  68.         sleep(delay)
  69.     end
  70. end
  71.  
  72. display_stats()
Add Comment
Please, Sign In to add comment