2020-03-21 22:40:39 +01:00
|
|
|
from owrx.config import Config
|
2023-05-06 01:56:28 +02:00
|
|
|
from owrx.source import SdrSource
|
2023-05-06 23:40:49 +02:00
|
|
|
from owrx.feature import FeatureDetector, UnknownFeatureException
|
2023-05-09 19:42:43 +02:00
|
|
|
from owrx.active.list import ActiveListTransformation
|
2019-12-21 20:58:28 +01:00
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2023-05-09 19:42:43 +02:00
|
|
|
class ProfileNameMapper(ActiveListTransformation):
|
|
|
|
|
def __init__(self, source_id, source_name):
|
|
|
|
|
self.source_id = source_id
|
|
|
|
|
self.source_name = source_name
|
|
|
|
|
self.subscriptions = []
|
|
|
|
|
|
|
|
|
|
def transform(self, profile):
|
|
|
|
|
return {"id": "{}|{}".format(self.source_id, profile["id"]), "name": "{} {}".format(self.source_name, profile["name"])}
|
|
|
|
|
|
|
|
|
|
def monitor(self, profile, callback: callable):
|
|
|
|
|
self.subscriptions.append(profile.filter("name").wire(lambda _: callback()))
|
|
|
|
|
|
|
|
|
|
def unmonitor(self, member):
|
|
|
|
|
affected = [sub for sub in self.subscriptions if sub.subscriptee is member]
|
|
|
|
|
for sub in affected:
|
|
|
|
|
sub.cancel()
|
|
|
|
|
self.subscriptions.remove(sub)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProfileMapper(ActiveListTransformation):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.subscriptions = []
|
|
|
|
|
|
|
|
|
|
def transform(self, source):
|
|
|
|
|
return source.getProfiles().map(ProfileNameMapper(source.getId(), source.getName()))
|
|
|
|
|
|
|
|
|
|
def monitor(self, source, callback: callable):
|
|
|
|
|
self.subscriptions.append(source.getProps().filter("name").wire(lambda _: callback()))
|
|
|
|
|
|
|
|
|
|
def unmonitor(self, member):
|
|
|
|
|
affected = [sub for sub in self.subscriptions if sub.subscriptee is member]
|
|
|
|
|
for sub in affected:
|
|
|
|
|
sub.cancel()
|
|
|
|
|
self.subscriptions.remove(sub)
|
|
|
|
|
|
|
|
|
|
|
2021-03-18 18:59:38 +01:00
|
|
|
class SdrService(object):
|
|
|
|
|
sources = None
|
2021-03-20 01:10:18 +01:00
|
|
|
activeSources = None
|
2021-03-24 15:57:25 +01:00
|
|
|
availableProfiles = None
|
2019-12-21 20:58:28 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
2019-12-23 21:12:28 +01:00
|
|
|
def getFirstSource():
|
2021-03-18 21:53:59 +01:00
|
|
|
sources = SdrService.getActiveSources()
|
2019-12-21 20:58:28 +01:00
|
|
|
if not sources:
|
|
|
|
|
return None
|
2019-12-23 21:12:28 +01:00
|
|
|
# TODO: configure default sdr in config? right now it will pick the first one off the list.
|
2023-05-06 01:56:28 +02:00
|
|
|
return sources[0]
|
2019-12-21 20:58:28 +01:00
|
|
|
|
2019-12-23 21:12:28 +01:00
|
|
|
@staticmethod
|
2023-05-06 23:40:49 +02:00
|
|
|
def _findSource(sources, id):
|
2019-12-23 21:12:28 +01:00
|
|
|
if not sources:
|
|
|
|
|
return None
|
2023-05-06 01:56:28 +02:00
|
|
|
try:
|
|
|
|
|
return next(s for s in sources if s.getId() == id)
|
|
|
|
|
except StopIteration:
|
2019-12-21 20:58:28 +01:00
|
|
|
return None
|
|
|
|
|
|
2023-05-06 23:40:49 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def getActiveSource(id):
|
|
|
|
|
return SdrService._findSource(SdrService.getActiveSources(), id)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def getSource(id):
|
|
|
|
|
return SdrService._findSource(SdrService.getAllSources(), id)
|
|
|
|
|
|
2019-12-21 20:58:28 +01:00
|
|
|
@staticmethod
|
2021-03-18 21:53:59 +01:00
|
|
|
def getAllSources():
|
2023-05-06 23:40:49 +02:00
|
|
|
def hasProfiles(device):
|
|
|
|
|
return "profiles" in device and device["profiles"] and len(device["profiles"]) > 0
|
|
|
|
|
|
|
|
|
|
def sdrTypeAvailable(value):
|
|
|
|
|
featureDetector = FeatureDetector()
|
|
|
|
|
try:
|
|
|
|
|
if not featureDetector.is_available(value["type"]):
|
|
|
|
|
logger.error(
|
|
|
|
|
'The SDR source type "{0}" is not available. please check the feature report for details.'.format(
|
|
|
|
|
value["type"]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
except UnknownFeatureException:
|
|
|
|
|
logger.error(
|
|
|
|
|
'The SDR source type "{0}" is invalid. Please check your configuration'.format(value["type"])
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
|
2023-05-06 01:56:28 +02:00
|
|
|
def buildNewSource(props):
|
|
|
|
|
sdrType = props["type"]
|
|
|
|
|
className = "".join(x for x in sdrType.title() if x.isalnum()) + "Source"
|
|
|
|
|
module = __import__("owrx.source.{0}".format(sdrType), fromlist=[className])
|
|
|
|
|
cls = getattr(module, className)
|
|
|
|
|
return cls(props)
|
|
|
|
|
|
2021-03-18 18:59:38 +01:00
|
|
|
if SdrService.sources is None:
|
2023-05-08 18:23:34 +02:00
|
|
|
SdrService.sources = Config.get()["sdrs"] \
|
|
|
|
|
.filter(sdrTypeAvailable) \
|
|
|
|
|
.filter(hasProfiles) \
|
|
|
|
|
.map(buildNewSource)
|
2021-03-18 21:53:59 +01:00
|
|
|
return SdrService.sources
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def getActiveSources():
|
2023-05-06 01:56:28 +02:00
|
|
|
def isAvailable(source: SdrSource):
|
|
|
|
|
return source.isEnabled() and not source.isFailed()
|
|
|
|
|
|
2021-03-20 01:10:18 +01:00
|
|
|
if SdrService.activeSources is None:
|
2023-05-06 01:56:28 +02:00
|
|
|
SdrService.activeSources = SdrService.getAllSources().filter(isAvailable)
|
2021-03-20 01:10:18 +01:00
|
|
|
return SdrService.activeSources
|
2021-03-24 15:57:25 +01:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def getAvailableProfiles():
|
|
|
|
|
if SdrService.availableProfiles is None:
|
2023-05-09 19:42:43 +02:00
|
|
|
SdrService.availableProfiles = SdrService.getActiveSources().map(ProfileMapper()).flatten()
|
2021-03-24 15:57:25 +01:00
|
|
|
return SdrService.availableProfiles
|
2021-09-15 15:03:11 +02:00
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def stopAllSources():
|
2023-05-06 01:56:28 +02:00
|
|
|
for source in SdrService.getAllSources():
|
2021-09-15 15:03:11 +02:00
|
|
|
source.stop()
|