/
laundry_main.py
executable file
·295 lines (208 loc) · 7.12 KB
/
laundry_main.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
#!/envs/laundry/bin/python
import os
import sys, time
from datetime import datetime
from daemon import Daemon
import subprocess
import eSSP
import time
from multiprocessing import Process
from errors import *
import serial
from sql_operator import *
from tabulate import tabulate
from threading import Thread
# MOVE THIS TO THE __init__ file...
# Figure out which /dev to listen to.
# If it's a Mac, you're going to have to assign it manually, or change the
# script accordingly.
def bv_port(looker):
# Find the port for the BV
find_bv = os.path.expanduser(looker)
bv = subprocess.check_output([find_bv])
print "\nThis is the port for the Bill Validator:", bv[:-1]
return eSSP.eSSP(bv[:-1])
def arduino_port(finder):
# Find the port for the RFID
arduino = os.path.expanduser(finder)
usb = subprocess.check_output([arduino])
print "This is the port for the RFID antenna:", usb[:-1]
return serial.Serial(usb[:-1], 115200, timeout = 0.1, bytesize=serial.EIGHTBITS)
if sys.platform.startswith('darwin'):
mpath = os.path.expanduser('~/raspy/find_bv.sh')
k = bv_port(mpath)
mpath = os.path.expanduser('~/raspy/find_arduino.sh')
ser = arduino_port(mpath)
# Find the port for the BV
find_bv = os.path.expanduser('~/raspy/find_bv.sh')
bv = subprocess.check_output([find_bv])
print "\nThis is the port for the Bill Validator:", bv[:-1]
k = eSSP.eSSP(bv[:-1])
elif sys.platform.startswith('linux'):
# Find the port for the BV
k = bv_port("~/laundry_prog/find_bv.sh")
# Find the port for the RFID returns Serial instance
station_arduino = arduino_port("~/laundry_prog/find_arduino.sh")
# Set screen width in characters
SCREEN_WIDTH = 16
# Initialize serial connection
disp = serial.Serial(port='/dev/ttyAMA0', baudrate=19200)
# Do some inital set-up on the usb devices.
# Start with the Bill Validator:
k.sync()
k.enable_higher_protocol()
# Original
k.set_inhibits(k.easy_inhibit([1, 1, 1, 1]), '0x00')
# print k.set_inhibits(k.easy_inhibit([0, 1, 1, 1]), '0x00')
k.enable()
bills = [1,5,10,20]
def disp_q(value):
# Need to do things diffrently if the screen is OFF
# Make sure the screen is on and backlit
disp.write(chr(22))
disp.write(chr(17))
# Clear the screen
disp.write(chr(12))
time.sleep(.01)
disp.write(chr(128))
# Write the string and value to the screen
disp.write("To add: " + str(int(value )))
class Bank(Daemon):
"""docstring for Bank"""
def __init__(self, pid = '/tmp/CashStation.pid'):
super(Bank, self).__init__(pid)
print "This is a Bank object. The initial value is 0."
# Hopefully it foesn't matter that user hasn't been initiated yet...
self.screener = []
disp.write(chr(22))
disp.write(chr(17))
self.screener.append( Process(target = self.disp_bal, args = ( "Initial Screen", "Second Line" )) )
self.screener[0].start()
self.q = 0 # This value will change
self.account_num = int()
self.value = 0 # This is how much TOTAL money has been collected
self.last_swipe = None
def disp_bal(self, first_line = "No record" , second_line = "None", keep_on = False):
print "Display: ON"
print " ", first_line
print " ", second_line
disp.write(chr(22))
disp.write(chr(17))
# Make a sound so we know the swipe worked.
disp.write(chr(216))
disp.write(chr(209))
disp.write(chr(223))
disp.write(chr(225))
disp.write(chr(227))
# Clear the screen.
disp.write(chr(12))
time.sleep(.01)
disp.write(chr(128))
# write some words
disp.write(str(first_line))
# Move down a line
disp.write(chr(148))
# Print the balance and wait
disp.write(second_line)
# Clear the screen.
if keep_on == True:
print " This should keep the screen lit"
else:
time.sleep(5)
disp.write(chr(12))
time.sleep(.01)
disp.write(chr(21))
disp.write(chr(18))
print "Display: OFF"
def check_for_bill(self):
poll = k.poll()
# len pol = 0 when nothing is happening.
if len(poll) > 1:
if len( poll[1] ) == 2 :
if poll[1][0] == '0xee':
rn = datetime.now()
print "Accepted $%s on %s-%s-%s at %s:%s" % ( str(bills[poll[1][1] - 1 ]), rn.month, rn.day, rn.year, rn.hour, rn.minute )
self.value += bills[poll[1][1] - 1 ]
self.q += bills[poll[1][1] - 1 ]
print "The amount in the queue is", self.q
if self.screener[0].is_alive():
self.screener[0].terminate()
time.sleep(.01)
self.screener[0] = Process(target = self.disp_bal, args = ( "Swipe now to add", "value:" + str(self.q) , True))
self.screener[0].start()
def check_for_swipe(self):
# Use look_for to parse out the ID you wnt to lookup/check
line = station_arduino.readline()
if line is not None:
#~ print "length of line =", len(line)
#~ print line
# A length of 6 means you're getting all 4 bytes of the UID and the
# newline and return characters in the line.
if len(line) == 6 and not self.screener[0].is_alive():
# Get the UID
card_id = line[:-2].encode('hex')
self.last_swipe = card_id
# Look up who swiped using the UID
who_swiped = decode_card(card_id) # HEX --> INT
return who_swiped
else:
return False
# This doesn't have to be a part of the Class,
# it can be a separate function but then it will need to be Threaded...
def run(self):
while True:
#~ print "This should scroll"
self.check_for_bill()
card_id = self.check_for_swipe()
# Only handle card swipes if the display is free AND there's a card at the station.
if card_id :
try:
accepted_card = account_lookup(card_id)
print "There was a swipe at the station."
user = Tenant(accepted_card)
# If there's money saved in the queue, add it to the db then display the information.
if self.q != 0:
old_bal = user.balance
new_bal = user.balance + self.q
user.update_balance(new_bal)
# RESET the queue
self.q = 0
print " Queue reset to:" , self.q
time.sleep(.1)
# Display old and new card values
self.screener[0] = Process(
target = self.disp_bal,
args = ( "OLD: $" + str(int(old_bal)),
"NEW: $" + str(int(new_bal)) )
)
self.screener[0].start()
else:
self.screener[0] = Process(
target = self.disp_bal,
args = ( user.name,
" $" + str(int(user.balance)) )
)
self.screener[0].start()
except CardNotFound as NC:
print NC.card_id, "was just swiped. It was not in the database"
if self.screener[0].is_alive():
self.screener[0].terminate()
time.sleep(.05)
self.screener[0] = Process(target = self.disp_bal, args = ( "NOT RECOGNIZED:", NC.card_id ) )
self.screener[0].start()
if __name__ == '__main__':
daemon = Bank()
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
else:
print "Unknown command"
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart" % sys.argv[0]
sys.exit(2)