From 10337f7db8684c01e15643428fea54af29f2dde7 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Tue, 22 Aug 2023 21:16:38 +0200 Subject: [PATCH] add message parsing --- csdr/chain/dump1090.py | 6 ++++-- owrx/adsb/dump1090.py | 47 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/csdr/chain/dump1090.py b/csdr/chain/dump1090.py index 0eaf609f..73cb5309 100644 --- a/csdr/chain/dump1090.py +++ b/csdr/chain/dump1090.py @@ -1,14 +1,16 @@ from pycsdr.modules import Convert from pycsdr.types import Format from csdr.chain.demodulator import ServiceDemodulator -from owrx.adsb.dump1090 import Dump1090Module +from owrx.adsb.dump1090 import Dump1090Module, RawDeframer, ModeSParser class Dump1090(ServiceDemodulator): def __init__(self): workers = [ Convert(Format.COMPLEX_FLOAT, Format.COMPLEX_SHORT), - Dump1090Module() + Dump1090Module(), + RawDeframer(), + ModeSParser(), ] super().__init__(workers) diff --git a/owrx/adsb/dump1090.py b/owrx/adsb/dump1090.py index 3032999e..ffd4dd1d 100644 --- a/owrx/adsb/dump1090.py +++ b/owrx/adsb/dump1090.py @@ -1,8 +1,9 @@ from pycsdr.modules import ExecModule, Writer, TcpSource from pycsdr.types import Format -from csdr.module import LogWriter +from csdr.module import LogWriter, ThreadModule, PickleModule from owrx.socket import getAvailablePort import time +import pickle import logging @@ -44,3 +45,47 @@ class Dump1090Module(ExecModule): self.writer = writer if self.tcpSource is not None: self.tcpSource.setWriter(writer) + + +class RawDeframer(ThreadModule): + def __init__(self): + self.retained = bytes() + super().__init__() + + def getInputFormat(self) -> Format: + return Format.CHAR + + def getOutputFormat(self) -> Format: + return Format.CHAR + + def run(self): + while self.doRun: + data = self.reader.read() + if data is None: + self.doRun = False + else: + self.retained += data + lines = self.retained.split(b"\n") + + # keep the last line + # this should either be empty if the last char was \n + # or an incomplete line if the read returned early + self.retained = lines[-1] + + # log all completed lines + for line in lines[0:-1]: + self.writer.write(pickle.dumps(self.parse(line))) + + def parse(self, line): + if line.startswith(b'*') and line.endswith(b';') and len(line) in [16, 30]: + return bytes.fromhex(line[1:-1].decode()) + else: + logger.warning("invalid raw message: %s", line) + + +class ModeSParser(PickleModule): + def process(self, input): + return { + "mode": "ADSB", + "df": (input[0] & 0b11111000) >> 3 + }