From d379e4073ccda17696da2c47a07b23bc93a3b128 Mon Sep 17 00:00:00 2001 From: 30hours Date: Sun, 11 Feb 2024 02:27:20 +0000 Subject: [PATCH] Remove URL for hashing --- event/event.py | 88 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 9 deletions(-) diff --git a/event/event.py b/event/event.py index a7242b4..2b4f6b8 100644 --- a/event/event.py +++ b/event/event.py @@ -1,3 +1,9 @@ +""" +@file event.py +@brief Event loop for 3lips. +@author 30hours +""" + import asyncio import requests import threading @@ -5,6 +11,7 @@ import asyncio import time import copy import json +import hashlib from algorithm.associator.AdsbAssociator import AdsbAssociator from algorithm.coordreg.EllipseParametric import EllipseParametric @@ -15,20 +22,68 @@ api = [] # init config tDelete = 60 +adsbAssociator = AdsbAssociator() +ellipseParametric = EllipseParametric() async def event(): global api - timestamp = int(time.time()*1000) - print("Event triggered at: " + str(timestamp), flush=True) # main event loop api_event = copy.copy(api) - + + # list all blah2 radars + radar_names = [] + for item in api_event: + for radar in item["server"]: + radar_names.append(radar) + radar_names = list(set(radar_names)) + + # get detections all radar + radar_detections_url = [ + "http://" + radar_name + "/api/detection" for radar_name in radar_names] + radar_detections = [] + for url in radar_detections_url: + try: + response = requests.get(url, timeout=1) + response.raise_for_status() + data = response.json() + radar_detections.append(data) + except requests.exceptions.RequestException as e: + print(f"Error fetching data from {url}: {e}") + radar_detections.append(None) + + # get config all radar + radar_config_url = [ + "http://" + radar_name + "/api/config" for radar_name in radar_names] + radar_config = [] + for url in radar_config_url: + try: + response = requests.get(url, timeout=1) + response.raise_for_status() + data = response.json() + radar_config.append(data) + except requests.exceptions.RequestException as e: + print(f"Error fetching data from {url}: {e}") + radar_config.append(None) + + # store detections in dict + radar_dict = {} + for i in range(len(radar_names)): + radar_dict[radar_names[i]] = { + "detection": radar_detections[i], + "config": radar_config[i] + } + + print(radar_dict, flush=True) + + # main processing for item in api_event: - print(item) + for radar in item["server"]: + + print(radar, flush=True) # delete old API requests api_event = [ @@ -45,6 +100,12 @@ async def main(): await event() await asyncio.sleep(1) +def short_hash(input_string, length=10): + + hash_object = hashlib.sha256(input_string.encode()) + short_hash = hash_object.hexdigest()[:length] + return short_hash + # message received callback async def callback_message_received(msg): @@ -52,16 +113,25 @@ async def callback_message_received(msg): timestamp = int(time.time()*1000) - # update timestamp if api entry exists + # update timestamp if API entry exists for x in api: - if x["url"] == msg: + if x["hash"] == short_hash(msg): x["timestamp"] = timestamp break - # add api entry if does not exist - if not any(x.get("url") == msg for x in api): + # add API entry if does not exist, split URL + if not any(x.get("hash") == short_hash(msg) for x in api): api.append({}) - api[-1]["url"] = msg + api[-1]["hash"] = short_hash(msg) + url_parts = msg.split("&") + for part in url_parts: + key, value = part.split("=") + if key in api[-1]: + if not isinstance(api[-1][key], list): + api[-1][key] = [api[-1][key]] + api[-1][key].append(value) + else: + api[-1][key] = value api[-1]["timestamp"] = timestamp # json dump