Improve Create peripheral detection and add diagnostics
This commit is contained in:
@@ -34,10 +34,21 @@ local function center(y, text, foreground, background)
|
|||||||
monitor.write(text)
|
monitor.write(text)
|
||||||
end
|
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 = {}
|
local names = {}
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
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
|
names[#names + 1] = name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -139,10 +150,12 @@ end
|
|||||||
|
|
||||||
local function readMainShaft(stressometerName)
|
local function readMainShaft(stressometerName)
|
||||||
if not stressometerName then return nil, nil, nil end
|
if not stressometerName then return nil, nil, nil end
|
||||||
local okCapacity, capacity = pcall(peripheral.call,
|
local capacityMethod = hasMethod(stressometerName, "getStressCapacity")
|
||||||
stressometerName, "getNetworkCapacity")
|
and "getStressCapacity" or "getNetworkCapacity"
|
||||||
local okUsage, usage = pcall(peripheral.call,
|
local usageMethod = hasMethod(stressometerName, "getStress")
|
||||||
stressometerName, "getNetworkStress")
|
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
|
if not okCapacity or not okUsage then return nil, nil, nil end
|
||||||
capacity = tonumber(capacity) or 0
|
capacity = tonumber(capacity) or 0
|
||||||
usage = tonumber(usage) or 0
|
usage = tonumber(usage) or 0
|
||||||
@@ -152,9 +165,9 @@ end
|
|||||||
|
|
||||||
local function render()
|
local function render()
|
||||||
local _, height = monitor.getSize()
|
local _, height = monitor.getSize()
|
||||||
local speedometers = getPeripheralsByType(SPEEDOMETER_TYPE)
|
local speedometers = getPeripherals(SPEEDOMETER_TYPE, "getSpeed")
|
||||||
local stressometers = getPeripheralsByType(STRESSOMETER_TYPE)
|
local stressometers = getPeripherals(STRESSOMETER_TYPE, "getStress")
|
||||||
local readers = getPeripheralsByType(BLOCK_READER_TYPE)
|
local readers = getPeripherals(BLOCK_READER_TYPE, "getBlockData")
|
||||||
|
|
||||||
monitor.setBackgroundColor(colors.black)
|
monitor.setBackgroundColor(colors.black)
|
||||||
monitor.clear()
|
monitor.clear()
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
-- Writes all attached peripheral types, methods and Block Reader data to a file.
|
||||||
|
|
||||||
|
local outputName = "create_debug.txt"
|
||||||
|
local output = assert(fs.open(outputName, "w"))
|
||||||
|
|
||||||
|
local function writeLine(text)
|
||||||
|
text = tostring(text or "")
|
||||||
|
output.writeLine(text)
|
||||||
|
print(text)
|
||||||
|
end
|
||||||
|
|
||||||
|
local names = peripheral.getNames()
|
||||||
|
table.sort(names)
|
||||||
|
|
||||||
|
writeLine("CREATE PERIPHERAL DEBUG")
|
||||||
|
writeLine("Computer ID: " .. os.getComputerID())
|
||||||
|
writeLine("Peripherals: " .. #names)
|
||||||
|
writeLine("")
|
||||||
|
|
||||||
|
for _, name in ipairs(names) do
|
||||||
|
local types = { peripheral.getType(name) }
|
||||||
|
local methods = peripheral.getMethods(name) or {}
|
||||||
|
table.sort(methods)
|
||||||
|
|
||||||
|
writeLine(("[%s]"):format(name))
|
||||||
|
writeLine("Types: " .. table.concat(types, ", "))
|
||||||
|
writeLine("Methods: " .. table.concat(methods, ", "))
|
||||||
|
|
||||||
|
local hasBlockData = false
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
if method == "getBlockData" then hasBlockData = true end
|
||||||
|
end
|
||||||
|
|
||||||
|
if hasBlockData then
|
||||||
|
local ok, data = pcall(peripheral.call, name, "getBlockData")
|
||||||
|
writeLine("getBlockData OK: " .. tostring(ok))
|
||||||
|
writeLine(ok and textutils.serialize(data) or tostring(data))
|
||||||
|
end
|
||||||
|
writeLine("")
|
||||||
|
end
|
||||||
|
|
||||||
|
output.close()
|
||||||
|
print("Fertig: " .. outputName)
|
||||||
@@ -5,6 +5,7 @@ return {
|
|||||||
files = {
|
files = {
|
||||||
"startup.lua",
|
"startup.lua",
|
||||||
"create_boiler_display.lua",
|
"create_boiler_display.lua",
|
||||||
|
"create_peripheral_debug.lua",
|
||||||
"core/constants.lua",
|
"core/constants.lua",
|
||||||
"core/log.lua",
|
"core/log.lua",
|
||||||
"core/protocol.lua",
|
"core/protocol.lua",
|
||||||
|
|||||||
Reference in New Issue
Block a user