2021-02-15 14:29:02 +00:00
|
|
|
from owrx.controllers.admin import AuthorizationMixin
|
|
|
|
from owrx.controllers.template import WebpageController
|
2021-02-18 23:46:52 +00:00
|
|
|
from owrx.controllers.settings import SettingsFormController
|
2021-02-27 19:48:37 +00:00
|
|
|
from owrx.source import SdrDeviceDescription, SdrDeviceDescriptionMissing, SdrClientClass
|
2021-02-15 14:29:02 +00:00
|
|
|
from owrx.config import Config
|
2021-02-27 19:48:37 +00:00
|
|
|
from owrx.connection import OpenWebRxReceiverClient
|
2021-02-18 23:03:25 +00:00
|
|
|
from urllib.parse import quote, unquote
|
2021-02-20 21:57:17 +00:00
|
|
|
from owrx.sdr import SdrService
|
2021-02-23 17:32:23 +00:00
|
|
|
from abc import ABCMeta
|
2021-02-19 17:18:25 +00:00
|
|
|
|
2021-02-15 14:29:02 +00:00
|
|
|
|
2021-02-18 23:03:25 +00:00
|
|
|
class SdrDeviceListController(AuthorizationMixin, WebpageController):
|
2021-02-15 14:29:02 +00:00
|
|
|
def header_variables(self):
|
|
|
|
variables = super().header_variables()
|
|
|
|
variables["assets_prefix"] = "../"
|
|
|
|
return variables
|
|
|
|
|
|
|
|
def template_variables(self):
|
|
|
|
variables = super().template_variables()
|
2021-02-18 22:05:43 +00:00
|
|
|
variables["content"] = self.render_devices()
|
2021-02-18 21:12:13 +00:00
|
|
|
variables["title"] = "SDR device settings"
|
2021-02-18 23:03:25 +00:00
|
|
|
variables["assets_prefix"] = "../"
|
2021-02-15 14:29:02 +00:00
|
|
|
return variables
|
|
|
|
|
|
|
|
def render_devices(self):
|
2021-02-18 22:05:43 +00:00
|
|
|
def render_device(device_id, config):
|
2021-02-20 21:57:17 +00:00
|
|
|
# TODO: this only returns non-failed sources...
|
|
|
|
source = SdrService.getSource(device_id)
|
|
|
|
|
2021-02-27 19:48:37 +00:00
|
|
|
additional_info = ""
|
|
|
|
|
|
|
|
if source is not None:
|
|
|
|
profiles = source.getProfiles()
|
|
|
|
currentProfile = profiles[source.getProfileId()]
|
|
|
|
clients = {c: len(source.getClients(c)) for c in SdrClientClass}
|
|
|
|
clients = {c: v for c, v in clients.items() if v}
|
|
|
|
connections = len([c for c in source.getClients() if isinstance(c, OpenWebRxReceiverClient)])
|
|
|
|
additional_info = """
|
|
|
|
<div>Current profile: {current_profile}</div>
|
|
|
|
<div>Clients: {clients}</div>
|
|
|
|
<div>Connections: {connections}</div>
|
|
|
|
""".format(
|
|
|
|
current_profile=currentProfile["name"],
|
|
|
|
clients=", ".join("{cls}: {count}".format(cls=c.name, count=v) for c, v in clients.items()),
|
|
|
|
connections=connections,
|
|
|
|
)
|
|
|
|
|
2021-02-18 22:05:43 +00:00
|
|
|
return """
|
2021-02-20 21:57:17 +00:00
|
|
|
<li class="list-group-item">
|
|
|
|
<a href="{device_link}">
|
|
|
|
<h3>{device_name}</h3>
|
|
|
|
</a>
|
|
|
|
<div>State: {state}</div>
|
|
|
|
<div>{num_profiles} profile(s)</div>
|
2021-02-27 19:48:37 +00:00
|
|
|
{additional_info}
|
2021-02-20 21:57:17 +00:00
|
|
|
</li>
|
|
|
|
""".format(
|
2021-02-18 23:03:25 +00:00
|
|
|
device_name=config["name"],
|
|
|
|
device_link="{}/{}".format(self.request.path, quote(device_id)),
|
2021-02-20 21:57:17 +00:00
|
|
|
state="Unknown" if source is None else source.getState(),
|
|
|
|
num_profiles=len(config["profiles"]),
|
2021-02-27 19:48:37 +00:00
|
|
|
additional_info=additional_info,
|
2021-02-18 22:05:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return """
|
2021-02-18 23:03:25 +00:00
|
|
|
<ul class="row list-group list-group-flush sdr-device-list">
|
2021-02-18 22:05:43 +00:00
|
|
|
{devices}
|
2021-02-18 23:03:25 +00:00
|
|
|
</ul>
|
2021-02-18 22:05:43 +00:00
|
|
|
""".format(
|
|
|
|
devices="".join(render_device(key, value) for key, value in Config.get()["sdrs"].items())
|
2021-02-15 14:29:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def indexAction(self):
|
2021-02-18 21:12:13 +00:00
|
|
|
self.serve_template("settings/general.html", **self.template_variables())
|
2021-02-18 23:03:25 +00:00
|
|
|
|
|
|
|
|
2021-02-23 17:32:23 +00:00
|
|
|
class SdrFormController(SettingsFormController, metaclass=ABCMeta):
|
2021-02-18 23:46:52 +00:00
|
|
|
def __init__(self, handler, request, options):
|
|
|
|
super().__init__(handler, request, options)
|
2021-02-19 17:45:29 +00:00
|
|
|
self.device_id, self.device = self._get_device()
|
2021-02-18 23:46:52 +00:00
|
|
|
|
2021-02-19 17:45:29 +00:00
|
|
|
def store(self):
|
|
|
|
# need to overwrite the existing key in the config since the layering won't capture the changes otherwise
|
|
|
|
config = Config.get()
|
|
|
|
sdrs = config["sdrs"]
|
2021-02-23 17:41:49 +00:00
|
|
|
sdrs[self.device_id] = self.device
|
2021-02-19 17:45:29 +00:00
|
|
|
config["sdrs"] = sdrs
|
|
|
|
super().store()
|
2021-02-19 17:18:25 +00:00
|
|
|
|
2021-02-23 17:32:23 +00:00
|
|
|
def _get_device(self):
|
2021-02-23 17:41:49 +00:00
|
|
|
config = Config.get()
|
2021-02-23 17:32:23 +00:00
|
|
|
device_id = unquote(self.request.matches.group(1))
|
2021-02-23 17:41:49 +00:00
|
|
|
if device_id not in config["sdrs"]:
|
2021-02-23 17:32:23 +00:00
|
|
|
return None
|
2021-02-23 17:41:49 +00:00
|
|
|
return device_id, config["sdrs"][device_id]
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SdrDeviceController(SdrFormController):
|
|
|
|
def getData(self):
|
|
|
|
return self.device
|
|
|
|
|
2021-02-18 23:46:52 +00:00
|
|
|
def getSections(self):
|
2021-02-19 13:44:16 +00:00
|
|
|
try:
|
|
|
|
description = SdrDeviceDescription.getByType(self.device["type"])
|
2021-02-23 17:32:23 +00:00
|
|
|
return [description.getDeviceSection()]
|
2021-02-19 13:44:16 +00:00
|
|
|
except SdrDeviceDescriptionMissing:
|
2021-02-18 23:46:52 +00:00
|
|
|
# TODO provide a generic interface that allows to switch the type
|
|
|
|
return []
|
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return self.device["name"]
|
|
|
|
|
2021-02-18 23:03:25 +00:00
|
|
|
def header_variables(self):
|
|
|
|
variables = super().header_variables()
|
|
|
|
variables["assets_prefix"] = "../../"
|
|
|
|
return variables
|
|
|
|
|
2021-02-18 23:46:52 +00:00
|
|
|
def template_variables(self):
|
2021-02-18 23:03:25 +00:00
|
|
|
variables = super().template_variables()
|
|
|
|
variables["assets_prefix"] = "../../"
|
|
|
|
return variables
|
|
|
|
|
2021-02-20 22:45:06 +00:00
|
|
|
def render_sections(self):
|
|
|
|
return super().render_sections() + self.render_profile_list(self.device["profiles"])
|
|
|
|
|
|
|
|
def render_profile_list(self, profiles):
|
|
|
|
def render_profile(profile_id, profile):
|
|
|
|
return """
|
|
|
|
<li class="list-group-item">
|
|
|
|
<a href="{profile_link}">{profile_name}</a>
|
|
|
|
</li>
|
|
|
|
""".format(
|
|
|
|
profile_name=profile["name"],
|
|
|
|
profile_link="{}/{}".format(self.request.path, quote(profile_id)),
|
|
|
|
)
|
|
|
|
|
|
|
|
return """
|
|
|
|
<h3 class="settings-header">Profiles</h3>
|
|
|
|
<ul class="row list-group list-group-flush sdr-profile-list">
|
|
|
|
{profiles}
|
|
|
|
</ul>
|
|
|
|
""".format(
|
|
|
|
profiles="".join(render_profile(p_id, p) for p_id, p in profiles.items())
|
|
|
|
)
|
|
|
|
|
2021-02-18 23:03:25 +00:00
|
|
|
def indexAction(self):
|
2021-02-18 23:46:52 +00:00
|
|
|
if self.device is None:
|
2021-02-18 23:03:25 +00:00
|
|
|
self.send_response("device not found", code=404)
|
|
|
|
return
|
2021-02-18 23:46:52 +00:00
|
|
|
self.serve_template("settings/general.html", **self.template_variables())
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SdrProfileController(SdrFormController):
|
|
|
|
def __init__(self, handler, request, options):
|
|
|
|
super().__init__(handler, request, options)
|
|
|
|
self.profile_id, self.profile = self._get_profile()
|
|
|
|
|
|
|
|
def getData(self):
|
|
|
|
return self.profile
|
|
|
|
|
|
|
|
def _get_profile(self):
|
2021-02-23 17:41:49 +00:00
|
|
|
if self.device is None:
|
2021-02-23 17:32:23 +00:00
|
|
|
return None
|
2021-02-23 17:41:49 +00:00
|
|
|
profile_id = unquote(self.request.matches.group(2))
|
|
|
|
if profile_id not in self.device["profiles"]:
|
2021-02-23 17:32:23 +00:00
|
|
|
return None
|
2021-02-23 17:41:49 +00:00
|
|
|
return profile_id, self.device["profiles"][profile_id]
|
2021-02-23 17:32:23 +00:00
|
|
|
|
|
|
|
def getSections(self):
|
|
|
|
try:
|
|
|
|
description = SdrDeviceDescription.getByType(self.device["type"])
|
|
|
|
return [description.getProfileSection()]
|
|
|
|
except SdrDeviceDescriptionMissing:
|
|
|
|
# TODO provide a generic interface that allows to switch the type
|
|
|
|
return []
|
|
|
|
|
|
|
|
def getTitle(self):
|
|
|
|
return self.profile["name"]
|
|
|
|
|
|
|
|
def header_variables(self):
|
|
|
|
variables = super().header_variables()
|
|
|
|
variables["assets_prefix"] = "../../../"
|
|
|
|
return variables
|
|
|
|
|
|
|
|
def template_variables(self):
|
|
|
|
variables = super().template_variables()
|
|
|
|
variables["assets_prefix"] = "../../../"
|
|
|
|
return variables
|
|
|
|
|
|
|
|
def indexAction(self):
|
|
|
|
if self.profile is None:
|
|
|
|
self.send_response("profile not found", code=404)
|
|
|
|
return
|
|
|
|
self.serve_template("settings/general.html", **self.template_variables())
|