diff --git a/owrx/__main__.py b/owrx/__main__.py index a0e83dc..6afcb93 100644 --- a/owrx/__main__.py +++ b/owrx/__main__.py @@ -11,7 +11,7 @@ from owrx.sdr import SdrService from socketserver import ThreadingMixIn from owrx.service import Services from owrx.websocket import WebSocketConnection -from owrx.pskreporter import PskReporter +from owrx.reporting import ReportingEngine from owrx.version import openwebrx_version @@ -67,4 +67,4 @@ Support and info: https://groups.io/g/openwebrx except KeyboardInterrupt: WebSocketConnection.closeAll() Services.stop() - PskReporter.stop() + ReportingEngine.stop() diff --git a/owrx/js8.py b/owrx/js8.py index 18a6cef..79e1850 100644 --- a/owrx/js8.py +++ b/owrx/js8.py @@ -4,10 +4,10 @@ import re from js8py import Js8 from js8py.frames import Js8FrameHeartbeat, Js8FrameCompound from .map import Map, LocatorLocation -from .pskreporter import PskReporter from .metrics import Metrics, CounterMetric from .config import Config from abc import ABCMeta, abstractmethod +from owrx.reporting import ReportingEngine import logging @@ -102,7 +102,7 @@ class Js8Parser(Parser): Map.getSharedInstance().updateLocation( frame.callsign, LocatorLocation(frame.grid), "JS8", self.band ) - PskReporter.getSharedInstance().spot({ + ReportingEngine.getSharedInstance().spot({ "callsign": frame.callsign, "mode": "JS8", "locator": frame.grid, diff --git a/owrx/pskreporter.py b/owrx/pskreporter.py index 662b2a7..2b8f69f 100644 --- a/owrx/pskreporter.py +++ b/owrx/pskreporter.py @@ -9,43 +9,19 @@ from owrx.config import Config from owrx.version import openwebrx_version from owrx.locator import Locator from owrx.metrics import Metrics, CounterMetric +from owrx.reporting import Reporter logger = logging.getLogger(__name__) -class PskReporterDummy(object): - """ - used in place of the PskReporter when reporting is disabled. - does nothing. - """ - - def spot(self, spot): - pass - - def cancelTimer(self): - pass - - -class PskReporter(object): - sharedInstance = None - creationLock = threading.Lock() +class PskReporter(Reporter): interval = 300 - supportedModes = ["FT8", "FT4", "JT9", "JT65", "FST4", "FST4W", "JS8"] - @staticmethod - def getSharedInstance(): - with PskReporter.creationLock: - if PskReporter.sharedInstance is None: - if Config.get()["pskreporter_enabled"]: - PskReporter.sharedInstance = PskReporter() - else: - PskReporter.sharedInstance = PskReporterDummy() - return PskReporter.sharedInstance + def getSupportedModes(self): + return ["FT8", "FT4", "JT9", "JT65", "FST4", "FST4W", "JS8"] - @staticmethod - def stop(): - if PskReporter.sharedInstance: - PskReporter.sharedInstance.cancelTimer() + def stop(self): + self.cancelTimer() def __init__(self): self.spots = [] @@ -72,8 +48,6 @@ class PskReporter(object): return reduce(and_, map(lambda key: s1[key] == s2[key], keys)) def spot(self, spot): - if not spot["mode"] in PskReporter.supportedModes: - return with self.spotLock: if any(x for x in self.spots if self.spotEquals(spot, x)): # dupe diff --git a/owrx/reporting.py b/owrx/reporting.py new file mode 100644 index 0000000..3b14935 --- /dev/null +++ b/owrx/reporting.py @@ -0,0 +1,51 @@ +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 = [] + if Config.get()["pskreporter_enabled"]: + # inline import due to circular dependencies + from owrx.pskreporter import PskReporter + self.reporters += [PskReporter()] + + 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) diff --git a/owrx/wsjt.py b/owrx/wsjt.py index 90c4d9c..fd23550 100644 --- a/owrx/wsjt.py +++ b/owrx/wsjt.py @@ -2,7 +2,7 @@ from datetime import datetime, timezone from owrx.map import Map, LocatorLocation import re from owrx.metrics import Metrics, CounterMetric -from owrx.pskreporter import PskReporter +from owrx.reporting import ReportingEngine from owrx.parser import Parser from owrx.audio import AudioChopperProfile from abc import ABC, ABCMeta, abstractmethod @@ -168,7 +168,7 @@ class WsjtParser(Parser): Map.getSharedInstance().updateLocation( out["callsign"], LocatorLocation(out["locator"]), mode, self.band ) - PskReporter.getSharedInstance().spot(out) + ReportingEngine.getSharedInstance().spot(out) self.handler.write_wsjt_message(out) except (ValueError, IndexError):