-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
62 lines (52 loc) · 1.85 KB
/
main.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
import argparse
import binascii
import nfc
import logging
import yaml
import urllib.request
import os
import json
# logging
formatter = '%(levelname)s : %(asctime)s : %(message)s'
logging.basicConfig(level=logging.DEBUG, format=formatter)
class CardReader:
def __init__(self, yaml_path):
try:
with open(yaml_path) as file:
self.idm_dict = yaml.safe_load(file)
except Exception as e:
logging.error('exception occurred while loading YAML: %s', e)
exit(1)
def on_connect(self, tag):
logging.info("Touched card %s", tag)
self.idm = binascii.hexlify(tag._nfcid)
idm_str = self.idm.decode('ascii')
if idm_str in self.idm_dict:
endpoint = os.environ.get("SECRETARY_ENDPOINT")
headers = {"Content-Type": "application/json"}
body = {"name": self.idm_dict[idm_str]}
json_data = json.dumps(body).encode("utf-8")
request = urllib.request.Request(endpoint, data=json_data, method="POST", headers=headers)
with urllib.request.urlopen(request) as response:
response_body = response.read().decode("utf-8")
logging.info('response body: %s', response_body)
else:
logging.info('unknown IDm %s', self.idm)
return True
def read_id(self):
clf = nfc.ContactlessFrontend('usb:054c:06c3')
try:
clf.connect(rdwr={
'on-connect': self.on_connect
})
finally:
clf.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--yaml', default="config.yaml", type=str, help="path to yaml config file")
args = parser.parse_args()
yaml_path = args.yaml
cr = CardReader(yaml_path)
while True:
print("Please Touch")
cr.read_id()