mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-27 04:49:47 +01:00
chore(app.py): translate comments and log messages to English
This commit is contained in:
parent
2fe87b06d1
commit
00fd2195ce
1 changed files with 67 additions and 39 deletions
|
|
@ -2,7 +2,9 @@ import sys
|
||||||
import uuid
|
import uuid
|
||||||
import signal
|
import signal
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from aioconsole import ainput
|
from aioconsole import ainput
|
||||||
|
|
||||||
from config.settings import load_config
|
from config.settings import load_config
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
from core.utils.util import get_local_ip, validate_mcp_endpoint
|
from core.utils.util import get_local_ip, validate_mcp_endpoint
|
||||||
|
|
@ -12,14 +14,15 @@ from core.utils.util import check_ffmpeg_installed
|
||||||
from core.utils.gc_manager import get_gc_manager
|
from core.utils.gc_manager import get_gc_manager
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
|
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_exit() -> None:
|
async def wait_for_exit() -> None:
|
||||||
"""
|
"""
|
||||||
阻塞直到收到 Ctrl‑C / SIGTERM。
|
Wait until Ctrl+C or SIGTERM is received.
|
||||||
- Unix: 使用 add_signal_handler
|
- Unix: uses add_signal_handler.
|
||||||
- Windows: 依赖 KeyboardInterrupt
|
- Windows: relies on KeyboardInterrupt.
|
||||||
"""
|
"""
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
stop_event = asyncio.Event()
|
stop_event = asyncio.Event()
|
||||||
|
|
@ -27,126 +30,151 @@ async def wait_for_exit() -> None:
|
||||||
if sys.platform != "win32": # Unix / macOS
|
if sys.platform != "win32": # Unix / macOS
|
||||||
for sig in (signal.SIGINT, signal.SIGTERM):
|
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||||
loop.add_signal_handler(sig, stop_event.set)
|
loop.add_signal_handler(sig, stop_event.set)
|
||||||
|
|
||||||
await stop_event.wait()
|
await stop_event.wait()
|
||||||
else:
|
else:
|
||||||
# Windows:await一个永远pending的fut,
|
# Windows: wait indefinitely to allow KeyboardInterrupt
|
||||||
# 让 KeyboardInterrupt 冒泡到 asyncio.run,以此消除遗留普通线程导致进程退出阻塞的问题
|
# to propagate to asyncio.run, preventing remaining
|
||||||
|
# threads from blocking the shutdown process.
|
||||||
try:
|
try:
|
||||||
await asyncio.Future()
|
await asyncio.Future()
|
||||||
except KeyboardInterrupt: # Ctrl‑C
|
except KeyboardInterrupt: # Ctrl+C
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def monitor_stdin():
|
async def monitor_stdin():
|
||||||
"""监控标准输入,消费回车键"""
|
"""Monitor standard input and wait for Enter to be pressed."""
|
||||||
while True:
|
while True:
|
||||||
await ainput() # 异步等待输入,消费回车
|
await ainput() # Wait for input asynchronously
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
check_ffmpeg_installed()
|
check_ffmpeg_installed()
|
||||||
config = await load_config()
|
config = await load_config()
|
||||||
|
|
||||||
# auth_key优先级:配置文件server.auth_key > manager-api.secret > 自动生成
|
# Authentication key priority:
|
||||||
# auth_key用于jwt认证,比如视觉分析接口的jwt认证、ota接口的token生成与websocket认证
|
# server.auth_key > manager-api.secret > automatically generated key.
|
||||||
# 获取配置文件中的auth_key
|
# The key is used for JWT authentication, visual analysis,
|
||||||
|
# OTA token generation, and WebSocket authentication.
|
||||||
|
|
||||||
|
# Retrieve the authentication key from the configuration file.
|
||||||
auth_key = config["server"].get("auth_key", "")
|
auth_key = config["server"].get("auth_key", "")
|
||||||
|
|
||||||
# 验证auth_key,无效则尝试使用manager-api.secret
|
# If the key is invalid, try using the management API secret.
|
||||||
if not auth_key or len(auth_key) == 0 or "你" in auth_key:
|
if not auth_key or len(auth_key) == 0 or "你" in auth_key:
|
||||||
auth_key = config.get("manager-api", {}).get("secret", "")
|
auth_key = config.get("manager-api", {}).get("secret", "")
|
||||||
# 验证secret,无效则生成随机密钥
|
|
||||||
|
# If the secret is also invalid, generate a random key.
|
||||||
if not auth_key or len(auth_key) == 0 or "你" in auth_key:
|
if not auth_key or len(auth_key) == 0 or "你" in auth_key:
|
||||||
auth_key = str(uuid.uuid4().hex)
|
auth_key = str(uuid.uuid4().hex)
|
||||||
|
|
||||||
config["server"]["auth_key"] = auth_key
|
config["server"]["auth_key"] = auth_key
|
||||||
|
|
||||||
# 添加 stdin 监控任务
|
# Start the standard input monitoring task.
|
||||||
stdin_task = asyncio.create_task(monitor_stdin())
|
stdin_task = asyncio.create_task(monitor_stdin())
|
||||||
|
|
||||||
# 启动全局GC管理器(5分钟清理一次)
|
# Start the global garbage collection manager (cleanup every 5 minutes).
|
||||||
gc_manager = get_gc_manager(interval_seconds=300)
|
gc_manager = get_gc_manager(interval_seconds=300)
|
||||||
await gc_manager.start()
|
await gc_manager.start()
|
||||||
|
|
||||||
# 启动 WebSocket 服务器
|
# Start the WebSocket server.
|
||||||
ws_server = WebSocketServer(config)
|
ws_server = WebSocketServer(config)
|
||||||
ws_task = asyncio.create_task(ws_server.start())
|
ws_task = asyncio.create_task(ws_server.start())
|
||||||
# 启动 Simple http 服务器
|
|
||||||
|
# Start the simple HTTP server.
|
||||||
ota_server = SimpleHttpServer(config)
|
ota_server = SimpleHttpServer(config)
|
||||||
ota_task = asyncio.create_task(ota_server.start())
|
ota_task = asyncio.create_task(ota_server.start())
|
||||||
|
|
||||||
read_config_from_api = config.get("read_config_from_api", False)
|
read_config_from_api = config.get("read_config_from_api", False)
|
||||||
port = int(config["server"].get("http_port", 8003))
|
port = int(config["server"].get("http_port", 8003))
|
||||||
|
|
||||||
if not read_config_from_api:
|
if not read_config_from_api:
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"OTA接口是\t\thttp://{}:{}/xiaozhi/ota/",
|
"OTA interface available at:\thttp://{}:{}/xiaozhi/ota/",
|
||||||
get_local_ip(),
|
get_local_ip(),
|
||||||
port,
|
port,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"视觉分析接口是\thttp://{}:{}/mcp/vision/explain",
|
"Visual analysis interface available at:\thttp://{}:{}/mcp/vision/explain",
|
||||||
get_local_ip(),
|
get_local_ip(),
|
||||||
port,
|
port,
|
||||||
)
|
)
|
||||||
|
|
||||||
mcp_endpoint = config.get("mcp_endpoint", None)
|
mcp_endpoint = config.get("mcp_endpoint", None)
|
||||||
|
|
||||||
if mcp_endpoint is not None and "你" not in mcp_endpoint:
|
if mcp_endpoint is not None and "你" not in mcp_endpoint:
|
||||||
# 校验MCP接入点格式
|
# Validate the MCP endpoint URL format.
|
||||||
if validate_mcp_endpoint(mcp_endpoint):
|
if validate_mcp_endpoint(mcp_endpoint):
|
||||||
logger.bind(tag=TAG).info("mcp接入点是\t{}", mcp_endpoint)
|
logger.bind(tag=TAG).info(
|
||||||
# 将mcp计入点地址转成调用点
|
"MCP endpoint: {}",
|
||||||
|
mcp_endpoint,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Convert the MCP endpoint URL to the corresponding call URL.
|
||||||
mcp_endpoint = mcp_endpoint.replace("/mcp/", "/call/")
|
mcp_endpoint = mcp_endpoint.replace("/mcp/", "/call/")
|
||||||
config["mcp_endpoint"] = mcp_endpoint
|
config["mcp_endpoint"] = mcp_endpoint
|
||||||
else:
|
else:
|
||||||
logger.bind(tag=TAG).error("mcp接入点不符合规范")
|
logger.bind(tag=TAG).error(
|
||||||
config["mcp_endpoint"] = "你的接入点 websocket地址"
|
"The MCP endpoint URL has an invalid format."
|
||||||
|
)
|
||||||
|
config["mcp_endpoint"] = "MCP endpoint WebSocket URL"
|
||||||
|
|
||||||
# 获取WebSocket配置,使用安全的默认值
|
# Retrieve the WebSocket configuration, using safe default values.
|
||||||
websocket_port = 8000
|
websocket_port = 8000
|
||||||
server_config = config.get("server", {})
|
server_config = config.get("server", {})
|
||||||
|
|
||||||
if isinstance(server_config, dict):
|
if isinstance(server_config, dict):
|
||||||
websocket_port = int(server_config.get("port", 8000))
|
websocket_port = int(server_config.get("port", 8000))
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"Websocket地址是\tws://{}:{}/xiaozhi/v1/",
|
"WebSocket endpoint available at:\tws://{}:{}/xiaozhi/v1/",
|
||||||
get_local_ip(),
|
get_local_ip(),
|
||||||
websocket_port,
|
websocket_port,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"=======上面的地址是websocket协议地址,请勿用浏览器访问======="
|
"======= The endpoint above uses the WebSocket protocol. Do not access it through a browser. ======="
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"如想测试websocket请启动digital-human模块,打开浏览器交互测试"
|
"To test the WebSocket connection, start the digital-human module and open the interaction test in your browser."
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).info(
|
logger.bind(tag=TAG).info(
|
||||||
"=============================================================\n"
|
"================================================================================\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await wait_for_exit() # 阻塞直到收到退出信号
|
await wait_for_exit() # Wait for a shutdown signal.
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
print("任务被取消,清理资源中...")
|
print("Task cancelled. Cleaning up resources...")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# 停止全局GC管理器
|
# Stop the global garbage collection manager.
|
||||||
await gc_manager.stop()
|
await gc_manager.stop()
|
||||||
|
|
||||||
# 取消所有任务(关键修复点)
|
# Cancel all tasks to ensure a clean shutdown.
|
||||||
stdin_task.cancel()
|
stdin_task.cancel()
|
||||||
ws_task.cancel()
|
ws_task.cancel()
|
||||||
|
|
||||||
if ota_task:
|
if ota_task:
|
||||||
ota_task.cancel()
|
ota_task.cancel()
|
||||||
|
|
||||||
# 等待任务终止(必须加超时)
|
# Wait for the tasks to finish, with a timeout.
|
||||||
await asyncio.wait(
|
await asyncio.wait(
|
||||||
[stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task],
|
[stdin_task, ws_task, ota_task]
|
||||||
|
if ota_task
|
||||||
|
else [stdin_task, ws_task],
|
||||||
timeout=3.0,
|
timeout=3.0,
|
||||||
return_when=asyncio.ALL_COMPLETED,
|
return_when=asyncio.ALL_COMPLETED,
|
||||||
)
|
)
|
||||||
print("服务器已关闭,程序退出。")
|
|
||||||
|
print("Server stopped. Exiting the program.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("手动中断,程序终止。")
|
print("Manual interruption detected. Shutting down the program.")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue