root / trunk / swipe / tooltron.py @ 146
History | View | Annotate | Download (9.96 KB)
1 |
#!/usr/bin/python
|
---|---|
2 |
|
3 |
"""
|
4 |
This file is part of Tooltron.
|
5 |
|
6 |
Tooltron is free software: you can redistribute it and/or modify
|
7 |
it under the terms of the Lesser GNU General Public License as published by
|
8 |
the Free Software Foundation, either version 3 of the License, or
|
9 |
(at your option) any later version.
|
10 |
|
11 |
Tooltron is distributed in the hope that it will be useful,
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
Lesser GNU General Public License for more details.
|
15 |
You should have received a copy of the Lesser GNU General Public License
|
16 |
along with Tooltron. If not, see <http://www.gnu.org/licenses/>.
|
17 |
|
18 |
Copyright 2009 Bradford Neuman <bneuman@andrew.cmu.edu>
|
19 |
|
20 |
"""
|
21 |
|
22 |
################################################################################
|
23 |
## dustmite.py is the main tooltron server
|
24 |
# It connects to the civicrm database of mysql and sends commands to
|
25 |
# the cardbox and tools. It must be connected over usb to the card
|
26 |
# reader, and the two arguments specify the devices to use for the
|
27 |
# serial communication to the card and tool boxes.
|
28 |
#
|
29 |
# This script requires the modules seen below, which may not be
|
30 |
# present by default
|
31 |
#
|
32 |
# Because of a problem with card scans buffering in stdio, there is a
|
33 |
# seperate thread which stores the last swiped id. To cleanly exit the
|
34 |
# program type C-d
|
35 |
#
|
36 |
# email bradneuman@gmail.com if you have any questions
|
37 |
################################################################################
|
38 |
|
39 |
|
40 |
import re |
41 |
import sys |
42 |
import serial |
43 |
import MySQLdb |
44 |
import getpass |
45 |
from time import * |
46 |
|
47 |
keypad = [] |
48 |
keypadTimeout = 11 #in seconds |
49 |
|
50 |
#this table maps which keypad button to press for each tool. This
|
51 |
#list should probably be printed next to the swipe box
|
52 |
tools = { |
53 |
'Bandsaw':['2','8'], |
54 |
'DrillPress':['1','9'], |
55 |
'Mill':['4'], |
56 |
'Lathe':['5'], |
57 |
#HACK: since the saw isn't used I use it for testing random boards
|
58 |
'ChopMiterSaw':['3','7','0'] |
59 |
} |
60 |
|
61 |
#this table maps keypad button numbers to tool IDs. The default is
|
62 |
#that the tool id is 10+the keypad number, but this would change if
|
63 |
#boards were swapped out. The tool ID number should be printed on the
|
64 |
#tool board, but needs to be hand coded into the toolbox code
|
65 |
ids = { |
66 |
'1':11, |
67 |
'2':12, |
68 |
'3':13, |
69 |
'4':14, |
70 |
'5':15, |
71 |
'6':16, |
72 |
'7':17, |
73 |
'8':18, |
74 |
'9':19, |
75 |
'0':13 |
76 |
} |
77 |
|
78 |
|
79 |
TT_GET_KEY = 'k'
|
80 |
TT_ACK = 'a'
|
81 |
TT_NACK = 'n'
|
82 |
TT_TO = 'f'
|
83 |
TT_TIMEOUT = 't'
|
84 |
|
85 |
BAUD_RATE = 9600
|
86 |
|
87 |
#fails until a warning is sent
|
88 |
MAX_TOOL_FAILS = 5
|
89 |
|
90 |
#################################################
|
91 |
#this code spawns a thread which always puts the last card id in the
|
92 |
#lastid variable.
|
93 |
#################################################
|
94 |
import threading |
95 |
import time |
96 |
|
97 |
lastid = None
|
98 |
idready = False
|
99 |
|
100 |
cv = threading.Condition() |
101 |
|
102 |
class idThread ( threading.Thread ): |
103 |
|
104 |
def run ( self ): |
105 |
global lastid
|
106 |
global idready
|
107 |
|
108 |
try:
|
109 |
while True: |
110 |
l = raw_input()
|
111 |
cv.acquire() |
112 |
id = re.search('%([0-9]*)=.*', l)
|
113 |
if id != None: |
114 |
lastid = id
|
115 |
print "!!! setting idready" |
116 |
idready = True
|
117 |
cv.notify() |
118 |
cv.release() |
119 |
|
120 |
except EOFError: |
121 |
cv.acquire() |
122 |
idready = True
|
123 |
lastid=None
|
124 |
cv.notify() |
125 |
cv.release() |
126 |
#end thread
|
127 |
###############################################
|
128 |
|
129 |
flog = open("tooltron.log", "a") |
130 |
|
131 |
def logMessage(str): |
132 |
flog.write(strftime("%Y.%m.%d %H:%M:%S ") + str + "\n") |
133 |
flog.flush() |
134 |
|
135 |
logMessage("----- SERVER STARTED -----")
|
136 |
|
137 |
def keypad2toolID(t): |
138 |
if t in ids: |
139 |
return ids[t]
|
140 |
else:
|
141 |
return -1 |
142 |
|
143 |
def reverseTool(tn): |
144 |
for t in tools: |
145 |
if tn in tools[t]: |
146 |
return t
|
147 |
|
148 |
return 'invalid' |
149 |
|
150 |
#keep count of successive non-acks
|
151 |
toolFails = tools.copy() |
152 |
for t in toolFails: |
153 |
toolFails[t] = 0
|
154 |
|
155 |
def get_last_id(): |
156 |
global idready
|
157 |
global lastid
|
158 |
|
159 |
got = None
|
160 |
|
161 |
print ">>> locking for last_id" |
162 |
|
163 |
cv.acquire() |
164 |
while not idready: |
165 |
print ">>> wait" |
166 |
cv.wait() |
167 |
|
168 |
print ">>> idready set" |
169 |
|
170 |
got = lastid |
171 |
idready = False
|
172 |
cv.release() |
173 |
print ">>> done" |
174 |
|
175 |
return got
|
176 |
|
177 |
def clear_id(): |
178 |
global idready
|
179 |
global lastid
|
180 |
|
181 |
print "=== locking to clear idready flag" |
182 |
cv.acquire() |
183 |
idready = False
|
184 |
lastid = None
|
185 |
cv.release() |
186 |
print "=== done" |
187 |
|
188 |
return
|
189 |
|
190 |
|
191 |
if len(sys.argv) < 3: |
192 |
print "usage: dustmite.py /path/to/keypad/device /path/to/tool/bus/device" |
193 |
|
194 |
else:
|
195 |
|
196 |
pw = getpass.getpass("mysql password: ")
|
197 |
db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm") |
198 |
print "connected, now accepting input" |
199 |
logMessage("connected to database")
|
200 |
|
201 |
#start the id scan thread AFTER the getpass prompt
|
202 |
idThread().start() |
203 |
|
204 |
cursor = db.cursor() |
205 |
|
206 |
qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
|
207 |
|
208 |
# user has 10 seconds to press a key, after that the keypad device
|
209 |
# should send a timeout packet. This is a non-blocking serial
|
210 |
# device so we can re-transmit the key request when the user
|
211 |
# swipes a card instead of being stuck on a blocking read call
|
212 |
keypad = serial.Serial(sys.argv[1], BAUD_RATE, timeout = 0) |
213 |
keypad.flushInput() |
214 |
print keypad
|
215 |
|
216 |
bus = serial.Serial(sys.argv[2], BAUD_RATE, timeout = 2) |
217 |
bus.flushInput() |
218 |
print bus
|
219 |
|
220 |
# ^ <src> <dest> <data>
|
221 |
def sendTool(t): |
222 |
tn = keypad2toolID(t) |
223 |
msg = '^' + chr(1) + chr(tn) + 'O' + chr(1 ^ tn ^ ord('O')) |
224 |
print "seding power to tool ID",tn |
225 |
bus.write(msg) |
226 |
return
|
227 |
|
228 |
#returns [src, data] or [] on error
|
229 |
def readTool(): |
230 |
ret = [0,0,0] |
231 |
|
232 |
if bus.read(1) == '^': |
233 |
ret[0] = bus.read(1) |
234 |
ret[1] = bus.read(1) |
235 |
ret[2] = bus.read(1) |
236 |
x = bus.read(1)
|
237 |
|
238 |
print "got packet",ret |
239 |
|
240 |
if chr(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) == x: |
241 |
return ret
|
242 |
else:
|
243 |
print "xor fail. got", x, "should have been got",(ord(ret[0]) ^ ord(ret[1]) ^ ord(ret[2])) |
244 |
|
245 |
return []
|
246 |
|
247 |
def checkAck(t): |
248 |
tn = keypad2toolID(t) |
249 |
m = readTool() |
250 |
|
251 |
if m== []:
|
252 |
return False |
253 |
|
254 |
|
255 |
return m[0] == chr(tn) and m[1] == chr(1) and m[2] == 'A' |
256 |
|
257 |
|
258 |
while True: |
259 |
|
260 |
id = get_last_id() |
261 |
|
262 |
if id != None: |
263 |
print "\n-----------\nid# ", id.group(1) |
264 |
|
265 |
|
266 |
print "sending key request" |
267 |
keypad.write(TT_GET_KEY) |
268 |
|
269 |
cursor.execute(qry + id.group(1)) |
270 |
|
271 |
result = cursor.fetchall() |
272 |
|
273 |
acl = [] |
274 |
|
275 |
for r in result: |
276 |
tls = r[0].split("\x01") |
277 |
for t in tls: |
278 |
if t != '': |
279 |
try:
|
280 |
acl.extend (tools[t]) |
281 |
except KeyError: |
282 |
#this doesn't really matter
|
283 |
pass
|
284 |
|
285 |
print "user has access to:", acl |
286 |
|
287 |
user = ""
|
288 |
|
289 |
#query for name
|
290 |
qry2 = "SELECT civicrm_contact.display_name \
|
291 |
FROM civicrm_value_roboclub_info_2 \
|
292 |
INNER JOIN (civicrm_contact) \
|
293 |
ON civicrm_value_roboclub_info_2.entity_id = civicrm_contact.id \
|
294 |
WHERE civicrm_value_roboclub_info_2.card_number_1 = " + id.group(1) + ";" |
295 |
|
296 |
cursor.execute(qry2) |
297 |
result = cursor.fetchall() |
298 |
|
299 |
if len(result) > 0: |
300 |
user += result[0][0] |
301 |
|
302 |
qry2 = "SELECT civicrm_email.email \
|
303 |
FROM civicrm_value_roboclub_info_2 \
|
304 |
INNER JOIN (civicrm_email) \
|
305 |
ON civicrm_email.contact_id = civicrm_value_roboclub_info_2.entity_id \
|
306 |
WHERE civicrm_value_roboclub_info_2.card_number_1 = " + id.group(1) + ";" |
307 |
|
308 |
cursor.execute(qry2) |
309 |
result = cursor.fetchall() |
310 |
|
311 |
if len(result) > 0: |
312 |
user += " ("+result[0][0]+")" |
313 |
|
314 |
if user == "": |
315 |
user = str(id.group(1)) |
316 |
|
317 |
print user
|
318 |
|
319 |
startTime = time.time(); |
320 |
resp = ""
|
321 |
while resp=="": |
322 |
resp = keypad.read(1)
|
323 |
#no lock since we are just reading and we can afford to
|
324 |
#miss a loop
|
325 |
if idready == True: |
326 |
#if someone swipes, break out of the loop
|
327 |
print "**swipe during read, bailing out" |
328 |
logMessage("user swipped during keypad read")
|
329 |
break
|
330 |
if time.time() - startTime > keypadTimeout:
|
331 |
print "KEYPAD TIMEOUT!" |
332 |
logMessage("keypad timed out")
|
333 |
break
|
334 |
|
335 |
#if we have a vaid response
|
336 |
if resp != "" and resp != TT_TIMEOUT: |
337 |
|
338 |
toolName = reverseTool(resp) |
339 |
toolNameLong = toolName + " (k="+str(resp)+", t="+str(keypad2toolID(resp))+")" |
340 |
print "request:",resp,toolNameLong |
341 |
|
342 |
if acl.count(resp) > 0: |
343 |
keypad.write(TT_ACK) |
344 |
sendTool(resp) |
345 |
print "ACCESS GRANTED" |
346 |
logMessage(user+" ACCESSED tool "+toolNameLong)
|
347 |
if checkAck(resp):
|
348 |
print "ACK" |
349 |
toolFails[toolName] = 0
|
350 |
else:
|
351 |
print "NO ACK!!!!" |
352 |
logMessage("tool "+toolNameLong+" did not ACK") |
353 |
toolFails[toolName] += 1
|
354 |
if toolFails[toolName] > MAX_TOOL_FAILS:
|
355 |
#TODO: send email
|
356 |
logMessage("WARNING: tool "+toolNameLong+
|
357 |
" has failed to ACK "+
|
358 |
str(MAX_TOOL_FAILS)+" times in a row.") |
359 |
|
360 |
else:
|
361 |
keypad.write(TT_NACK) |
362 |
print "ACCESS DENIED!!" |
363 |
logMessage(user + " DENIED on tool "+toolNameLong)
|
364 |
|
365 |
clear_id() |
366 |
|
367 |
else:
|
368 |
break
|