Revision 144
added some logging but doesn't look up usernames yet
trunk/swipe/tooltron.py | ||
---|---|---|
42 | 42 |
import serial |
43 | 43 |
import MySQLdb |
44 | 44 |
import getpass |
45 |
from time import * |
|
45 | 46 |
|
46 | 47 |
keypad = [] |
47 | 48 |
keypadTimeout = 11 #in seconds |
... | ... | |
122 | 123 |
#end thread |
123 | 124 |
############################################### |
124 | 125 |
|
126 |
flog = open("tooltron.log", "wa") |
|
127 |
|
|
128 |
def logMessage(str): |
|
129 |
flog.write(strftime("%Y.%m.%d %H:%M:%S ") + str + "\n") |
|
130 |
|
|
125 | 131 |
def keypad2toolID(t): |
126 | 132 |
return ids[t] |
127 |
#int(t) + 10 |
|
128 | 133 |
|
134 |
def reverseTool(tn): |
|
135 |
for t in tools: |
|
136 |
if tn in tools[t]: |
|
137 |
return t |
|
138 |
|
|
139 |
return 0 |
|
140 |
|
|
129 | 141 |
def get_last_id(): |
130 | 142 |
global idready |
131 | 143 |
global lastid |
... | ... | |
162 | 174 |
return |
163 | 175 |
|
164 | 176 |
|
165 |
|
|
166 | 177 |
if len(sys.argv) < 3: |
167 | 178 |
print "usage: dustmite.py /path/to/keypad/device /path/to/tool/bus/device" |
168 | 179 |
|
169 | 180 |
else: |
170 | 181 |
|
171 |
|
|
172 | 182 |
pw = getpass.getpass("mysql password: ") |
173 | 183 |
db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm") |
174 | 184 |
print "connected, now accepting input" |
185 |
logMessage("connected to database") |
|
175 | 186 |
|
176 | 187 |
#start the id scan thread AFTER the getpass prompt |
177 | 188 |
idThread().start() |
... | ... | |
191 | 202 |
bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 2) |
192 | 203 |
bus.flushInput() |
193 | 204 |
print bus |
194 |
|
|
205 |
|
|
195 | 206 |
# ^ <src> <dest> <data> |
196 | 207 |
def sendTool(t): |
197 | 208 |
tn = keypad2toolID(t) |
... | ... | |
268 | 279 |
if idready == True: |
269 | 280 |
#if someone swipes, break out of the loop |
270 | 281 |
print "**swipe during read, bailing out" |
282 |
logMessage("user swipped during keypad read") |
|
271 | 283 |
break |
272 | 284 |
if time.time() - startTime > keypadTimeout: |
273 | 285 |
print "KEYPAD TIMEOUT!" |
286 |
logMessage("keypad timed out") |
|
274 | 287 |
break |
275 | 288 |
|
276 | 289 |
#if we have a vaid response |
... | ... | |
282 | 295 |
keypad.write(TT_ACK) |
283 | 296 |
sendTool(resp) |
284 | 297 |
print "ACCESS GRANTED" |
298 |
logMessage("user accessed tool "+reverseTool(resp)) |
|
285 | 299 |
if checkAck(resp): |
286 | 300 |
print "ACK" |
287 | 301 |
else: |
288 | 302 |
print "NO ACK!!!!" |
303 |
logMessage("tool "+reverseTool(resp)+" did not ACK") |
|
289 | 304 |
|
290 | 305 |
else: |
291 | 306 |
keypad.write(TT_NACK) |
Also available in: Unified diff