-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
49 lines (40 loc) · 1.6 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Stand-in for flask.run to start event listener and subscription threads
ahead of the app, and stop them afterward. This ensures that we unsubscribe
and stop the event listener even when werkzeug automatically reloads the
application, or when we hit ctrl-c
Okay, that's bogus. This doesn't work with the Werkzeug reloader either.
"""
import logging
import os
import sys
import flask.cli
import soco.config
from soco.events import event_listener
from sonos.sonos import sonos # yep
from sonos import subs
def run():
# Override default soco config from environment.
if os.environ.get('SOCO_CACHE_ENABLED'):
soco.config.CACHE_ENABLED = (os.environ['SOCO_CACHE_ENABLED'].lower() in
['0', 'no', 'off', 'false'])
if os.environ.get('SOCO_EVENT_LISTENER_PORT'):
soco.config.EVENT_LISTENER_PORT = int(os.environ['SOCO_EVENT_LISTENER_PORT'])
# Start the event listener thread immediately, since it's not threadsafe
# and will try to start twice through the subscription threads.
event_listener.start(sonos)
try:
# Start the subscription threads.
# This immediately populates the cache of SONOS responses.
threads = subs.start_threads()
# Run flask. This depends on FLASK_APP being set in the environemnt.
try:
sys.argv.insert(1, 'run') # like "flask run" for flask.cli.main()
return flask.cli.main()
finally:
subs.stop_threads(threads)
finally:
event_listener.stop()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
sys.exit(run())