Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Find peripherals
- local monitor = peripheral.find("monitor")
- local ae2 = peripheral.find("meBridge")
- local modem = peripheral.find("modem", function(_, m) return m.isWireless() end)
- -- Check for required peripherals
- if not monitor or not ae2 or not modem then
- print("Missing peripherals:")
- if not monitor then print(" - No monitor found!") end
- if not ae2 then print(" - No meBridge found!") end
- if not modem then print(" - No wireless modem found!") end
- return
- end
- -- Initialize rednet
- rednet.open(peripheral.getName(modem))
- -- Setup monitor
- monitor.setTextScale(0.5)
- monitor.clear()
- -- Create terminal and monitor windows
- local termWindow = window.create(term.current(), 1, 1, term.getSize())
- local monWindow = window.create(monitor, 1, 1, monitor.getSize())
- local monx, mony = monWindow.getSize()
- local termx, termy = termWindow.getSize()
- local receivedItemStorage = 0 -- Storage data received from network
- -- Utility functions
- local function clearWindow(win, width, height)
- win.setBackgroundColor(colors.black)
- for i = 1, height do
- win.setCursorPos(1, i)
- win.write(string.rep(' ', width))
- end
- end
- local function roundDec(num, decimals)
- local mult = 10^(decimals or 0)
- return math.floor(num * mult + 0.5) / mult
- end
- local function formatNumber(n)
- return tostring(n):reverse():gsub("(%d%d%d)", "%1,"):reverse():gsub("^,", "")
- end
- local function drawBar(label, y, percent)
- local barlen = monx - 13
- local loadlen = math.floor(barlen * percent)
- monWindow.setCursorPos(1, y)
- monWindow.setBackgroundColor(colors.black)
- monWindow.write(label .. " ")
- if label == "Items" then
- monWindow.write(" ")
- end
- monWindow.setBackgroundColor(colors.gray)
- monWindow.write(string.rep(' ', barlen))
- monWindow.setBackgroundColor(colors.black)
- monWindow.write(" " .. roundDec(percent * 100, 1) .. "%")
- monWindow.setCursorPos(8, y)
- monWindow.setBackgroundColor(colors.green)
- monWindow.write(string.rep(' ', loadlen))
- end
- local function getRealItemStorage(maxStorage)
- local cells = ae2.listCells() or {}
- local totalStorage = 0
- for _, cell in ipairs(cells) do
- if cell.type == "item" then
- totalStorage = totalStorage + (cell.totalBytes or 0)
- end
- end
- local byteconvert = math.floor((maxStorage - totalStorage) / 8)
- return byteconvert + totalStorage
- end
- local function debugInfo(max, used, percent)
- clearWindow(termWindow, termx, termy)
- local debugData = {
- {"Max", max},
- {"Used", used},
- {"Percentage", roundDec(percent * 100, 2) .. "%"},
- {"Received Storage", receivedItemStorage}
- }
- for i, data in ipairs(debugData) do
- termWindow.setCursorPos(1, i)
- termWindow.write(data[1] .. ": " .. formatNumber(data[2]))
- end
- end
- local function receiveUpdates()
- while true do
- local _, message = rednet.receive("drawers_update")
- if type(message) == "number" then
- receivedItemStorage = message
- end
- end
- end
- local function main()
- parallel.waitForAny(function()
- while true do
- -- Get storage info
- local itemMax = getRealItemStorage(math.abs(ae2.getTotalItemStorage()))
- local itemUsed = (ae2.getUsedItemStorage() - (receivedItemStorage * 8)) + receivedItemStorage
- local itemPercent = itemUsed / itemMax
- local fluidMax = math.abs(ae2.getTotalFluidStorage())
- local fluidUsed = fluidMax - math.abs(ae2.getAvailableFluidStorage() / 1000)
- local fluidPercent = fluidUsed / fluidMax
- -- Update display
- clearWindow(monWindow, monx, mony)
- drawBar("Items", 2, itemPercent)
- drawBar("Fluids", 4, fluidPercent)
- debugInfo(itemMax, itemUsed, itemPercent)
- sleep(3)
- end
- end, receiveUpdates)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement