diff --git a/create_boiler_display.lua b/create_boiler_display.lua index d37a633..296d484 100644 --- a/create_boiler_display.lua +++ b/create_boiler_display.lua @@ -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() diff --git a/create_peripheral_debug.lua b/create_peripheral_debug.lua new file mode 100644 index 0000000..90ee7a5 --- /dev/null +++ b/create_peripheral_debug.lua @@ -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) diff --git a/manifest.lua b/manifest.lua index 43477a7..7df3138 100644 --- a/manifest.lua +++ b/manifest.lua @@ -5,6 +5,7 @@ return { files = { "startup.lua", "create_boiler_display.lua", + "create_peripheral_debug.lua", "core/constants.lua", "core/log.lua", "core/protocol.lua",