diff --git a/mpc.py b/mpc.py index f817bac..71ac52c 100644 --- a/mpc.py +++ b/mpc.py @@ -10,10 +10,12 @@ from subprocess import check_output import gevent from gevent.queue import Queue +from gevent.wsgi import WSGIServer from eventutils import ParentedLet, CeleryTask, Timer from task import create as create_continous +import rpc class MpcWatcher(ParentedLet): @@ -45,6 +47,8 @@ class Player(gevent.Greenlet): def _run(self): MpcWatcher(self.q).start() Timer(6000, self.q).start() + http_server = WSGIServer(('', 5000), rpc.create_app(self.q)) + http_server.start() while True: value = self.q.get() # emitter = value['emitter'] diff --git a/rpc.py b/rpc.py new file mode 100644 index 0000000..1e4f670 --- /dev/null +++ b/rpc.py @@ -0,0 +1,36 @@ +import logging + +from flask import current_app, Blueprint, Flask +rpc = Blueprint('rpc', __name__, url_prefix='/api') + + +def send_to_parent(kind, *args): + if not hasattr(current_app, 'queue'): + logging.debug('no parent queue; aborting send') + return + msg = { + 'emitter': current_app._get_current_object(), + 'class': current_app._get_current_object().__class__.__name__, + 'kind': kind, + 'args': args + } + current_app.queue.put(msg) + + +@rpc.route('/') +def rpc_index(): + return 'So, what command would you like to give?' + + +@rpc.route('/refresh') +def rpc_refresh(): + print current_app.queue + send_to_parent('rpc') + return 'ok, put' + + +def create_app(queue): + app = Flask(__name__) + app.register_blueprint(rpc) + app.queue = queue + return app