From 46b5e9034f092c0f5f9c69d00283c9e322771870 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Mon, 23 Dec 2019 21:12:28 +0100 Subject: [PATCH] attempt to select new sdr on failure --- owrx/connection.py | 53 ++++++++++++++++++++++++++--------------- owrx/dsp.py | 1 - owrx/sdr.py | 14 +++++++---- owrx/source/__init__.py | 6 +++++ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/owrx/connection.py b/owrx/connection.py index e28c7aa..9221239 100644 --- a/owrx/connection.py +++ b/owrx/connection.py @@ -2,6 +2,7 @@ from owrx.config import PropertyManager from owrx.dsp import DspManager from owrx.cpu import CpuUsageThread from owrx.sdr import SdrService +from owrx.source import SdrSource from owrx.client import ClientRegistry from owrx.feature import FeatureDetector from owrx.version import openwebrx_version @@ -107,6 +108,14 @@ class OpenWebRxReceiverClient(Client): receiver_details["locator"] = Locator.fromCoordinates(receiver_details["receiver_gps"]) self.write_receiver_details(receiver_details) + self.__sendProfiles() + + features = FeatureDetector().feature_availability() + self.write_features(features) + + CpuUsageThread.getSharedInstance().add_client(self) + + def __sendProfiles(self): profiles = [ {"name": s.getName() + " " + p["name"], "id": sid + "|" + pid} for (sid, s) in SdrService.getSources().items() @@ -114,11 +123,6 @@ class OpenWebRxReceiverClient(Client): ] self.write_profiles(profiles) - features = FeatureDetector().feature_availability() - self.write_features(features) - - CpuUsageThread.getSharedInstance().add_client(self) - def handleTextMessage(self, conn, message): try: message = json.loads(message) @@ -155,24 +159,34 @@ class OpenWebRxReceiverClient(Client): logger.warning("message is not json: {0}".format(message)) def setSdr(self, id=None): - next = SdrService.getSource(id) + while True: + next = None + if id is not None: + next = SdrService.getSource(id) + if next is None: + next = SdrService.getFirstSource() + if next is None: + # exit condition: no sdrs available + self.handleNoSdrsAvailable() + return - if next is None: - self.handleSdrFailure("sdr device unavailable") - return + # exit condition: no change + if next == self.sdr: + return - if next == self.sdr: - return + self.stopDsp() - self.stopDsp() + if self.configSub is not None: + self.configSub.cancel() + self.configSub = None - if self.configSub is not None: - self.configSub.cancel() - self.configSub = None + self.sdr = next - self.sdr = next + self.startDsp() - self.startDsp() + # keep trying until we find a suitable SDR + if self.sdr.getState() != SdrSource.STATE_FAILED: + break # send initial config self.setDspProperties(self.connectionProperties) @@ -200,11 +214,12 @@ class OpenWebRxReceiverClient(Client): self.configSub = configProps.wire(sendConfig) sendConfig(None, None) + self.__sendProfiles() self.sdr.addSpectrumClient(self) - def handleSdrFailure(self, message): - self.write_sdr_error(message) + def handleNoSdrsAvailable(self): + self.write_sdr_error("No SDR Devices available") def startDsp(self): if self.dsp is None and self.sdr is not None: diff --git a/owrx/dsp.py b/owrx/dsp.py index c7f7b76..a42ef7f 100644 --- a/owrx/dsp.py +++ b/owrx/dsp.py @@ -146,7 +146,6 @@ class DspManager(csdr.output): elif state == SdrSource.STATE_FAILED: logger.debug("received STATE_FAILED, shutting down DspSource") self.dsp.stop() - self.handler.handleSdrFailure("sdr device failed") def onBusyStateChange(self, state): pass diff --git a/owrx/sdr.py b/owrx/sdr.py index 71bda62..b63e67e 100644 --- a/owrx/sdr.py +++ b/owrx/sdr.py @@ -63,15 +63,19 @@ class SdrService(object): ) @staticmethod - def getSource(id=None): + def getFirstSource(): + sources = SdrService.getSources() + if not sources: + return None + # TODO: configure default sdr in config? right now it will pick the first one off the list. + return sources[list(sources.keys())[0]] + + @staticmethod + def getSource(id): SdrService.loadProps() sources = SdrService.getSources() if not sources: return None - if id is None: - # TODO: configure default sdr in config? right now it will pick the first one off the list. - id = list(sources.keys())[0] - if not id in sources: return None return sources[id] diff --git a/owrx/source/__init__.py b/owrx/source/__init__.py index d56c10f..d146745 100644 --- a/owrx/source/__init__.py +++ b/owrx/source/__init__.py @@ -80,6 +80,9 @@ class SdrSource(object): profiles = self.props["profiles"] if profile_id is None: profile_id = list(profiles.keys())[0] + if profile_id not in profiles: + logger.warning("invalid profile %s for sdr %s. ignoring", profile_id, self.id) + return if profile_id == self.profile_id: return logger.debug("activating profile {0}".format(profile_id)) @@ -275,6 +278,9 @@ class SdrSource(object): for c in self.spectrumClients: c.write_spectrum_data(data) + def getState(self): + return self.state + def setState(self, state): if state == self.state: return