Improve Create peripheral detection and add diagnostics

This commit is contained in:
2026-07-16 21:00:29 +02:00
parent aac7e4f52d
commit d586491a07
3 changed files with 66 additions and 9 deletions
+22 -9
View File
@@ -34,10 +34,21 @@ local function center(y, text, foreground, background)
monitor.write(text)
end
local function getPeripheralsByType(wantedType)
local function hasMethod(name, wantedMethod)
local methods = peripheral.getMethods(name) or {}
for _, method in ipairs(methods) do
if method == wantedMethod then return true end
end
return false
end
local function getPeripherals(wantedType, requiredMethod)
local names = {}
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == wantedType then
local peripheralType = peripheral.getType(name)
local typeMatches = type(peripheralType) == "string"
and peripheralType:lower() == wantedType:lower()
if typeMatches or (requiredMethod and hasMethod(name, requiredMethod)) then
names[#names + 1] = name
end
end
@@ -139,10 +150,12 @@ end
local function readMainShaft(stressometerName)
if not stressometerName then return nil, nil, nil end
local okCapacity, capacity = pcall(peripheral.call,
stressometerName, "getNetworkCapacity")
local okUsage, usage = pcall(peripheral.call,
stressometerName, "getNetworkStress")
local capacityMethod = hasMethod(stressometerName, "getStressCapacity")
and "getStressCapacity" or "getNetworkCapacity"
local usageMethod = hasMethod(stressometerName, "getStress")
and "getStress" or "getNetworkStress"
local okCapacity, capacity = pcall(peripheral.call, stressometerName, capacityMethod)
local okUsage, usage = pcall(peripheral.call, stressometerName, usageMethod)
if not okCapacity or not okUsage then return nil, nil, nil end
capacity = tonumber(capacity) or 0
usage = tonumber(usage) or 0
@@ -152,9 +165,9 @@ end
local function render()
local _, height = monitor.getSize()
local speedometers = getPeripheralsByType(SPEEDOMETER_TYPE)
local stressometers = getPeripheralsByType(STRESSOMETER_TYPE)
local readers = getPeripheralsByType(BLOCK_READER_TYPE)
local speedometers = getPeripherals(SPEEDOMETER_TYPE, "getSpeed")
local stressometers = getPeripherals(STRESSOMETER_TYPE, "getStress")
local readers = getPeripherals(BLOCK_READER_TYPE, "getBlockData")
monitor.setBackgroundColor(colors.black)
monitor.clear()