mrWhiskasss

Система защиты дома : пинкод+карта [OpenSecuruty]

Jul 27th, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. local component = require "component"
  2. local keypad1 = component.proxy("80a")
  3. local keypad2 = component.proxy("637")
  4. event = require("event")
  5. local door = require("component").os_door
  6.  
  7. local pin = "1111"
  8. local keypadInput = ""
  9.  
  10. -- set this to true if you want to run the script as daemon
  11. local runScriptInBackground = false
  12.  
  13.  
  14. function myFunction(eventName, address, playerName, cardData, cardUniqueId, isCardLocked, side)
  15. local door = require("component").os_door
  16.    print("player " .. playerName .. " used card " .. cardUniqueId .. ", data: хххххххх")
  17.    if  ((cardData) == ("1111")) then
  18.     door.toggle()
  19. end
  20. end
  21.  
  22. function updateDisplay()
  23.     local displayString = ""
  24.     for i=1,#keypadInput do
  25.         displayString = displayString .. "*"
  26.     end
  27.  
  28.  
  29.     keypad1.setDisplay(displayString, 7)
  30.     keypad2.setDisplay(displayString, 7)
  31. end
  32.  
  33. function checkPin()
  34. local door = require("component").os_door
  35.     if keypadInput == pin then
  36.         keypad1.setDisplay("granted", 2)
  37.         keypad2.setDisplay("granted", 2)
  38.     door.toggle()
  39.     else
  40.         keypad1.setDisplay("denied", 4)  
  41.         keypad2.setDisplay("denied", 4)  
  42.   end    
  43.     keypadInput = ""
  44.     os.sleep(1)
  45. end
  46.  
  47. function keypadEvent(eventName, address, button, button_label)
  48.     print("button pressed: " )
  49.  
  50.     if button_label == "*" then
  51.         -- remove last character from input cache
  52.         keypadInput = string.sub(keypadInput, 1, -2)
  53.     elseif button_label == "#" then
  54.         -- check the pin when the user confirmed the input
  55.         checkPin()
  56.     else
  57.         -- add key to input cache if none of the above action apply
  58.         keypadInput = keypadInput .. button_label
  59.     end
  60.  
  61.     updateDisplay()  
  62. end
  63.  
  64.  
  65. -- subscribe to the event "magData" with myFunction()
  66. event.listen("magData", myFunction)
  67.  
  68. -- listen to keypad events
  69. event.listen("keypad", keypadEvent)
  70.  
  71. -- wait for an interrupt before closing the script (CTRL + C)
  72. event.pull("interrupted")
  73.  
  74. -- unsubscribe from the event bus
  75. event.ignore("magData", myFunction)
  76.  
  77. -- clear keypad display
  78. keypad1.setDisplay("")
  79. keypad2.setDisplay("")
  80.  
  81.  
  82. if not runScriptInBackground then
  83.     -- sleep until the user interrupts the program with CTRL + C
  84.     local stopMe = false
  85.     event.listen("interrupted", function() stopMe = true; end)
  86.     while not stopMe do os.sleep(0.1) end
  87.  
  88.     -- ignore keypad events on exit
  89.     event.ignore("keypad", keypadEvent)
  90.  
  91.     -- show that the keypad is inactive
  92.     keypad1.setDisplay("inactive", 6)
  93.     keypad2.setDisplay("inactive", 6)
  94. end
Advertisement
Add Comment
Please, Sign In to add comment