link spectrum closer to the sdr source, since the other solution is unstable

This commit is contained in:
Jakob Ketterl 2019-05-10 20:59:06 +02:00
parent 52098cf9f9
commit 859e3931c6
2 changed files with 31 additions and 44 deletions

View File

@ -107,7 +107,7 @@ class OpenWebRxClient(object):
self.configProps.wire(self.sendConfig) self.configProps.wire(self.sendConfig)
self.sendConfig(None, None) self.sendConfig(None, None)
self.sdr.getSpectrumThread().add_client(self) self.sdr.addSpectrumClient(self)
def startDsp(self): def startDsp(self):
if self.dsp is None: if self.dsp is None:
@ -124,7 +124,7 @@ class OpenWebRxClient(object):
self.dsp.stop() self.dsp.stop()
self.dsp = None self.dsp = None
if self.sdr is not None: if self.sdr is not None:
self.sdr.spectrumThread.remove_client(self) self.sdr.removeSpectrumClient(self)
def setParams(self, params): def setParams(self, params):
# only the keys in the protected property manager can be overridden from the web # only the keys in the protected property manager can be overridden from the web

View File

@ -83,6 +83,7 @@ class SdrSource(object):
self.port = port self.port = port
self.monitor = None self.monitor = None
self.clients = [] self.clients = []
self.spectrumClients = []
self.spectrumThread = None self.spectrumThread = None
self.modificationLock = threading.Lock() self.modificationLock = threading.Lock()
@ -163,6 +164,9 @@ class SdrSource(object):
self.monitor = threading.Thread(target = wait_for_process_to_end) self.monitor = threading.Thread(target = wait_for_process_to_end)
self.monitor.start() self.monitor.start()
self.spectrumThread = SpectrumThread(self)
self.spectrumThread.start()
self.modificationLock.release() self.modificationLock.release()
for c in self.clients: for c in self.clients:
@ -171,22 +175,21 @@ class SdrSource(object):
def isAvailable(self): def isAvailable(self):
return self.monitor is not None return self.monitor is not None
def getSpectrumThread(self):
if self.spectrumThread is None:
self.spectrumThread = SpectrumThread(self)
return self.spectrumThread
def stop(self): def stop(self):
for c in self.clients: for c in self.clients:
c.onSdrUnavailable() c.onSdrUnavailable()
self.modificationLock.acquire() self.modificationLock.acquire()
self.spectrumThread.stop()
try: try:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM) os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
except ProcessLookupError: except ProcessLookupError:
# been killed by something else, ignore # been killed by something else, ignore
pass pass
self.monitor.join() if self.monitor:
self.monitor.join()
self.sleepOnRestart() self.sleepOnRestart()
self.modificationLock.release() self.modificationLock.release()
@ -204,6 +207,20 @@ class SdrSource(object):
if not self.clients: if not self.clients:
self.stop() self.stop()
def addSpectrumClient(self, c):
self.spectrumClients.append(c)
def removeSpectrumClient(self, c):
try:
self.spectrumClients.remove(c)
except ValueError:
pass
def writeSpectrumData(self, data):
for c in self.spectrumClients:
c.write_spectrum_data(data)
class RtlSdrSource(SdrSource): class RtlSdrSource(SdrSource):
def __init__(self, props, port): def __init__(self, props, port):
super().__init__(props, port) super().__init__(props, port)
@ -225,18 +242,11 @@ class SdrplaySource(SdrSource):
def sleepOnRestart(self): def sleepOnRestart(self):
time.sleep(1) time.sleep(1)
class SpectrumThread(object): class SpectrumThread(threading.Thread):
def __init__(self, sdrSource): def __init__(self, sdrSource):
self.clients = [] self.doRun = True
self.doRun = False
self.sdrSource = sdrSource self.sdrSource = sdrSource
self.sdrSource.addClient(self) super().__init__()
self.thread = None
def start(self):
if self.thread is None:
self.thread = threading.Thread(target = self.run)
self.thread.start()
def run(self): def run(self):
props = self.sdrSource.props.collect( props = self.sdrSource.props.collect(
@ -277,41 +287,18 @@ class SpectrumThread(object):
if len(data) == 0: if len(data) == 0:
time.sleep(1) time.sleep(1)
else: else:
for c in self.clients: self.sdrSource.writeSpectrumData(data)
c.write_spectrum_data(data)
dsp.stop() dsp.stop()
print("spectrum thread shut down") print("spectrum thread shut down")
self.thread = None self.thread = None
self.sdrSource.removeClient(self)
def add_client(self, c):
self.clients.append(c)
self.doRun = self.sdrSource.isAvailable()
if self.doRun:
self.start()
def remove_client(self, c):
try:
self.clients.remove(c)
except ValueError:
pass
if not self.clients:
self.stop()
def stop(self): def stop(self):
print("stopping spectrum thread") print("stopping spectrum thread")
self.doRun = False self.doRun = False
def onSdrAvailable(self):
print("Spectrum Thread: onSdrAvailable")
self.doRun = bool(self.clients)
if self.doRun:
self.start()
def onSdrUnavailable(self):
print("Spectrum Thread: onSdrUnavailable")
self.stop()
class DspManager(object): class DspManager(object):
def __init__(self, handler, sdrSource): def __init__(self, handler, sdrSource):
self.doRun = False self.doRun = False