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.

149 lines
5.2 KiB

from fastapi import APIRouter, HTTPException, status, BackgroundTasks
from typing import List, Dict, Any
from app.services.device_monitor_manager import device_monitor_manager
from app.utils.log import get_logger
logger = get_logger(__name__)
router = APIRouter()
@router.post("/usb-monitor/start", summary="启动USB设备监听")
async def start_usb_monitoring(auto_register: bool = True):
"""启动USB设备监听服务"""
try:
success = await device_monitor_manager.start_monitoring(auto_register=auto_register)
if success:
return {
"success": True,
"message": "USB设备监听服务已启动",
"auto_register": auto_register
}
else:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="启动USB监听服务失败"
)
except Exception as e:
logger.error(f"启动USB监听失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"启动USB监听失败: {str(e)}"
)
@router.post("/usb-monitor/stop", summary="停止USB设备监听")
async def stop_usb_monitoring():
"""停止USB设备监听服务"""
try:
device_monitor_manager.stop_monitoring()
return {
"success": True,
"message": "USB设备监听服务已停止"
}
except Exception as e:
logger.error(f"停止USB监听失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"停止USB监听失败: {str(e)}"
)
@router.get("/usb-monitor/status", summary="获取USB监听状态")
async def get_usb_monitor_status():
"""获取USB设备监听服务状态"""
try:
monitor_info = device_monitor_manager.get_monitor_info()
return monitor_info
except Exception as e:
logger.error(f"获取USB监听状态失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取USB监听状态失败: {str(e)}"
)
@router.get("/usb-monitor/devices", summary="获取已知USB设备列表")
async def get_known_usb_devices():
"""获取已知的USB设备列表"""
try:
devices = device_monitor_manager.get_known_devices()
return {
"success": True,
"devices": devices,
"count": len(devices)
}
except Exception as e:
logger.error(f"获取USB设备列表失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取USB设备列表失败: {str(e)}"
)
@router.post("/usb-monitor/scan", summary="扫描当前USB设备")
async def scan_usb_devices():
"""扫描当前已连接的USB设备"""
try:
await device_monitor_manager.scan_devices()
devices = device_monitor_manager.get_known_devices()
return {
"success": True,
"message": "USB设备扫描完成",
"devices": devices,
"count": len(devices)
}
except Exception as e:
logger.error(f"扫描USB设备失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"扫描USB设备失败: {str(e)}"
)
@router.get("/usb-monitor/device/{device_key}", summary="获取指定USB设备信息")
async def get_usb_device_info(device_key: str):
"""获取指定USB设备的详细信息"""
try:
device_info = device_monitor_manager.get_device_info(device_key)
if device_info:
return {
"success": True,
"device": device_info.to_dict()
}
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="设备未找到"
)
except HTTPException:
raise
except Exception as e:
logger.error(f"获取USB设备信息失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"获取USB设备信息失败: {str(e)}"
)
@router.post("/usb-monitor/auto-register/{device_key}", summary="手动注册指定USB设备")
async def register_usb_device(device_key: str):
"""手动注册指定的USB设备"""
try:
device_info = device_monitor_manager.get_device_info(device_key)
if not device_info:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="设备未找到"
)
# 触发自动注册
if device_monitor_manager.current_monitor:
await device_monitor_manager.current_monitor._auto_register_device(device_info)
return {
"success": True,
"message": f"设备 {device_key} 注册成功",
"device": device_info.to_dict()
}
except HTTPException:
raise
except Exception as e:
logger.error(f"注册USB设备失败: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"注册USB设备失败: {str(e)}"
)