refactor event system

This commit is contained in:
Jakob Ketterl 2019-11-15 22:13:00 +01:00
parent bcbb911b24
commit 097f8a2b82
2 changed files with 53 additions and 51 deletions

View File

@ -1,7 +1,7 @@
import threading import threading
from owrx.socket import getAvailablePort from owrx.socket import getAvailablePort
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
from owrx.source import SdrService from owrx.source import SdrService, SdrSource
from owrx.bands import Bandplan from owrx.bands import Bandplan
from csdr import dsp, output from csdr import dsp, output
from owrx.wsjt import WsjtParser from owrx.wsjt import WsjtParser
@ -136,14 +136,11 @@ class ServiceScheduler(object):
def isActive(self): def isActive(self):
return self.active return self.active
def onSdrAvailable(self): def onStateChange(self, state):
pass if state == SdrSource.STATE_STOPPING:
self.scheduleSelection()
def onSdrUnavailable(self): elif state == SdrSource.STATE_FAILED:
self.scheduleSelection() self.cancelTimer()
def onSdrFailed(self):
self.cancelTimer()
def onFrequencyChange(self, name, value): def onFrequencyChange(self, name, value):
self.scheduleSelection() self.scheduleSelection()
@ -192,16 +189,15 @@ class ServiceHandler(object):
def isActive(self): def isActive(self):
return False return False
def onSdrAvailable(self): def onStateChange(self, state):
self.scheduleServiceStartup() if state == SdrSource.STATE_RUNNING:
self.scheduleServiceStartup()
def onSdrUnavailable(self): elif state == SdrSource.STATE_STOPPING:
logger.debug("sdr source becoming unavailable; stopping services.") logger.debug("sdr source becoming unavailable; stopping services.")
self.stopServices() self.stopServices()
elif state == SdrSource.STATE_FAILED:
def onSdrFailed(self): logger.debug("sdr source failed; stopping services.")
logger.debug("sdr source failed; stopping services.") self.stopServices()
self.stopServices()
def isSupported(self, mode): def isSupported(self, mode):
# TODO this should be in a more central place (the frontend also needs this) # TODO this should be in a more central place (the frontend also needs this)

View File

@ -100,6 +100,13 @@ class SdrService(object):
class SdrSource(object): class SdrSource(object):
STATE_STOPPED = 0
STATE_STARTING = 1
STATE_RUNNING = 2
STATE_STOPPING = 3
STATE_TUNING = 4
STATE_FAILED = 5
def __init__(self, id, props, port): def __init__(self, id, props, port):
self.id = id self.id = id
self.props = props self.props = props
@ -118,6 +125,7 @@ class SdrSource(object):
self.process = None self.process = None
self.modificationLock = threading.Lock() self.modificationLock = threading.Lock()
self.failed = False self.failed = False
self.state = SdrSource.STATE_STOPPED
def getEventNames(self): def getEventNames(self):
return ["samp_rate", "nmux_memory", "center_freq", "ppm", "rf_gain", "lna_gain", "rf_amp", "antenna", "if_gain"] return ["samp_rate", "nmux_memory", "center_freq", "ppm", "rf_gain", "lna_gain", "rf_amp", "antenna", "if_gain"]
@ -245,11 +253,7 @@ class SdrSource(object):
self.modificationLock.release() self.modificationLock.release()
for c in self.clients: self.setState(SdrSource.STATE_FAILED if self.failed else SdrSource.STATE_RUNNING)
if self.failed:
c.onSdrFailed()
else:
c.onSdrAvailable()
def postStart(self): def postStart(self):
pass pass
@ -261,8 +265,7 @@ class SdrSource(object):
return self.failed return self.failed
def stop(self): def stop(self):
for c in self.clients: self.setState(SdrSource.STATE_STOPPING)
c.onSdrUnavailable()
self.modificationLock.acquire() self.modificationLock.acquire()
@ -277,6 +280,8 @@ class SdrSource(object):
self.sleepOnRestart() self.sleepOnRestart()
self.modificationLock.release() self.modificationLock.release()
self.setState(SdrSource.STATE_STOPPED)
def sleepOnRestart(self): def sleepOnRestart(self):
pass pass
@ -316,6 +321,13 @@ class SdrSource(object):
for c in self.spectrumClients: for c in self.spectrumClients:
c.write_spectrum_data(data) c.write_spectrum_data(data)
def setState(self, state):
if state == self.state:
return
self.state = state
for c in self.clients:
c.onStateChange(state)
class Resampler(SdrSource): class Resampler(SdrSource):
def __init__(self, props, port, sdr): def __init__(self, props, port, sdr):
@ -338,6 +350,8 @@ class Resampler(SdrSource):
self.modificationLock.release() self.modificationLock.release()
return return
self.setState(SdrSource.STATE_STARTING)
props = self.rtlProps props = self.rtlProps
resampler_command = [ resampler_command = [
@ -397,11 +411,7 @@ class Resampler(SdrSource):
self.modificationLock.release() self.modificationLock.release()
for c in self.clients: self.setState(SdrSource.STATE_FAILED if self.failed else SdrSource.STATE_RUNNING)
if self.failed:
c.onSdrFailed()
else:
c.onSdrAvailable()
def activateProfile(self, profile_id=None): def activateProfile(self, profile_id=None):
pass pass
@ -568,14 +578,11 @@ class SpectrumThread(csdr.output):
def isActive(self): def isActive(self):
return True return True
def onSdrAvailable(self): def onStateChange(self, state):
self.dsp.start() if state in [SdrSource.STATE_STOPPING, SdrSource.STATE_FAILED]:
self.dsp.stop()
def onSdrUnavailable(self): elif state == SdrSource.STATE_RUNNING:
self.dsp.stop() self.dsp.start()
def onSdrFailed(self):
self.dsp.stop()
class DspManager(csdr.output): class DspManager(csdr.output):
@ -703,18 +710,17 @@ class DspManager(csdr.output):
def isActive(self): def isActive(self):
return True return True
def onSdrAvailable(self): def onStateChange(self, state):
logger.debug("received onSdrAvailable, attempting DspSource restart") if state == SdrSource.STATE_RUNNING:
self.dsp.start() logger.debug("received STATE_RUNNING, attempting DspSource restart")
self.dsp.start()
def onSdrUnavailable(self): elif state == SdrSource.STATE_STOPPING:
logger.debug("received onSdrUnavailable, shutting down DspSource") logger.debug("received STATE_STOPPING, shutting down DspSource")
self.dsp.stop() self.dsp.stop()
elif state == SdrSource.STATE_FAILED:
def onSdrFailed(self): logger.debug("received STATE_FAILED, shutting down DspSource")
logger.debug("received onSdrFailed, shutting down DspSource") self.dsp.stop()
self.dsp.stop() self.handler.handleSdrFailure("sdr device failed")
self.handler.handleSdrFailure("sdr device failed")
class CpuUsageThread(threading.Thread): class CpuUsageThread(threading.Thread):