update:优化

This commit is contained in:
hrz
2025-12-13 14:35:55 +08:00
parent dc170edbc1
commit 7699596597
+12 -18
View File
@@ -68,7 +68,7 @@ class ConnectionHandler:
self.logger = setup_logging() self.logger = setup_logging()
self.server = server # 保存server实例的引用 self.server = server # 保存server实例的引用
self.need_bind = True # 是否需要绑定设备 self.need_bind = False # 是否需要绑定设备
self.bind_completed_event = asyncio.Event() self.bind_completed_event = asyncio.Event()
self.bind_code = None # 绑定设备的验证码 self.bind_code = None # 绑定设备的验证码
self.last_bind_prompt_time = 0 # 上次播放绑定提示的时间戳(秒) self.last_bind_prompt_time = 0 # 上次播放绑定提示的时间戳(秒)
@@ -270,15 +270,12 @@ class ConnectionHandler:
async def _route_message(self, message): async def _route_message(self, message):
"""消息路由""" """消息路由"""
try: try:
await asyncio.wait_for(self.bind_completed_event.wait(), timeout=1) await asyncio.wait_for(self.bind_completed_event.wait(), timeout=2)
except asyncio.TimeoutError: except asyncio.TimeoutError:
# 未绑定设备直接丢弃所有消息 # 未绑定设备直接丢弃所有消息
current_time = time.time() current_time = time.time()
# 检查是否需要播放绑定提示 # 检查是否需要播放绑定提示
if ( if current_time - self.last_bind_prompt_time >= self.bind_prompt_interval:
current_time - self.last_bind_prompt_time
>= self.bind_prompt_interval
):
self.last_bind_prompt_time = current_time self.last_bind_prompt_time = current_time
# 复用现有的绑定提示逻辑 # 复用现有的绑定提示逻辑
from core.handle.receiveAudioHandle import check_bind_device from core.handle.receiveAudioHandle import check_bind_device
@@ -416,6 +413,14 @@ class ConnectionHandler:
def _initialize_components(self): def _initialize_components(self):
try: try:
if self.tts is None:
self.tts = self._initialize_tts()
# 打开语音合成通道
asyncio.run_coroutine_threadsafe(
self.tts.open_audio_channels(self), self.loop
)
if self.need_bind:
return
self.selected_module_str = build_module_string( self.selected_module_str = build_module_string(
self.config.get("selected_module", {}) self.config.get("selected_module", {})
) )
@@ -439,17 +444,10 @@ class ConnectionHandler:
# 初始化声纹识别 # 初始化声纹识别
self._initialize_voiceprint() self._initialize_voiceprint()
# 打开语音识别通道 # 打开语音识别通道
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.asr.open_audio_channels(self), self.loop self.asr.open_audio_channels(self), self.loop
) )
if self.tts is None:
self.tts = self._initialize_tts()
# 打开语音合成通道
asyncio.run_coroutine_threadsafe(
self.tts.open_audio_channels(self), self.loop
)
"""加载记忆""" """加载记忆"""
self._initialize_memory() self._initialize_memory()
@@ -464,8 +462,6 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}") self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}")
def _init_prompt_enhancement(self): def _init_prompt_enhancement(self):
if self.need_bind:
return
# 更新上下文信息 # 更新上下文信息
self.prompt_manager.update_context_info(self, self.client_ip) self.prompt_manager.update_context_info(self, self.client_ip)
@@ -515,9 +511,6 @@ class ConnectionHandler:
def _initialize_voiceprint(self): def _initialize_voiceprint(self):
"""为当前连接初始化声纹识别""" """为当前连接初始化声纹识别"""
if self.need_bind:
return
try: try:
voiceprint_config = self.config.get("voiceprint", {}) voiceprint_config = self.config.get("voiceprint", {})
if voiceprint_config: if voiceprint_config:
@@ -545,6 +538,7 @@ class ConnectionHandler:
async def _initialize_private_config_async(self): async def _initialize_private_config_async(self):
"""从接口异步获取差异化配置(异步版本,不阻塞主循环)""" """从接口异步获取差异化配置(异步版本,不阻塞主循环)"""
if not self.read_config_from_api: if not self.read_config_from_api:
self.bind_completed_event.set()
return return
try: try:
begin_time = time.time() begin_time = time.time()