pandorrama

secDoorKeypad

Sep 8th, 2020 (edited)
982
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TO INSTALL JUST EXECUTE: pastebin get uhS8WLWT /etc/rc.d/secDoorKey.lua
  2.  
  3. keypad = require("component").os_keypad
  4. rzdoor = require("component").os_doorcontroller
  5. identify = require("component").os_entdetector
  6. event = require("event")
  7. local keypadInput = ""                  -- empty var for inserted kay
  8.  
  9. local pin = "1234"                      -- secret key
  10. local logFilePath = "/home/access_log"  -- place to save log file
  11. local scanAuth = 3                      -- range to scan player name, who entered key
  12. local scanPresent = 5                   -- range to scan, if authorized player is still in range, to keep door open
  13.  
  14. function updateDisplay()
  15.   local displayString = ""
  16.   for i=1,#keypadInput do
  17.     displayString = displayString .. "*"
  18.   end
  19.   keypad.setDisplay(displayString, 7)
  20. end
  21.  
  22. local function checkPin()
  23.   if keypadInput == pin then
  24.     keypad.setDisplay("granted", 2)
  25.     grantAcess()
  26.   else
  27.     keypad.setDisplay("denied", 4)
  28.     logAccess(detectPlayer().." tryed wrong key")
  29.     rzdoor.close()
  30.   end
  31.   keypadInput = ""
  32.   os.sleep(1)
  33. end
  34.  
  35. function detectPlayer()
  36.   players = identify.scanPlayers(scanAuth)
  37.   if #players ~= 0 then
  38.     return players[1].name
  39.   else
  40.     return "unknown"
  41.   end
  42. end
  43.  
  44. function grantAcess()
  45.   authPlayer = detectPlayer()
  46.   if authPlayer ~= "unknown" then
  47.     authPlayerPresent = true
  48.   else
  49.     logAccess("Could not identify user, who sucessfully logged in. Access could NOT be granted")
  50.     authPlayerPresent = false
  51.     return
  52.   end
  53.   rzdoor.open()
  54.   logAccess(authPlayer.." sucessfully logged in. Access granted")
  55.   while authPlayerPresent == true do
  56.     os.sleep(1)
  57.     players = identify.scanPlayers(scanPresent)
  58.     foundIt = false
  59.     for k in pairs(players) do
  60.       if players[k].name == authPlayer then
  61.         foundIt = true
  62.       end
  63.     end
  64.     if foundIt == false then authPlayerPresent = false end
  65.   end
  66.   rzdoor.close()
  67. end
  68.  
  69. function logAccess(logText)
  70.   file = io.open(logFilePath, "a")
  71.   file:write(os.date().." - "..logText.."\n")
  72.   file:close()
  73. end
  74.  
  75. local function keypadEvent(eventName, address, button, button_label)
  76.   if button_label == "*" then
  77.     -- remove last character from input cache
  78.     keypadInput = string.sub(keypadInput, 1, -2)
  79.   elseif button_label == "#" then
  80.     -- check the pin when the user confirmed the input
  81.     checkPin()
  82.   else
  83.     -- add key to input cache if none of the above action apply
  84.     keypadInput = keypadInput .. button_label
  85.   end
  86.   updateDisplay()  
  87. end
  88.  
  89. function start()
  90.   -- listen to keypad events
  91.   event.listen("keypad", keypadEvent)
  92.    
  93.   -- clear keypad display
  94.   keypad.setDisplay("")
  95. end
RAW Paste Data