fixed Stop Timer, added Debug Mode, thread safety
This commit is contained in:
parent
e495c34bda
commit
e7e894332c
42
leinwand.py
42
leinwand.py
|
@ -9,12 +9,18 @@ import pifaceio
|
||||||
|
|
||||||
PORT = 8080
|
PORT = 8080
|
||||||
BIND_ADDRESS = '127.0.0.1'
|
BIND_ADDRESS = '127.0.0.1'
|
||||||
WAITING_TIME = 10
|
|
||||||
|
# time to wait until sending STOP_COMMAND
|
||||||
|
WAITING_TIME = 120
|
||||||
|
|
||||||
# commands
|
# commands
|
||||||
UP_COMMAND = 0b00000000
|
UP_COMMAND = 0b00000000
|
||||||
DOWN_COMMAND = 0b00000000
|
DOWN_COMMAND = 0b00000000
|
||||||
STOP_COMMAND = 0b00000000
|
STOP_COMMAND = 0b00000000
|
||||||
|
|
||||||
|
# debug
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
class ScreenStates(enum.Enum):
|
class ScreenStates(enum.Enum):
|
||||||
stop = 'stop'
|
stop = 'stop'
|
||||||
moving_up = 'moving_up'
|
moving_up = 'moving_up'
|
||||||
|
@ -26,25 +32,46 @@ class LeinwandRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
|
|
||||||
current_state = ScreenStates.stop
|
current_state = ScreenStates.stop
|
||||||
stop_timer = None
|
stop_timer = None
|
||||||
pf = pifaceio.PiFace()
|
|
||||||
lock = threading.Semaphore()
|
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):
|
def handle_screen_up(self):
|
||||||
|
LeinwandRequestHandler.lock.acquire()
|
||||||
|
if DEBUG:
|
||||||
print('moving up...')
|
print('moving up...')
|
||||||
|
else:
|
||||||
LeinwandRequestHandler.pf.write(UP_COMMAND)
|
LeinwandRequestHandler.pf.write(UP_COMMAND)
|
||||||
LeinwandRequestHandler.current_state = ScreenStates.moving_up
|
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 = threading.Timer(WAITING_TIME, self.screen_stop)
|
||||||
LeinwandRequestHandler.stop_timer.start()
|
LeinwandRequestHandler.stop_timer.start()
|
||||||
|
|
||||||
|
LeinwandRequestHandler.lock.release()
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
print(self.current_state)
|
print(self.current_state)
|
||||||
|
|
||||||
def handle_screen_down(self):
|
def handle_screen_down(self):
|
||||||
|
LeinwandRequestHandler.lock.acquire()
|
||||||
|
if DEBUG:
|
||||||
print('moving down...')
|
print('moving down...')
|
||||||
|
else:
|
||||||
LeinwandRequestHandler.pf.write(DOWN_COMMAND)
|
LeinwandRequestHandler.pf.write(DOWN_COMMAND)
|
||||||
LeinwandRequestHandler.current_state = ScreenStates.moving_down
|
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 = threading.Timer(WAITING_TIME, self.screen_stop)
|
||||||
LeinwandRequestHandler.stop_timer.start()
|
LeinwandRequestHandler.stop_timer.start()
|
||||||
|
|
||||||
|
LeinwandRequestHandler.lock.release()
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
|
|
||||||
def return_screen_state(self):
|
def return_screen_state(self):
|
||||||
|
@ -52,15 +79,20 @@ class LeinwandRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header('content-type', 'text/plain')
|
self.send_header('content-type', 'text/plain')
|
||||||
self.wfile.write('\n')
|
self.wfile.write('\n')
|
||||||
self.wfile.write('{status: "' + LeinwandRequestHandler.current_state + '"}')
|
self.wfile.write('{state: "' + LeinwandRequestHandler.current_state + '"}')
|
||||||
|
|
||||||
def screen_stop(self):
|
def screen_stop(self):
|
||||||
|
LeinwandRequestHandler.lock.acquire()
|
||||||
|
if DEBUG:
|
||||||
|
print('stop')
|
||||||
|
else:
|
||||||
|
LeinwandRequestHandler.pf.write(STOP_COMMAND)
|
||||||
if LeinwandRequestHandler.current_state == ScreenStates.moving_down:
|
if LeinwandRequestHandler.current_state == ScreenStates.moving_down:
|
||||||
LeinwandRequestHandler.current_state = ScreenStates.down
|
LeinwandRequestHandler.current_state = ScreenStates.down
|
||||||
elif LeinwandRequestHandler.current_state == ScreenStates.moving_up:
|
elif LeinwandRequestHandler.current_state == ScreenStates.moving_up:
|
||||||
LeinwandRequestHandler.current_state = ScreenStates.up
|
LeinwandRequestHandler.current_state = ScreenStates.up
|
||||||
print('stop')
|
LeinwandRequestHandler.lock.release()
|
||||||
LeinwandRequestHandler.pf.write(STOP_COMMAND)
|
|
||||||
|
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
|
|
Loading…
Reference in New Issue