2024-05-06 11:39:45 +08:00
|
|
|
from ..schemas.user_schema import *
|
2024-05-01 17:18:30 +08:00
|
|
|
from ..dependencies.logger import get_logger
|
2024-05-22 17:26:29 +08:00
|
|
|
from ..dependencies.tts import get_tts
|
2024-05-16 13:28:47 +08:00
|
|
|
from ..models import User, Hardware, Audio
|
2024-05-01 17:18:30 +08:00
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from fastapi import HTTPException, status
|
2024-05-22 17:26:29 +08:00
|
|
|
from pydub import AudioSegment
|
|
|
|
import numpy as np
|
|
|
|
import io
|
2024-05-01 17:18:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
#依赖注入获取logger
|
|
|
|
logger = get_logger()
|
|
|
|
|
2024-05-22 17:26:29 +08:00
|
|
|
#依赖注入获取tts
|
2024-05-24 15:08:55 +08:00
|
|
|
tts = get_tts("OPENVOICE")
|
2024-05-01 17:18:30 +08:00
|
|
|
|
|
|
|
#创建用户
|
|
|
|
async def create_user_handler(user:UserCrateRequest, db: Session):
|
|
|
|
new_user = User(created_at=datetime.now(), open_id=user.open_id, username=user.username, avatar_id=user.avatar_id, tags=user.tags, persona=user.persona)
|
|
|
|
try:
|
|
|
|
db.add(new_user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_user)
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
user_create_data = UserCrateData(user_id=new_user.id, createdAt=new_user.created_at.isoformat())
|
|
|
|
return UserCrateResponse(status="success", message="创建用户成功", data=user_create_data)
|
|
|
|
|
|
|
|
|
|
|
|
#更新用户信息
|
|
|
|
async def update_user_handler(user_id:int, user:UserUpdateRequest, db: Session):
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
existing_user.open_id = user.open_id
|
|
|
|
existing_user.username = user.username
|
|
|
|
existing_user.avatar_id = user.avatar_id
|
|
|
|
existing_user.tags = user.tags
|
|
|
|
existing_user.persona = user.persona
|
|
|
|
try:
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
user_update_data = UserUpdateData(updatedAt=datetime.now().isoformat())
|
|
|
|
return UserUpdateResponse(status="success", message="更新用户信息成功", data=user_update_data)
|
|
|
|
|
|
|
|
|
|
|
|
#查询用户信息
|
|
|
|
async def get_user_handler(user_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
user_query_data = UserQueryData(open_id=existing_user.open_id, username=existing_user.username, avatar_id=existing_user.avatar_id, tags=existing_user.tags, persona=existing_user.persona)
|
|
|
|
return UserQueryResponse(status="success", message="查询用户信息成功", data=user_query_data)
|
|
|
|
|
|
|
|
|
|
|
|
#删除用户
|
|
|
|
async def delete_user_handler(user_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
try:
|
|
|
|
db.delete(existing_user)
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
user_delete_data = UserDeleteData(deletedAt=datetime.now().isoformat())
|
|
|
|
return UserDeleteResponse(status="success", message="删除用户成功", data=user_delete_data)
|
|
|
|
|
|
|
|
|
|
|
|
#绑定硬件
|
|
|
|
async def bind_hardware_handler(hardware, db: Session):
|
|
|
|
new_hardware = Hardware(mac=hardware.mac, user_id=hardware.user_id, firmware=hardware.firmware, model=hardware.model)
|
|
|
|
try:
|
|
|
|
db.add(new_hardware)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_hardware)
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_bind_data = HardwareBindData(hardware_id=new_hardware.id, bindedAt=datetime.now().isoformat())
|
|
|
|
return HardwareBindResponse(status="success", message="绑定硬件成功", data=hardware_bind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#解绑硬件
|
|
|
|
async def unbind_hardware_handler(hardware_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
try:
|
|
|
|
db.delete(existing_hardware)
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_unbind_data = HardwareUnbindData(unbindedAt=datetime.now().isoformat())
|
|
|
|
return HardwareUnbindResponse(status="success", message="解绑硬件成功", data=hardware_unbind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#硬件换绑
|
|
|
|
async def change_bind_hardware_handler(hardware_id, user, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
existing_hardware.user_id = user.user_id
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_change_bind_data = HardwareChangeBindData(bindChangedAt=datetime.now().isoformat())
|
|
|
|
return HardwareChangeBindResponse(status="success", message="硬件换绑成功", data=hardware_change_bind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#硬件信息更新
|
|
|
|
async def update_hardware_handler(hardware_id, hardware, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
existing_hardware.mac = hardware.mac
|
|
|
|
existing_hardware.firmware = hardware.firmware
|
|
|
|
existing_hardware.model = hardware.model
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_update_data = HardwareUpdateData(updatedAt=datetime.now().isoformat())
|
|
|
|
return HardwareUpdateResponse(status="success", message="硬件信息更新成功", data=hardware_update_data)
|
|
|
|
|
|
|
|
|
|
|
|
#查询硬件
|
|
|
|
async def get_hardware_handler(hardware_id, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
hardware_query_data = HardwareQueryData(mac=existing_hardware.mac, user_id=existing_hardware.user_id, firmware=existing_hardware.firmware, model=existing_hardware.model)
|
2024-05-16 13:28:47 +08:00
|
|
|
return HardwareQueryResponse(status="success", message="查询硬件信息成功", data=hardware_query_data)
|
|
|
|
|
|
|
|
|
|
|
|
#用户上传音频
|
|
|
|
async def upload_audio_handler(user_id, audio, db):
|
|
|
|
try:
|
2024-05-22 17:26:29 +08:00
|
|
|
audio_data = await audio.read()
|
2024-05-23 15:19:21 +08:00
|
|
|
emb_data = tts.audio2emb(np.frombuffer(AudioSegment.from_file(io.BytesIO(audio_data), format="mp3").raw_data, dtype=np.int32),rate=44100,vad=True)
|
|
|
|
out = io.BytesIO()
|
|
|
|
np.save(out, emb_data)
|
|
|
|
out.seek(0)
|
|
|
|
emb_binary = out.read()
|
|
|
|
new_audio = Audio(user_id=user_id, audio_data=audio_data,emb_data=emb_binary) #创建音频
|
2024-05-16 13:28:47 +08:00
|
|
|
db.add(new_audio)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_audio)
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
audio_upload_data = AudioUploadData(audio_id=new_audio.id, uploadedAt=datetime.now().isoformat())
|
|
|
|
return AudioUploadResponse(status="success", message="用户上传音频成功", data=audio_upload_data)
|
|
|
|
|
|
|
|
|
|
|
|
#用户更新音频
|
|
|
|
async def update_audio_handler(audio_id, audio_file, db):
|
|
|
|
try:
|
|
|
|
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
|
|
|
if existing_audio is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
2024-05-22 17:26:29 +08:00
|
|
|
audio_data = await audio_file.read()
|
|
|
|
raw_data = AudioSegment.from_file(io.BytesIO(audio_data), format="mp3").raw_data
|
|
|
|
emb_data = tts.audio2emb(np.frombuffer(raw_data, dtype=np.int32),rate=44100,vad=True).tobytes()
|
2024-05-16 13:28:47 +08:00
|
|
|
existing_audio.audio_data = audio_data
|
2024-05-22 17:26:29 +08:00
|
|
|
existing_audio.emb_data = emb_data
|
2024-05-16 13:28:47 +08:00
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
audio_update_data = AudioUpdateData(updatedAt=datetime.now().isoformat())
|
|
|
|
return AudioUpdateResponse(status="success", message="用户更新音频成功", data=audio_update_data)
|
|
|
|
|
|
|
|
|
|
|
|
#用户查询音频
|
|
|
|
async def download_audio_handler(audio_id, db):
|
|
|
|
try:
|
|
|
|
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_audio is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
|
|
|
audio_data = existing_audio.audio_data
|
|
|
|
return audio_data
|
|
|
|
|
|
|
|
|
|
|
|
#用户删除音频
|
|
|
|
async def delete_audio_handler(audio_id, db):
|
|
|
|
try:
|
|
|
|
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
2024-05-23 09:57:53 +08:00
|
|
|
existing_user = db.query(User).filter(User.selected_audio_id == audio_id).first()
|
2024-05-16 13:28:47 +08:00
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_audio is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
|
|
|
try:
|
2024-05-23 09:57:53 +08:00
|
|
|
if existing_user.selected_audio_id == audio_id:
|
|
|
|
existing_user.selected_audio_id = None
|
2024-05-16 13:28:47 +08:00
|
|
|
db.delete(existing_audio)
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
audio_delete_data = AudioDeleteData(deletedAt=datetime.now().isoformat())
|
2024-05-23 09:57:53 +08:00
|
|
|
return AudioDeleteResponse(status="success", message="用户删除音频成功", data=audio_delete_data)
|
|
|
|
|
|
|
|
|
|
|
|
#用户绑定音频
|
|
|
|
async def bind_audio_handler(bind_req, db):
|
|
|
|
try:
|
|
|
|
existing_user = db.query(User).filter(User.id == bind_req.user_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
try:
|
|
|
|
existing_user.selected_audio_id = bind_req.audio_id
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
audio_bind_data = AudioBindData(bindedAt=datetime.now().isoformat())
|
|
|
|
return AudioBindResponse(status="success", message="用户绑定音频成功", data=audio_bind_data)
|