Revision 283 trunk/common/common.py
| common.py (revision 283) | ||
|---|---|---|
| 23 | 23 |
import serial |
| 24 | 24 |
import sys |
| 25 | 25 |
import time |
| 26 |
import re |
|
| 26 | 27 |
|
| 27 | 28 |
keypadTimeout = 31 #in seconds |
| 28 | 29 |
|
| ... | ... | |
| 76 | 77 |
else: |
| 77 | 78 |
print str(ord(msg[0])) |
| 78 | 79 |
|
| 79 |
for c in msg[1:]: |
|
| 80 |
print str(ord(msg[1])), str(ord(msg[2])), |
|
| 81 |
print msg[3], |
|
| 82 |
|
|
| 83 |
for c in msg[4:]: |
|
| 80 | 84 |
print str(ord(c)), |
| 81 | 85 |
|
| 82 | 86 |
|
| ... | ... | |
| 100 | 104 |
printMsg(msg) |
| 101 | 105 |
bus.write(msg) |
| 102 | 106 |
|
| 107 |
#time.sleep(0.05) #TODO: do I still need this? |
|
| 108 |
|
|
| 103 | 109 |
def sendTool(t): |
| 104 | 110 |
tn = t |
| 105 | 111 |
print "sending power to tool ID",tn |
| 106 | 112 |
sendMessage(tn, TT_GRANT, "") |
| 107 | 113 |
|
| 108 | 114 |
def sendGrant(): |
| 109 |
print "seding grant to cardbox (",CARDBOX_ID," )"
|
|
| 115 |
print "seding grant to cardbox (",CARDBOX_ID,")"
|
|
| 110 | 116 |
sendMessage(CARDBOX_ID, TT_GRANT, "") |
| 111 | 117 |
|
| 112 | 118 |
def sendDeny(): |
| 113 |
print "seding deny to cardbox (",CARDBOX_ID," )"
|
|
| 119 |
print "seding deny to cardbox (",CARDBOX_ID,")"
|
|
| 114 | 120 |
sendMessage(CARDBOX_ID, TT_DENY, "") |
| 115 | 121 |
|
| 116 | 122 |
def sendAck(toolNum): |
| ... | ... | |
| 179 | 185 |
return None |
| 180 | 186 |
else: |
| 181 | 187 |
if start == '': |
| 182 |
print "ERROR: timeout on start delim" |
|
| 188 |
#print "ERROR: timeout on start delim" |
|
| 189 |
pass #TODO: do I need this printout for other (non-transaction reading) cases??? |
|
| 183 | 190 |
else: |
| 184 | 191 |
print "ERROR: did not get start delimiter: "+str(ord(start)) |
| 185 | 192 |
return None |
| 186 | 193 |
|
| 187 |
#returns [key, carnum] pair |
|
| 194 |
# This function keeps trying to read the message until it gets something |
|
| 195 |
def readMessageBlocking(): |
|
| 196 |
print "blocking on read" |
|
| 197 |
m = None |
|
| 198 |
while m == None: |
|
| 199 |
m = readMessage(); |
|
| 200 |
|
|
| 201 |
return m |
|
| 202 |
|
|
| 203 |
#returns [key, cardnum] pair |
|
| 188 | 204 |
def readTransaction(): |
| 189 |
m = readMessage() |
|
| 205 |
m = readMessageBlocking() |
|
| 190 | 206 |
if m != None: |
| 207 |
print "got a message:" |
|
| 191 | 208 |
[src, dest, cmd, data] = m |
| 209 |
src = ord(src) |
|
| 210 |
dest = ord(dest) |
|
| 211 |
print "src:", src, " (should be:",CARDBOX_ID,")" |
|
| 212 |
print "dest:", dest |
|
| 213 |
print "cmd:", cmd, " (should be:",TT_KC,")" |
|
| 214 |
print "len(data)", len(data) |
|
| 215 |
print "data:", data |
|
| 192 | 216 |
if src == CARDBOX_ID and cmd == TT_KC and len(data) >= 2: |
| 193 |
return data[0], data[1:end] |
|
| 194 |
else: |
|
| 195 |
print "ERROR: wrong size data from transaction" |
|
| 196 |
logMessage("ERROR: got bad packet from cardbox")
|
|
| 197 |
sendNack(CARDBOX_IDX) |
|
| 198 |
return None |
|
| 217 |
print "looks like a transaction" |
|
| 218 |
card = re.search('%([0-9]*)=.*', data[1:])
|
|
| 219 |
if card != None: |
|
| 220 |
print "got a card number!" |
|
| 221 |
sendAck(CARDBOX_ID) |
|
| 222 |
return [data[0], card.group(1)] #return the key and parsed out cardnum |
|
| 199 | 223 |
|
| 224 |
# send nack if things didn't look right |
|
| 225 |
sendNack(CARDBOX_ID) |
|
| 226 |
return None |
|
| 200 | 227 |
|
| 228 |
|
|
| 201 | 229 |
#returns [src, dest, command] or [] on error |
| 202 | 230 |
# because of reflection, this will quietly ignore packets send by the server |
| 203 | 231 |
def readTool(): |
Also available in: Unified diff