2020-04-22 21:53:19 +00:00
|
|
|
from datetime import datetime, timezone
|
2019-07-06 20:21:47 +00:00
|
|
|
from owrx.map import Map, LocatorLocation
|
|
|
|
import re
|
2020-04-22 21:53:19 +00:00
|
|
|
from owrx.metrics import Metrics, CounterMetric
|
2021-01-14 19:52:56 +00:00
|
|
|
from owrx.reporting import ReportingEngine
|
2020-01-09 14:11:53 +00:00
|
|
|
from owrx.parser import Parser
|
2020-04-22 21:53:19 +00:00
|
|
|
from owrx.audio import AudioChopperProfile
|
2020-04-25 14:22:40 +00:00
|
|
|
from abc import ABC, ABCMeta, abstractmethod
|
|
|
|
from owrx.config import Config
|
2019-07-06 16:21:43 +00:00
|
|
|
|
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-07-06 16:21:43 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
class WsjtProfile(AudioChopperProfile, metaclass=ABCMeta):
|
2020-12-09 10:38:46 +00:00
|
|
|
def decoding_depth(self):
|
2020-04-25 14:22:40 +00:00
|
|
|
pm = Config.get()
|
2020-12-09 10:38:46 +00:00
|
|
|
mode = self.getMode().lower()
|
2020-04-25 14:22:40 +00:00
|
|
|
# mode-specific setting?
|
|
|
|
if "wsjt_decoding_depths" in pm and mode in pm["wsjt_decoding_depths"]:
|
|
|
|
return pm["wsjt_decoding_depths"][mode]
|
|
|
|
# return global default
|
|
|
|
if "wsjt_decoding_depth" in pm:
|
|
|
|
return pm["wsjt_decoding_depth"]
|
|
|
|
# default when no setting is provided
|
|
|
|
return 3
|
|
|
|
|
2020-12-09 10:38:46 +00:00
|
|
|
def getTimestampFormat(self):
|
|
|
|
if self.getInterval() < 60:
|
|
|
|
return "%H%M%S"
|
|
|
|
return "%H%M"
|
|
|
|
|
|
|
|
def getFileTimestampFormat(self):
|
|
|
|
return "%y%m%d_" + self.getTimestampFormat()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def getMode(self):
|
|
|
|
pass
|
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
|
|
|
|
class Ft8Profile(WsjtProfile):
|
2020-04-05 17:08:58 +00:00
|
|
|
def getInterval(self):
|
|
|
|
return 15
|
|
|
|
|
2019-07-13 21:16:25 +00:00
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--ft8", "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "FT8"
|
2019-07-13 21:16:25 +00:00
|
|
|
|
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
class WsprProfile(WsjtProfile):
|
2020-04-05 17:08:58 +00:00
|
|
|
def getInterval(self):
|
|
|
|
return 120
|
|
|
|
|
2019-07-13 21:16:25 +00:00
|
|
|
def decoder_commandline(self, file):
|
2019-09-15 14:37:12 +00:00
|
|
|
cmd = ["wsprd"]
|
2020-12-09 10:38:46 +00:00
|
|
|
if self.decoding_depth() > 1:
|
2019-09-15 14:37:12 +00:00
|
|
|
cmd += ["-d"]
|
|
|
|
cmd += [file]
|
|
|
|
return cmd
|
2019-07-13 21:16:25 +00:00
|
|
|
|
2020-12-09 10:38:46 +00:00
|
|
|
def getMode(self):
|
|
|
|
return "WSPR"
|
|
|
|
|
2019-07-13 21:16:25 +00:00
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
class Jt65Profile(WsjtProfile):
|
2020-04-05 17:08:58 +00:00
|
|
|
def getInterval(self):
|
|
|
|
return 60
|
|
|
|
|
2019-07-14 15:09:34 +00:00
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--jt65", "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "JT65"
|
2019-07-14 15:09:34 +00:00
|
|
|
|
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
class Jt9Profile(WsjtProfile):
|
2020-04-05 17:08:58 +00:00
|
|
|
def getInterval(self):
|
|
|
|
return 60
|
|
|
|
|
2019-07-14 15:09:34 +00:00
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--jt9", "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "JT9"
|
2019-07-14 15:09:34 +00:00
|
|
|
|
|
|
|
|
2020-04-25 14:22:40 +00:00
|
|
|
class Ft4Profile(WsjtProfile):
|
2020-04-05 17:08:58 +00:00
|
|
|
def getInterval(self):
|
|
|
|
return 7.5
|
|
|
|
|
2019-07-20 11:38:25 +00:00
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--ft4", "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "FT4"
|
2019-07-20 11:38:25 +00:00
|
|
|
|
|
|
|
|
2020-12-07 10:56:01 +00:00
|
|
|
class Fst4Profile(WsjtProfile):
|
2021-01-20 16:01:46 +00:00
|
|
|
availableIntervals = [15, 30, 60, 120, 300, 900, 1800]
|
2020-12-07 10:56:01 +00:00
|
|
|
|
|
|
|
def __init__(self, interval):
|
|
|
|
self.interval = interval
|
|
|
|
|
|
|
|
def getInterval(self):
|
|
|
|
return self.interval
|
|
|
|
|
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--fst4", "-p", str(self.interval), "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "FST4"
|
2020-12-07 10:56:01 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getEnabledProfiles():
|
|
|
|
config = Config.get()
|
|
|
|
profiles = config["fst4_enabled_intervals"] if "fst4_enabled_intervals" in config else []
|
|
|
|
return [Fst4Profile(i) for i in profiles if i in Fst4Profile.availableIntervals]
|
|
|
|
|
|
|
|
|
|
|
|
class Fst4wProfile(WsjtProfile):
|
|
|
|
availableIntervals = [120, 300, 900, 1800]
|
|
|
|
|
|
|
|
def __init__(self, interval):
|
|
|
|
self.interval = interval
|
|
|
|
|
|
|
|
def getInterval(self):
|
|
|
|
return self.interval
|
|
|
|
|
|
|
|
def decoder_commandline(self, file):
|
2020-12-09 10:38:46 +00:00
|
|
|
return ["jt9", "--fst4w", "-p", str(self.interval), "-d", str(self.decoding_depth()), file]
|
|
|
|
|
|
|
|
def getMode(self):
|
|
|
|
return "FST4W"
|
2020-12-07 10:56:01 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getEnabledProfiles():
|
|
|
|
config = Config.get()
|
|
|
|
profiles = config["fst4w_enabled_intervals"] if "fst4w_enabled_intervals" in config else []
|
2020-12-08 00:06:21 +00:00
|
|
|
return [Fst4wProfile(i) for i in profiles if i in Fst4wProfile.availableIntervals]
|
2020-12-07 10:56:01 +00:00
|
|
|
|
|
|
|
|
2020-01-09 14:11:53 +00:00
|
|
|
class WsjtParser(Parser):
|
2020-04-22 22:21:59 +00:00
|
|
|
def parse(self, messages):
|
|
|
|
for data in messages:
|
|
|
|
try:
|
2020-12-09 10:38:46 +00:00
|
|
|
profile, freq, raw_msg = data
|
2020-04-22 22:21:59 +00:00
|
|
|
self.setDialFrequency(freq)
|
|
|
|
msg = raw_msg.decode().rstrip()
|
|
|
|
# known debug messages we know to skip
|
|
|
|
if msg.startswith("<DecodeFinished>"):
|
|
|
|
return
|
|
|
|
if msg.startswith(" EOF on input file"):
|
|
|
|
return
|
|
|
|
|
2020-12-09 10:38:46 +00:00
|
|
|
mode = profile.getMode()
|
2021-01-15 15:19:45 +00:00
|
|
|
if mode in ["WSPR", "FST4W"]:
|
|
|
|
messageParser = BeaconMessageParser()
|
|
|
|
else:
|
|
|
|
messageParser = QsoMessageParser()
|
2020-12-09 10:38:46 +00:00
|
|
|
if mode == "WSPR":
|
2021-01-15 15:19:45 +00:00
|
|
|
decoder = WsprDecoder(profile, messageParser)
|
2020-04-22 22:21:59 +00:00
|
|
|
else:
|
2021-01-15 15:19:45 +00:00
|
|
|
decoder = Jt9Decoder(profile, messageParser)
|
2020-04-22 22:21:59 +00:00
|
|
|
out = decoder.parse(msg, freq)
|
2020-12-09 10:38:46 +00:00
|
|
|
out["mode"] = mode
|
2021-01-15 15:19:45 +00:00
|
|
|
out["interval"] = profile.getInterval()
|
2020-12-09 10:38:46 +00:00
|
|
|
|
|
|
|
self.pushDecode(mode)
|
|
|
|
if "callsign" in out and "locator" in out:
|
|
|
|
Map.getSharedInstance().updateLocation(
|
|
|
|
out["callsign"], LocatorLocation(out["locator"]), mode, self.band
|
|
|
|
)
|
2021-01-14 19:52:56 +00:00
|
|
|
ReportingEngine.getSharedInstance().spot(out)
|
2020-04-22 22:21:59 +00:00
|
|
|
|
|
|
|
self.handler.write_wsjt_message(out)
|
2020-12-07 10:56:01 +00:00
|
|
|
except (ValueError, IndexError):
|
2020-04-22 22:21:59 +00:00
|
|
|
logger.exception("error while parsing wsjt message")
|
2019-07-06 20:21:47 +00:00
|
|
|
|
2019-09-12 20:50:29 +00:00
|
|
|
def pushDecode(self, mode):
|
|
|
|
metrics = Metrics.getSharedInstance()
|
2019-09-12 21:23:50 +00:00
|
|
|
band = "unknown"
|
|
|
|
if self.band is not None:
|
|
|
|
band = self.band.getName()
|
2019-09-12 20:50:29 +00:00
|
|
|
if band is None:
|
|
|
|
band = "unknown"
|
|
|
|
|
|
|
|
if mode is None:
|
|
|
|
mode = "unknown"
|
|
|
|
|
|
|
|
name = "wsjt.decodes.{band}.{mode}".format(band=band, mode=mode)
|
|
|
|
metric = metrics.getMetric(name)
|
|
|
|
if metric is None:
|
|
|
|
metric = CounterMetric()
|
|
|
|
metrics.addMetric(name, metric)
|
|
|
|
|
|
|
|
metric.inc()
|
|
|
|
|
2019-09-23 16:33:52 +00:00
|
|
|
|
2020-04-05 19:48:05 +00:00
|
|
|
class Decoder(ABC):
|
2021-01-15 15:19:45 +00:00
|
|
|
def __init__(self, profile, messageParser):
|
2020-12-09 10:38:46 +00:00
|
|
|
self.profile = profile
|
2021-01-15 15:19:45 +00:00
|
|
|
self.messageParser = messageParser
|
2020-12-09 10:38:46 +00:00
|
|
|
|
2020-12-09 11:02:40 +00:00
|
|
|
def parse_timestamp(self, instring):
|
|
|
|
dateformat = self.profile.getTimestampFormat()
|
|
|
|
remain = instring[len(dateformat) + 1:]
|
|
|
|
try:
|
2021-01-20 16:01:46 +00:00
|
|
|
ts = datetime.strptime(instring[0: len(dateformat)], dateformat)
|
2020-12-09 11:02:40 +00:00
|
|
|
return remain, int(
|
|
|
|
datetime.combine(datetime.utcnow().date(), ts.time()).replace(tzinfo=timezone.utc).timestamp() * 1000
|
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
return remain, None
|
2019-09-23 16:33:52 +00:00
|
|
|
|
2020-04-05 19:48:05 +00:00
|
|
|
@abstractmethod
|
|
|
|
def parse(self, msg, dial_freq):
|
|
|
|
pass
|
|
|
|
|
2021-01-15 15:19:45 +00:00
|
|
|
|
|
|
|
class MessageParser(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def parse(self, msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Used in QSO-style modes (FT8, FT4, FST4)
|
|
|
|
class QsoMessageParser(MessageParser):
|
|
|
|
locator_pattern = re.compile(".*\\s([A-Z0-9/]{2,})(\\sR)?\\s([A-R]{2}[0-9]{2})$")
|
|
|
|
|
|
|
|
def parse(self, msg):
|
|
|
|
m = QsoMessageParser.locator_pattern.match(msg)
|
2020-12-07 10:56:01 +00:00
|
|
|
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.
|
2020-12-07 23:01:00 +00:00
|
|
|
if m.group(3) == "RR73":
|
2020-12-07 10:56:01 +00:00
|
|
|
return {"callsign": m.group(1)}
|
2020-12-07 23:01:00 +00:00
|
|
|
return {"callsign": m.group(1), "locator": m.group(3)}
|
2019-09-23 16:33:52 +00:00
|
|
|
|
|
|
|
|
2021-01-15 15:19:45 +00:00
|
|
|
# Used in propagation reporting / beacon modes (WSPR / FST4W)
|
|
|
|
class BeaconMessageParser(MessageParser):
|
|
|
|
wspr_splitter_pattern = re.compile("([A-Z0-9/]*)\\s([A-R]{2}[0-9]{2})\\s([0-9]+)")
|
|
|
|
|
|
|
|
def parse(self, msg):
|
|
|
|
m = BeaconMessageParser.wspr_splitter_pattern.match(msg)
|
|
|
|
if m is None:
|
|
|
|
return {}
|
|
|
|
return {"callsign": m.group(1), "locator": m.group(2), "dbm": m.group(3)}
|
|
|
|
|
|
|
|
|
2020-12-07 10:56:01 +00:00
|
|
|
class Jt9Decoder(Decoder):
|
2019-09-23 20:45:55 +00:00
|
|
|
def parse(self, msg, dial_freq):
|
2019-07-13 21:16:25 +00:00
|
|
|
# ft8 sample
|
|
|
|
# '222100 -15 -0.0 508 ~ CQ EA7MJ IM66'
|
2019-07-14 15:09:34 +00:00
|
|
|
# jt65 sample
|
2019-07-20 10:47:10 +00:00
|
|
|
# '2352 -7 0.4 1801 # R0WAS R2ABM KO85'
|
|
|
|
# '0003 -4 0.4 1762 # CQ R2ABM KO85'
|
2020-12-07 10:56:01 +00:00
|
|
|
# fst4 sample
|
|
|
|
# '**** -23 0.6 3023 ` <...> <...> R 591631 BI53PV'
|
2020-12-09 11:02:40 +00:00
|
|
|
msg, timestamp = self.parse_timestamp(msg)
|
2019-07-14 15:09:34 +00:00
|
|
|
wsjt_msg = msg[17:53].strip()
|
2019-09-12 20:50:29 +00:00
|
|
|
|
2019-09-23 16:33:52 +00:00
|
|
|
result = {
|
2019-07-20 10:47:10 +00:00
|
|
|
"timestamp": timestamp,
|
|
|
|
"db": float(msg[0:3]),
|
|
|
|
"dt": float(msg[4:8]),
|
2019-09-23 20:45:55 +00:00
|
|
|
"freq": dial_freq + int(msg[9:13]),
|
2019-07-21 17:40:28 +00:00
|
|
|
"msg": wsjt_msg,
|
2019-07-20 10:47:10 +00:00
|
|
|
}
|
2021-01-15 15:19:45 +00:00
|
|
|
result.update(self.messageParser.parse(wsjt_msg))
|
2019-09-23 16:33:52 +00:00
|
|
|
return result
|
2019-07-13 21:16:25 +00:00
|
|
|
|
|
|
|
|
2019-09-23 16:33:52 +00:00
|
|
|
class WsprDecoder(Decoder):
|
2019-09-23 20:45:55 +00:00
|
|
|
def parse(self, msg, dial_freq):
|
2019-07-13 21:16:25 +00:00
|
|
|
# wspr sample
|
|
|
|
# '2600 -24 0.4 0.001492 -1 G8AXA JO01 33'
|
2019-07-20 10:47:10 +00:00
|
|
|
# '0052 -29 2.6 0.001486 0 G02CWT IO92 23'
|
2020-12-09 11:02:40 +00:00
|
|
|
msg, timestamp = self.parse_timestamp(msg)
|
|
|
|
wsjt_msg = msg[24:].strip()
|
2019-09-23 16:33:52 +00:00
|
|
|
result = {
|
2020-12-09 11:02:40 +00:00
|
|
|
"timestamp": timestamp,
|
|
|
|
"db": float(msg[0:3]),
|
|
|
|
"dt": float(msg[4:8]),
|
|
|
|
"freq": dial_freq + int(float(msg[10:20]) * 1e6),
|
|
|
|
"drift": int(msg[20:23]),
|
2019-07-21 17:40:28 +00:00
|
|
|
"msg": wsjt_msg,
|
2019-07-20 10:47:10 +00:00
|
|
|
}
|
2021-01-15 15:19:45 +00:00
|
|
|
result.update(self.messageParser.parse(wsjt_msg))
|
2019-09-23 16:33:52 +00:00
|
|
|
return result
|