almost usable. threads are not all temrinating yet

This commit is contained in:
Florian Streibelt 2011-10-19 00:12:49 +02:00
parent 65255fb53a
commit dd49d650fb
2 changed files with 41 additions and 47 deletions

View File

@ -8,6 +8,7 @@ class Display(threading.Thread):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.serialport=None self.serialport=None
self.runme=True self.runme=True
self.idlemessage=None
self.portlock = allocate_lock() self.portlock = allocate_lock()
self.portname=portname self.portname=portname
self.brightness=5 self.brightness=5
@ -32,13 +33,13 @@ class Display(threading.Thread):
self.mutex_get() self.mutex_get()
try: try:
self.serialport = serial.Serial(self.portname,9600,timeout=2,rtscts=True, dsrdtr=True) self.serialport = serial.Serial(self.portname,9600,timeout=2,rtscts=True, dsrdtr=True)
print ("Initializing display on port:\n %s\n" % self.serialport ) #print ("Initializing display on port:\n %s\n" % self.serialport )
self.cmd_reset() self.cmd_reset()
self.mutex_release() self.mutex_release()
except Exception as e: except Exception as e:
print ("Exception opening serial port '%s' for display\n" % self.portname) #print ("Exception opening serial port '%s' for display\n" % self.portname)
print ("Ignoring and trying to open it again later\n") #print ("Ignoring and trying to open it again later\n")
print (e) #print (e)
self.serialport = None self.serialport = None
self.mutex_release() self.mutex_release()
pass pass
@ -75,17 +76,17 @@ class Display(threading.Thread):
def run(self): def run(self):
print "Starting Display thread" #print "Starting Display thread"
self.screensaver.start() self.screensaver.start()
while(self.runme): while(self.runme):
print("display thread loop\n") #print("display thread loop\n")
self.mutex_get() self.mutex_get()
print("display got mutex and handles scroll\n") #print("display got mutex and handles scroll\n")
self.offset_line1 = self.display_handle_scroll(1,self.scroll_line1,self.offset_line1) self.offset_line1 = self.display_handle_scroll(1,self.scroll_line1,self.offset_line1)
self.offset_line2 = self.display_handle_scroll(2,self.scroll_line2,self.offset_line2) self.offset_line2 = self.display_handle_scroll(2,self.scroll_line2,self.offset_line2)
self.mutex_release() self.mutex_release()
time.sleep(.5) time.sleep(.5)
print "Exiting Display thread" #print "Exiting Display thread"
def terminate(self): def terminate(self):
self.runme = False self.runme = False
@ -137,7 +138,7 @@ class Display(threading.Thread):
def cmd_brightness(self,level): def cmd_brightness(self,level):
if (self.brightness==level): if (self.brightness==level):
return return
print("setting brightness to %i \n" %level) #print("setting brightness to %i \n" %level)
if (level==0): #turn off: if (level==0): #turn off:
self.cmd_display_on(False) self.cmd_display_on(False)
self.brightness = 0 self.brightness = 0
@ -155,13 +156,13 @@ class Display(threading.Thread):
if (on): if (on):
if (not self.brightness==0): if (not self.brightness==0):
return return
print("setting display to on: %s \n" % str(on)) #print("setting display to on: %s \n" % str(on))
self.cmd_blink(0) self.cmd_blink(0)
self.brightness=5 self.brightness=5
else: else:
if (self.brightness==0): if (self.brightness==0):
return return
print("setting display to on: %s \n" % str(on)) #print("setting display to on: %s \n" % str(on))
self.cmd_blink(255) self.cmd_blink(255)
self.brightness=0 self.brightness=0
@ -209,6 +210,11 @@ class Display(threading.Thread):
else: else:
self.open_port() self.open_port()
def setIdlemessage(self,text):
self.idlemessage=text
def getIdlemessage(self):
return self.idlemessage
def mutex_get(self): def mutex_get(self):
self.portlock.acquire() self.portlock.acquire()
@ -256,6 +262,7 @@ class Screensaver (threading.Thread):
self.display.cmd_brightness(5) self.display.cmd_brightness(5)
self.mutex.release() self.mutex.release()
def _main_loop(self): def _main_loop(self):
if (self.idlecounter < self.timeout_off): if (self.idlecounter < self.timeout_off):
self.idlecounter+=1 self.idlecounter+=1
@ -269,6 +276,7 @@ class Screensaver (threading.Thread):
if (self.idlecounter==self.timeout_message): if (self.idlecounter==self.timeout_message):
self.display.mutex_get() self.display.mutex_get()
self.display.cmd_time_set() self.display.cmd_time_set()
self.display.scroll_line1 = self.display.getIdlemessage()
self.display.mutex_release() self.display.mutex_release()
return return

View File

@ -42,7 +42,6 @@ display_fifo = None
scroll_line1 = None scroll_line1 = None
scroll_line2 = None scroll_line2 = None
idlemessage = None
offset_line1 = 0 offset_line1 = 0
offset_line2 = 0 offset_line2 = 0
@ -56,19 +55,6 @@ SCREENSAVER_OFF = 2* 10*60
lock = allocate_lock() lock = allocate_lock()
def send_display(s):
global myDisplay
# myDisplay.write(s,False)
#Front-End Funtion to display a Screen
# with heading
def display_screen(title,message):
myDisplay.display_screen(title,message)
#Front-End function to send data to the display.
def print_display(s):
myDisplay.write(s,False)
def clear(): def clear():
os.system('clear') os.system('clear')
@ -165,7 +151,7 @@ class Status:
if self.logged_in(): if self.logged_in():
print('Eingeloggt als: %s%s%s' % (COLOR_SOME, self.login_name, COLOR_RESET)) print('Eingeloggt als: %s%s%s' % (COLOR_SOME, self.login_name, COLOR_RESET))
print() print()
print_display('\x0cHallo %-14s' % (self.login_name[:13]+"!") ) myDisplay.write('\x0cHallo %-14s' % (self.login_name[:13]+"!") )
if self.transfers: if self.transfers:
initial_command, initial_balance = self.transfers[0] initial_command, initial_balance = self.transfers[0]
@ -197,11 +183,11 @@ class Status:
mylabel="Pfand "+command.commodity_label()[:9] mylabel="Pfand "+command.commodity_label()[:9]
if (mycmd==3): if (mycmd==3):
mylabel=("%-13s" % (command.commodity_label()[:13]))+"+P" mylabel=("%-13s" % (command.commodity_label()[:13]))+"+P"
print_display('\x0b%-15s %4.2f' % (asciify(mylabel)[:15],abs(command.difference()))); myDisplay.write('\x0b%-15s %4.2f' % (asciify(mylabel)[:15],abs(command.difference())));
print_display('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance)); myDisplay.write('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
else: else:
print_display('\x0b%-15s %4.2f' % (command.label()[:15],abs(command.difference()))); myDisplay.write('\x0b%-15s %4.2f' % (command.label()[:15],abs(command.difference())));
print_display('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance)); myDisplay.write('\x0b\nSUMME: {%02i} %8.2f' % ((i-1),initial_balance - self.balance));
if len(self.transfers) > 1: if len(self.transfers) > 1:
show_total(self.balance - initial_balance, plus='+') show_total(self.balance - initial_balance, plus='+')
@ -210,13 +196,13 @@ class Status:
if self.balance < 0: if self.balance < 0:
warn_balance() warn_balance()
print_display('\x0b\nKonto: %5.2f!' % (self.balance) ) myDisplay.write('\x0b\nKonto: %5.2f!' % (self.balance) )
print() print()
print(COLOR_MUCH + 'Committen nicht vergessen.' + COLOR_RESET) print(COLOR_MUCH + 'Committen nicht vergessen.' + COLOR_RESET)
else: else:
print('Kontostand beträgt: %s%.2f Euro%s' % (COLOR_MUCH, self.balance, COLOR_RESET)) print('Kontostand beträgt: %s%.2f Euro%s' % (COLOR_MUCH, self.balance, COLOR_RESET))
display_screen("KONTOSTAND","%s: %.2f Euro" % (self.login_name,self.balance)) myDisplay.display_screen("KONTOSTAND","%s: %.2f Euro" % (self.login_name,self.balance))
if self.balance < 0: if self.balance < 0:
print() print()
warn_balance() warn_balance()
@ -224,7 +210,7 @@ class Status:
print(COLOR_MUCH + 'Bitte einloggen.' + COLOR_RESET) print(COLOR_MUCH + 'Bitte einloggen.' + COLOR_RESET)
print() print()
print('Scanne dazu deine ID-Karte mit dem Barcode-Leser.') print('Scanne dazu deine ID-Karte mit dem Barcode-Leser.')
display_screen("LOGIN","Bitte scanne Deinen login-Token! *** ") myDisplay.display_screen("LOGIN","Bitte scanne Deinen login-Token! *** ")
print() print()
def logged_in(self): def logged_in(self):
@ -253,7 +239,7 @@ class Status:
try: try:
command.run(self.login_name) command.run(self.login_name)
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
print_display('\x0cFEHLER: Server Error%20s' % str(e)[:20]) myDisplay.write('\x0cFEHLER: Server Error%20s' % str(e)[:20])
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
break break
else: else:
@ -336,11 +322,11 @@ class Status:
error_page(_PRODUCT_FIRST) error_page(_PRODUCT_FIRST)
return return
if prev.includes_deposit(): if prev.includes_deposit():
print_display('\x0cFEHLER: schon Pfand %20s' % prev.item_name()[:20]) myDisplay.write('\x0cFEHLER: schon Pfand %20s' % prev.item_name()[:20])
error_page('FEHLER: Pfand für Produkt "%s" bereits aktiviert' % prev.item_name()) error_page('FEHLER: Pfand für Produkt "%s" bereits aktiviert' % prev.item_name())
return return
if prev.deposit_value() <= 0: if prev.deposit_value() <= 0:
print_display('\x0cFEHLER: Pfandfrei! %20s' % prev.item_name()[:20]) myDisplay.write('\x0cFEHLER: Pfandfrei! %20s' % prev.item_name()[:20])
error_page('FEHLER: Produkt "%s" hat kein Pfand' % prev.item_name()) error_page('FEHLER: Produkt "%s" hat kein Pfand' % prev.item_name())
return return
before = prev.difference() before = prev.difference()
@ -382,14 +368,13 @@ def print_prompt():
def handle(line, status): def handle(line, status):
global idlemessage
if line == 'exit': if line == 'exit':
clear() clear()
print_display("\x0c") myDisplay.write("\x0c")
sys.exit(0) sys.exit(0)
if status.logged_in(): if status.logged_in():
idlemessage=" Comitten nicht vergessen! ***" myDisplay.setIdlemessage(" Comitten nicht vergessen! ***")
if line in CODES: if line in CODES:
call = CODES[line] call = CODES[line]
method = call[0] method = call[0]
@ -400,27 +385,27 @@ def handle(line, status):
item = status.find(line) item = status.find(line)
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
if e.code == 404: # URL not found == item not found with REST if e.code == 404: # URL not found == item not found with REST
print_display('\x0cERROR: %13sCode ist unbekannt' % ( line[:13])) myDisplay.display_screen("FEHLER","Code ist unbekannt: '%s'" % ( line[:23]))
error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line) error_page('FEHLER: Aktion oder Ware "%s" nicht bekannt' % line)
else: else:
print_display('\x0cERROR: Server Error%20s' % str(e)[:20]) myDisplay.display_screen("Server error",'%20s' % str(e)[:20])
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
else: else:
status.buy(item) status.buy(item)
else: else:
idlemessage="Mir ist langweilig!" myDisplay.setIdlemessage("Mir ist langweilig!")
try: try:
status.login(line) status.login(line)
except urllib2.HTTPError as e: except urllib2.HTTPError as e:
if e.code == 404: # URL not found == user unknown if e.code == 404: # URL not found == user unknown
display_screen("FEHLER","Nutzer ist unbekannt: '%s' *** " % line) myDisplay.display_screen("FEHLER","Nutzer ist unbekannt: '%s' *** " % line)
error_page('FEHLER: Benutzer "%s" nicht bekannt' % line, error_page('FEHLER: Benutzer "%s" nicht bekannt' % line,
hint_message='Ist in der WebApp unter "Einstellungen" ' \ hint_message='Ist in der WebApp unter "Einstellungen" ' \
'für Ihren Account Plugin "BarcodePlugin" ' \ 'für Ihren Account Plugin "BarcodePlugin" ' \
'als erlaubt markiert?') 'als erlaubt markiert?')
else: else:
print_display('\x0cFEHLER: Server Error%20s' % str(e)[:20]) myDisplay.display_screen("FEHLER",'Server Error %20s' % str(e)[:20])
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
except urllib2.URLError as e: except urllib2.URLError as e:
error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e)) error_page('FEHLER bei Kommunikation mit Server "%s"' % str(e))
@ -447,14 +432,15 @@ def main():
time.sleep(2) time.sleep(2)
myDisplay.cmd_cursor_show(False) myDisplay.cmd_cursor_show(False)
display_screen("HINWEIS","Herzlich willkommen bei der Freitagsrunde! *** ") myDisplay.display_screen("HINWEIS","Herzlich willkommen bei der Freitagsrunde! *** ")
time.sleep(5)
while True: while True:
clear() clear()
status.dump() status.dump()
print_prompt() print_prompt()
line = read_line(sys.stdin, timeout=3*60.0, timeout_func=status.logout) line = read_line(sys.stdin, timeout=3*60.0, timeout_func=status.logout)
if line: if line:
print_display('\x0cBarcode:\n%20s' % line[:20]) myDisplay.write('\x0cBarcode:\n%20s' % line[:20])
handle(line, status) handle(line, status)
@ -463,6 +449,6 @@ if __name__ == '__main__':
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
print_display("\x1b\x40Goodbye!") myDisplay.write("\x1b\x40Goodbye!")
myDisplay.terminate() myDisplay.terminate()