add message parsing

This commit is contained in:
Jakob Ketterl 2023-08-22 21:16:38 +02:00
parent 476e4ebc5b
commit 10337f7db8
2 changed files with 50 additions and 3 deletions

View File

@ -1,14 +1,16 @@
from pycsdr.modules import Convert from pycsdr.modules import Convert
from pycsdr.types import Format from pycsdr.types import Format
from csdr.chain.demodulator import ServiceDemodulator from csdr.chain.demodulator import ServiceDemodulator
from owrx.adsb.dump1090 import Dump1090Module from owrx.adsb.dump1090 import Dump1090Module, RawDeframer, ModeSParser
class Dump1090(ServiceDemodulator): class Dump1090(ServiceDemodulator):
def __init__(self): def __init__(self):
workers = [ workers = [
Convert(Format.COMPLEX_FLOAT, Format.COMPLEX_SHORT), Convert(Format.COMPLEX_FLOAT, Format.COMPLEX_SHORT),
Dump1090Module() Dump1090Module(),
RawDeframer(),
ModeSParser(),
] ]
super().__init__(workers) super().__init__(workers)

View File

@ -1,8 +1,9 @@
from pycsdr.modules import ExecModule, Writer, TcpSource from pycsdr.modules import ExecModule, Writer, TcpSource
from pycsdr.types import Format from pycsdr.types import Format
from csdr.module import LogWriter from csdr.module import LogWriter, ThreadModule, PickleModule
from owrx.socket import getAvailablePort from owrx.socket import getAvailablePort
import time import time
import pickle
import logging import logging
@ -44,3 +45,47 @@ class Dump1090Module(ExecModule):
self.writer = writer self.writer = writer
if self.tcpSource is not None: if self.tcpSource is not None:
self.tcpSource.setWriter(writer) 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
}