Skip to content

Instantly share code, notes, and snippets.

@eritbh
Created November 22, 2023 02:39
Show Gist options
  • Save eritbh/4ed84113a2e0f191acaf9665d28ce0b4 to your computer and use it in GitHub Desktop.
Save eritbh/4ed84113a2e0f191acaf9665d28ce0b4 to your computer and use it in GitHub Desktop.
computercraft thingy
-- Aggregates status information for all peripherals connected to networked computers.
local pretty = require("cc.pretty")
local DISPLAY_REFRESH_DELAY = 0.1
local DATA_REFRESH_DELAY = 0.1
local BROADCAST_DELAY = 0.1
-- Connect to rednet on all modems
peripheral.find("modem", rednet.open)
local peripheralLabels = {
back = "Back peripheral",
front = "Front peripheral",
top = "Top peripheral",
bottom = "Bottom peripheral",
left = "Left peripheral",
right = "Right peripheral",
}
local peripheralTypeLabels = {
["energy_storage"] = {
energy = "Stored energy",
energyCapacity = "Energy capacity",
},
}
-- define how to handle each type of peripheral
local peripheralTypeRules = {
["energy_storage"] = {
energy = function (storage)
return storage.getEnergy()
end,
energyCapacity = function (storage)
return storage.getEnergyCapacity()
end,
},
["inventory"] = {
-- TODO
},
["fluid_storage"] = {
-- TODO
},
}
-- Returns a table of connected peripheral names organized by type
local function getPeripheralNamesByType()
local peripheralNames = {}
for peripheralType, _ in pairs(peripheralTypeRules) do
peripheralNames[peripheralType] = {}
for _, peripheralName in ipairs(peripheral.getNames()) do
if peripheral.hasType(peripheralName, peripheralType) then
table.insert(peripheralNames[peripheralType], peripheralName)
end
end
end
return peripheralNames
end
-- stores all known data from all networked computers, including ourselves
local data = {}
local function refreshLocalData()
while true do
-- Recompute connected peripherals
local peripheralNames = getPeripheralNamesByType()
-- Get information about each peripheral
local myData = {}
for peripheralType, rules in pairs(peripheralTypeRules) do
for _, peripheralName in ipairs(peripheralNames[peripheralType]) do
-- create a table for info about this peripheral if none exists
local peripheralData = myData[peripheralName]
if peripheralData == nil then
peripheralData = {}
myData[peripheralName] = peripheralData
end
-- add entries to the peripheral's data table according to rules
for ruleKey, computeRuleValue in pairs(rules) do
peripheralData[ruleKey] = computeRuleValue(peripheral.wrap(peripheralName))
end
end
end
data[""..os.getComputerID()] = myData
-- Wait half a second before updating again
sleep(DATA_REFRESH_DELAY)
end
end
local function receiveRemoteData()
while true do
local computerID, receivedData = rednet.receive("eritbh:monitor")
data[""..computerID] = receivedData
end
end
local function broadcastLocalData()
while true do
rednet.broadcast(data[""..os.getComputerID()], "eritbh:monitor")
sleep(BROADCAST_DELAY)
end
end
local function updateDisplay()
while true do
term.clear()
term.setCursorPos(1, 1)
print("I am computer "..os.getComputerID())
pretty.pretty_print(data)
sleep(DISPLAY_REFRESH_DELAY)
end
end
parallel.waitForAny(
refreshLocalData,
receiveRemoteData,
broadcastLocalData,
updateDisplay
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment