-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtabulator.py
318 lines (272 loc) · 8.59 KB
/
tabulator.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
from scanning import nfc
from scanning import barcode
from search import upc
import os
import time
import threading
import curses
from data import simple_text_db as db
from data.tab import Tab
IDLE, PRIMED, USED, PRICE_CHECK, CHECKOUT = range(5)
stateStr = [ "IDLE", "PRIMED", "USED", "PRICE_CHECK", "CHECKOUT" ]
RFID, BARCODE = range(2)
undo_card = 1253887727
def RFIDThread(s):
while True:
rfid = nfc.getRFID(300)
if rfid != None:
TransactionState.lock.acquire()
if s.state == IDLE:
record = db.get_record(1, str(rfid))
if record != None:
s.startTransaction(rfid, RFID, record[-1])
elif s.state == PRIMED:
if rfid == s.ID and s.Type == RFID:
s.reset()
elif rfid == undo_card:
s.reset()
else:
record = db.get_record(1, str(rfid))
if record != None:
s.startTransaction(rfid, RFID, record[-1])
elif s.state == USED:
if rfid == s.ID and s.Type == RFID:
s.checkout()
TransactionState.lock.release()
time.sleep(3)
TransactionState.lock.acquire()
s.reset()
elif rfid == undo_card:
s.undo()
else:
record = db.get_record(1, str(rfid))
if record != None:
s.startTransaction(rfid, RFID, record[-1])
TransactionState.lock.release()
time.sleep(1)
def BarcodeThread(s, mainwin):
while True:
code = barcode.scan(mainwin)
record = db.get_record(1, code)
TransactionState.lock.acquire()
if s.state == IDLE:
if record != None:
s.startTransaction(code, BARCODE, record[-1])
else:
s.priceCheck(upc.lookup(code))
TransactionState.lock.release()
time.sleep(3)
TransactionState.lock.acquire()
s.reset()
elif s.state == PRIMED:
if code == s.ID and s.Type == BARCODE:
s.reset()
elif record != None:
s.startTransaction(code, BARCODE, record[-1])
else:
s.addTransaction(upc.lookup(code))
elif s.state == USED:
if code == s.ID and s.Type == BARCODE:
s.checkout()
TransactionState.lock.release()
time.sleep(3)
TransactionState.lock.acquire()
s.reset()
elif record != None:
s.startTransaction(code, BARCODE, record[-1])
else:
s.addTransaction(upc.lookup(code))
TransactionState.lock.release()
class TransactionState:
lock = threading.RLock()
def __init__(self, initialState = IDLE):
self.state = initialState
self.ID = None
self.Type = None
self.uname = None
self.stack = []
def startTransaction(self, ID, Type, uname):
self.state = PRIMED
self.ID = ID
self.Type = Type
self.stack = []
self.uname = uname
def reset(self):
self.state = IDLE
self.ID = None
self.Type = None
self.uname = None
def addTransaction(self, transaction):
hasNone = False
if len(self.stack) > 0 and self.stack[-1] == None:
hasNone = True
if transaction != None:
if hasNone:
self.stack.pop()
self.stack.append(transaction)
elif not hasNone:
self.stack.append(None)
self.state = USED
def checkout(self):
total = 0
self.state = CHECKOUT
f = open('.tabulator.log', 'a')
for item in self.stack:
if item != None:
f.write("%s,%s,%s,%s\n" % (time.ctime(), self.uname,
self.ID,item['description']))
total += float(item['price'])
Tab(self.uname).addTo(total)
def undo(self):
self.stack.pop()
if len(self.stack) == 0:
self.state = PRIMED
def priceCheck(self, data):
self.state = PRICE_CHECK
self.pc_data = data
def __str__(self):
retval = stateStr[self.state]
if self.ID != None:
retval += " : "
retval += str(self.ID)
retval += " : %d transactions" % len(self.stack)
return retval
csua_rows = [
' .d8888b. .d8888b. 888 888 d8888',
'd88P Y88b d88P Y88b 888 888 d88888',
'888 888 Y88b. 888 888 d88P888',
'888 "Y888b. 888 888 d88P 888',
'888 "Y88b. 888 888 d88P 888',
'888 888 "888 888 888 d88P 888',
'Y88b d88P Y88b d88P Y88b. .d88P d8888888888',
' "Y8888P" "Y8888P" "Y88888P" d88P 888'
]
price_check_rows = [
' ###### ###### ### ## ##',
'## ## ## ## ## ## ### ##',
'## ## ## ## #### ##',
' ###### ## ## ## ## ## ##',
' ## ## ######### ## ####',
'## ## ## ## ## ## ## ###',
' ###### ###### ## ## ## ##',
'',
'#### ######## ######## ## ##',
' ## ## ## ### ###',
' ## ## ## #### ####',
' ## ## ###### ## ## ## ##',
' ## ## ## ## ### ##',
' ## ## ## ## ##',
'#### ## ######## ## ##'
]
login_rows = [
' ###### ###### ### ## ##',
'## ## ## ## ## ## ### ##',
'## ## ## ## #### ##',
' ###### ## ## ## ## ## ##',
' ## ## ######### ## ####',
'## ## ## ## ## ## ## ###',
' ###### ###### ## ## ## ##',
'',
'#### ##### ',
' ## ## ### ',
' ## ## ##',
' ## ## ##',
' ## ## ##',
' ## ## ### ',
'#### ##### '
]
def graphics(stdscr, state):
r = threading.Thread(target=RFIDThread, args=[state])
b = threading.Thread(target=BarcodeThread, args=[state, stdscr])
r.daemon = True
b.daemon = True
r.start()
b.start()
height, width = stdscr.getmaxyx()
topWin = curses.newwin(10, 0)
leftWin = curses.newwin(height - 11, int(width/2), 10, 0)
rightWin = curses.newwin(height - 11, int(width/2), 10, int(width/2))
mainWin = curses.newwin(height - 11, 0, 10, 0)
statusBar = curses.newwin(1, 0, height - 1, 0)
while r.isAlive() and b.isAlive():
topWin.erase()
startrow = 1
for row in csua_rows:
startix = int(width / 2 - (len(row) / 2))
topWin.addstr(startrow, startix, row)
startrow += 1
topWin.refresh()
TransactionState.lock.acquire()
statusBar.erase()
statusBar.addstr(0, 0, str(state))
statusBar.refresh()
if state.state == IDLE:
leftWin.erase()
leftWin.border()
startrow = 1
for row in price_check_rows:
startix = int(width / 4 - (len(row) / 2))
leftWin.addstr(startrow, startix, row)
startrow += 1
t = "For a price check"
leftWin.addstr(startrow + 1, int(width / 4 - (len(t) / 2)), t)
leftWin.refresh()
rightWin.erase()
rightWin.border()
startrow = 1
for row in login_rows:
startix = int(width / 4 - (len(row) / 2))
rightWin.addstr(startrow, startix, row)
startrow += 1
t = "To begin a transaction"
rightWin.addstr(startrow + 1, int(width / 4 - (len(t) / 2)), t)
rightWin.refresh()
rightWin.refresh()
elif state.state == PRICE_CHECK:
mainWin.erase()
mainWin.border()
data = state.pc_data
if data != None and 'price' in data and 'description' in data:
price = data['price']
description = data['description']
mainWin.addstr(1, 1, description + (" - $%03.2f" % float(price)))
else:
mainWin.addstr(1, 1, "Item not found")
mainWin.refresh()
elif state.state == PRIMED:
mainWin.erase()
mainWin.border()
mainWin.addstr(1, 1, "Welcome %s" % state.uname)
t = "Your tab is currently: $%03.2f" % Tab(state.uname).value()
mainWin.addstr(1, width - 1 - len(t), t)
mainWin.addstr(height-13, 1, "Please scan your first item")
mainWin.refresh()
elif state.state == USED:
mainWin.erase()
mainWin.border()
mainWin.addstr(1, 1, "Welcome %s" % state.uname)
t = "Your tab is currently: $%03.2f" % Tab(state.uname).value()
mainWin.addstr(1, width - 1 - len(t), t)
row = 3
total = 0
for item in state.stack:
if item != None and 'price' in item and 'description' in item:
price = float(item['price'])
total += price
description = item['description']
mainWin.addstr(row, 1, description + (" - $%03.2f" % price))
else:
mainWin.addstr(row, 1, "Item not found")
row += 1
mainWin.addstr(row+1, 1, "Total: $%03.2f" % total)
mainWin.refresh()
else:
mainWin.erase()
mainWin.border()
mainWin.addstr(1, 1, "Thank you %s. Your tab is now $%03.2f" % (state.uname, Tab(state.uname).value()))
mainWin.refresh()
TransactionState.lock.release()
time.sleep(.5)
if __name__ == "__main__":
state = TransactionState()
curses.wrapper(graphics, state)