local DEVICE_NAME = "tuer_vorne"   -- <- anpassen pro standort
local DEVICE_TYPE = "redstone"     -- <- "redstone" | "energy" | "inventory"
local OUTPUT_SIDE = "front"        -- <- nur relevant bei redstone

rednet.open("back")

local energy = (DEVICE_TYPE == "energy") and peripheral.find("energyDetector")
local inv    = (DEVICE_TYPE == "inventory") and peripheral.find("inventoryManager")

while true do
    local id, message, protocol = rednet.receive()

    if protocol == "device_discovery" and message == "discover" then
        local state = (DEVICE_TYPE == "redstone") and redstone.getOutput(OUTPUT_SIDE) or nil
        rednet.send(id, {name = DEVICE_NAME, type = DEVICE_TYPE, state = state}, "device_discovery")

    elseif protocol == "device_command" then
        if DEVICE_TYPE == "redstone" then
            if message.cmd == "on" then redstone.setOutput(OUTPUT_SIDE, true)
            elseif message.cmd == "off" then redstone.setOutput(OUTPUT_SIDE, false) end
            rednet.send(id, {state = redstone.getOutput(OUTPUT_SIDE)}, "device_command")

        elseif DEVICE_TYPE == "energy" then
            if message.cmd == "get" then
                rednet.send(id, {stored = energy.getEnergy(), max = energy.getMaxEnergy()}, "device_command")
            end

        elseif DEVICE_TYPE == "inventory" then
            if message.cmd == "get" then
                local items = {}
                for slot, item in pairs(inv.list()) do
                    table.insert(items, {name = item.name, count = item.count})
                end
                rednet.send(id, {items = items}, "device_command")
            end
        end
    end
end