local PROTOCOL = "resource-monitor" local function pprint(...) local pretty = require("cc.pretty") pretty.pretty_print(...) end local function isInventory(peripheral_name) for _, t in ipairs{peripheral.getType(peripheral_name)} do if t == "inventory" then return true end end return false end local function isWirelessModem(peripheral_name) return peripheral.getType(peripheral_name) == "modem" and peripheral.call(peripheral_name, "isWireless") end local function getInventories() local inventories = {} for _, name in ipairs(peripheral.getNames()) do if isInventory(name) then table.insert(inventories, name) end end return inventories end local function getWirelessModem() for _, name in ipairs(peripheral.getNames()) do if isWirelessModem(name) then return name end end end local function showDiagnostics(hostname, modem_name, inventories) term.clear() term.setCursorPos(1, 1) print("Currently running resource monitor...") term.setCursorPos(1, 3) term.write("Protocol: ") print(PROTOCOL) term.write("Hostname: ") print(hostname) term.write("Wireless modem: ") print(modem_name) term.setCursorPos(1, 7) if #inventories == 0 then term.write("Inventories: None") else print("Inventories:") for _, name in ipairs(inventories) do term.write("* ") print(name) end end end local function getItems(inventories) local allItems = {} for _, inventory in ipairs(inventories) do for slot, item in pairs(peripheral.call(inventory, "list")) do local detail = peripheral.call(inventory, "getItemDetail", slot) local name = detail.displayName allItems[name] = (allItems[name] or 0) + item.count end end return allItems end local function main(hostname) if not hostname then print("ERROR: please provide a hostname") return end local modem_name = getWirelessModem() if not modem_name then print("ERROR: please attach a wireless modem") return end rednet.open(modem_name) rednet.host(PROTOCOL, hostname) local inventories = getInventories() showDiagnostics(hostname, modem_name, inventories) while true do local sender = rednet.receive(PROTOCOL) local items = getItems(inventories) local payload = textutils.serialise(items, { compact = true }) rednet.send(sender, payload, PROTOCOL) end end main(...)