root / trunk / swipe / dustmite.py @ 119
History | View | Annotate | Download (1.82 KB)
1 |
#!/usr/bin/python
|
---|---|
2 |
|
3 |
import re |
4 |
import sys |
5 |
import serial |
6 |
import MySQLdb |
7 |
import getpass |
8 |
|
9 |
|
10 |
TT_GET_KEY = 'k'
|
11 |
TT_ACK = 'a'
|
12 |
TT_NACK = 'n'
|
13 |
TT_TO = 'f'
|
14 |
TT_TIMEOUT = 't'
|
15 |
|
16 |
BAUD_RATE = 9600
|
17 |
|
18 |
tools = { |
19 |
'Bandsaw':'1', |
20 |
'DrillPress':'2', |
21 |
'Mill':'3', |
22 |
'Lathe':'4', |
23 |
'Welder':'5', |
24 |
'CircSaw':'6', |
25 |
'ChopMiterSaw':'7', |
26 |
'JigSaw':'8', |
27 |
'Router':'9'} |
28 |
|
29 |
if len(sys.argv) < 2: |
30 |
print "usage: dustmite.py /path/to/keypad/dev" |
31 |
|
32 |
else:
|
33 |
|
34 |
|
35 |
pw = getpass.getpass("mysql password: ")
|
36 |
db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm") |
37 |
print "connected, now accepting input" |
38 |
|
39 |
#TODO: errors!
|
40 |
|
41 |
cursor = db.cursor() |
42 |
|
43 |
qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
|
44 |
|
45 |
keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout=10) |
46 |
|
47 |
keypad.flushInput() |
48 |
|
49 |
|
50 |
while True: |
51 |
#TODO: only consider the last valid id so only the last swipe since a button press will work
|
52 |
s = raw_input()
|
53 |
id = re.search('%([0-9]*)=.*', s)
|
54 |
|
55 |
if id != None: |
56 |
print "-----------\nid# ", id.group(1) |
57 |
|
58 |
|
59 |
print "waiting for key..." |
60 |
keypad.write(TT_GET_KEY) |
61 |
|
62 |
cursor.execute(qry + id.group(1)) |
63 |
|
64 |
result = cursor.fetchall() |
65 |
|
66 |
acl = [] |
67 |
|
68 |
for r in result: |
69 |
tls = r[0].split("\x01") |
70 |
for t in tls: |
71 |
if t != '': |
72 |
try:
|
73 |
acl.append (tools[t]) |
74 |
except KeyError: |
75 |
print "ERROR: could not find key!" |
76 |
|
77 |
print "user has access to:", acl |
78 |
resp = keypad.read(1)
|
79 |
|
80 |
print "request:",resp |
81 |
|
82 |
|
83 |
if acl.count(resp) > 0: |
84 |
keypad.write(TT_ACK) |
85 |
else:
|
86 |
keypad.write(TT_NACK) |