2026-03-17 01:14:40 +00:00
|
|
|
|
"""
|
|
|
|
|
|
Devices Router - 设备管理接口
|
|
|
|
|
|
API endpoints for device CRUD operations and statistics.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
2026-03-17 01:14:40 +00:00
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from app.database import get_db
|
|
|
|
|
|
from app.schemas import (
|
|
|
|
|
|
APIResponse,
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
BatchDeviceCreateRequest,
|
|
|
|
|
|
BatchDeviceCreateResponse,
|
|
|
|
|
|
BatchDeviceCreateResult,
|
|
|
|
|
|
BatchDeviceDeleteRequest,
|
|
|
|
|
|
BatchDeviceUpdateRequest,
|
2026-03-17 01:14:40 +00:00
|
|
|
|
DeviceCreate,
|
|
|
|
|
|
DeviceResponse,
|
|
|
|
|
|
DeviceUpdate,
|
|
|
|
|
|
PaginatedList,
|
|
|
|
|
|
)
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
from app.config import settings
|
|
|
|
|
|
from app.extensions import limiter
|
2026-03-17 01:14:40 +00:00
|
|
|
|
from app.services import device_service
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/devices", tags=["Devices / 设备管理"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
|
|
"",
|
|
|
|
|
|
response_model=APIResponse[PaginatedList[DeviceResponse]],
|
|
|
|
|
|
summary="获取设备列表 / List devices",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def list_devices(
|
|
|
|
|
|
page: int = Query(default=1, ge=1, description="页码 / Page number"),
|
|
|
|
|
|
page_size: int = Query(default=20, ge=1, le=100, description="每页数量 / Items per page"),
|
|
|
|
|
|
status: str | None = Query(default=None, description="状态过滤 / Status filter (online/offline)"),
|
|
|
|
|
|
search: str | None = Query(default=None, description="搜索IMEI或名称 / Search by IMEI or name"),
|
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
|
):
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取设备列表,支持分页、状态过滤和搜索。
|
|
|
|
|
|
List devices with pagination, optional status filter, and search.
|
|
|
|
|
|
"""
|
|
|
|
|
|
devices, total = await device_service.get_devices(
|
|
|
|
|
|
db, page=page, page_size=page_size, status_filter=status, search=search
|
|
|
|
|
|
)
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
data=PaginatedList(
|
|
|
|
|
|
items=[DeviceResponse.model_validate(d) for d in devices],
|
|
|
|
|
|
total=total,
|
|
|
|
|
|
page=page,
|
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
|
total_pages=math.ceil(total / page_size) if total else 0,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
|
|
"/stats",
|
|
|
|
|
|
response_model=APIResponse[dict],
|
|
|
|
|
|
summary="获取设备统计 / Get device statistics",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def device_stats(db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取设备统计信息:总数、在线、离线。
|
|
|
|
|
|
Get device statistics: total, online, offline counts.
|
|
|
|
|
|
"""
|
|
|
|
|
|
stats = await device_service.get_device_stats(db)
|
|
|
|
|
|
return APIResponse(data=stats)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
|
|
|
|
|
"/imei/{imei}",
|
|
|
|
|
|
response_model=APIResponse[DeviceResponse],
|
|
|
|
|
|
summary="按IMEI查询设备 / Get device by IMEI",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def get_device_by_imei(imei: str, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
按IMEI号查询设备信息。
|
|
|
|
|
|
Get device details by IMEI number.
|
|
|
|
|
|
"""
|
|
|
|
|
|
device = await device_service.get_device_by_imei(db, imei)
|
|
|
|
|
|
if device is None:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail=f"Device with IMEI {imei} not found / 未找到IMEI为{imei}的设备")
|
|
|
|
|
|
return APIResponse(data=DeviceResponse.model_validate(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
@router.post(
|
|
|
|
|
|
"/batch",
|
|
|
|
|
|
response_model=APIResponse[BatchDeviceCreateResponse],
|
|
|
|
|
|
status_code=201,
|
|
|
|
|
|
summary="批量创建设备 / Batch create devices",
|
|
|
|
|
|
)
|
|
|
|
|
|
@limiter.limit(settings.RATE_LIMIT_WRITE)
|
|
|
|
|
|
async def batch_create_devices(request: Request, body: BatchDeviceCreateRequest, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
批量注册设备(最多500台),跳过IMEI重复的设备。
|
|
|
|
|
|
Batch register devices (up to 500). Skips devices with duplicate IMEIs.
|
|
|
|
|
|
"""
|
|
|
|
|
|
results = await device_service.batch_create_devices(db, body.devices)
|
|
|
|
|
|
created = sum(1 for r in results if r["success"])
|
|
|
|
|
|
failed = len(results) - created
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
message=f"Batch create: {created} created, {failed} failed",
|
|
|
|
|
|
data=BatchDeviceCreateResponse(
|
|
|
|
|
|
total=len(results),
|
|
|
|
|
|
created=created,
|
|
|
|
|
|
failed=failed,
|
|
|
|
|
|
results=[BatchDeviceCreateResult(**r) for r in results],
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put(
|
|
|
|
|
|
"/batch",
|
|
|
|
|
|
response_model=APIResponse[dict],
|
|
|
|
|
|
summary="批量更新设备 / Batch update devices",
|
|
|
|
|
|
)
|
|
|
|
|
|
@limiter.limit(settings.RATE_LIMIT_WRITE)
|
|
|
|
|
|
async def batch_update_devices(request: Request, body: BatchDeviceUpdateRequest, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
批量更新设备信息(名称、状态等),最多500台。
|
|
|
|
|
|
Batch update device fields (name, status, etc.) for up to 500 devices.
|
|
|
|
|
|
"""
|
|
|
|
|
|
results = await device_service.batch_update_devices(db, body.device_ids, body.update)
|
|
|
|
|
|
updated = sum(1 for r in results if r["success"])
|
|
|
|
|
|
failed = len(results) - updated
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
message=f"Batch update: {updated} updated, {failed} failed",
|
|
|
|
|
|
data={"total": len(results), "updated": updated, "failed": failed, "results": results},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post(
|
|
|
|
|
|
"/batch-delete",
|
|
|
|
|
|
response_model=APIResponse[dict],
|
|
|
|
|
|
summary="批量删除设备 / Batch delete devices",
|
|
|
|
|
|
)
|
|
|
|
|
|
@limiter.limit(settings.RATE_LIMIT_WRITE)
|
|
|
|
|
|
async def batch_delete_devices(
|
|
|
|
|
|
request: Request,
|
|
|
|
|
|
body: BatchDeviceDeleteRequest,
|
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
|
):
|
|
|
|
|
|
"""
|
|
|
|
|
|
批量删除设备(最多100台)。通过 POST body 传递 device_ids 列表。
|
|
|
|
|
|
Batch delete devices (up to 100). Pass device_ids in request body.
|
|
|
|
|
|
"""
|
|
|
|
|
|
results = await device_service.batch_delete_devices(db, body.device_ids)
|
|
|
|
|
|
deleted = sum(1 for r in results if r["success"])
|
|
|
|
|
|
failed = len(results) - deleted
|
|
|
|
|
|
return APIResponse(
|
|
|
|
|
|
message=f"Batch delete: {deleted} deleted, {failed} failed",
|
|
|
|
|
|
data={"total": len(results), "deleted": deleted, "failed": failed, "results": results},
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-17 01:14:40 +00:00
|
|
|
|
@router.get(
|
|
|
|
|
|
"/{device_id}",
|
|
|
|
|
|
response_model=APIResponse[DeviceResponse],
|
|
|
|
|
|
summary="获取设备详情 / Get device details",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def get_device(device_id: int, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
按ID获取设备详细信息。
|
|
|
|
|
|
Get device details by ID.
|
|
|
|
|
|
"""
|
|
|
|
|
|
device = await device_service.get_device(db, device_id)
|
|
|
|
|
|
if device is None:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail=f"Device {device_id} not found / 未找到设备{device_id}")
|
|
|
|
|
|
return APIResponse(data=DeviceResponse.model_validate(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post(
|
|
|
|
|
|
"",
|
|
|
|
|
|
response_model=APIResponse[DeviceResponse],
|
|
|
|
|
|
status_code=201,
|
|
|
|
|
|
summary="创建设备 / Create device",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def create_device(device_data: DeviceCreate, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
手动注册新设备。
|
|
|
|
|
|
Manually register a new device.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Check for duplicate IMEI
|
|
|
|
|
|
existing = await device_service.get_device_by_imei(db, device_data.imei)
|
|
|
|
|
|
if existing is not None:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400,
|
|
|
|
|
|
detail=f"Device with IMEI {device_data.imei} already exists / IMEI {device_data.imei} 已存在",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
device = await device_service.create_device(db, device_data)
|
|
|
|
|
|
return APIResponse(data=DeviceResponse.model_validate(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.put(
|
|
|
|
|
|
"/{device_id}",
|
|
|
|
|
|
response_model=APIResponse[DeviceResponse],
|
|
|
|
|
|
summary="更新设备信息 / Update device",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def update_device(
|
|
|
|
|
|
device_id: int, device_data: DeviceUpdate, db: AsyncSession = Depends(get_db)
|
|
|
|
|
|
):
|
|
|
|
|
|
"""
|
|
|
|
|
|
更新设备信息(名称、状态等)。
|
|
|
|
|
|
Update device information (name, status, etc.).
|
|
|
|
|
|
"""
|
|
|
|
|
|
device = await device_service.update_device(db, device_id, device_data)
|
|
|
|
|
|
if device is None:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail=f"Device {device_id} not found / 未找到设备{device_id}")
|
|
|
|
|
|
return APIResponse(data=DeviceResponse.model_validate(device))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete(
|
|
|
|
|
|
"/{device_id}",
|
|
|
|
|
|
response_model=APIResponse,
|
|
|
|
|
|
summary="删除设备 / Delete device",
|
|
|
|
|
|
)
|
|
|
|
|
|
async def delete_device(device_id: int, db: AsyncSession = Depends(get_db)):
|
|
|
|
|
|
"""
|
|
|
|
|
|
删除设备及其关联数据。
|
|
|
|
|
|
Delete a device and all associated records.
|
|
|
|
|
|
"""
|
|
|
|
|
|
deleted = await device_service.delete_device(db, device_id)
|
|
|
|
|
|
if not deleted:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail=f"Device {device_id} not found / 未找到设备{device_id}")
|
|
|
|
|
|
return APIResponse(message="Device deleted successfully / 设备删除成功")
|