-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.py
615 lines (478 loc) · 17.3 KB
/
bus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
from rfid import writeBlock, readBlock, readValue, initValue, incValue, decValue, readCardID, sleepCard, readSector, writeSector
import rfid
import vcs
import eventreader
import struct
import time
from stations import Stations
from eventreader import Buttons
import screen
currentStation = 1
busNumber = "111111"
BLOCK_HEADER = 1
BLOCK_CARDNO = 2
BLOCK_TRANS = [8,9,10,12]
BLOCK_MONEY = 13
BLOCKS = [BLOCK_HEADER, BLOCK_CARDNO, BLOCK_MONEY] + BLOCK_TRANS
SECTORS = [0,2,3]
STATUS_OK = 1
STATUS_NOOFFTAG = 2
STATUS_TRANSPORT = 4
STATUS_GETOFF = 8
ERROR_COMM = -1
ERROR_RETAG = -2
ERROR_NOMONEY = -3
ERROR_OVERCHARGED = -4
ERROR_CARDNO = [ord('F')]*16
FEE_SCALE = [1, 0.7, 0.5, 0]
blockBuffer = [0] * 16
outFile = file
def goNext():
global currentStation
currentStation += 1
if(currentStation >= len(Stations)):
currentStation = 1
def goPrev():
global currentStation
currentStation -= 1
if(currentStation <= 0):
currentStation = len(Stations) - 1
def isTagged():
reply = readCardID()
if(reply[0] == ERROR_COMM):
return [ERROR_COMM, reply[1]]
return [STATUS_OK, reply[1:5]]
#Top up amount
def topUp(amount, stationID):
tm = time.localtime(time.time())
value = readValue(BLOCK_MONEY)
if(value[0] == ERROR_COMM): return ERROR_COMM
value = value[1]
if(value + amount > 500000): return ERROR_COMM
header = blockBuffer[BLOCK_HEADER]
dateFormat = "%02d%02d%02d%02d%02d" % (tm.tm_year %100,
tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min)
header[1:5] = [ord(ch) for ch in struct.pack('>I',value + amount)]
header[5:15] = [ord(ch) for ch in dateFormat]
header[15] = stationID
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
if(incValue(BLOCK_MONEY, amount)[0] == ERROR_COMM): return ERROR_COMM
return STATUS_OK
#Charge amount from card
def charge(amount):
global blockBuffer
tm = time.localtime(time.time())
value = readValue(BLOCK_MONEY)
if(value[0] == ERROR_COMM): return ERROR_COMM
value = value[1]
if(value - amount < 0): return ERROR_NOMONEY
header = blockBuffer[BLOCK_HEADER]
dateFormat = "%02d%02d%02d%02d%02d" % (tm.tm_year %100,
tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min)
header[1:5] = [ord(ch) for ch in struct.pack('>I',value - amount)]
header[5:15] = [ord(ch) for ch in dateFormat]
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
if(decValue(BLOCK_MONEY, amount)[0] == ERROR_COMM): return ERROR_COMM
return STATUS_OK
def initCard(CARDNO, cardType, stationID, newValue = 0):
global blockBuffer
CARDNO = [ord(c) for c in CARDNO]
header = [0]*16
header[0] = cardType
blockBuffer[BLOCK_HEADER] = header
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_CARDNO, CARDNO)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_TRANS[0], [0]*16)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_TRANS[1], [0]*16)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_TRANS[2], [0]*16)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_TRANS[3], [0]*16)[0] == ERROR_COMM): return ERROR_COMM
if(initValue(BLOCK_MONEY, 0)[0] == ERROR_COMM): return ERROR_COMM
return topUp(newValue, stationID)
#returns seconds elapsed from today 00:00
def elapsedSecond():
tm = time.localtime(time.time())
return int(time.mktime(tm) - time.mktime(tm[0:3] + (0, 0, 0) + tm[6:9]))
#First 4 bits of header[0] contains transportation info vector
#(0000 for first tag, then 1000, 1100, 1110, 1111 for subsequent onboard tagging)
def getTransportVector(header):
vector = [0]*4
for i in range(4,8):
if((1<<i) & header[0] > 0):
vector[7 - i] = 1
return vector
def setTransportVector(header, vector):
if(len(vector) != 4):
return header
mask = 0
for bit in vector:
mask = (mask << 1) | bit
header[0] = header[0] & 0x0f
header[0] = header[0] | (mask << 4)
return header
def getTransportRecord(index):
global blockBuffer
record = blockBuffer[BLOCK_TRANS[index]]
return record
def lastOffTagged(record):
return record[6] != 0xff
def isTransport(record, busNo):
if(busNo == unpackData(record)[0:6].strip()): return False
offTime = (record[6] << 16) + (record[7] << 8) + record[8]
diff = (elapsedSecond() - offTime) % 86400
if(diff < 300): return True
return False
def getUserType(header):
return header[0] & 0x3
def isReTaggedOnboard(record, busNo, stationID):
if(record[13] != stationID or busNo != unpackData(record)[0:6].strip()): return False
if(record[6] != 0xff): return False
onTime = (record[3] << 16) + (record[4] << 8) + record[5]
diff = (elapsedSecond() - onTime) % 86400
if(diff < 300): return True
return False
def isReTaggedOffboard(record, busNo, stationID):
if(record[14] != stationID or busNo != unpackData(record)[0:6].strip()): return False
offTime = (record[19] << 16) + (record[20] << 8) + record[21]
diff = (elapsedSecond() - onTime) % 86400
if(diff < 300): return True
return False
#precondition : not isRetagged()
def isOffBoard(record, busNo, stationID):
if(record[6] == 0xff and busNo == unpackData(record)[0:6].strip()): return True
return False
def tagged(busNo, stationID):
global blockBuffer
print "\n<Tagged on station " + Stations[stationID] + ", bus No. " + busNo + ">\n"
header = blockBuffer[BLOCK_HEADER]
if(header == 0):
for i in range(3):
header = readBlock(BLOCK_HEADER)
if(header[0] != ERROR_COMM):
header = header[1:]
break
if(header[0] == ERROR_COMM):
return ERROR_COMM
vector = getTransportVector(header)
numTransport = 0
for i in range(4):
if(vector[i] == 0): break
numTransport += 1
lastRecord = [0]*16
if(numTransport > 0):
lastRecord = getTransportRecord(numTransport - 1)
currentRecord = getTransportRecord(numTransport)
if(isReTaggedOnboard(currentRecord, busNo, stationID)):
print "\n<Retagged>\n"
return ERROR_RETAG
if(isReTaggedOffboard(lastRecord, busNo, stationID)):
print "\n<Retagged>\n"
return ERROR_RETAG
if(isOffBoard(currentRecord, busNo, stationID)):
print "\n<off_board>\n"
result = offboardTag(busNo, stationID, header, currentRecord, numTransport)
else:
print "\n<on_board>\n"
result = onboardTag(busNo, stationID, header, lastRecord, currentRecord, numTransport)
if(result == ERROR_COMM): return ERROR_COMM
if(sleepCard()[0] == ERROR_COMM): return ERROR_COMM
return result
def writeRecordToFile(cardNo, record):
cardNo = ''.join([chr(c) for c in cardNo])
record = unpackData(record)
buf = "%16s%32s\n" % (cardNo, record)
outFile.write(buf)
#precondition : not isReTagged() and not isOffBoard()
def onboardTag(busNo, stationID, header, lastRecord, currentRecord, numTransport):
ret = 0
fee = 1000
if(not lastOffTagged(currentRecord)):
ret += STATUS_NOOFFTAG
fee += 500
numTransport = 0
header = setTransportVector(header, [0]*4)
writeRecordToFile(blockBuffer[BLOCK_CARDNO], currentRecord)
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
if(numTransport > 0):
if(isTransport(lastRecord, busNo)):
fee = 0
ret += STATUS_TRANSPORT
else:
numTransport = 0
header = setTransportVector(header, [0]*4)
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
fee = int(fee * FEE_SCALE[getUserType(header)])
result = charge(fee)
if(result == ERROR_COMM): return ERROR_COMM
elif(result == ERROR_NOMONEY): return ERROR_NOMONEY
currentRecord = packOnboardData(busNo, secondFormat(elapsedSecond()), fee, stationID)
if(writeBlock(BLOCK_TRANS[numTransport], currentRecord)[0] == ERROR_COMM):
return ERROR_COMM
return [STATUS_OK + ret, fee, getUserType(header), currentRecord]
def calculateFee(record, stationID):
onStation = record[13]
diff = (stationID - onStation) % len(Stations)
if(diff < 5): return 0
return (diff - 5) * 100
def offboardTag(busNo, stationID, header, currentRecord, numTransport):
ret = 0
fee = 0
vector = [0]*4
if(numTransport >= 3): numTransport = -1
for i in range(numTransport + 1):
vector[i] = 1
header = setTransportVector(header, vector)
fee = int(calculateFee(currentRecord, stationID) * FEE_SCALE[getUserType(header)])
result = charge(fee)
if(result == ERROR_COMM): return ERROR_COMM
elif(result == ERROR_NOMONEY): return ERROR_NOMONEY
currentRecord = repackData(currentRecord, secondFormat(elapsedSecond()), fee, stationID)
writeRecordToFile(blockBuffer[BLOCK_CARDNO], currentRecord)
if(writeBlock(BLOCK_HEADER, header)[0] == ERROR_COMM): return ERROR_COMM
if(writeBlock(BLOCK_TRANS[numTransport], currentRecord)[0] == ERROR_COMM):
return ERROR_COMM
return [STATUS_GETOFF + STATUS_OK, fee, getUserType(header), currentRecord]
def secondFormat(seconds):
return "%02d%02d%02d" % (int(seconds / 60 / 60), int(seconds / 60) % 60, seconds % 60)
def packData(busNo, onTime, feeBasic, onStationID, offTime, feeAdd, offStationID):
busNoCoded = 0xffffffff
packed = []
for ch in busNo:
if(ch == '-'): n = 0xa
elif(ord(ch) >= ord('0') and ord(ch) <= ord('9')): n = int(ch)
else: n = 0xf
busNoCoded = (busNoCoded << 4) + n
onTimeInt = ( int(onTime[0:2]) * 60 + int(onTime[2:4]) ) * 60 + int(onTime[4:6])
offTimeInt = ( int(offTime[0:2]) * 60 + int(offTime[2:4]) ) * 60 + int(offTime[4:6])
packed = [ord(ch) for ch in struct.pack('>I', busNoCoded & 0xffffffff)[1:]]
packed += [ord(ch) for ch in struct.pack('>I', onTimeInt)[1:]]
packed += [ord(ch) for ch in struct.pack('>I', offTimeInt)[1:]]
packed += [ord(ch) for ch in struct.pack('>H', feeBasic)]
packed += [ord(ch) for ch in struct.pack('>H', feeAdd)]
packed += [onStationID, offStationID, 0]
return packed
def repackData(packed, offTime, feeAdd, offStationID):
unpacked = unpackData(packed)
return packData(unpacked[0:6], unpacked[6:12], int(unpacked[12:16]), int(unpacked[16:19]),
offTime, feeAdd, offStationID)
def packOnboardData(busNo, onTime, feeBasic, onstationID):
busNoCoded = 0xffffffff
packed = []
for ch in busNo:
if(ch == '-'): n = 0xa
elif(ord(ch) >= ord('0') and ord(ch) <= ord('9')): n = int(ch)
else: n = 0xf
busNoCoded = (busNoCoded << 4) + n
onTimeInt = ( int(onTime[0:2]) * 60 + int(onTime[2:4]) ) * 60 + int(onTime[4:6])
packed = [ord(ch) for ch in struct.pack('>I', busNoCoded & 0xffffffff)[1:]]
packed += [ord(ch) for ch in struct.pack('>I', onTimeInt)[1:]]
packed += [0xff, 0xff, 0xff]
packed += [ord(ch) for ch in struct.pack('>H', feeBasic)]
packed += [0, 0]
packed += [onstationID, 0xff, 0]
return packed
def unpackData(packed):
busNoEncoded = struct.unpack('sss', ''.join([chr(c) for c in packed[0:3]]))
busNo = ''
for ch in busNoEncoded:
chPair = [(ord(ch) >> 4) & 0xf, ord(ch) & 0xf]
for c in chPair:
if (c >= 0 and c <= 9):
busNo += str(c)
elif (c == 0xa):
busNo += '-'
else:
busNo += ' '
onTimeInt = struct.unpack('>I', '\x00' + ''.join([chr(c) for c in packed[3:6]]))[0]
offTimeInt = struct.unpack('>I', '\x00' + ''.join([chr(c) for c in packed[6:9]]))[0]
onTime = secondFormat(onTimeInt)
offTime = secondFormat(offTimeInt)
feeBasic = "%04d" % struct.unpack('>H', ''.join([chr(c) for c in packed[9:11]]))[0]
feeAdd = "%04d" % struct.unpack('>H', ''.join([chr(c) for c in packed[11:13]]))[0]
onstationID = "%03d" % packed[13]
offstationID = "%03d" % packed[14]
return busNo + onTime + feeBasic + onstationID + offTime + feeAdd + offstationID
def setBusNo(busNo):
global busNumber
busNumber = busNo
def main(busNos):
screen.busNumScreen(busNos[0],busNos[1],busNos[2],busNos[3])
while(True):
eventreader.updateButtonState()
busNumber = ""
for i in range(4):
if(eventreader.isButtonDown(i)): busNumber = busNos[i]
if(len(busNumber) > 0):
result = drive(busNumber, 1)
screen.busNumScreen(busNos[0],busNos[1],busNos[2],busNos[3])
if(eventreader.isButtonDown(Buttons.SEARCH)):
break
time.sleep(0.1)
vcs.clear()
vcs.drawRect(30,10,40,10)
vcs.write(48,14,"Bye!")
vcs.write(47,15,"-ECHO-")
def getFileName():
now = time.localtime(time.time())
return "trans_%04d_%02d%02d_%02d%02d%02d" % (now.tm_year, now.tm_mon, now.tm_mday,
now.tm_hour, now.tm_min, now.tm_sec)
def getValueFromBlock(block):
packed = ''.join([chr(c) for c in block[0:4]])
return struct.unpack('I',packed)[0]
def drive(busNo = busNumber, stationID = 1):
global blockBuffer
global currentStation
currentStation = stationID
prevTime = int(time.time())
prevTime2 = int(time.time())
errorFlag = 0
screen.runScreen(Stations[currentStation], busNo)
backup = [0]*4
global outFile
outFile = open(getFileName(), "w")
while(True):
eventreader.updateButtonState()
errorFlag = 0
if(int(time.time()) != prevTime2):
prevTime2 = int(time.time())
screen.timeScreen()
if(eventreader.isButtonDown(Buttons.VOLUMEDOWN)):
goPrev()
screen.runScreen(Stations[currentStation], busNo)
if(eventreader.isButtonDown(Buttons.VOLUMEUP)):
goNext()
screen.runScreen(Stations[currentStation], busNo)
if(eventreader.isButtonDown(Buttons.BACK)):
outFile.close()
return 0
if(eventreader.isButtonDown(Buttons.MENU)):
rfid.flush()
vcs.write(35, 20, "Please tag your bus card!")
while True:
while(isTagged()[0] != STATUS_OK): time.sleep(0.1)
for i in range(3):
blockBuffer[BLOCK_HEADER] = readBlock(BLOCK_HEADER)
if(blockBuffer[BLOCK_HEADER][0] != ERROR_COMM):
blockBuffer[BLOCK_HEADER] = blockBuffer[BLOCK_HEADER][1:]
break
time.sleep(0.1)
for i in range(3):
[res,Money] = readValue(BLOCK_MONEY)
if(res != ERROR_COMM): break
time.sleep(0.1)
if(blockBuffer[BLOCK_HEADER][0] == ERROR_COMM or res == ERROR_COMM):
errorFlag = ERROR_COMM
vcs.write(35, 20, "READ ERROR" )
print "\n<READ ERROR>\n"
rfid.sleepCard()
time.sleep(2)
screen.runScreen(Stations[currentStation], busNo)
break
screen.chargeScreen(Money)
while True:
eventreader.updateButtonState()
amount = 0
if(eventreader.isButtonDown(Buttons.HOME)):
amount = 1000
elif(eventreader.isButtonDown(Buttons.ENTER)):
amount = 5000
elif(eventreader.isButtonDown(Buttons.MENU)):
amount = 10000
elif(eventreader.isButtonDown(Buttons.BACK)):
amount = 20000
else:
time.sleep(0.1)
continue
res = topUp(amount, currentStation)
if(res != STATUS_OK):
errorFlag = res
vcs.write(35, 20, "WRITE ERROR. Rolling back.. ")
print "\n<WRITE ERROR. Rolling back..>\n"
for i in range(3):
res = writeBlock(BLOCK_HEADER, blockBuffer[BLOCK_HEADER])
if(res != ERROR_COMM): break
time.sleep(0.1)
writeRecordToFile(ERROR_CARDNO,
packOnboardData("000000",secondFormat(elapsedSecond()), -res,0))
rfid.sleepCard()
time.sleep(2)
screen.runScreen(Stations[currentStation], busNo)
break
if(errorFlag == 0):
screen.chargeFinishScreen(Money + amount)
break
time.sleep(3)
screen.runScreen(Stations[currentStation], busNo)
rfid.sleepCard()
break
if(isTagged()[0] == STATUS_OK):
rfid.flush()
screen.cardTagScreen(Stations[currentStation], 0, 0, busNo,
True, "Tagging...Wait")
for sectorIdx in SECTORS:
for i in range(3):
sector = readSector(sectorIdx)
if(sector[0] != -1): break
if (i == 2):
errorFlag = ERROR_COMM
backup[sectorIdx] = sector[1:]
if(errorFlag < 0):
break
if(errorFlag < 0): continue
for blockIdx in BLOCKS:
sectorIdx = int(blockIdx / 4)
offset = blockIdx % 4
print "Sector[%d][%d] => Block[%d]" % (sectorIdx, offset, blockIdx)
blockBuffer[blockIdx] = backup[sectorIdx][offset*16:offset*16+16]
value = 0
if(blockBuffer[BLOCK_MONEY] !=0):
value = getValueFromBlock(blockBuffer[BLOCK_MONEY])
screen.cardTagScreen(Stations[currentStation], 0, value, busNo,
True, "Tagging...Wait")
result = tagged(busNo, currentStation)
if(result == ERROR_COMM):
errorFlag = result
print "\n<ERROR. Try rolling-back>\n"
screen.cardTagScreen(Stations[currentStation], 0, value, busNo,
True, "ERROR. Try rolling-back")
for sectorIdx in SECTORS:
for i in range(3):
print sectorIdx
if(writeSector(sectorIdx, backup[sectorIdx])[0] != -1): break
writeRecordToFile(ERROR_CARDNO,
packOnboardData("000000",secondFormat(elapsedSecond()), -result,0))
elif(result == ERROR_NOMONEY):
errorFlag = result
screen.cardTagScreen(Stations[currentStation], 0, value, busNo,
True, "Not enough minerals.")
print "\n<Not enough minerals.>\n"
writeRecordToFile(ERROR_CARDNO,
packOnboardData("000000",secondFormat(elapsedSecond()), -result,0))
time.sleep(2)
elif(result == ERROR_RETAG):
errorFlag = result
screen.cardTagScreen(Stations[currentStation], 0, value, busNo,
True, "Retagged.")
print "\n<Retagged.>\n"
writeRecordToFile(ERROR_CARDNO,
packOnboardData("000000",secondFormat(elapsedSecond()), -result,0))
time.sleep(2)
elif(result[0] > 0):
print "\n<Tagging success>\n"
msg = "Tagging success"
if(result[0] & STATUS_NOOFFTAG > 0): msg = "NOT TAGGED FOR LAST GETOFF"
if(result[0] & STATUS_TRANSPORT > 0): msg += ", transport"
if(result[0] & STATUS_GETOFF > 0):
msg += ", Bye!"
else:
msg +=". Welcome!"
screen.cardTagScreen(Stations[currentStation], result[1], value - result[1],
busNo, True, msg)
print "\n<" + msg + ">\n"
time.sleep(2)
screen.runScreen(Stations[currentStation], busNo)
if(int(time.time()) - prevTime > 300):
prevTime = int(time.time())
outFile.close()
outFile = open(getFileName(), "w")
time.sleep(0.1)
main(["1550-1","5511","650","5528"])