Event loop coming together

This commit is contained in:
30hours 2024-02-10 04:19:08 +00:00
parent b2cbc6283f
commit 40246c5e33
3 changed files with 36 additions and 18 deletions

View file

@ -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()

View file

@ -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:

View file

@ -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()