-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-erfid.py
137 lines (110 loc) · 2.82 KB
/
py-erfid.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import nfc
import time
import requests
from gpiozero import LED
import netifaces
from threading import Thread
import signal
import sys
import traceback
class PyErfid:
def __init__(self):
self.green = LED(23)
self.red = LED(24)
self.yellow = LED(25)
self.green.on()
self.yellow.on()
self.red.on()
self.clf = nfc.ContactlessFrontend()
self.mac = netifaces.ifaddresses("wlan0")[netifaces.AF_LINK][0]["addr"]
print "MAC address: " + self.mac
try:
self.clf.open('tty:S0:pn532')
except:
traceback.print_exc()
self.green.off()
self.yellow.off()
self.red.on()
signal.pause()
return
print "Connected to RFID board"
self.thread = None
self.working = False
self.green.off()
self.yellow.off()
self.red.off()
def start_blink(self):
self.thread = Thread(target=self.run_blink)
self.thread.start()
def stop_blink(self):
self.working = False
if self.thread:
self.thread.join()
self.thread = None
def run_blink(self):
self.working = True
while self.working:
self.green.on()
self.yellow.on()
self.red.on()
time.sleep(0.05)
self.green.off()
self.yellow.off()
self.red.off()
time.sleep(0.05)
def success(self):
for i in range(3):
self.green.on()
time.sleep(0.25)
self.green.off()
time.sleep(0.25)
def error(self):
for i in range(3):
self.red.on()
time.sleep(0.25)
self.red.off()
time.sleep(0.25)
def warning(self):
for i in range(3):
self.yellow.on()
time.sleep(0.25)
self.yellow.off()
time.sleep(0.25)
def run(self):
while True:
try:
tag = self.clf.connect(rdwr={'on-connect': lambda t: False})
except:
self.green.off()
self.yellow.off()
self.red.on()
signal.pause()
break
if tag:
try:
id = tag.identifier.encode('HEX').upper()
data = {"rfid": id, "mac_address": self.mac + "\n"} # newline for backwards compat with ruby version
self.start_blink()
res = requests.post("https://makerepo.com/rfid/card_number", json=data, headers={"Content-Type": "application/json"})
self.stop_blink()
if not res or res.status_code != 200:
self.error()
continue
obj = res.json()
if obj["success"] == "RFID sign in":
self.success()
continue
elif obj["success"] == "RFID sign out":
self.warning()
continue
# fallback to error
self.error()
except:
traceback.print_exc()
self.stop_blink()
self.error()
time.sleep(0.5)
self.clf.close()
if __name__ == "__main__":
erfid = PyErfid()
erfid.run()