Advertisement
Banane9

networkMonitor

Jul 2nd, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. blocks = {}
  2.  
  3. blocks.MJ = {}
  4. blocks.MJ.Storage = {"redstone_energy_cell"}
  5. blocks.MJ.Producer = {"engine", "engineBlock"}
  6. blocks.MJ.Consumer = {"machine", "factory"}
  7.  
  8. blocks.MJ.Functions = {getEnergyReceive = {{name = "value", vType = "number", alias = "Maximum Energy Received", unit = "MJ/t"}}, getEnergySend = {{name = "value", vType = "number", alias = "Maximum Energy Send", unit = "MJ/t"}}, getPowerProvider = {{name = "maxEnergyStored", vType = "number", alias = "Maximum Energy Stored", unit = "MJ"}, {name = "energyStored", vType = "number", alias = "Current Energy Stored", unit = "MJ"}}, getPower = {{name = "value", vType = "number", alias = "Current Energy Production", unit = "MJ/t"}}}
  9.  
  10. --[[blocks.EU = {}
  11. blocks.EU.Storage = {"blockElectric"}
  12. blocks.EU.Producer = {"blockGenerator"}
  13. blocks.EU.Consumer = {"blockMachine"}
  14.  
  15. blocks.EU.Functions = {}]]
  16.  
  17. clear = function()
  18.     term.clear()
  19.     term.setCursorPos(1, 1)
  20. end
  21.  
  22. getNetworkStats = function(network)
  23.     local stats = {}
  24.     for blockType, blockNames in pairs(network) do
  25.         if blockType ~= "Functions" then
  26.             for blockName, blocks in pairs(blockNames) do
  27.                 for _, block in pairs(blocks) do
  28.                     for functionName, functionUsages in pairs(network.Functions) do
  29.                         if type(block[functionName]) == "function" then
  30.                             functionValue = block[functionName]()
  31.                             for __, usage in pairs(functionUsages) do
  32.                                 if usage.name == "value" then
  33.                                     if usage.vType == "number" then
  34.                                         functionValue = {[usage.name] = tonumber(functionValue)}
  35.                                     else
  36.                                         functionValue = {[usage.name] = functionValue}
  37.                                     end
  38.                                 end
  39.                                 if type(functionValue[usage.name]) == usage.vType then
  40.                                     if not stats[usage.alias] then
  41.                                         stats[usage.alias] = {}
  42.                                         if usage.vType == "number" then
  43.                                             stats[usage.alias].value = 0
  44.                                         end
  45.                                     end
  46.                                     if usage.vType == "number" then
  47.                                         stats[usage.alias].value = stats[usage.alias].value + functionValue[usage.name]
  48.                                         stats[usage.alias].unit = usage.unit
  49.                                     end
  50.                                 end
  51.                             end
  52.                         end
  53.                     end
  54.                 end
  55.             end
  56.         end
  57.     end
  58.     return stats
  59. end
  60.  
  61. scanNetwork = function(blocklist)
  62.     local network = {}
  63.     for blockType, blockNames in pairs(blocklist) do
  64.         if blockType ~= "Functions" then
  65.             for _, blockName in pairs(blockNames) do
  66.                 if not network[blockType] then
  67.                     network[blockType] = {}
  68.                 end
  69.                 if not network[blockType][blockName] then
  70.                     network[blockType][blockName] = {}
  71.                 end
  72.             end
  73.         else
  74.             network[blockType] = blockNames
  75.         end
  76.     end
  77.  
  78.     for _, side in pairs(peripheral.getNames()) do
  79.         for blockType, blockNames in pairs(network) do
  80.             if blockType ~= "Functions" then
  81.                 for blockName, blocks in pairs(blockNames) do
  82.                     if string.find(side, blockName.."_") then
  83.                         network[blockType][blockName][#blocks + 1] = peripheral.wrap(side)
  84.                         print("    Added ", blockType, " ", blockName, " (", side, ")!")
  85.                     end
  86.                 end
  87.             end
  88.         end
  89.     end
  90.     return network
  91. end    
  92.  
  93. print("Initialising...")
  94.  
  95. networks = {}
  96. for networkName, networkComponents in pairs(blocks) do
  97.     print("  Scanning for ", networkName, " Components!")
  98.     networks[networkName] = scanNetwork(networkComponents)
  99.     print("  Successfully scanned for ", networkName, " Components!")
  100. end
  101.  
  102. print("  Checking for Refreshinterval argument...")
  103. refreshTime = tonumber((...))
  104. if not refreshTime then
  105.     print("    No Argument found! Defaulting to 30s!")
  106.     refreshTime = 30
  107. else
  108.     print("    Argument found! Refreshinterval set to ", refreshTime, "s!")
  109. end
  110.  
  111. print("Initialised successfully!")
  112. os.startTimer(2.5)
  113. while true do
  114.     event, param1, param2, param3 = os.pullEvent()
  115.     if event == "timer" then
  116.         networkStats = {}
  117.         for networkName, networkComponents in pairs(networks) do
  118.             networkStats[networkName] = getNetworkStats(networkComponents)
  119.         end
  120.         clear()
  121.         for networkName, stats in pairs(networkStats) do
  122.             print("Statistics for ", networkName, " Network:")
  123.             for statName, statInfo in pairs(stats) do
  124.                 print("  ", statName, ": ", statInfo.value, " ", statInfo.unit)
  125.             end
  126.             print("")
  127.         end
  128.        
  129.         os.startTimer(refreshTime)
  130.     elseif event == "peripheral" or event == "peripheral_detach" then
  131.         print("Change in connected Peripherals detected!")
  132.         local relevantTo = nil
  133.         for networkName, network in pairs(networks) do
  134.             for blockType, blockNames in pairs(network) do
  135.                 if blockType ~= "Function" then
  136.                     for blockName, _ in pairs(blockNames) do
  137.                         if string.find(param1, blockName.."_") then
  138.                             relevantTo = networkName
  139.                         end
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.         if relevantTo then
  145.             print("  Change relevant to ", relevantTo, " Network! Rescanning...")
  146.             networks[relevantTo] = scanNetwork(blocks[relevantTo])
  147.             print("  Rescanned ", relevantTo, " Network successfully!")
  148.         end
  149.     end
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement