2026-03-17 01:14:40 +00:00
|
|
|
|
"""
|
|
|
|
|
|
Command Service - 指令管理服务
|
|
|
|
|
|
Provides CRUD operations for device command logs.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from app.models import CommandLog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_commands(
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
device_id: int | None = None,
|
Add WebSocket, multi API key, geocoding proxy, beacon map picker, and comprehensive bug fixes
- Multi API Key + permission system (read/write/admin) with SHA-256 hash
- WebSocket real-time push (location, alarm, device_status, attendance, bluetooth)
- Geocoding proxy endpoints for Amap POI search and reverse geocode
- Beacon modal map-based location picker with search and click-to-select
- GCJ-02 ↔ WGS-84 bidirectional coordinate conversion
- Data cleanup scheduler (configurable retention days)
- Fix GPS longitude sign inversion (course_status bit 11: 0=East, 1=West)
- Fix 2G CellID 2→3 bytes across all protocols (0x28, 0x2C, parser.py)
- Fix parser loop guards, alarm_source field length, CommandLog.sent_at
- Fix geocoding IMEI parameterization, require_admin import
- Improve API error messages for 422 validation errors
- Remove beacon floor/area fields (consolidated into name)
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-24 05:10:05 +00:00
|
|
|
|
command_type: str | None = None,
|
2026-03-17 01:14:40 +00:00
|
|
|
|
status: str | None = None,
|
|
|
|
|
|
page: int = 1,
|
|
|
|
|
|
page_size: int = 20,
|
|
|
|
|
|
) -> tuple[list[CommandLog], int]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取指令列表(分页)/ Get paginated command logs.
|
|
|
|
|
|
"""
|
|
|
|
|
|
query = select(CommandLog)
|
|
|
|
|
|
count_query = select(func.count(CommandLog.id))
|
|
|
|
|
|
|
|
|
|
|
|
if device_id is not None:
|
|
|
|
|
|
query = query.where(CommandLog.device_id == device_id)
|
|
|
|
|
|
count_query = count_query.where(CommandLog.device_id == device_id)
|
|
|
|
|
|
|
Add WebSocket, multi API key, geocoding proxy, beacon map picker, and comprehensive bug fixes
- Multi API Key + permission system (read/write/admin) with SHA-256 hash
- WebSocket real-time push (location, alarm, device_status, attendance, bluetooth)
- Geocoding proxy endpoints for Amap POI search and reverse geocode
- Beacon modal map-based location picker with search and click-to-select
- GCJ-02 ↔ WGS-84 bidirectional coordinate conversion
- Data cleanup scheduler (configurable retention days)
- Fix GPS longitude sign inversion (course_status bit 11: 0=East, 1=West)
- Fix 2G CellID 2→3 bytes across all protocols (0x28, 0x2C, parser.py)
- Fix parser loop guards, alarm_source field length, CommandLog.sent_at
- Fix geocoding IMEI parameterization, require_admin import
- Improve API error messages for 422 validation errors
- Remove beacon floor/area fields (consolidated into name)
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-24 05:10:05 +00:00
|
|
|
|
if command_type:
|
|
|
|
|
|
query = query.where(CommandLog.command_type == command_type)
|
|
|
|
|
|
count_query = count_query.where(CommandLog.command_type == command_type)
|
|
|
|
|
|
|
2026-03-17 01:14:40 +00:00
|
|
|
|
if status:
|
|
|
|
|
|
query = query.where(CommandLog.status == status)
|
|
|
|
|
|
count_query = count_query.where(CommandLog.status == status)
|
|
|
|
|
|
|
|
|
|
|
|
total_result = await db.execute(count_query)
|
|
|
|
|
|
total = total_result.scalar() or 0
|
|
|
|
|
|
|
|
|
|
|
|
offset = (page - 1) * page_size
|
|
|
|
|
|
query = query.order_by(CommandLog.created_at.desc()).offset(offset).limit(page_size)
|
|
|
|
|
|
result = await db.execute(query)
|
|
|
|
|
|
commands = list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
|
|
return commands, total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_command(
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
device_id: int,
|
|
|
|
|
|
command_type: str,
|
|
|
|
|
|
command_content: str,
|
|
|
|
|
|
server_flag: str = "badge_admin",
|
|
|
|
|
|
) -> CommandLog:
|
|
|
|
|
|
"""
|
|
|
|
|
|
创建指令记录 / Create a new command log entry.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
----------
|
|
|
|
|
|
db : AsyncSession
|
|
|
|
|
|
Database session.
|
|
|
|
|
|
device_id : int
|
|
|
|
|
|
Target device ID.
|
|
|
|
|
|
command_type : str
|
|
|
|
|
|
Type of command.
|
|
|
|
|
|
command_content : str
|
|
|
|
|
|
Command payload content.
|
|
|
|
|
|
server_flag : str
|
|
|
|
|
|
Server flag identifier.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
|
-------
|
|
|
|
|
|
CommandLog
|
|
|
|
|
|
The newly created command log.
|
|
|
|
|
|
"""
|
|
|
|
|
|
command = CommandLog(
|
|
|
|
|
|
device_id=device_id,
|
|
|
|
|
|
command_type=command_type,
|
|
|
|
|
|
command_content=command_content,
|
|
|
|
|
|
server_flag=server_flag,
|
|
|
|
|
|
status="pending",
|
|
|
|
|
|
)
|
|
|
|
|
|
db.add(command)
|
|
|
|
|
|
await db.flush()
|
|
|
|
|
|
await db.refresh(command)
|
|
|
|
|
|
return command
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_command(db: AsyncSession, command_id: int) -> CommandLog | None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
按ID获取指令 / Get command log by ID.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
----------
|
|
|
|
|
|
db : AsyncSession
|
|
|
|
|
|
Database session.
|
|
|
|
|
|
command_id : int
|
|
|
|
|
|
Command log primary key.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
|
-------
|
|
|
|
|
|
CommandLog | None
|
|
|
|
|
|
"""
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(CommandLog).where(CommandLog.id == command_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
return result.scalar_one_or_none()
|