Message and reply working

This commit is contained in:
30hours 2024-02-10 02:18:08 +00:00
parent d7299b425e
commit b2cbc6283f
3 changed files with 33 additions and 13 deletions

View file

@ -7,6 +7,7 @@ import requests
import time
import socket
import asyncio
import threading
from common.Message import Message
@ -36,8 +37,12 @@ adsbs = [
{"name": "None", "url": ""}
]
# message received callback
async def callback_message_received(msg):
print(f"Callback: Received message in main.py: {msg}", flush=True)
# init messaging
message = Message('event', 6969)
message_api_request = Message('event', 6969)
@app.route("/")
def index():
@ -54,11 +59,11 @@ def serve_static(file):
@app.route("/api")
def api():
api = request.query_string.decode('utf-8')
message.send_message(api)
reply = message_api_request.send_message(api)
print(reply, flush=True)
urls = request.args.getlist("url")
data = [{"url": 'http://' + url} for url in urls]
return jsonify(data)
return reply
@app.route("/map/<path:file>")
def serve_map(file):

View file

@ -67,7 +67,9 @@ class Message:
# Call the callback function if set
if self.callback_message_received:
asyncio.run(self.callback_message_received(decoded_data))
reply = asyncio.run(self.callback_message_received(decoded_data))
if reply:
conn.sendall(reply.encode())
def send_message(self, message):
@ -78,8 +80,14 @@ class Message:
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
client_socket.connect((self.host, self.port))
client_socket.sendall(message.encode())
client_socket.settimeout(3)
try:
client_socket.connect((self.host, self.port))
client_socket.sendall(message.encode())
reply = client_socket.recv(1024).decode()
return reply
except ConnectionRefusedError:
print(f"Connection to {self.host}:{self.port} refused.")
def set_callback_message_received(self, callback):

View file

@ -18,6 +18,7 @@ async def event():
timestamp = int(time.time()*1000)
print("Event triggered at: " + str(timestamp), flush=True)
print(api)
# main event loop
#for api_x in api:
@ -30,7 +31,8 @@ async def event():
# delete old API requests
# update output data on db
# send output data
#message_output.send_message(str(int(time.time()*1000)))
# event loop
async def main():
@ -41,13 +43,18 @@ async def main():
api = api_update
# message received callback
async def callback_message_received(message):
print(f"Callback: Received message in event.py: {message}", flush=True)
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)
# init messaging
message = Message('event', 6969)
message.set_callback_message_received(callback_message_received)
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.start_listener).start()
threading.Thread(target=message_api_request.start_listener).start()
asyncio.run(main())