aboutsummaryrefslogtreecommitdiffstats
path: root/gui/api/__init__.py
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2018-11-28 09:38:05 +0100
committerMatthias P. Braendli <matthias.braendli@mpb.li>2018-11-28 09:38:05 +0100
commitee435c029eac59e0399dc3ae765cc74d66b9442e (patch)
treecdd01efdb5f137a924559dcfbe55833247fc1493 /gui/api/__init__.py
parent64898e72aacd26d1dfb3b925fab571d658ad5af4 (diff)
downloaddabmod-ee435c029eac59e0399dc3ae765cc74d66b9442e.tar.gz
dabmod-ee435c029eac59e0399dc3ae765cc74d66b9442e.tar.bz2
dabmod-ee435c029eac59e0399dc3ae765cc74d66b9442e.zip
GUI: Use cherry bus to communicate internally
Diffstat (limited to 'gui/api/__init__.py')
-rwxr-xr-xgui/api/__init__.py55
1 files changed, 36 insertions, 19 deletions
diff --git a/gui/api/__init__.py b/gui/api/__init__.py
index cef81c6..a535eb3 100755
--- a/gui/api/__init__.py
+++ b/gui/api/__init__.py
@@ -20,8 +20,8 @@
# You should have received a copy of the GNU General Public License
# along with ODR-DabMod. If not, see <http://www.gnu.org/licenses/>.
-import json
import cherrypy
+from cherrypy.process import wspbus, plugins
from cherrypy.lib.httputil import parse_query_string
import urllib
@@ -30,30 +30,47 @@ import os
import io
import datetime
-def send_ok(data):
- return json.dumps({'status' : 'ok', 'data': data}).encode()
+def send_ok(data=None):
+ if data is not None:
+ return {'status' : 'ok', 'data': data}
+ else:
+ return {'status': 'ok'}
def send_error(reason=""):
- return json.dumps({'status' : 'error', 'reason': reason}).encode()
+ if reason:
+ return {'status' : 'error', 'reason': reason}
+ else:
+ return {'status' : 'error'}
-class API:
- def __init__(self, mod_rc, dpd):
+class API(plugins.SimplePlugin):
+ def __init__(self, mod_rc, bus):
+ plugins.SimplePlugin.__init__(self, bus)
self.mod_rc = mod_rc
- self.dpd = dpd
+ self.dpd_state = None
+
+ def start(self):
+ self.bus.subscribe("dpd-state", self.dpd_state)
+
+ def stop(self):
+ self.bus.unsubscribe("dpd-state", self.dpd_state)
+
+ def dpd_state(self, new_state):
+ print("API got new dpd-state {}".format(new_state))
+ self.dpd_state = new_state
@cherrypy.expose
def index(self):
return """This is the api area."""
@cherrypy.expose
+ @cherrypy.tools.json_out()
def rc_parameters(self):
- cherrypy.response.headers["Content-Type"] = "application/json"
return send_ok(self.mod_rc.get_modules())
@cherrypy.expose
+ @cherrypy.tools.json_out()
def parameter(self, **kwargs):
if cherrypy.request.method == 'POST':
- cherrypy.response.headers["Content-Type"] = "application/json"
cl = cherrypy.request.headers['Content-Length']
rawbody = cherrypy.request.body.read(int(cl))
params = json.loads(rawbody)
@@ -62,26 +79,26 @@ class API:
except ValueError as e:
cherrypy.response.status = 400
return send_error(str(e))
- return send_ok(None)
+ return send_ok()
else:
- cherrypy.response.headers["Content-Type"] = "application/json"
cherrypy.response.status = 400
return send_error("POST only")
@cherrypy.expose
+ @cherrypy.tools.json_out()
def trigger_capture(self, **kwargs):
if cherrypy.request.method == 'POST':
- cherrypy.response.headers["Content-Type"] = "application/json"
- try:
- return send_ok(self.dpd.capture_samples())
- except ValueError as e:
- return send_error(str(e))
+ cherrypy.engine.publish('dpd-capture', None)
+ return send_ok()
else:
- cherrypy.response.headers["Content-Type"] = "application/json"
cherrypy.response.status = 400
return send_error("POST only")
@cherrypy.expose
+ @cherrypy.tools.json_out()
def dpd_status(self, **kwargs):
- cherrypy.response.headers["Content-Type"] = "application/json"
- return send_ok(self.dpd.status())
+ if self.dpd_state is not None:
+ return send_ok(self.dpd_state)
+ else:
+ return send_error("DPD state unknown")
+