Browse Source

almost usable. threads are not all temrinating yet

Florian Streibelt 9 years ago
parent
commit
dd49d650fb
2 changed files with 41 additions and 47 deletions
  1. 19
    11
      client-barcode/display.py
  2. 22
    36
      client-barcode/freitagskasse.py

+ 19
- 11
client-barcode/display.py View File

@@ -8,6 +8,7 @@ class Display(threading.Thread):
8 8
 	        threading.Thread.__init__(self)
9 9
 		self.serialport=None
10 10
 		self.runme=True
11
+		self.idlemessage=None
11 12
 		self.portlock = allocate_lock()
12 13
 		self.portname=portname
13 14
 		self.brightness=5
@@ -32,13 +33,13 @@ class Display(threading.Thread):
32 33
 		self.mutex_get()
33 34
 		try:
34 35
 			self.serialport = serial.Serial(self.portname,9600,timeout=2,rtscts=True, dsrdtr=True)
35
-			print ("Initializing display on port:\n  %s\n" % self.serialport )
36
+			#print ("Initializing display on port:\n  %s\n" % self.serialport )
36 37
 			self.cmd_reset()
37 38
 			self.mutex_release()
38 39
 		except Exception as e:
39
-			print ("Exception opening serial port '%s' for display\n" % self.portname)
40
-			print ("Ignoring and trying to open it again later\n")
41
-			print (e)
40
+			#print ("Exception opening serial port '%s' for display\n" % self.portname)
41
+			#print ("Ignoring and trying to open it again later\n")
42
+			#print (e)
42 43
 			self.serialport = None
43 44
 			self.mutex_release()
44 45
 			pass
@@ -75,17 +76,17 @@ class Display(threading.Thread):
75 76
 
76 77
 
77 78
 	def run(self):
78
-		print "Starting Display thread" 
79
+		#print "Starting Display thread" 
79 80
 		self.screensaver.start()
80 81
 		while(self.runme):
81
-			print("display thread loop\n")
82
+			#print("display thread loop\n")
82 83
 			self.mutex_get()
83
-			print("display got mutex and handles scroll\n")
84
+			#print("display got mutex and handles scroll\n")
84 85
 			self.offset_line1 = self.display_handle_scroll(1,self.scroll_line1,self.offset_line1)
85 86
 	                self.offset_line2 = self.display_handle_scroll(2,self.scroll_line2,self.offset_line2)
86 87
 			self.mutex_release()
87 88
 			time.sleep(.5)
88
-		print "Exiting Display thread"
89
+		#print "Exiting Display thread"
89 90
 
90 91
 	def terminate(self):
91 92
 		self.runme = False
@@ -137,7 +138,7 @@ class Display(threading.Thread):
137 138
 	def cmd_brightness(self,level):
138 139
 		if (self.brightness==level):
139 140
 			return
140
-		print("setting brightness to %i \n" %level)
141
+		#print("setting brightness to %i \n" %level)
141 142
 		if (level==0): #turn off:
142 143
 			self.cmd_display_on(False)	
143 144
                         self.brightness = 0
@@ -155,13 +156,13 @@ class Display(threading.Thread):
155 156
 		if (on):
156 157
 			if (not self.brightness==0):
157 158
 				return
158
-			print("setting display to on: %s \n" % str(on))
159
+			#print("setting display to on: %s \n" % str(on))
159 160
 			self.cmd_blink(0)
160 161
 			self.brightness=5
161 162
 		else:
162 163
 			if (self.brightness==0):
163 164
 				return
164
-			print("setting display to on: %s \n" % str(on))
165
+			#print("setting display to on: %s \n" % str(on))
165 166
 			self.cmd_blink(255)
166 167
 			self.brightness=0
167 168
 
@@ -209,6 +210,11 @@ class Display(threading.Thread):
209 210
 		else:
210 211
 			self.open_port()	
211 212
 
213
+	def setIdlemessage(self,text):
214
+		self.idlemessage=text
215
+	
216
+	def getIdlemessage(self):
217
+		return self.idlemessage
212 218
 
213 219
 	def mutex_get(self):
214 220
 		self.portlock.acquire()
@@ -256,6 +262,7 @@ class Screensaver (threading.Thread):
256 262
 		self.display.cmd_brightness(5)
257 263
 		self.mutex.release()
258 264
 
265
+
259 266
 	def _main_loop(self):
260 267
 		if (self.idlecounter < self.timeout_off):
261 268
 			self.idlecounter+=1
@@ -269,6 +276,7 @@ class Screensaver (threading.Thread):
269 276
 		if (self.idlecounter==self.timeout_message):
270 277
 			self.display.mutex_get()
271 278
 			self.display.cmd_time_set()
279
+			self.display.scroll_line1 = self.display.getIdlemessage()
272 280
 			self.display.mutex_release()
273 281
 			return
274 282
 

+ 22
- 36
client-barcode/freitagskasse.py View File

@@ -42,7 +42,6 @@ display_fifo = None
42 42
 scroll_line1 = None
43 43
 scroll_line2 = None
44 44
 
45
-idlemessage = None
46 45
 
47 46
 offset_line1 = 0
48 47
 offset_line2 = 0
@@ -56,19 +55,6 @@ SCREENSAVER_OFF     = 2* 10*60
56 55
 
57 56
 lock = allocate_lock()
58 57
 
59
-def send_display(s):
60
-	global myDisplay
61
-#	myDisplay.write(s,False)
62
-
63
-#Front-End Funtion to display a Screen
64
-# with heading
65
-def display_screen(title,message):
66
-	myDisplay.display_screen(title,message)
67
-
68
-
69
-#Front-End function to send data to the display.
70
-def print_display(s):
71
-	myDisplay.write(s,False)
72 58
 
73 59
 def clear():
74 60
 	os.system('clear')
@@ -165,7 +151,7 @@ class Status:
165 151
 		if self.logged_in():
166 152
 			print('Eingeloggt als:  %s%s%s' % (COLOR_SOME, self.login_name, COLOR_RESET))
167 153
 			print()
168
-			print_display('\x0cHallo %-14s' % (self.login_name[:13]+"!") )
154
+			myDisplay.write('\x0cHallo %-14s' % (self.login_name[:13]+"!") )
169 155
 			if self.transfers:
170 156
 				initial_command, initial_balance = self.transfers[0]
171 157
 
@@ -197,11 +183,11 @@ class Status:
197 183
 						mylabel="Pfand "+command.commodity_label()[:9]
198 184
 					if (mycmd==3):	
199 185
 						mylabel=("%-13s" % (command.commodity_label()[:13]))+"+P"
200
-					print_display('\x0b%-15s %4.2f' % (asciify(mylabel)[:15],abs(command.difference())));
201
-					print_display('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
186
+					myDisplay.write('\x0b%-15s %4.2f' % (asciify(mylabel)[:15],abs(command.difference())));
187
+					myDisplay.write('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
202 188
 				else:
203
-					print_display('\x0b%-15s %4.2f' % (command.label()[:15],abs(command.difference())));
204
-					print_display('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
189
+					myDisplay.write('\x0b%-15s %4.2f' % (command.label()[:15],abs(command.difference())));
190
+					myDisplay.write('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
205 191
 
206 192
 				if len(self.transfers) > 1:
207 193
 					show_total(self.balance - initial_balance, plus='+')
@@ -210,13 +196,13 @@ class Status:
210 196
 
211 197
 				if self.balance < 0:
212 198
 					warn_balance()
213
-					print_display('\x0b\nKonto: %5.2f!' % (self.balance) )
199
+					myDisplay.write('\x0b\nKonto: %5.2f!' % (self.balance) )
214 200
 					print()
215 201
 
216 202
 				print(COLOR_MUCH + 'Committen nicht vergessen.' + COLOR_RESET)
217 203
 			else:
218 204
 				print('Kontostand beträgt:  %s%.2f Euro%s' % (COLOR_MUCH, self.balance, COLOR_RESET))
219
-				display_screen("KONTOSTAND","%s: %.2f Euro" % (self.login_name,self.balance))
205
+				myDisplay.display_screen("KONTOSTAND","%s: %.2f Euro" % (self.login_name,self.balance))
220 206
 				if self.balance < 0:
221 207
 					print()
222 208
 					warn_balance()
@@ -224,7 +210,7 @@ class Status:
224 210
 			print(COLOR_MUCH + 'Bitte einloggen.' + COLOR_RESET)
225 211
 			print()
226 212
 			print('Scanne dazu deine ID-Karte mit dem Barcode-Leser.')
227
-			display_screen("LOGIN","Bitte scanne Deinen login-Token! *** ")
213
+			myDisplay.display_screen("LOGIN","Bitte scanne Deinen login-Token! *** ")
228 214
 		print()
229 215
 
230 216
 	def logged_in(self):
@@ -253,7 +239,7 @@ class Status:
253 239
 			try:
254 240
 				command.run(self.login_name)
255 241
 			except urllib2.HTTPError as e:
256
-				print_display('\x0cFEHLER: Server Error%20s' % str(e)[:20])
242
+				myDisplay.write('\x0cFEHLER: Server Error%20s' % str(e)[:20])
257 243
 				error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
258 244
 				break
259 245
 			else:
@@ -336,11 +322,11 @@ class Status:
336 322
 				error_page(_PRODUCT_FIRST)
337 323
 				return
338 324
 			if prev.includes_deposit():
339
-				print_display('\x0cFEHLER: schon Pfand %20s' % prev.item_name()[:20])
325
+				myDisplay.write('\x0cFEHLER: schon Pfand %20s' % prev.item_name()[:20])
340 326
 				error_page('FEHLER: Pfand für Produkt "%s" bereits aktiviert' % prev.item_name())
341 327
 				return
342 328
 			if prev.deposit_value() <= 0:
343
-				print_display('\x0cFEHLER: Pfandfrei!  %20s' % prev.item_name()[:20])
329
+				myDisplay.write('\x0cFEHLER: Pfandfrei!  %20s' % prev.item_name()[:20])
344 330
 				error_page('FEHLER: Produkt "%s" hat kein Pfand' % prev.item_name())
345 331
 				return
346 332
 			before = prev.difference()
@@ -382,14 +368,13 @@ def print_prompt():
382 368
 
383 369
 
384 370
 def handle(line, status):
385
-	global idlemessage
386 371
 	if line == 'exit':
387 372
 		clear()
388
-		print_display("\x0c")
373
+		myDisplay.write("\x0c")
389 374
 		sys.exit(0)
390 375
 
391 376
 	if status.logged_in():
392
-		idlemessage=" Comitten nicht vergessen! ***"
377
+		myDisplay.setIdlemessage(" Comitten nicht vergessen! ***")
393 378
 		if line in CODES:
394 379
 			call = CODES[line]
395 380
 			method = call[0]
@@ -400,27 +385,27 @@ def handle(line, status):
400 385
 				item = status.find(line)
401 386
 			except urllib2.HTTPError as e:
402 387
 				if e.code == 404:  # URL not found == item not found with REST
403
-					print_display('\x0cERROR: %13sCode ist unbekannt' % ( line[:13]))
388
+					myDisplay.display_screen("FEHLER","Code ist unbekannt: '%s'" % ( line[:23]))
404 389
 					error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line)
405 390
 				else:
406
-					print_display('\x0cERROR: Server Error%20s' % str(e)[:20])
391
+					myDisplay.display_screen("Server error",'%20s' % str(e)[:20])
407 392
 					error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
408 393
 			else:
409 394
 				status.buy(item)
410 395
 	else:
411
-		idlemessage="Mir ist langweilig!"
396
+		myDisplay.setIdlemessage("Mir ist langweilig!")
412 397
 		try:
413 398
 			status.login(line)
414 399
 		except urllib2.HTTPError as e:
415 400
 			if e.code == 404:  # URL not found == user unknown
416
-				display_screen("FEHLER","Nutzer ist  unbekannt: '%s' *** " % line)
401
+				myDisplay.display_screen("FEHLER","Nutzer ist  unbekannt: '%s' *** " % line)
417 402
 
418 403
 				error_page('FEHLER: Benutzer "%s" nicht bekannt' % line,
419 404
 						hint_message='Ist in der WebApp unter "Einstellungen" ' \
420 405
 						'für Ihren Account Plugin "BarcodePlugin" ' \
421 406
 						'als erlaubt markiert?')
422 407
 			else:
423
-				print_display('\x0cFEHLER: Server Error%20s' % str(e)[:20])
408
+				myDisplay.display_screen("FEHLER",'Server Error %20s' % str(e)[:20])
424 409
 				error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
425 410
 		except urllib2.URLError as e:
426 411
 			error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
@@ -447,14 +432,15 @@ def main():
447 432
 	time.sleep(2)
448 433
 	myDisplay.cmd_cursor_show(False)
449 434
 
450
-	display_screen("HINWEIS","Herzlich willkommen bei der Freitagsrunde! *** ")
435
+	myDisplay.display_screen("HINWEIS","Herzlich willkommen bei der Freitagsrunde! *** ")
436
+	time.sleep(5)
451 437
 	while True:
452 438
 		clear()
453 439
 		status.dump()
454 440
 		print_prompt()
455 441
 		line = read_line(sys.stdin, timeout=3*60.0, timeout_func=status.logout)
456 442
 		if line:
457
-			print_display('\x0cBarcode:\n%20s' % line[:20])
443
+			myDisplay.write('\x0cBarcode:\n%20s' % line[:20])
458 444
 			handle(line, status)
459 445
 
460 446
 
@@ -463,6 +449,6 @@ if __name__ == '__main__':
463 449
 		main()
464 450
 	except KeyboardInterrupt:
465 451
 		pass
466
-		print_display("\x1b\x40Goodbye!")
452
+		myDisplay.write("\x1b\x40Goodbye!")
467 453
 		myDisplay.terminate()
468 454
 

Loading…
Cancel
Save