From 693d5988332b0af7e78234c17f1b0d45d787acd5 Mon Sep 17 00:00:00 2001 From: D4VID Date: Sat, 26 Oct 2024 21:24:21 +0200 Subject: [PATCH] Magnetic card door controller --- door_controller.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 door_controller.lua diff --git a/door_controller.lua b/door_controller.lua new file mode 100644 index 0000000..c585db0 --- /dev/null +++ b/door_controller.lua @@ -0,0 +1,49 @@ +local component = require("component") +local event = require("event") +local os = require("os") + +local doorControllers = {} +local cardReader = component.os_magreader + +for address, name in component.list("os_doorcontroller", true) do + table.insert(doorControllers, component.proxy(address)) +end + +GREEN_LED = 4 +YELLOW_LED = 2 +RED_LED = 1 + + +local function magCardCallback(eventName, address, playerName, cardData, cardUniqueId, isCardLocked, side) + -- bits for green, yellow and red leds + cardReader.setLightState(YELLOW_LED) + if cardData == "supertajneheslo" then + cardReader.setLightState(GREEN_LED) + -- open the doors + for _, doorController in ipairs(doorControllers) do + doorController.open() + end + os.sleep(3) + -- close the doors + for _, doorController in ipairs(doorControllers) do + doorController.close() + end + else + cardReader.setLightState(RED_LED) + os.sleep(2) + end + -- turn light indicators off + cardReader.setLightState(0) +end + +-- control led indicators manually +cardReader.swipeIndicator(false) + +-- subscribe to the event "magData" with +event.listen("magData", magCardCallback) + +-- wait for an interrupt before closing the script (CTRL + C) +event.pull("interrupted") + +-- unsubscribe from the event bus +event.ignore("magData", magCardCallback)