From 363d08c9d1510faa575e07f4c46cea78696c48fe Mon Sep 17 00:00:00 2001 From: 30hours Date: Thu, 7 Mar 2024 04:35:06 +0000 Subject: [PATCH] Added truth to API --- event/algorithm/truth/AdsbTruth.py | 67 ++++++++++++++++++++++++++++++ event/event.py | 13 ++++++ 2 files changed, 80 insertions(+) create mode 100644 event/algorithm/truth/AdsbTruth.py diff --git a/event/algorithm/truth/AdsbTruth.py b/event/algorithm/truth/AdsbTruth.py new file mode 100644 index 0000000..9cf5159 --- /dev/null +++ b/event/algorithm/truth/AdsbTruth.py @@ -0,0 +1,67 @@ +""" +@file AdsbTruth.py +@author 30hours +""" + +import requests +import math + +class AdsbTruth: + + """ + @class AdsbTruth + @brief A class for storing ADS-B truth in the API response. + @details Uses truth data in delay-Doppler space from an tar1090 server. + """ + + def __init__(self, seen_pos_limit): + + """ + @brief Constructor for the AdsbTruth class. + """ + + self.seen_pos_limit = seen_pos_limit + + def process(self, server): + + """ + @brief Store ADS-B truth for each target in LLA. + @param server (str): The tar1090 server to get truth from. + @return dict: Associated detections by [hex]. + """ + + output = {} + + # get tar1090 URL + url = 'https://' + server + '/data/aircraft.json' + + # get ADSB detections + try: + response = requests.get(url, timeout=1) + response.raise_for_status() + data = response.json() + adsb = data + except requests.exceptions.RequestException as e: + print(f"Error fetching data from {url}: {e}") + adsb = None + + # store relevant data + if adsb: + + # loop over aircraft + for aircraft in adsb["aircraft"]: + + if aircraft.get("seen_pos") and \ + aircraft.get("alt_geom") and \ + aircraft.get("flight") and \ + aircraft.get("seen_pos") < self.seen_pos_limit: + + output[aircraft["hex"]] = {} + output[aircraft["hex"]]["lat"] = aircraft["lat"] + output[aircraft["hex"]]["lon"] = aircraft["lon"] + output[aircraft["hex"]]["alt"] = aircraft["alt_geom"] + output[aircraft["hex"]]["flight"] = aircraft["flight"] + output[aircraft["hex"]]["timestamp"] = \ + adsb["now"] - aircraft["seen_pos"] + + return output diff --git a/event/event.py b/event/event.py index acfabc2..146bd08 100644 --- a/event/event.py +++ b/event/event.py @@ -17,6 +17,7 @@ from algorithm.associator.AdsbAssociator import AdsbAssociator from algorithm.localisation.EllipseParametric import EllipseParametric from algorithm.localisation.EllipsoidParametric import EllipsoidParametric from algorithm.localisation.SphericalIntersection import SphericalIntersection +from algorithm.truth.AdsbTruth import AdsbTruth from common.Message import Message from data.Ellipsoid import Ellipsoid @@ -31,6 +32,7 @@ adsbAssociator = AdsbAssociator() ellipseParametric = EllipseParametric() ellipsoidParametric = EllipsoidParametric() sphericalIntersection = SphericalIntersection() +adsbTruth = AdsbTruth(5) async def event(): @@ -81,6 +83,16 @@ async def event(): "config": radar_config[i] } + # store truth in dict + truth_adsb = {} + adsb_urls = [] + for item in api_event: + adsb_urls.append(item["adsb"]) + adsb_urls = list(set(adsb_urls)) + for url in adsb_urls: + truth_adsb[url] = adsbTruth.process(url) + print(truth_adsb, flush=True) + # main processing for item in api_event: @@ -157,6 +169,7 @@ async def event(): # output data to API item["timestamp_event"] = timestamp + item["truth"] = truth_adsb[item["adsb"]] item["detections_associated"] = associated_dets item["detections_localised"] = localised_dets item["ellipsoids"] = ellipsoids