Browse Source

重构代码

origin/hotfix/hlk-flight
root 4 months ago
parent
commit
9b323b8bcc
  1. 130
      app/api/v1/endpoints/devices.py

130
app/api/v1/endpoints/devices.py

@ -1,19 +1,15 @@
"""
设备管理API
提供统一的设备管理接口包括设备注册自动发现操作控制等功能
提供统一的设备管理接口包括设备操作控制ADB管理等功能
注意设备注册更新注销等操作是自动化的不对外提供API
"""
from fastapi import APIRouter, HTTPException, status
from typing import List, Optional, Dict, Any
from app.services.device_service import device_service
from app.core.device.manager import device_manager
from app.schemas.adb import (
ClickRequest, InputRequest, ScreenshotResponse,
InstallRequest, InstallResponse, LogcatRequest, LogcatResponse
)
from app.schemas.device import (
Device, DeviceCreate, DeviceUpdate, DeviceList,
DeviceResponse, DeviceStatusResponse
)
from app.utils.log import get_logger
logger = get_logger(__name__)
@ -254,128 +250,6 @@ async def clear_device_cache():
detail=f"清除设备缓存失败: {str(e)}"
)
# ==================== 设备注册管理 ====================
@router.post("/devices/register", response_model=DeviceResponse, summary="注册新设备")
async def register_device(device: DeviceCreate):
"""注册新设备"""
try:
new_device = await device_manager.register_device(device)
logger.info(f"设备注册成功: {device.device_id}")
return DeviceResponse(
success=True,
message="设备注册成功",
data=new_device
)
except ValueError as e:
logger.warning(f"设备注册失败: {e}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=str(e)
)
except Exception as e:
logger.error(f"设备注册异常: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"设备注册失败: {str(e)}"
)
@router.put("/devices/{device_id}/update", response_model=DeviceResponse, summary="更新设备信息")
async def update_device(device_id: str, update_data: DeviceUpdate):
"""更新设备信息"""
try:
updated_device = await device_manager.update_device_info(device_id, update_data)
if not updated_device:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"设备 {device_id} 不存在"
)
logger.info(f"设备信息更新成功: {device_id}")
return DeviceResponse(
success=True,
message="设备信息更新成功",
data=updated_device
)
except HTTPException:
raise
except Exception as e:
logger.error(f"更新设备信息失败: {device_id}, 错误: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"更新设备信息失败: {str(e)}"
)
@router.delete("/devices/{device_id}/unregister", response_model=DeviceResponse, summary="注销设备")
async def unregister_device(device_id: str):
"""注销设备"""
try:
success = await device_manager.unregister_device(device_id)
if not success:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"设备 {device_id} 不存在"
)
logger.info(f"设备注销成功: {device_id}")
return DeviceResponse(
success=True,
message="设备注销成功",
data=None
)
except HTTPException:
raise
except Exception as e:
logger.error(f"注销设备失败: {device_id}, 错误: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"注销设备失败: {str(e)}"
)
@router.get("/devices/{device_id}/status", response_model=DeviceStatusResponse, summary="获取设备状态")
async def get_device_status(device_id: str):
"""获取设备当前状态"""
try:
device = await device_manager.get_device(device_id)
if not device:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"设备 {device_id} 不存在"
)
from datetime import datetime
return DeviceStatusResponse(
device_id=device_id,
status=device.status,
timestamp=device.updated_at
)
except HTTPException:
raise
except Exception as e:
logger.error(f"获取设备状态失败: {device_id}, 错误: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取设备状态失败: {str(e)}"
)
@router.get("/devices/protocol/{protocol_type}", response_model=DeviceList, summary="按协议类型获取设备")
async def get_devices_by_protocol(protocol_type: str):
"""按协议类型获取设备列表"""
try:
all_devices = await device_manager.get_all_devices()
filtered_devices = [d for d in all_devices if d.protocol_type == protocol_type]
return DeviceList(
devices=filtered_devices,
total=len(filtered_devices)
)
except Exception as e:
logger.error(f"按协议类型获取设备失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"按协议类型获取设备失败: {str(e)}"
)
# ==================== ADB管理功能 ====================
@router.get("/adb/server/info", summary="获取ADB服务器信息")

Loading…
Cancel
Save