implement "new device" page (redirects not working yet)

This commit is contained in:
Jakob Ketterl 2021-03-03 00:16:28 +01:00
parent 2b1dc76e48
commit 37d89c074b
3 changed files with 85 additions and 3 deletions

View File

@ -4,8 +4,11 @@ from owrx.controllers.settings import SettingsFormController
from owrx.source import SdrDeviceDescription, SdrDeviceDescriptionMissing, SdrClientClass from owrx.source import SdrDeviceDescription, SdrDeviceDescriptionMissing, SdrClientClass
from owrx.config import Config from owrx.config import Config
from owrx.connection import OpenWebRxReceiverClient from owrx.connection import OpenWebRxReceiverClient
from owrx.controllers.settings import Section
from urllib.parse import quote, unquote from urllib.parse import quote, unquote
from owrx.sdr import SdrService from owrx.sdr import SdrService
from owrx.form import TextInput, DropdownInput, Option
from owrx.property import PropertyLayer, PropertyStack
from abc import ABCMeta from abc import ABCMeta
@ -77,13 +80,16 @@ class SdrDeviceListController(AuthorizationMixin, WebpageController):
state="Unknown" if source is None else source.getState(), state="Unknown" if source is None else source.getState(),
num_profiles=len(config["profiles"]), num_profiles=len(config["profiles"]),
additional_info=additional_info, additional_info=additional_info,
profiles="".join(render_profile(p_id, p) for p_id, p in config["profiles"].items()) profiles="".join(render_profile(p_id, p) for p_id, p in config["profiles"].items()),
) )
return """ return """
<ul class="list-group list-group-flush sdr-device-list"> <ul class="list-group list-group-flush sdr-device-list">
{devices} {devices}
</ul> </ul>
<div class="buttons container">
<a class="btn btn-success" href="sdr/new">Add new device...</a>
</div>
""".format( """.format(
devices="".join(render_device(key, value) for key, value in Config.get()["sdrs"].items()) devices="".join(render_device(key, value) for key, value in Config.get()["sdrs"].items())
) )
@ -109,7 +115,7 @@ class SdrFormController(SettingsFormController, metaclass=ABCMeta):
config = Config.get() config = Config.get()
device_id = unquote(self.request.matches.group(1)) device_id = unquote(self.request.matches.group(1))
if device_id not in config["sdrs"]: if device_id not in config["sdrs"]:
return None return None, None
return device_id, config["sdrs"][device_id] return device_id, config["sdrs"][device_id]
@ -167,6 +173,56 @@ class SdrDeviceController(SdrFormController):
return return
self.serve_template("settings/general.html", **self.template_variables()) self.serve_template("settings/general.html", **self.template_variables())
def processFormData(self):
if self.device is None:
self.send_response("device not found", code=404)
return
return super().processFormData()
class NewSdrDeviceController(SettingsFormController):
def __init__(self, handler, request, options):
super().__init__(handler, request, options)
id_layer = PropertyLayer(id="")
self.data_layer = PropertyLayer(name="", type="", profiles={})
self.stack = PropertyStack()
self.stack.addLayer(0, id_layer)
self.stack.addLayer(1, self.data_layer)
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 getSections(self):
return [
Section(
"New device settings",
TextInput("name", "Device name"),
DropdownInput("type", "Device type", [Option(name, name) for name in SdrDeviceDescription.getTypes()]),
TextInput("id", "Device ID"),
)
]
def getTitle(self):
return "New device"
def getData(self):
return self.stack
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"]
sdrs[self.stack["id"]] = self.data_layer
config["sdrs"] = sdrs
super().store()
class SdrProfileController(SdrFormController): class SdrProfileController(SdrFormController):
def __init__(self, handler, request, options): def __init__(self, handler, request, options):
@ -210,3 +266,9 @@ class SdrProfileController(SdrFormController):
self.send_response("profile not found", code=404) self.send_response("profile not found", code=404)
return return
self.serve_template("settings/general.html", **self.template_variables()) self.serve_template("settings/general.html", **self.template_variables())
def processFormData(self):
if self.profile is None:
self.send_response("profile not found", code=404)
return
return super().processFormData()

View File

@ -6,7 +6,12 @@ from owrx.controllers.api import ApiController
from owrx.controllers.metrics import MetricsController from owrx.controllers.metrics import MetricsController
from owrx.controllers.settings import SettingsController from owrx.controllers.settings import SettingsController
from owrx.controllers.settings.general import GeneralSettingsController from owrx.controllers.settings.general import GeneralSettingsController
from owrx.controllers.settings.sdr import SdrDeviceListController, SdrDeviceController, SdrProfileController from owrx.controllers.settings.sdr import (
SdrDeviceListController,
SdrDeviceController,
SdrProfileController,
NewSdrDeviceController,
)
from owrx.controllers.settings.reporting import ReportingController from owrx.controllers.settings.reporting import ReportingController
from owrx.controllers.settings.backgrounddecoding import BackgroundDecodingController from owrx.controllers.settings.backgrounddecoding import BackgroundDecodingController
from owrx.controllers.settings.decoding import DecodingSettingsController from owrx.controllers.settings.decoding import DecodingSettingsController
@ -116,6 +121,10 @@ class Router(object):
"/settings/general", GeneralSettingsController, method="POST", options={"action": "processFormData"} "/settings/general", GeneralSettingsController, method="POST", options={"action": "processFormData"}
), ),
StaticRoute("/settings/sdr", SdrDeviceListController), StaticRoute("/settings/sdr", SdrDeviceListController),
StaticRoute("/settings/sdr/new", NewSdrDeviceController),
StaticRoute(
"/settings/sdr/new", NewSdrDeviceController, method="POST", options={"action": "processFormData"}
),
RegexRoute("^/settings/sdr/([^/]+)$", SdrDeviceController), RegexRoute("^/settings/sdr/([^/]+)$", SdrDeviceController),
RegexRoute( RegexRoute(
"^/settings/sdr/([^/]+)$", SdrDeviceController, method="POST", options={"action": "processFormData"} "^/settings/sdr/([^/]+)$", SdrDeviceController, method="POST", options={"action": "processFormData"}

View File

@ -6,6 +6,7 @@ import socket
import shlex import shlex
import time import time
import signal import signal
import pkgutil
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from owrx.command import CommandMapper from owrx.command import CommandMapper
from owrx.socket import getAvailablePort from owrx.socket import getAvailablePort
@ -476,6 +477,16 @@ class SdrDeviceDescription(object):
except (ModuleNotFoundError, AttributeError): except (ModuleNotFoundError, AttributeError):
raise SdrDeviceDescriptionMissing("Device description for type {} not available".format(sdr_type)) raise SdrDeviceDescriptionMissing("Device description for type {} not available".format(sdr_type))
@staticmethod
def getTypes():
def has_description(module_name):
try:
SdrDeviceDescription.getByType(module_name)
return True
except SdrDeviceDescriptionMissing:
return False
return [module_name for _, module_name, _ in pkgutil.walk_packages(__path__) if has_description(module_name)]
def getDeviceInputs(self) -> List[Input]: def getDeviceInputs(self) -> List[Input]:
return [TextInput("name", "Device name")] + self.getInputs() return [TextInput("name", "Device name")] + self.getInputs()