root / trunk / swipe / dustmite.py @ 125
History | View | Annotate | Download (3.58 KB)
1 | 99 | bneuman | #!/usr/bin/python
|
---|---|---|---|
2 | |||
3 | 125 | bneuman | ################################################################################
|
4 | ## dustmite.py is the main tooltron server
|
||
5 | # It connects to the civicrm database of mysql and sends commands to
|
||
6 | # the cardbox and tools. It must be connected over usb to the card
|
||
7 | # reader, and the two arguments specify the devices to use for the
|
||
8 | # serial communication to the card and tool boxes.
|
||
9 | #
|
||
10 | # This script requires the modules seen below, which may not be
|
||
11 | # present by default
|
||
12 | #
|
||
13 | # email bradneuman@gmail.com if you have any questions
|
||
14 | ################################################################################
|
||
15 | |||
16 | |||
17 | 99 | bneuman | import re |
18 | import sys |
||
19 | 100 | bneuman | import serial |
20 | 119 | bneuman | import MySQLdb |
21 | import getpass |
||
22 | 99 | bneuman | |
23 | 119 | bneuman | |
24 | 99 | bneuman | TT_GET_KEY = 'k'
|
25 | TT_ACK = 'a'
|
||
26 | TT_NACK = 'n'
|
||
27 | TT_TO = 'f'
|
||
28 | TT_TIMEOUT = 't'
|
||
29 | |||
30 | 100 | bneuman | BAUD_RATE = 9600
|
31 | 99 | bneuman | |
32 | 120 | bneuman | #tools for messages are these + 1 but in decimal, not ascii
|
33 | 119 | bneuman | tools = { |
34 | 120 | bneuman | 'Bandsaw':'2', |
35 | 'DrillPress':'1', |
||
36 | 'Mill':'4', |
||
37 | 'Lathe':'5', |
||
38 | 'ChopMiterSaw':'3' |
||
39 | } |
||
40 | 119 | bneuman | |
41 | 99 | bneuman | |
42 | 120 | bneuman | if len(sys.argv) < 3: |
43 | print "usage: dustmite.py /path/to/keypad/device /path/to/tool/bus/device" |
||
44 | |||
45 | 99 | bneuman | else:
|
46 | |||
47 | 119 | bneuman | |
48 | pw = getpass.getpass("mysql password: ")
|
||
49 | db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm") |
||
50 | print "connected, now accepting input" |
||
51 | |||
52 | cursor = db.cursor() |
||
53 | |||
54 | qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
|
||
55 | |||
56 | 120 | bneuman | keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 30) |
57 | 100 | bneuman | keypad.flushInput() |
58 | 120 | bneuman | print keypad
|
59 | 100 | bneuman | |
60 | 125 | bneuman | bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 15) |
61 | 120 | bneuman | bus.flushInput() |
62 | print bus
|
||
63 | 119 | bneuman | |
64 | 120 | bneuman | # ^ <src> <dest> <data>
|
65 | def sendTool(t): |
||
66 | tn = int(t) + 1 |
||
67 | msg = '^' + chr(1) + chr(tn) + 'O' + chr(1 ^ tn ^ ord('O')) |
||
68 | |||
69 | bus.write(msg) |
||
70 | return
|
||
71 | |||
72 | #returns [src, data] or [] on error
|
||
73 | def readTool(): |
||
74 | ret = [0,0,0] |
||
75 | |||
76 | if bus.read(1) == '^': |
||
77 | ret[0] = bus.read(1) |
||
78 | ret[1] = bus.read(1) |
||
79 | ret[2] = bus.read(1) |
||
80 | x = bus.read(1)
|
||
81 | |||
82 | 122 | bneuman | print "got packet",ret |
83 | |||
84 | 120 | bneuman | if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x: |
85 | return ret
|
||
86 | else:
|
||
87 | print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) |
||
88 | |||
89 | return []
|
||
90 | |||
91 | 121 | bneuman | def checkAck(t): |
92 | tn = int(t) + 1 |
||
93 | m = readTool() |
||
94 | 120 | bneuman | |
95 | 124 | bneuman | if m== []:
|
96 | return False |
||
97 | |||
98 | |||
99 | 122 | bneuman | return m[0] == chr(tn) and m[1] == chr(1) and m[2] == 'A' |
100 | 121 | bneuman | |
101 | |||
102 | 99 | bneuman | while True: |
103 | s = raw_input()
|
||
104 | id = re.search('%([0-9]*)=.*', s)
|
||
105 | |||
106 | if id != None: |
||
107 | 125 | bneuman | print "\n-----------\nid# ", id.group(1) |
108 | 99 | bneuman | |
109 | 119 | bneuman | |
110 | 125 | bneuman | print "sending key request" |
111 | 100 | bneuman | keypad.write(TT_GET_KEY) |
112 | 99 | bneuman | |
113 | 119 | bneuman | cursor.execute(qry + id.group(1)) |
114 | |||
115 | result = cursor.fetchall() |
||
116 | |||
117 | acl = [] |
||
118 | |||
119 | for r in result: |
||
120 | tls = r[0].split("\x01") |
||
121 | for t in tls: |
||
122 | if t != '': |
||
123 | try:
|
||
124 | acl.append (tools[t]) |
||
125 | except KeyError: |
||
126 | 125 | bneuman | #this doesn't really matter
|
127 | pass
|
||
128 | 119 | bneuman | |
129 | print "user has access to:", acl |
||
130 | 125 | bneuman | |
131 | 100 | bneuman | resp = keypad.read(1)
|
132 | 99 | bneuman | |
133 | 119 | bneuman | print "request:",resp |
134 | 99 | bneuman | |
135 | 119 | bneuman | |
136 | if acl.count(resp) > 0: |
||
137 | 100 | bneuman | keypad.write(TT_ACK) |
138 | 120 | bneuman | sendTool(resp) |
139 | 125 | bneuman | print "ACCESS GRANTED" |
140 | 122 | bneuman | if checkAck(resp):
|
141 | 121 | bneuman | print "ACK" |
142 | else:
|
||
143 | print "NO ACK!!!!" |
||
144 | 120 | bneuman | |
145 | 100 | bneuman | else:
|
146 | keypad.write(TT_NACK) |
||
147 | 125 | bneuman | print "ACCESS DENIED!!" |
148 | |||
149 | fflush(stdin) |