import asyncio from crud import save_device_data async def handle_connection(reader, writer): addr = writer.get_extra_info('peername') print(f"New connection from {addr}") while True: try: data = await reader.read(1024) if not data: break print(f"Received from {addr}: {data.hex()}") msg = data.decode().strip() dtu_id, device_id, status = msg.split(",") await save_device_data(dtu_id, device_id, status) print("save data over") except Exception as e: print(f"Error: {e}") break writer.close() await writer.wait_closed() print("process over") async def start_tcp_server(host, port): server = await asyncio.start_server(handle_connection, host, port) print(f"TCP server running on {host}:{port}") async with server: await server.serve_forever()