Remove URL for hashing

This commit is contained in:
30hours 2024-02-11 02:27:20 +00:00
parent 6a04b321aa
commit d379e4073c

View file

@ -1,3 +1,9 @@
"""
@file event.py
@brief Event loop for 3lips.
@author 30hours
"""
import asyncio import asyncio
import requests import requests
import threading import threading
@ -5,6 +11,7 @@ import asyncio
import time import time
import copy import copy
import json import json
import hashlib
from algorithm.associator.AdsbAssociator import AdsbAssociator from algorithm.associator.AdsbAssociator import AdsbAssociator
from algorithm.coordreg.EllipseParametric import EllipseParametric from algorithm.coordreg.EllipseParametric import EllipseParametric
@ -15,20 +22,68 @@ api = []
# init config # init config
tDelete = 60 tDelete = 60
adsbAssociator = AdsbAssociator()
ellipseParametric = EllipseParametric()
async def event(): async def event():
global api global api
timestamp = int(time.time()*1000) timestamp = int(time.time()*1000)
print("Event triggered at: " + str(timestamp), flush=True)
# main event loop # main event loop
api_event = copy.copy(api) 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: for item in api_event:
print(item) for radar in item["server"]:
print(radar, flush=True)
# delete old API requests # delete old API requests
api_event = [ api_event = [
@ -45,6 +100,12 @@ async def main():
await event() await event()
await asyncio.sleep(1) 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 # message received callback
async def callback_message_received(msg): async def callback_message_received(msg):
@ -52,16 +113,25 @@ async def callback_message_received(msg):
timestamp = int(time.time()*1000) timestamp = int(time.time()*1000)
# update timestamp if api entry exists # update timestamp if API entry exists
for x in api: for x in api:
if x["url"] == msg: if x["hash"] == short_hash(msg):
x["timestamp"] = timestamp x["timestamp"] = timestamp
break break
# add api entry if does not exist # add API entry if does not exist, split URL
if not any(x.get("url") == msg for x in api): if not any(x.get("hash") == short_hash(msg) for x in api):
api.append({}) 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 api[-1]["timestamp"] = timestamp
# json dump # json dump