Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- TO INSTALL JUST EXECUTE: pastebin get uhS8WLWT /etc/rc.d/secDoorKey.lua
- keypad = require("component").os_keypad
- rzdoor = require("component").os_doorcontroller
- identify = require("component").os_entdetector
- event = require("event")
- local keypadInput = "" -- empty var for inserted kay
- local pin = "1234" -- secret key
- local logFilePath = "/home/access_log" -- place to save log file
- local scanAuth = 3 -- range to scan player name, who entered key
- local scanPresent = 5 -- range to scan, if authorized player is still in range, to keep door open
- function updateDisplay()
- local displayString = ""
- for i=1,#keypadInput do
- displayString = displayString .. "*"
- end
- keypad.setDisplay(displayString, 7)
- end
- local function checkPin()
- if keypadInput == pin then
- keypad.setDisplay("granted", 2)
- grantAcess()
- else
- keypad.setDisplay("denied", 4)
- logAccess(detectPlayer().." tryed wrong key")
- rzdoor.close()
- end
- keypadInput = ""
- os.sleep(1)
- end
- function detectPlayer()
- players = identify.scanPlayers(scanAuth)
- if #players ~= 0 then
- return players[1].name
- else
- return "unknown"
- end
- end
- function grantAcess()
- authPlayer = detectPlayer()
- if authPlayer ~= "unknown" then
- authPlayerPresent = true
- else
- logAccess("Could not identify user, who sucessfully logged in. Access could NOT be granted")
- authPlayerPresent = false
- return
- end
- rzdoor.open()
- logAccess(authPlayer.." sucessfully logged in. Access granted")
- while authPlayerPresent == true do
- os.sleep(1)
- players = identify.scanPlayers(scanPresent)
- foundIt = false
- for k in pairs(players) do
- if players[k].name == authPlayer then
- foundIt = true
- end
- end
- if foundIt == false then authPlayerPresent = false end
- end
- rzdoor.close()
- end
- function logAccess(logText)
- file = io.open(logFilePath, "a")
- file:write(os.date().." - "..logText.."\n")
- file:close()
- end
- local function keypadEvent(eventName, address, button, button_label)
- if button_label == "*" then
- -- remove last character from input cache
- keypadInput = string.sub(keypadInput, 1, -2)
- elseif button_label == "#" then
- -- check the pin when the user confirmed the input
- checkPin()
- else
- -- add key to input cache if none of the above action apply
- keypadInput = keypadInput .. button_label
- end
- updateDisplay()
- end
- function start()
- -- listen to keypad events
- event.listen("keypad", keypadEvent)
- -- clear keypad display
- keypad.setDisplay("")
- end
Advertisement
Add Comment
Please, Sign In to add comment