2019-07-06 16:21:43 +00:00
|
|
|
import threading
|
|
|
|
import wave
|
2019-07-06 18:03:17 +00:00
|
|
|
from datetime import datetime, timedelta, date
|
2019-07-06 16:21:43 +00:00
|
|
|
import time
|
|
|
|
import sched
|
|
|
|
import subprocess
|
2019-07-06 18:03:17 +00:00
|
|
|
import os
|
|
|
|
from multiprocessing.connection import Pipe
|
2019-07-06 20:21:47 +00:00
|
|
|
from owrx.map import Map, LocatorLocation
|
|
|
|
import re
|
2019-07-06 16:21:43 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Ft8Chopper(threading.Thread):
|
|
|
|
def __init__(self, source):
|
|
|
|
self.source = source
|
|
|
|
(self.wavefilename, self.wavefile) = self.getWaveFile()
|
|
|
|
self.scheduler = sched.scheduler(time.time, time.sleep)
|
2019-07-06 18:03:17 +00:00
|
|
|
self.fileQueue = []
|
|
|
|
(self.outputReader, self.outputWriter) = Pipe()
|
2019-07-06 16:21:43 +00:00
|
|
|
self.doRun = True
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def getWaveFile(self):
|
|
|
|
filename = "/tmp/openwebrx-ft8chopper-{0}.wav".format(datetime.now().strftime("%Y%m%d-%H%M%S"))
|
|
|
|
wavefile = wave.open(filename, "wb")
|
|
|
|
wavefile.setnchannels(1)
|
|
|
|
wavefile.setsampwidth(2)
|
|
|
|
wavefile.setframerate(12000)
|
|
|
|
return (filename, wavefile)
|
|
|
|
|
|
|
|
def getNextDecodingTime(self):
|
|
|
|
t = datetime.now()
|
|
|
|
seconds = (int(t.second / 15) + 1) * 15
|
|
|
|
if seconds >= 60:
|
|
|
|
t = t + timedelta(minutes = 1)
|
|
|
|
seconds = 0
|
|
|
|
t = t.replace(second = seconds, microsecond = 0)
|
|
|
|
logger.debug("scheduling: {0}".format(t))
|
|
|
|
return t.timestamp()
|
|
|
|
|
|
|
|
def startScheduler(self):
|
|
|
|
self._scheduleNextSwitch()
|
|
|
|
threading.Thread(target = self.scheduler.run).start()
|
|
|
|
|
|
|
|
def emptyScheduler(self):
|
|
|
|
for event in self.scheduler.queue:
|
|
|
|
self.scheduler.cancel(event)
|
|
|
|
|
|
|
|
def _scheduleNextSwitch(self):
|
|
|
|
self.scheduler.enterabs(self.getNextDecodingTime(), 1, self.switchFiles)
|
|
|
|
|
|
|
|
def switchFiles(self):
|
|
|
|
file = self.wavefile
|
|
|
|
filename = self.wavefilename
|
|
|
|
(self.wavefilename, self.wavefile) = self.getWaveFile()
|
|
|
|
|
|
|
|
file.close()
|
2019-07-06 18:03:17 +00:00
|
|
|
self.fileQueue.append(filename)
|
2019-07-06 16:21:43 +00:00
|
|
|
self._scheduleNextSwitch()
|
|
|
|
|
|
|
|
def decode(self):
|
2019-07-06 18:03:17 +00:00
|
|
|
def decode_and_unlink(file):
|
2019-07-06 16:21:43 +00:00
|
|
|
#TODO expose decoding quality parameters through config
|
2019-07-06 18:03:17 +00:00
|
|
|
decoder = subprocess.Popen(["jt9", "--ft8", "-d", "3", file], stdout=subprocess.PIPE)
|
|
|
|
while True:
|
|
|
|
line = decoder.stdout.readline()
|
|
|
|
if line is None or (isinstance(line, bytes) and len(line) == 0):
|
|
|
|
break
|
|
|
|
self.outputWriter.send(line)
|
|
|
|
rc = decoder.wait()
|
|
|
|
logger.debug("decoder return code: %i", rc)
|
|
|
|
os.unlink(file)
|
|
|
|
|
|
|
|
self.decoder = decoder
|
|
|
|
|
|
|
|
if self.fileQueue:
|
|
|
|
file = self.fileQueue.pop()
|
|
|
|
logger.debug("processing file {0}".format(file))
|
|
|
|
threading.Thread(target=decode_and_unlink, args=[file]).start()
|
2019-07-06 16:21:43 +00:00
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
logger.debug("FT8 chopper starting up")
|
|
|
|
self.startScheduler()
|
|
|
|
while self.doRun:
|
|
|
|
data = self.source.read(256)
|
|
|
|
if data is None or (isinstance(data, bytes) and len(data) == 0):
|
|
|
|
logger.warning("zero read on ft8 chopper")
|
|
|
|
self.doRun = False
|
|
|
|
else:
|
|
|
|
self.wavefile.writeframes(data)
|
|
|
|
|
|
|
|
self.decode()
|
|
|
|
logger.debug("FT8 chopper shutting down")
|
2019-07-06 18:03:17 +00:00
|
|
|
self.outputReader.close()
|
|
|
|
self.outputWriter.close()
|
2019-07-06 16:21:43 +00:00
|
|
|
self.emptyScheduler()
|
2019-07-06 18:03:17 +00:00
|
|
|
|
|
|
|
def read(self):
|
|
|
|
try:
|
|
|
|
return self.outputReader.recv()
|
|
|
|
except EOFError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class WsjtParser(object):
|
|
|
|
def __init__(self, handler):
|
|
|
|
self.handler = handler
|
2019-07-06 20:21:47 +00:00
|
|
|
self.locator_pattern = re.compile(".*\s([A-Z0-9]+)\s([A-R]{2}[0-9]{2})$")
|
2019-07-06 18:03:17 +00:00
|
|
|
|
|
|
|
def parse(self, data):
|
|
|
|
try:
|
|
|
|
msg = data.decode().rstrip()
|
|
|
|
# known debug messages we know to skip
|
|
|
|
if msg.startswith("<DecodeFinished>"):
|
|
|
|
return
|
|
|
|
if msg.startswith(" EOF on input file"):
|
|
|
|
return
|
|
|
|
|
|
|
|
out = {}
|
2019-07-06 19:29:49 +00:00
|
|
|
ts = datetime.strptime(msg[0:6], "%H%M%S")
|
2019-07-07 12:09:24 +00:00
|
|
|
out["timestamp"] = int(datetime.combine(date.today(), ts.time(), datetime.now().tzinfo).timestamp() * 1000)
|
2019-07-06 18:03:17 +00:00
|
|
|
out["db"] = float(msg[7:10])
|
|
|
|
out["dt"] = float(msg[11:15])
|
|
|
|
out["freq"] = int(msg[16:20])
|
2019-07-06 22:52:28 +00:00
|
|
|
wsjt_msg = msg[24:61].strip()
|
2019-07-06 20:21:47 +00:00
|
|
|
self.getLocator(wsjt_msg)
|
|
|
|
out["msg"] = wsjt_msg
|
2019-07-06 18:03:17 +00:00
|
|
|
|
|
|
|
self.handler.write_wsjt_message(out)
|
|
|
|
except ValueError:
|
|
|
|
logger.exception("error while parsing wsjt message")
|
2019-07-06 20:21:47 +00:00
|
|
|
|
|
|
|
def getLocator(self, msg):
|
|
|
|
m = self.locator_pattern.match(msg)
|
|
|
|
if m is None:
|
|
|
|
return
|
|
|
|
# this is a valid locator in theory, but it's somewhere in the arctic ocean, near the north pole, so it's very
|
|
|
|
# likely this just means roger roger goodbye.
|
|
|
|
if m.group(2) == "RR73":
|
|
|
|
return
|
|
|
|
Map.getSharedInstance().updateLocation(m.group(1), LocatorLocation(m.group(2)))
|