You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.4 KiB
124 lines
4.4 KiB
"""
|
|
设备管理API端点
|
|
"""
|
|
import asyncio
|
|
from typing import Dict, List, Optional, Any
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from pydantic import BaseModel, Field
|
|
from app.services.device_service import device_service
|
|
from app.schemas.device import DeviceInfo, DeviceStatus, DeviceListResponse
|
|
from app.models.adb_models import (
|
|
UnifiedShellCommandRequest, ShellCommandTask, DeviceShellTask
|
|
)
|
|
from app.utils.structured_log import get_structured_logger
|
|
|
|
logger = get_structured_logger(__name__)
|
|
|
|
router = APIRouter()
|
|
|
|
@router.get("/devices", summary="获取所有设备")
|
|
async def get_devices():
|
|
"""获取所有设备列表(包括注册和自动发现的设备)"""
|
|
try:
|
|
devices = await device_service.get_all_devices()
|
|
return DeviceListResponse(
|
|
success=True,
|
|
message=f"成功获取 {len(devices)} 个设备",
|
|
data={
|
|
"devices": devices, # 直接返回字典列表,包含注册和自动发现的设备
|
|
"total": len(devices)
|
|
}
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"获取设备列表失败: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"获取设备列表失败: {str(e)}"
|
|
)
|
|
|
|
@router.get("/devices/{device_id}", summary="获取指定设备")
|
|
async def get_device(device_id: str):
|
|
"""获取指定设备信息(包括注册和自动发现的设备)"""
|
|
try:
|
|
device = await device_service.get_device(device_id)
|
|
if not device:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"设备 {device_id} 不存在"
|
|
)
|
|
return {
|
|
"success": True,
|
|
"message": "成功获取设备信息",
|
|
"data": 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.get("/devices/{device_id}/status", summary="获取设备状态")
|
|
async def get_device_status(device_id: str):
|
|
"""获取指定设备状态(包括注册和自动发现的设备)"""
|
|
try:
|
|
status_info = await device_service.get_device_status(device_id)
|
|
return {
|
|
"success": True,
|
|
"message": "成功获取设备状态",
|
|
"data": status_info
|
|
}
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=str(e)
|
|
)
|
|
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.post("/devices/execute-shell-commands", summary="批量执行Shell命令")
|
|
async def execute_shell_commands(request: UnifiedShellCommandRequest):
|
|
"""批量执行Shell命令
|
|
|
|
符合分层架构规范:
|
|
- API层 → DeviceService → CommandExecutor
|
|
- 统一通过DeviceService调用,保持架构一致性
|
|
"""
|
|
try:
|
|
# 通过DeviceService统一调用,符合分层架构规范
|
|
result = await device_service.execute_batch_shell_commands(request)
|
|
return result
|
|
|
|
except Exception as e:
|
|
logger.error(f"批量执行Shell命令失败: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"批量执行Shell命令失败: {str(e)}"
|
|
)
|
|
|
|
@router.get("/devices/{device_id}/adb-monitor-status", summary="获取ADB监控状态")
|
|
async def get_adb_monitor_status(device_id: str):
|
|
"""获取指定设备的ADB监控状态"""
|
|
try:
|
|
# 通过设备服务获取ADB监控状态
|
|
result = await device_service.execute_operation(
|
|
device_id=device_id,
|
|
operation="get_adb_monitor_status"
|
|
)
|
|
return {
|
|
"success": True,
|
|
"message": "成功获取ADB监控状态",
|
|
"data": result
|
|
}
|
|
except Exception as e:
|
|
logger.error(f"获取ADB监控状态失败: {e}")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"获取ADB监控状态失败: {str(e)}"
|
|
)
|