from fastapi import APIRouter, HTTPException, status from typing import List from app.core.device.manager import device_manager from app.schemas.device import ( Device, DeviceCreate, DeviceUpdate, DeviceList, DeviceResponse, DeviceStatusResponse ) from app.utils.log import get_logger logger = get_logger(__name__) router = APIRouter() @router.get("/devices", response_model=DeviceList, summary="获取设备列表") async def get_devices(): """获取所有注册的设备列表""" try: devices = await device_manager.get_all_devices() return DeviceList( 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.post("/devices", 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.get("/devices/{device_id}", response_model=DeviceResponse, summary="获取设备信息") async def get_device(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} 不存在" ) return DeviceResponse( 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.put("/devices/{device_id}", 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}", 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: devices = await device_manager.get_devices_by_protocol(protocol_type) return DeviceList( devices=devices, total=len(devices) ) except Exception as e: logger.error(f"按协议类型获取设备失败: {protocol_type}, 错误: {e}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"获取设备列表失败: {str(e)}" )