From 0e528c9267b5eaf1e911af6a2ec190089f2678b6 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 9 Jan 2020 15:11:53 +0100 Subject: [PATCH] refactor parsers; introduce new pocsag parser --- owrx/aprs.py | 10 ++++------ owrx/dsp.py | 21 ++++++++++++--------- owrx/meta.py | 12 +++--------- owrx/parser.py | 20 ++++++++++++++++++++ owrx/pocsag.py | 8 ++++++++ owrx/wsjt.py | 12 ++---------- 6 files changed, 49 insertions(+), 34 deletions(-) create mode 100644 owrx/parser.py create mode 100644 owrx/pocsag.py diff --git a/owrx/aprs.py b/owrx/aprs.py index c0ca5f6..9225908 100644 --- a/owrx/aprs.py +++ b/owrx/aprs.py @@ -2,6 +2,7 @@ from owrx.kiss import KissDeframer from owrx.map import Map, LatLngLocation from owrx.bands import Bandplan from owrx.metrics import Metrics, CounterMetric +from owrx.parser import Parser from datetime import datetime, timezone import re import logging @@ -148,18 +149,15 @@ class AprsLocation(LatLngLocation): return res -class AprsParser(object): +class AprsParser(Parser): def __init__(self, handler): + super().__init__(handler) self.ax25parser = Ax25Parser() self.deframer = KissDeframer() - self.dial_freq = None - self.band = None - self.handler = handler self.metric = self.getMetric() def setDialFrequency(self, freq): - self.dial_freq = freq - self.band = Bandplan.getSharedInstance().findBand(freq) + super().setDialFrequency(freq) self.metric = self.getMetric() def getMetric(self): diff --git a/owrx/dsp.py b/owrx/dsp.py index 4638d09..f12d171 100644 --- a/owrx/dsp.py +++ b/owrx/dsp.py @@ -2,6 +2,7 @@ from owrx.config import PropertyManager from owrx.meta import MetaParser from owrx.wsjt import WsjtParser from owrx.aprs import AprsParser +from owrx.pocsag import PocsagParser from owrx.source import SdrSource from csdr import csdr import threading @@ -15,9 +16,12 @@ class DspManager(csdr.output): def __init__(self, handler, sdrSource): self.handler = handler self.sdrSource = sdrSource - self.metaParser = MetaParser(self.handler) - self.wsjtParser = WsjtParser(self.handler) - self.aprsParser = AprsParser(self.handler) + self.parsers = { + "meta": MetaParser(self.handler), + "wsjt_demod": WsjtParser(self.handler), + "packet_demod": AprsParser(self.handler), + "pocsag_demod": PocsagParser(self.handler), + } self.localProps = ( self.sdrSource.getProps() @@ -53,9 +57,8 @@ class DspManager(csdr.output): def set_dial_freq(key, value): freq = self.localProps["center_freq"] + self.localProps["offset_freq"] - self.wsjtParser.setDialFrequency(freq) - self.aprsParser.setDialFrequency(freq) - self.metaParser.setDialFrequency(freq) + for parser in self.parsers.values(): + parser.setDialFrequency(freq) self.subscriptions = [ self.localProps.getProperty("audio_compression").wire(self.dsp.set_audio_compression), @@ -115,10 +118,10 @@ class DspManager(csdr.output): "smeter": self.handler.write_s_meter_level, "secondary_fft": self.handler.write_secondary_fft, "secondary_demod": self.handler.write_secondary_demod, - "meta": self.metaParser.parse, - "wsjt_demod": self.wsjtParser.parse, - "packet_demod": self.aprsParser.parse, } + for demod, parser in self.parsers.items(): + writers[demod] = parser.parse + write = writers[t] threading.Thread(target=self.pump(read_fn, write)).start() diff --git a/owrx/meta.py b/owrx/meta.py index f8f85a0..3d1b3d9 100644 --- a/owrx/meta.py +++ b/owrx/meta.py @@ -6,6 +6,7 @@ import logging import threading from owrx.map import Map, LatLngLocation from owrx.bands import Bandplan +from owrx.parser import Parser logger = logging.getLogger(__name__) @@ -83,17 +84,10 @@ class YsfMetaEnricher(object): return None -class MetaParser(object): +class MetaParser(Parser): def __init__(self, handler): - self.handler = handler + super().__init__(handler) self.enrichers = {"DMR": DmrMetaEnricher(), "YSF": YsfMetaEnricher(self)} - self.band = None - - def setDialFrequency(self, freq): - self.band = Bandplan.getSharedInstance().findBand(freq) - - def getBand(self): - return self.band def parse(self, meta): fields = meta.split(";") diff --git a/owrx/parser.py b/owrx/parser.py new file mode 100644 index 0000000..2bb75e2 --- /dev/null +++ b/owrx/parser.py @@ -0,0 +1,20 @@ +from abc import ABC, abstractmethod +from owrx.bands import Bandplan + + +class Parser(ABC): + def __init__(self, handler): + self.handler = handler + self.dial_freq = None + self.band = None + + @abstractmethod + def parse(self, raw): + pass + + def setDialFrequency(self, freq): + self.dial_freq = freq + self.band = Bandplan.getSharedInstance().findBand(freq) + + def getBand(self): + return self.band diff --git a/owrx/pocsag.py b/owrx/pocsag.py new file mode 100644 index 0000000..afccc79 --- /dev/null +++ b/owrx/pocsag.py @@ -0,0 +1,8 @@ +from owrx.parser import Parser + + +class PocsagParser(Parser): + def parse(self, raw): + fields = raw.decode("ascii", "replace").rstrip("\n").split(";") + meta = {v[0]: "".join(v[1:]) for v in map(lambda x: x.split(":"), fields) if v[0] != ""} + self.handler.write_pocsag_data(meta) diff --git a/owrx/wsjt.py b/owrx/wsjt.py index 7931b98..16d1a73 100644 --- a/owrx/wsjt.py +++ b/owrx/wsjt.py @@ -11,6 +11,7 @@ from owrx.config import PropertyManager from owrx.bands import Bandplan from owrx.metrics import Metrics, CounterMetric, DirectMetric from owrx.pskreporter import PskReporter +from owrx.parser import Parser import logging @@ -258,12 +259,7 @@ class Ft4Chopper(WsjtChopper): return ["jt9", "--ft4", "-d", str(self.decoding_depth("ft4")), file] -class WsjtParser(object): - def __init__(self, handler): - self.handler = handler - self.dial_freq = None - self.band = None - +class WsjtParser(Parser): modes = {"~": "FT8", "#": "JT65", "@": "JT9", "+": "FT4"} def parse(self, data): @@ -312,10 +308,6 @@ class WsjtParser(object): metric.inc() - def setDialFrequency(self, freq): - self.dial_freq = freq - self.band = Bandplan.getSharedInstance().findBand(freq) - class Decoder(object): def parse_timestamp(self, instring, dateformat):