-
Notifications
You must be signed in to change notification settings - Fork 1
/
door-entry-system.py
76 lines (65 loc) · 2.24 KB
/
door-entry-system.py
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
67
68
69
70
71
72
73
74
75
76
import datetime
import jwt
import requests
#import nfc
import settings
import os
import logging
import nfc_rdm6300 as nfc
import door
from message import matrix_message
from time import sleep
from dotenv import load_dotenv
load_dotenv()
#logging.basicConfig(filename='/tmp/rfid.log', level=logging.DEBUG)
logging.basicConfig(level=logging.DEBUG, handlers=[logging.StreamHandler()])
def allow(details):
door.unlock_door()
matrix_message('User ' + details.get('username') + ' has entered the building', prefix='')
logging.info('Request allowed')
pass
def deny():
logging.info('Request denied')
pass
def check_valid(rfid_token):
if not rfid_token:
return
encoded = jwt.encode({
'rfid_code': rfid_token.decode("utf-8"),
'device_id': os.getenv('DEVICE_ID'),
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=2)
}, os.getenv('SECRET'), algorithm='HS256')
logging.debug("Sending request with data: %s" % encoded)
response = requests.post(os.getenv('URL', 'https://maidstone-hackspace.org.uk/api/v1/rfid_auth/'), data={"data":encoded}, timeout=3)
if response.status_code != 200:
logging.info("Response code not valid")
return deny()
logging.debug("Response body: %s" % response.json())
parsed_response = jwt.decode(response.json(), os.getenv('SECRET'), algorithms=['HS256'], verify=False)
if parsed_response['authenticated'] is True:
print(parsed_response)
logging.info("User %s has been authenticated" % parsed_response['username'])
allow(parsed_response)
else:
deny()
clf = "/dev/serial0"
door.setup()
with nfc.nfc(clf) as port:
logging.info('RFID Reader started')
while True:
#with nfc.ContactlessFrontend('tty:S0:pn532') as clf:
logging.debug('Listening on %s' % clf)
#tag = clf.connect(rdwr={'on-connect': lambda tag: False})
#code = str(tag.identifier).encode('hex')
code = nfc.read_code(port)
print(code)
logging.info('Detected RFID card %s' % code)
try:
check_valid(code)
except Exception as e:
logging.exception(e)
deny()
if code:
sleep(0.1)
nfc.readall(port)
sleep(0.01)