make wsprnet and pskreporter settings work from the web config
This commit is contained in:
parent
ebd4d93908
commit
5487861da1
@ -1,58 +0,0 @@
|
|||||||
import threading
|
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
from owrx.config import Config
|
|
||||||
|
|
||||||
|
|
||||||
class Reporter(ABC):
|
|
||||||
@abstractmethod
|
|
||||||
def stop(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def spot(self, spot):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def getSupportedModes(self):
|
|
||||||
return []
|
|
||||||
|
|
||||||
|
|
||||||
class ReportingEngine(object):
|
|
||||||
creationLock = threading.Lock()
|
|
||||||
sharedInstance = None
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def getSharedInstance():
|
|
||||||
with ReportingEngine.creationLock:
|
|
||||||
if ReportingEngine.sharedInstance is None:
|
|
||||||
ReportingEngine.sharedInstance = ReportingEngine()
|
|
||||||
return ReportingEngine.sharedInstance
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def stopAll():
|
|
||||||
with ReportingEngine.creationLock:
|
|
||||||
if ReportingEngine.sharedInstance is not None:
|
|
||||||
ReportingEngine.sharedInstance.stop()
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.reporters = []
|
|
||||||
config = Config.get()
|
|
||||||
if "pskreporter_enabled" in config and config["pskreporter_enabled"]:
|
|
||||||
# inline import due to circular dependencies
|
|
||||||
from owrx.pskreporter import PskReporter
|
|
||||||
|
|
||||||
self.reporters += [PskReporter()]
|
|
||||||
if "wsprnet_enabled" in config and config["wsprnet_enabled"]:
|
|
||||||
# inline import due to circular dependencies
|
|
||||||
from owrx.wsprnet import WsprnetReporter
|
|
||||||
|
|
||||||
self.reporters += [WsprnetReporter()]
|
|
||||||
|
|
||||||
def stop(self):
|
|
||||||
for r in self.reporters:
|
|
||||||
r.stop()
|
|
||||||
|
|
||||||
def spot(self, spot):
|
|
||||||
for r in self.reporters:
|
|
||||||
if spot["mode"] in r.getSupportedModes():
|
|
||||||
r.spot(spot)
|
|
57
owrx/reporting/__init__.py
Normal file
57
owrx/reporting/__init__.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import threading
|
||||||
|
from owrx.config import Config
|
||||||
|
from owrx.reporting.reporter import Reporter
|
||||||
|
from owrx.reporting.pskreporter import PskReporter
|
||||||
|
from owrx.reporting.wsprnet import WsprnetReporter
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ReportingEngine(object):
|
||||||
|
creationLock = threading.Lock()
|
||||||
|
sharedInstance = None
|
||||||
|
|
||||||
|
reporterClasses = {
|
||||||
|
"pskreporter_enabled": PskReporter,
|
||||||
|
"wsprnet_enabled": WsprnetReporter,
|
||||||
|
}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def getSharedInstance():
|
||||||
|
with ReportingEngine.creationLock:
|
||||||
|
if ReportingEngine.sharedInstance is None:
|
||||||
|
ReportingEngine.sharedInstance = ReportingEngine()
|
||||||
|
return ReportingEngine.sharedInstance
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def stopAll():
|
||||||
|
with ReportingEngine.creationLock:
|
||||||
|
if ReportingEngine.sharedInstance is not None:
|
||||||
|
ReportingEngine.sharedInstance.stop()
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.reporters = []
|
||||||
|
self.configSub = Config.get().filter(*ReportingEngine.reporterClasses.keys()).wire(self.setupReporters)
|
||||||
|
self.setupReporters()
|
||||||
|
|
||||||
|
def setupReporters(self, *args):
|
||||||
|
config = Config.get()
|
||||||
|
for configKey, reporterClass in ReportingEngine.reporterClasses.items():
|
||||||
|
if configKey in config and config[configKey]:
|
||||||
|
if not any(isinstance(r, reporterClass) for r in self.reporters):
|
||||||
|
self.reporters += [reporterClass()]
|
||||||
|
else:
|
||||||
|
for reporter in [r for r in self.reporters if isinstance(r, reporterClass)]:
|
||||||
|
reporter.stop()
|
||||||
|
self.reporters.remove(reporter)
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
for r in self.reporters:
|
||||||
|
r.stop()
|
||||||
|
self.configSub.cancel()
|
||||||
|
|
||||||
|
def spot(self, spot):
|
||||||
|
for r in self.reporters:
|
||||||
|
if spot["mode"] in r.getSupportedModes():
|
||||||
|
r.spot(spot)
|
@ -9,7 +9,7 @@ from owrx.config import Config
|
|||||||
from owrx.version import openwebrx_version
|
from owrx.version import openwebrx_version
|
||||||
from owrx.locator import Locator
|
from owrx.locator import Locator
|
||||||
from owrx.metrics import Metrics, CounterMetric
|
from owrx.metrics import Metrics, CounterMetric
|
||||||
from owrx.reporting import Reporter
|
from owrx.reporting.reporter import Reporter
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -22,6 +22,8 @@ class PskReporter(Reporter):
|
|||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.cancelTimer()
|
self.cancelTimer()
|
||||||
|
with self.spotLock:
|
||||||
|
self.spots = []
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.spots = []
|
self.spots = []
|
15
owrx/reporting/reporter.py
Normal file
15
owrx/reporting/reporter.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Reporter(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def stop(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def spot(self, spot):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def getSupportedModes(self):
|
||||||
|
return []
|
@ -1,4 +1,4 @@
|
|||||||
from owrx.reporting import Reporter
|
from owrx.reporting.reporter import Reporter
|
||||||
from owrx.version import openwebrx_version
|
from owrx.version import openwebrx_version
|
||||||
from owrx.config import Config
|
from owrx.config import Config
|
||||||
from owrx.locator import Locator
|
from owrx.locator import Locator
|
||||||
@ -12,14 +12,13 @@ from datetime import datetime, timezone
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
PoisonPill = object()
|
||||||
|
|
||||||
|
|
||||||
class Worker(threading.Thread):
|
class Worker(threading.Thread):
|
||||||
def __init__(self, queue: Queue):
|
def __init__(self, queue: Queue):
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
self.doRun = True
|
self.doRun = True
|
||||||
# some constants that we don't expect to change
|
|
||||||
config = Config.get()
|
|
||||||
self.callsign = config["wsprnet_callsign"]
|
|
||||||
self.locator = Locator.fromCoordinates(config["receiver_gps"])
|
|
||||||
|
|
||||||
super().__init__(daemon=True)
|
super().__init__(daemon=True)
|
||||||
|
|
||||||
@ -27,6 +26,9 @@ class Worker(threading.Thread):
|
|||||||
while self.doRun:
|
while self.doRun:
|
||||||
try:
|
try:
|
||||||
spot = self.queue.get()
|
spot = self.queue.get()
|
||||||
|
if spot is PoisonPill:
|
||||||
|
self.doRun = False
|
||||||
|
else:
|
||||||
self.uploadSpot(spot)
|
self.uploadSpot(spot)
|
||||||
self.queue.task_done()
|
self.queue.task_done()
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -40,6 +42,7 @@ class Worker(threading.Thread):
|
|||||||
return interval
|
return interval
|
||||||
|
|
||||||
def uploadSpot(self, spot):
|
def uploadSpot(self, spot):
|
||||||
|
config = Config.get()
|
||||||
# function=wspr&date=210114&time=1732&sig=-15&dt=0.5&drift=0&tqrg=7.040019&tcall=DF2UU&tgrid=JN48&dbm=37&version=2.3.0-rc3&rcall=DD5JFK&rgrid=JN58SC&rqrg=7.040047&mode=2
|
# function=wspr&date=210114&time=1732&sig=-15&dt=0.5&drift=0&tqrg=7.040019&tcall=DF2UU&tgrid=JN48&dbm=37&version=2.3.0-rc3&rcall=DD5JFK&rgrid=JN58SC&rqrg=7.040047&mode=2
|
||||||
# {'timestamp': 1610655960000, 'db': -23.0, 'dt': 0.3, 'freq': 7040048, 'drift': -1, 'msg': 'LA3JJ JO59 37', 'callsign': 'LA3JJ', 'locator': 'JO59', 'mode': 'WSPR'}
|
# {'timestamp': 1610655960000, 'db': -23.0, 'dt': 0.3, 'freq': 7040048, 'drift': -1, 'msg': 'LA3JJ JO59 37', 'callsign': 'LA3JJ', 'locator': 'JO59', 'mode': 'WSPR'}
|
||||||
date = datetime.fromtimestamp(spot["timestamp"] / 1000, tz=timezone.utc)
|
date = datetime.fromtimestamp(spot["timestamp"] / 1000, tz=timezone.utc)
|
||||||
@ -57,9 +60,8 @@ class Worker(threading.Thread):
|
|||||||
"tgrid": spot["locator"],
|
"tgrid": spot["locator"],
|
||||||
"dbm": spot["dbm"],
|
"dbm": spot["dbm"],
|
||||||
"version": openwebrx_version,
|
"version": openwebrx_version,
|
||||||
"rcall": self.callsign,
|
"rcall": config["wsprnet_callsign"],
|
||||||
"rgrid": self.locator,
|
"rgrid": Locator.fromCoordinates(config["receiver_gps"]),
|
||||||
# mode 2 = WSPR 2 minutes
|
|
||||||
"mode": self._getMode(spot),
|
"mode": self._getMode(spot),
|
||||||
}
|
}
|
||||||
).encode()
|
).encode()
|
||||||
@ -79,7 +81,10 @@ class WsprnetReporter(Reporter):
|
|||||||
metrics.addMetric("wsprnet.spots", self.spotCounter)
|
metrics.addMetric("wsprnet.spots", self.spotCounter)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
pass
|
while not self.queue.empty():
|
||||||
|
self.queue.get(timeout=1)
|
||||||
|
self.queue.task_done()
|
||||||
|
self.queue.put(PoisonPill)
|
||||||
|
|
||||||
def spot(self, spot):
|
def spot(self, spot):
|
||||||
try:
|
try:
|
Loading…
Reference in New Issue
Block a user