add nicer console logging to server

This commit is contained in:
Camryn Thomas 2025-02-01 00:44:34 -06:00
parent d1e7834b8c
commit 1ad3ded60d
Signed by: cptlobster
GPG Key ID: 33D607425C830B4C
2 changed files with 28 additions and 17 deletions

View File

@ -1,2 +1,3 @@
opencv-python opencv-python
numpy numpy
rich

View File

@ -6,6 +6,7 @@ import numpy as np
from datetime import datetime from datetime import datetime
import asyncio import asyncio
import argparse import argparse
from rich.console import Console
from common import Packet, DoublyInterlacedPacket, from_bytes_dint from common import Packet, DoublyInterlacedPacket, from_bytes_dint
@ -37,7 +38,7 @@ async def read_packet():
"""Asynchronous coroutine to read UDP packets from the client(s).""" """Asynchronous coroutine to read UDP packets from the client(s)."""
while True: while True:
# we repeat this a ton of times all at once to hopefully capture all of the image data # we repeat this a ton of times all at once to hopefully capture all of the image data
for i in range(0, 1200): for i in range(0, 1600):
try: try:
data, addr = sock.recvfrom(DoublyInterlacedPacket.size) # packet buffer size based on the packet size data, addr = sock.recvfrom(DoublyInterlacedPacket.size) # packet buffer size based on the packet size
# print("received packet from", addr) # print("received packet from", addr)
@ -48,8 +49,9 @@ async def read_packet():
# if this is a new client, give it a new image # if this is a new client, give it a new image
if uuid not in frames.keys(): if uuid not in frames.keys():
print("New client acquired, naming %s", uuid) console.log(f"New client acquired, naming [bold cyan]{uuid}[bold cyan]")
frames[uuid] = Client() frames[uuid] = Client()
stat.update(f"[bold yellow]{len(frames.keys())}[/bold yellow] clients connected.")
frames[uuid].update(pkt) frames[uuid].update(pkt)
@ -65,9 +67,10 @@ async def show_frames():
# drop clients that have not sent packets for > 5 seconds # drop clients that have not sent packets for > 5 seconds
for id in list(frames.keys()): for id in list(frames.keys()):
if frames[id].latency() >= 5: if frames[id].latency() >= 5:
print("Client likely lost connection, dropping %s", id) console.log(f"Client likely lost connection, dropping [bold red]{id}[/bold red]")
cv2.destroyWindow(id) cv2.destroyWindow(id)
frames.pop(id) frames.pop(id)
stat.update(f"[bold yellow]{len(frames.keys())}[/bold yellow] clients connected.")
else: else:
# show the latest available frame # show the latest available frame
cv2.imshow(id, frames[id].read()) cv2.imshow(id, frames[id].read())
@ -85,6 +88,9 @@ if __name__ == "__main__":
parser.add_argument("-H", "--height", type=int, default=480) parser.add_argument("-H", "--height", type=int, default=480)
args = parser.parse_args() args = parser.parse_args()
# console
console = Console()
# assign constants based on argument parser # assign constants based on argument parser
UDP_IP = args.listen UDP_IP = args.listen
UDP_PORT = args.port UDP_PORT = args.port
@ -98,18 +104,22 @@ if __name__ == "__main__":
sock.setblocking(False) sock.setblocking(False)
sock.bind((UDP_IP, UDP_PORT)) sock.bind((UDP_IP, UDP_PORT))
# create the async event loop console.log("Ready to accept connections.", style="bold green")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# create async tasks for reading network packets, displaying windows with console.status("[bold yellow]0[/bold yellow] clients connected.", spinner="pong") as stat:
loop.create_task(read_packet())
loop.create_task(show_frames())
try:
loop.run_forever()
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
# Release the capture and close all windows # create the async event loop
cv2.destroyAllWindows() loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# create async tasks for reading network packets, displaying windows
loop.create_task(read_packet())
loop.create_task(show_frames())
try:
loop.run_forever()
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
# Release the capture and close all windows
cv2.destroyAllWindows()