diff --git a/api/main.py b/api/main.py index c989945..a29530b 100644 --- a/api/main.py +++ b/api/main.py @@ -89,4 +89,4 @@ def serve_cesium_content(file): return Response('Error fetching content from Apache server', status=500, content_type='text/plain') if __name__ == "__main__": - app.run(debug=True) + app.run() diff --git a/common/Message.py b/common/Message.py index f9d35c2..1f36d87 100644 --- a/common/Message.py +++ b/common/Message.py @@ -56,14 +56,12 @@ class Message: """ with conn: - print(f"Connected by {addr}") while True: data = conn.recv(1024) if not data: break decoded_data = data.decode() - print(f"Received data: {decoded_data}") # Call the callback function if set if self.callback_message_received: diff --git a/event/event.py b/event/event.py index 5b317e7..7cf79f3 100644 --- a/event/event.py +++ b/event/event.py @@ -5,6 +5,8 @@ import threading import socket import asyncio import time +import copy +import json from algorithm.associator.AdsbAssociator import AdsbAssociator from algorithm.coordreg.EllipseParametric import EllipseParametric @@ -12,27 +14,31 @@ from common.Message import Message # init event loop api = [] -api_update = [] + +# init config +tDelete = 60 async def event(): + global api + timestamp = int(time.time()*1000) print("Event triggered at: " + str(timestamp), flush=True) - print(api) # main event loop - #for api_x in api: + api_event = copy.copy(api) + + for item in api_event: - # request data from API + print(item) - # API management + # delete old API requests + api_event = [ + item for item in api_event if timestamp - item["timestamp"] <= tDelete*1000] - # add new API requests + # update API + api = api_event - # delete old API requests - - # send output data - #message_output.send_message(str(int(time.time()*1000))) # event loop async def main(): @@ -40,20 +46,34 @@ async def main(): while True: await event() await asyncio.sleep(1) - api = api_update # message received callback async def callback_message_received(msg): + print(f"Callback: Received message in event.py: {msg}", flush=True) - if msg not in api: - api.append(msg) + timestamp = int(time.time()*1000) - return str(timestamp) + + # update timestamp if api entry exists + for x in api: + if x["url"] == msg: + x["timestamp"] = timestamp + break + + # add api entry if does not exist + if not any(x.get("url") == msg for x in api): + api.append({}) + api[-1]["url"] = msg + api[-1]["timestamp"] = timestamp + + # json dump + output = json.dumps(api) + + return output # init messaging message_api_request = Message('event', 6969) message_api_request.set_callback_message_received(callback_message_received) -#message_output = Message('api', 6970) if __name__ == "__main__": threading.Thread(target=message_api_request.start_listener).start()