Files
banban/talkingq-url/services/tts_error_manager.py
2026-03-24 15:04:36 +08:00

39 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
from typing import Set, Tuple
from utils.logger import session_logger
class TTSErrorManager:
"""管理TTS错误状态确保TTS_START和TTS_END之间最多只发送一次TTS_ERROR"""
def __init__(self):
self.error_sent_sessions: Set[Tuple[str, str]] = set()
self.active_tts_sessions: Set[Tuple[str, str]] = set()
self.lock = asyncio.Lock()
async def start_tts_session(self, device_id: str, session_id: str):
"""标记TTS会话开始重置错误状态"""
session_key = (device_id, session_id)
async with self.lock:
self.active_tts_sessions.add(session_key)
if session_key in self.error_sent_sessions:
self.error_sent_sessions.remove(session_key)
async def end_tts_session(self, device_id: str, session_id: str):
"""标记TTS会话结束清理状态"""
session_key = (device_id, session_id)
async with self.lock:
if session_key in self.active_tts_sessions:
self.active_tts_sessions.remove(session_key)
if session_key in self.error_sent_sessions:
self.error_sent_sessions.remove(session_key)
async def can_send_error(self, device_id: str, session_id: str) -> bool:
"""检查是否可以发送错误通知"""
session_key = (device_id, session_id)
async with self.lock:
if session_key in self.active_tts_sessions and session_key not in self.error_sent_sessions:
self.error_sent_sessions.add(session_key)
return True
return False
tts_error_manager = TTSErrorManager()