add nicer console logging to server
This commit is contained in:
parent
d1e7834b8c
commit
1ad3ded60d
@ -1,2 +1,3 @@
|
||||
opencv-python
|
||||
numpy
|
||||
rich
|
16
server.py
16
server.py
@ -6,6 +6,7 @@ import numpy as np
|
||||
from datetime import datetime
|
||||
import asyncio
|
||||
import argparse
|
||||
from rich.console import Console
|
||||
|
||||
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)."""
|
||||
while True:
|
||||
# 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:
|
||||
data, addr = sock.recvfrom(DoublyInterlacedPacket.size) # packet buffer size based on the packet size
|
||||
# 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 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()
|
||||
stat.update(f"[bold yellow]{len(frames.keys())}[/bold yellow] clients connected.")
|
||||
|
||||
frames[uuid].update(pkt)
|
||||
|
||||
@ -65,9 +67,10 @@ async def show_frames():
|
||||
# drop clients that have not sent packets for > 5 seconds
|
||||
for id in list(frames.keys()):
|
||||
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)
|
||||
frames.pop(id)
|
||||
stat.update(f"[bold yellow]{len(frames.keys())}[/bold yellow] clients connected.")
|
||||
else:
|
||||
# show the latest available frame
|
||||
cv2.imshow(id, frames[id].read())
|
||||
@ -85,6 +88,9 @@ if __name__ == "__main__":
|
||||
parser.add_argument("-H", "--height", type=int, default=480)
|
||||
args = parser.parse_args()
|
||||
|
||||
# console
|
||||
console = Console()
|
||||
|
||||
# assign constants based on argument parser
|
||||
UDP_IP = args.listen
|
||||
UDP_PORT = args.port
|
||||
@ -98,6 +104,10 @@ if __name__ == "__main__":
|
||||
sock.setblocking(False)
|
||||
sock.bind((UDP_IP, UDP_PORT))
|
||||
|
||||
console.log("Ready to accept connections.", style="bold green")
|
||||
|
||||
with console.status("[bold yellow]0[/bold yellow] clients connected.", spinner="pong") as stat:
|
||||
|
||||
# create the async event loop
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
Loading…
x
Reference in New Issue
Block a user