From eaa98b0d641bd5ff81658d016364d3d19d2a5a18 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 9 Feb 2020 21:46:03 +0100 Subject: [PATCH] new status controller as json --- owrx/controllers.py | 44 +++++++++++++++++++++++++++++++++++++++++++- owrx/http.py | 2 ++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/owrx/controllers.py b/owrx/controllers.py index a26ebc3..e3aa664 100644 --- a/owrx/controllers.py +++ b/owrx/controllers.py @@ -11,13 +11,15 @@ from owrx.connection import WebSocketMessageHandler from owrx.version import openwebrx_version from owrx.feature import FeatureDetector from owrx.metrics import Metrics +from owrx.sdr import SdrService +from abc import ABC, abstractmethod import logging logger = logging.getLogger(__name__) -class Controller(object): +class Controller(ABC): def __init__(self, handler, request): self.handler = handler self.request = request @@ -35,6 +37,10 @@ class Controller(object): content = content.encode() self.handler.wfile.write(content) + @abstractmethod + def handle_request(self): + pass + class StatusController(Controller): def handle_request(self): @@ -55,6 +61,42 @@ class StatusController(Controller): self.send_response("\n".join(["{key}={value}".format(key=key, value=value) for key, value in vars.items()])) +class StatusJsonController(Controller): + def getProfileStats(self, profile): + return { + "name": profile["name"], + "center_freq": profile["center_freq"], + "sample_rate": profile["samp_rate"], + } + + def getReceiverStats(self, receiver): + stats = { + "name": receiver.getName(), + # TODO would be better to have types from the config here + "type": type(receiver).__name__, + "profiles": [self.getProfileStats(p) for p in receiver.getProfiles().values()] + } + return stats + + def handle_request(self): + pm = PropertyManager.getSharedInstance() + + gps = pm["receiver_gps"] + status = { + "receiver": { + "name": pm["receiver_name"], + "admin": pm["receiver_admin"], + "gps": {"lat": gps[0], "lon": gps[1]}, + "asl": pm["receiver_asl"], + "location": pm["receiver_location"], + }, + "max_clients": pm["max_clients"], + "version": openwebrx_version, + "sdrs": [self.getReceiverStats(r) for r in SdrService.getSources().values()] + } + self.send_response(json.dumps(status), content_type="application/json") + + class AssetsController(Controller): def getModified(self, file): return None diff --git a/owrx/http.py b/owrx/http.py index 196c6c4..4783a27 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -1,5 +1,6 @@ from owrx.controllers import ( StatusController, + StatusJsonController, IndexController, OwrxAssetsController, WebSocketController, @@ -41,6 +42,7 @@ class Router(object): mappings = [ {"route": "/", "controller": IndexController}, {"route": "/status", "controller": StatusController}, + {"route": "/status.json", "controller": StatusJsonController}, {"regex": "/static/(.+)", "controller": OwrxAssetsController}, {"regex": "/aprs-symbols/(.+)", "controller": AprsSymbolsController}, {"route": "/ws/", "controller": WebSocketController},