Repository navigation
Expand file tree
/
Copy pathRW_Card.py
More file actions
66 lines (52 loc) · 1.77 KB
/
Copy pathRW_Card.py
File metadata and controls
66 lines (52 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from consolemenu import ConsoleMenu
from consolemenu.items import FunctionItem
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
from time import sleep
import uuid
import MySQLdb
reader = SimpleMFRC522()
dbHost = "localhost"
dbName = "pi-access"
dbUser = "accessadmin"
dbPass = "accesspassword"
def add_user():
name = input("What is the user's name?: ")
is_admin = input("Is this user an Admin? (Y/N): ").upper().strip() == "Y"
print("Place card to write...")
reader.write(str(uuid.uuid4()).upper())
_id, key = reader.read()
dbConnection = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPass, db=dbName)
cur = dbConnection.cursor(MySQLdb.cursors.DictCursor)
cur.execute(
f"INSERT INTO access_list SET name = '{name}', card_key = '{key.strip()}', card_id = '{_id}', access = 1, admin = {1 if is_admin else 0}"
)
dbConnection.commit()
print("User added!")
sleep(1)
def read_card():
try:
while True:
try:
print("Ready to read card...")
_id, key = reader.read()
print(f"ID: {_id}")
print(f"Key: {key}")
print()
sleep(1)
except:
break
except:
pass
finally:
GPIO.cleanup()
menu = ConsoleMenu("Door Access Control", "")
# Create some items
# Docs: https://github.com/aegirhall/console-menu
# MenuItem is the base class for all items, it doesn't do anything when selected
# menu_item = MenuItem("Add User")
# A FunctionItem runs a Python function when selected
menu.append_item(FunctionItem("Add User and Write Card", add_user))
menu.append_item(FunctionItem("Read Card", read_card))
# Finally, we call show to show the menu and allow the user to interact
menu.show()