Guest User

Untitled

a guest
Dec 11th, 2012
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. local util = api.require("util")
  2. local sensor = peripheral.wrap("left")
  3. rednet.open("right")
  4. local config = dofile("storage.config")
  5. local reactorMonitorId = nil
  6. local statsSystemId = nil
  7. local bankData = nil
  8.  
  9.  
  10. local function handleReactorMonitor(senderId, message)
  11.     if message.command == "announce" then
  12.         reactorMonitorId = senderId
  13.         rednet.send(senderId, textutils.serialize{type="reactor", sender="storage", command="ack", id=config.id})
  14.     elseif message.command == "ack" and message.id == config.id then
  15.         reactorMonitorId = senderId    
  16.     end
  17. end
  18.  
  19.  
  20. local function handleStatsSystem(senderId, message)
  21.     if message.command == "announce" then
  22.         statsSystemId = senderId
  23.         rednet.send(storageSystemId, textutils.serialize{type="reactor", sender="storage", command="ack", id=config.id})
  24.     elseif message.command == "ack" and message.id == config.id then
  25.         statsSystemId = senderId
  26.     end
  27. end
  28.  
  29.  
  30. local function doNetwork()
  31.     print("Starting network...")
  32.     rednet.broadcast(textutils.serialize{type="reactor", sender="storage", command="announce", id=config.id})
  33.     while true do
  34.         local event, senderId, data, distance = os.pullEvent("rednet_message")
  35.         local message = textutils.unserialize(data)
  36.         if message and message.type == "reactor" then
  37.             if message.sender== "monitor" then
  38.                 handleReactorMonitor(senderId, message)
  39.             elseif message.sender == "stats" then
  40.                 handleStatsSystem(senderId, message)           
  41.             end
  42.         end
  43.     end
  44.     print("network coro exiting")
  45. end
  46.  
  47.  
  48. local function drawMFSUs() 
  49.     print("MFSUs:")
  50.     print("-------------------------------------")
  51.     for i = 1, #bankData.mfsus, 4 do
  52.         local blockOne = bankData.mfsus[i]
  53.         local blockTwo = bankData.mfsus[i+1]
  54.         local blockThree = bankData.mfsus[i+2]
  55.         local blockFour = bankData.mfsus[i+3]
  56.         local blockOneOutput = blockOne and string.format("%3d%%", blockOne.Stored / blockOne.Capacity * 100) or "    "
  57.         local blockTwoOutput = blockTwo and string.format("%3d%%", blockTwo.Stored / blockTwo.Capacity * 100) or "    "
  58.         local blockThreeOutput = blockThree and string.format("%3d%%", blockThree.Stored / blockThree.Capacity * 100) or "    "
  59.         local blockFourOutput = blockFour and string.format("%3d%%", blockFour.Stored / blockFour.Capacity * 100) or "    "
  60.         print(string.format("|  %s  |  %s  |  %s  |  %s  |", blockOneOutput, blockTwoOutput, blockThreeOutput, blockFourOutput))
  61.     end
  62.     print("-------------------------------------")
  63. end
  64.  
  65. local function doMenu()
  66.     print("Displaying menu...")
  67.     while true do
  68.         sleep(1)
  69.         term.clear()
  70.         term.setCursorPos(1,1)
  71.         if bankData then
  72.             print(string.format("EU Stored:     %s", util.commavalue(bankData.stored)))
  73.             print(string.format("EU Capacity:   %s", util.commavalue(bankData.capacity)))
  74.             print(string.format("EU Max output: %s", util.commavalue(bankData.maxOutput)))
  75.             drawMFSUs()
  76.         else
  77.             print("Scanning for data...")
  78.         end
  79.         print("Press Q to exit")
  80.     end
  81.     print("menu coro exiting")
  82. end
  83.  
  84. local function doKeyboard()
  85.     print("Starting keyboard handler...")
  86.     while true do
  87.         local event, key = os.pullEvent("char")
  88.         if string.lower(key) == "q" then
  89.             break
  90.         end
  91.     end
  92.     print("keyboard coro exiting")
  93. end
  94.  
  95. local function sendStats()
  96.     if reactorMonitorId then
  97.         rednet.send(reactorMonitorId, textutils.serialize{type="reactor", sender="storage", command="capacity", id = config.id, stored = bankData.stored, capacity = bankData.capacity })
  98.     end
  99.     if statsSystemId then
  100.         rednet.send(statsSystemId, textutils.serialize{type="reactor", sender="storage", command="stats", id = config.id, stats = bankData})
  101.     end
  102. end
  103.  
  104. local function scanForData(newData)
  105.     local dataFound = false
  106.     local targets = sensor.getTargets()
  107.     local foundTarget = false
  108.     if targets then            
  109.         for targetId, target in pairs(targets) do          
  110.             local position = util.strsplit(",", targetId)
  111.             if target.type == "MFSU" and util.inBounds(config.volume, tonumber(position[1]), tonumber(position[2]), tonumber(position[3])) then
  112.                 local targetStats = sensor.getTargetDetails(targetId)
  113.                 if targetStats then
  114.                     dataFound = true
  115.                     table.insert(newData.mfsus, targetStats)
  116.                     newData.capacity = newData.capacity + targetStats.Capacity
  117.                     newData.stored = newData.stored + targetStats.Stored
  118.                     newData.maxOutput = newData.maxOutput + targetStats.Output
  119.                 end
  120.             end
  121.         end
  122.     end
  123.     return dataFound
  124. end
  125.  
  126. local homeX, homeY, homeZ = nil
  127. local function doScanning()
  128.     while true do
  129.         local newData = { stored = 0, capacity=0, maxOutput=0, mfsus = {} }
  130.         if not scanForData(newData) then break end
  131.         bankData = newData
  132.         sendStats()
  133.         sleep(.5)
  134.     end
  135.     print("scanning coro exiting")
  136. end
  137.  
  138.  
  139. local function startup()
  140.     term.clear()
  141.     term.setCursorPos(1,1)
  142.     parallel.waitForAny(doNetwork, doScanning, doKeyboard, doMenu)
  143. end
  144.  
  145. local rtn, error = pcall(startup)
  146. if not rtn then
  147.     print("Storage scan failed: " .. error)
  148. end
  149. print("Exiting.")
Advertisement
Add Comment
Please, Sign In to add comment