上传后台修改
This commit is contained in:
@@ -18,6 +18,8 @@ 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
|
||||
from handlers.audio_file_handler import message_audio_storage_service, save_audio_file
|
||||
from fastapi import HTTPException
|
||||
|
||||
async def handle_websocket_messages(websocket: WebSocket, device_id: str, serial_number: str):
|
||||
"""
|
||||
@@ -265,11 +267,14 @@ async def process_cached_audio(device_id: str, target_device_id: str, serial_num
|
||||
return
|
||||
|
||||
# 保存音频文件
|
||||
audio_path = await save_audio_file(cached_audio, device_id)
|
||||
audio_url = f"http://{settings.server_host}:{settings.server_port}/{audio_path}"
|
||||
audio_file_key = await save_audio_file(cached_audio, device_id)
|
||||
audio_url = f"http://{settings.server_host}:{settings.server_port}/{audio_file_key}"
|
||||
# 将音频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)
|
||||
|
||||
# await im_conversation_service.create_device_message(device_id=device_id, serial_number=serial_number, target_device_id=target_device_id, audio_url=audio_file_key)
|
||||
# try:
|
||||
# audio_url = await message_audio_storage_service.get_audio_url(audio_file_key)
|
||||
# except Exception:
|
||||
# audio_url = audio_file_key
|
||||
# 发送URL给目标设备
|
||||
# target_websocket = await connection_manager.get_connection(target_device_id)
|
||||
# if target_websocket and target_websocket.client_state.name == "CONNECTED":
|
||||
@@ -290,7 +295,7 @@ async def process_cached_audio(device_id: str, target_device_id: str, serial_num
|
||||
await websocket.send_text(f"PROMPT_SOUND_URL:{success_audio_url}")
|
||||
session_logger.info(device_id, "device", f"留言已收到音频URL给设备 {device_id}")
|
||||
else:
|
||||
session_logger.warning(device_id, "device", f"设备 {device_id} 不在线,发送留言已收到音频URL失败")
|
||||
session_logger.warning(device_id, "device", f"设备 {device_id} 不在线,暂不发送留言已收到音频URL")
|
||||
# # # 目标设备不在线,保存到离线缓存
|
||||
# await offline_audio_cache.add_audio_url(target_device_id, audio_url)
|
||||
# session_logger.warning(device_id, "target", f"目标设备 {target_device_id} 不在线,保存音频URL到离线缓存")
|
||||
@@ -302,7 +307,19 @@ async def process_cached_audio(device_id: str, target_device_id: str, serial_num
|
||||
# else:
|
||||
# session_logger.warning(device_id, "device", f"设备 {device_id} 不在线,发送留言成功音频失败")
|
||||
except Exception as e:
|
||||
session_logger.error(device_id, "target", f"处理缓存音频时出错: {e}", exc_info=True)
|
||||
# 处理HTTPException异常
|
||||
if isinstance(e, HTTPException):
|
||||
session_logger.error(device_id, "target", f"保存音频文件时出错,返回HTTPException: {e}")
|
||||
websocket = await connection_manager.get_connection(device_id)
|
||||
if e.status_code == 404 or e.status_code == 400:
|
||||
if websocket and websocket.client_state.name == "CONNECTED":
|
||||
success_audio_url = f"http://{settings.server_host}:{settings.server_port}/assets/audio/save_audio_fail.mp3"
|
||||
await websocket.send_text(f"PROMPT_SOUND_URL:{success_audio_url}")
|
||||
session_logger.info(device_id, "device", f"留言已收到音频URL给设备 {device_id}")
|
||||
else:
|
||||
session_logger.warning(device_id, "device", f"设备 {device_id} 不在线,暂不发送留言已收到音频URL")
|
||||
else:
|
||||
session_logger.error(device_id, "target", f"处理缓存音频时出错: {e}", exc_info=True)
|
||||
finally:
|
||||
# 清除缓存
|
||||
await target_audio_cache.clear_audio_data(target_device_id)
|
||||
|
||||
Reference in New Issue
Block a user