fixed Stop Timer, added Debug Mode, thread safety
This commit is contained in:
parent
e495c34bda
commit
e7e894332c
52
leinwand.py
52
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):
|
||||
|
|
Loading…
Reference in New Issue