root / trunk / swipe / dustmite.py @ 119
History | View | Annotate | Download (1.82 KB)
1 | 99 | bneuman | #!/usr/bin/python
|
---|---|---|---|
2 | |||
3 | import re |
||
4 | import sys |
||
5 | 100 | bneuman | import serial |
6 | 119 | bneuman | import MySQLdb |
7 | import getpass |
||
8 | 99 | bneuman | |
9 | 119 | bneuman | |
10 | 99 | bneuman | TT_GET_KEY = 'k'
|
11 | TT_ACK = 'a'
|
||
12 | TT_NACK = 'n'
|
||
13 | TT_TO = 'f'
|
||
14 | TT_TIMEOUT = 't'
|
||
15 | |||
16 | 100 | bneuman | BAUD_RATE = 9600
|
17 | 99 | bneuman | |
18 | 119 | bneuman | 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 | 99 | bneuman | if len(sys.argv) < 2: |
30 | print "usage: dustmite.py /path/to/keypad/dev" |
||
31 | |||
32 | else:
|
||
33 | |||
34 | 119 | bneuman | |
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 | 100 | bneuman | keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout=10) |
46 | 99 | bneuman | |
47 | 100 | bneuman | keypad.flushInput() |
48 | |||
49 | 119 | bneuman | |
50 | 99 | bneuman | while True: |
51 | 100 | bneuman | #TODO: only consider the last valid id so only the last swipe since a button press will work
|
52 | 99 | bneuman | s = raw_input()
|
53 | id = re.search('%([0-9]*)=.*', s)
|
||
54 | |||
55 | if id != None: |
||
56 | 100 | bneuman | print "-----------\nid# ", id.group(1) |
57 | 99 | bneuman | |
58 | 119 | bneuman | |
59 | print "waiting for key..." |
||
60 | 100 | bneuman | keypad.write(TT_GET_KEY) |
61 | 99 | bneuman | |
62 | 119 | bneuman | 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 | 100 | bneuman | resp = keypad.read(1)
|
79 | 99 | bneuman | |
80 | 119 | bneuman | print "request:",resp |
81 | 99 | bneuman | |
82 | 119 | bneuman | |
83 | if acl.count(resp) > 0: |
||
84 | 100 | bneuman | keypad.write(TT_ACK) |
85 | else:
|
||
86 | keypad.write(TT_NACK) |