feat: webserver: keep alive; move playback write prot to handler
- Reset the app idle timer when interacting with the webapp, so that the device does not turn off while the web ui is used. - Handle denying put/post while playback is active centrally in the before_request handler, so that it does not need to be copy/pasted into every request handler. Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
@@ -116,6 +116,10 @@ class PlayerApp:
|
|||||||
# Check again in a minute
|
# Check again in a minute
|
||||||
self.timer_manager.schedule(time.ticks_ms() + self.idle_timeout_ms, self.onIdleTimeout)
|
self.timer_manager.schedule(time.ticks_ms() + self.idle_timeout_ms, self.onIdleTimeout)
|
||||||
|
|
||||||
|
def reset_idle_timeout(self):
|
||||||
|
if not self.playing:
|
||||||
|
self.timer_manager.schedule(time.ticks_ms() + self.idle_timeout_ms, self.onIdleTimeout)
|
||||||
|
|
||||||
def is_playing(self) -> bool:
|
def is_playing(self) -> bool:
|
||||||
return self.playing
|
return self.playing
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,13 @@ def start_webserver(config_, app_):
|
|||||||
app = app_
|
app = app_
|
||||||
|
|
||||||
|
|
||||||
|
@webapp.before_request
|
||||||
|
async def before_request_handler(request):
|
||||||
|
if request.method in ['PUT', 'POST'] and app.is_playing():
|
||||||
|
return "Cannot write to device while playback is active", 503
|
||||||
|
app.reset_idle_timeout()
|
||||||
|
|
||||||
|
|
||||||
@webapp.route('/')
|
@webapp.route('/')
|
||||||
async def index(request):
|
async def index(request):
|
||||||
print("wohoo, a guest :)")
|
print("wohoo, a guest :)")
|
||||||
@@ -52,8 +59,6 @@ async def config_get(request):
|
|||||||
|
|
||||||
@webapp.route('/api/v1/config', methods=['PUT'])
|
@webapp.route('/api/v1/config', methods=['PUT'])
|
||||||
async def config_put(request):
|
async def config_put(request):
|
||||||
if app.is_playing():
|
|
||||||
return 503
|
|
||||||
try:
|
try:
|
||||||
config.set_config(request.json)
|
config.set_config(request.json)
|
||||||
except ValueError as ex:
|
except ValueError as ex:
|
||||||
|
|||||||
Reference in New Issue
Block a user