修改音频保存逻辑,后续再完善,添加设备信息保存到数据库
This commit is contained in:
@@ -17,8 +17,9 @@ from utils.logger import session_logger
|
||||
from handlers.prompt_sound_handler import handle_prompt_sound_request
|
||||
from handlers.session_cleanup_handler import handle_old_session_cleanup
|
||||
from config import settings
|
||||
from banban.service.im import im_service as im_conversation_service
|
||||
|
||||
async def handle_websocket_messages(websocket: WebSocket, device_id: str):
|
||||
async def handle_websocket_messages(websocket: WebSocket, device_id: str, serial_number: str):
|
||||
"""
|
||||
处理WebSocket连接中的所有消息
|
||||
|
||||
@@ -49,6 +50,7 @@ async def handle_websocket_messages(websocket: WebSocket, device_id: str):
|
||||
current_active_session, first_audio_received_time = await handle_binary_message(
|
||||
websocket,
|
||||
device_id,
|
||||
serial_number,
|
||||
data,
|
||||
current_active_session,
|
||||
first_audio_received_time
|
||||
@@ -134,7 +136,7 @@ async def handle_text_message(websocket: WebSocket, device_id: str, text_data: s
|
||||
)
|
||||
|
||||
|
||||
async def handle_binary_message(websocket: WebSocket, device_id: str, binary_data, current_active_session, first_audio_received_time):
|
||||
async def handle_binary_message(websocket: WebSocket, device_id: str, serial_number: str, binary_data, current_active_session, first_audio_received_time):
|
||||
"""处理二进制音频消息"""
|
||||
session_key, session, packet_type, audio_data, sample_rate = await parse_packet(binary_data, websocket)
|
||||
|
||||
@@ -163,7 +165,7 @@ async def handle_binary_message(websocket: WebSocket, device_id: str, binary_dat
|
||||
if target_device_id:
|
||||
# 处理缓存的音频数据
|
||||
session_logger.info(device_id, session_id, f"收到结束包,开始处理缓存音频数据")
|
||||
await process_cached_audio(device_id, target_device_id)
|
||||
await process_cached_audio(device_id, target_device_id, serial_number)
|
||||
|
||||
# 移除目标设备关联
|
||||
await device_target_cache.remove_target(device_id)
|
||||
@@ -253,7 +255,7 @@ async def handle_target_audio_packet(device_id: str, audio_data: bytes):
|
||||
session_logger.error(device_id, "target", f"处理目标音频包时出错: {e}", exc_info=True)
|
||||
|
||||
|
||||
async def process_cached_audio(device_id: str, target_device_id: str):
|
||||
async def process_cached_audio(device_id: str, target_device_id: str, serial_number: str):
|
||||
"""处理缓存的音频数据并发送音频URL"""
|
||||
try:
|
||||
# 获取缓存的音频数据
|
||||
@@ -265,6 +267,8 @@ async def process_cached_audio(device_id: str, target_device_id: str):
|
||||
# 保存音频文件
|
||||
audio_path = await save_audio_file(cached_audio, device_id)
|
||||
audio_url = f"http://{settings.server_host}:{settings.server_port}/{audio_path}"
|
||||
# 将音频URL保存到数据库 im_conversation和im_message
|
||||
await im_conversation_service.create_device_message(device_id=device_id, serial_number=serial_number, target_device_id=target_device_id, audio_url=audio_url)
|
||||
|
||||
# 发送URL给目标设备
|
||||
# target_websocket = await connection_manager.get_connection(target_device_id)
|
||||
|
||||
Reference in New Issue
Block a user