Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Door computer startup.lua
- local modem = peripheral.find("modem")
- if not modem then error("No modem found. Please attach a wireless modem.") end
- modem.open(1) -- Use channel 1 for all communications
- local doorId = nil
- local function setDoorId()
- if fs.exists("door_id.txt") then
- local file = fs.open("door_id.txt", "r")
- doorId = file.readAll()
- file.close()
- print("Door ID loaded: " .. doorId)
- else
- print("Enter the door ID:")
- doorId = read()
- local file = fs.open("door_id.txt", "w")
- file.write(doorId)
- file.close()
- print("Door ID set and saved: " .. doorId)
- end
- end
- local function operateDoor(action)
- print("Attempting to " .. action .. " door " .. doorId)
- if action == "open" then
- -- Set redstone output to bottom (directly powering the door below)
- rs.setOutput("bottom", true)
- print("Door " .. doorId .. " opened")
- sleep(3) -- Keep the door open for 3 seconds
- rs.setOutput("bottom", false)
- print("Door " .. doorId .. " closed")
- else
- print("Invalid action: " .. action)
- end
- end
- local function monitorAccess()
- if not doorId then
- error("Door ID is not set. Please restart the computer and set a door ID.")
- end
- print("Monitoring for access requests on channel 1...")
- print("This door's ID: " .. doorId)
- while true do
- local _, _, _, _, message = os.pullEvent("modem_message")
- -- Only log the type of message and door ID for security reasons
- if type(message) == "table" then
- print("Received message: {type=" .. (message.type or "nil") .. ", doorId=" .. (message.doorId or "nil") .. "}")
- if message.type == "door_command" and message.doorId == doorId then
- print("Valid door command received for this door")
- operateDoor(message.action)
- else
- print("Ignoring message: not for this door or incorrect format")
- end
- else
- print("Ignoring non-table message")
- end
- end
- end
- setDoorId()
- monitorAccess()
Advertisement
Add Comment
Please, Sign In to add comment