From e7e894332c548bfcd951b651f36f894d926bf061 Mon Sep 17 00:00:00 2001 From: Stefan Kinzel Date: Tue, 11 Feb 2014 20:49:38 +0100 Subject: [PATCH] fixed Stop Timer, added Debug Mode, thread safety --- leinwand.py | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/leinwand.py b/leinwand.py index 523eb41..6181797 100644 --- a/leinwand.py +++ b/leinwand.py @@ -9,12 +9,18 @@ import pifaceio PORT = 8080 BIND_ADDRESS = '127.0.0.1' -WAITING_TIME = 10 + +# time to wait until sending STOP_COMMAND +WAITING_TIME = 120 + # commands UP_COMMAND = 0b00000000 DOWN_COMMAND = 0b00000000 STOP_COMMAND = 0b00000000 +# debug +DEBUG = True + class ScreenStates(enum.Enum): stop = 'stop' moving_up = 'moving_up' @@ -26,25 +32,46 @@ class LeinwandRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): current_state = ScreenStates.stop stop_timer = None - pf = pifaceio.PiFace() lock = threading.Semaphore() + def __init__(self, request, client_address, server): + BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request, client_address, server) + if DEBUG: + pass + else: + LeinwandRequestHandler.pf = pifaceio.PiFace() def handle_screen_up(self): - print('moving up...') - LeinwandRequestHandler.pf.write(UP_COMMAND) + LeinwandRequestHandler.lock.acquire() + if DEBUG: + print('moving up...') + else: + LeinwandRequestHandler.pf.write(UP_COMMAND) LeinwandRequestHandler.current_state = ScreenStates.moving_up + + if LeinwandRequestHandler.stop_timer is not None: + LeinwandRequestHandler.stop_timer.cancel() LeinwandRequestHandler.stop_timer = threading.Timer(WAITING_TIME, self.screen_stop) LeinwandRequestHandler.stop_timer.start() + + LeinwandRequestHandler.lock.release() self.send_response(200) print(self.current_state) def handle_screen_down(self): - print('moving down...') - LeinwandRequestHandler.pf.write(DOWN_COMMAND) + LeinwandRequestHandler.lock.acquire() + if DEBUG: + print('moving down...') + else: + LeinwandRequestHandler.pf.write(DOWN_COMMAND) LeinwandRequestHandler.current_state = ScreenStates.moving_down + + if LeinwandRequestHandler.stop_timer is not None: + LeinwandRequestHandler.stop_timer.cancel() LeinwandRequestHandler.stop_timer = threading.Timer(WAITING_TIME, self.screen_stop) LeinwandRequestHandler.stop_timer.start() + + LeinwandRequestHandler.lock.release() self.send_response(200) def return_screen_state(self): @@ -52,15 +79,20 @@ class LeinwandRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): self.send_response(200) self.send_header('content-type', 'text/plain') self.wfile.write('\n') - self.wfile.write('{status: "' + LeinwandRequestHandler.current_state + '"}') + self.wfile.write('{state: "' + LeinwandRequestHandler.current_state + '"}') def screen_stop(self): + LeinwandRequestHandler.lock.acquire() + if DEBUG: + print('stop') + else: + LeinwandRequestHandler.pf.write(STOP_COMMAND) if LeinwandRequestHandler.current_state == ScreenStates.moving_down: LeinwandRequestHandler.current_state = ScreenStates.down elif LeinwandRequestHandler.current_state == ScreenStates.moving_up: - LeinwandRequestHandler.current_state = ScreenStates.up - print('stop') - LeinwandRequestHandler.pf.write(STOP_COMMAND) + LeinwandRequestHandler.current_state = ScreenStates.up + LeinwandRequestHandler.lock.release() + def do_GET(self):