Refactored aircraft mode parsers, added initial data to MQTT reports.

This commit is contained in:
Marat Fayzullin 2024-04-27 21:13:39 -04:00
parent e8d4e4c9ea
commit 756aeade9d
1 changed files with 30 additions and 23 deletions

View File

@ -171,12 +171,22 @@ class AircraftManager(object):
if not id: if not id:
return updated return updated
# Add time-to-live, if missing, assume HFDL longevity # Add timestamp, if missing
if "timestamp" not in data: if "timestamp" not in data:
data["timestamp"] = round(datetime.now().timestamp() * 1000)
# Add time-to-live
pm = Config.get() pm = Config.get()
ts = round(datetime.now().timestamp() * 1000) mode = data["mode"]
data["timestamp"] = ts if mode is "ACARS":
data["ttl"] = ts + pm["hfdl_ttl"] * 1000 data["ttl"] = data["timestamp"] + pm["acars_ttl"] * 1000
elif mode is "VDL2":
data["ttl"] = data["timestamp"] + pm["vdl2_ttl"] * 1000
elif mode is "HFDL":
data["ttl"] = data["timestamp"] + pm["hfdl_ttl"] * 1000
else:
# Assume ADSB time-to-live
data["ttl"] = data["timestamp"] + pm["adsb_ttl"] * 1000
# Now operating on the database... # Now operating on the database...
with self.lock: with self.lock:
@ -325,8 +335,14 @@ class AircraftParser(TextParser):
# Add communications frequency, if known # Add communications frequency, if known
if self.frequency != 0: if self.frequency != 0:
out["freq"] = self.frequency out["freq"] = self.frequency
# Add timestamp, if missing
if "timestamp" not in out:
out["timestamp"] = round(datetime.now().timestamp() * 1000)
# Report message # Report message
ReportingEngine.getSharedInstance().spot(out) ReportingEngine.getSharedInstance().spot(out)
# Remove original data from the message
if "data" in out:
del out["data"]
# Update aircraft database with the new data # Update aircraft database with the new data
AircraftManager.getSharedInstance().update(out) AircraftManager.getSharedInstance().update(out)
# Do not return anything when in service mode # Do not return anything when in service mode
@ -363,16 +379,14 @@ class HfdlParser(AircraftParser):
def parseAircraft(self, msg: bytes): def parseAircraft(self, msg: bytes):
# Expect JSON data in text form # Expect JSON data in text form
data = json.loads(msg) data = json.loads(msg)
pm = Config.get()
ts = round(data["hfdl"]["t"]["sec"] * 1000 + data["hfdl"]["t"]["usec"] / 1000)
# @@@ Only parse messages that have LDPU frames for now !!! # @@@ Only parse messages that have LDPU frames for now !!!
if "lpdu" not in data["hfdl"]: if "lpdu" not in data["hfdl"]:
return {} return {}
# Collect basic data first # Collect basic data first
out = { out = {
"mode" : "HFDL", "mode" : "HFDL",
"timestamp" : ts, "timestamp" : round(data["hfdl"]["t"]["sec"] * 1000 + data["hfdl"]["t"]["usec"] / 1000),
"ttl" : ts + pm["hfdl_ttl"] * 1000 "data" : data
} }
# Parse LPDU if present # Parse LPDU if present
if "lpdu" in data["hfdl"]: if "lpdu" in data["hfdl"]:
@ -449,13 +463,11 @@ class Vdl2Parser(AircraftParser):
def parseAircraft(self, msg: bytes): def parseAircraft(self, msg: bytes):
# Expect JSON data in text form # Expect JSON data in text form
data = json.loads(msg) data = json.loads(msg)
pm = Config.get()
ts = round(data["vdl2"]["t"]["sec"] * 1000 + data["vdl2"]["t"]["usec"] / 1000)
# Collect basic data first # Collect basic data first
out = { out = {
"mode" : "VDL2", "mode" : "VDL2",
"timestamp" : ts, "timestamp" : round(data["vdl2"]["t"]["sec"] * 1000 + data["vdl2"]["t"]["usec"] / 1000),
"ttl" : ts + pm["vdl2_ttl"] * 1000 "data" : data
} }
# Parse AVLC if present # Parse AVLC if present
if "avlc" in data["vdl2"]: if "avlc" in data["vdl2"]:
@ -569,10 +581,8 @@ class AdsbParser(AircraftParser):
if "aircraft" not in data or "now" not in data: if "aircraft" not in data or "now" not in data:
return 0 return 0
# Going to add timestamps and TTLs # This is our current timestamp
pm = Config.get()
now = data["now"] now = data["now"]
ttl = now + pm["adsb_ttl"]
# Iterate over aircraft # Iterate over aircraft
for entry in data["aircraft"]: for entry in data["aircraft"]:
@ -586,9 +596,8 @@ class AdsbParser(AircraftParser):
"mode" : "ADSB", "mode" : "ADSB",
"icao" : entry["hex"].upper(), "icao" : entry["hex"].upper(),
"timestamp" : round(ts * 1000), "timestamp" : round(ts * 1000),
"ttl" : round((ttl - entry["seen"]) * 1000),
"msgs" : entry["messages"], "msgs" : entry["messages"],
"rssi" : entry["rssi"], "rssi" : entry["rssi"]
} }
# Position # Position
@ -668,15 +677,13 @@ class AcarsParser(AircraftParser):
def parseAircraft(self, msg: bytes): def parseAircraft(self, msg: bytes):
# Expect JSON data in text form # Expect JSON data in text form
data = json.loads(msg) data = json.loads(msg)
pm = Config.get()
ts = round(data["timestamp"] * 1000)
#logger.debug("@@@ ACARS: {0}".format(data)) #logger.debug("@@@ ACARS: {0}".format(data))
# Collect basic data first # Collect basic data first
out = { out = {
"mode" : "ACARS", "mode" : "ACARS",
"type" : "ACARS frame", "type" : "ACARS frame",
"timestamp" : ts, "timestamp" : round(data["timestamp"] * 1000),
"ttl" : ts + pm["acars_ttl"] * 1000 "data" : data
} }
# Fetch other data # Fetch other data
for key in self.attrMap: for key in self.attrMap: