feat: KKS P240/P241 蓝牙工牌管理系统初始提交
FastAPI + SQLAlchemy + asyncio TCP 服务器,支持设备管理、实时定位、 告警、考勤打卡、蓝牙记录、指令下发、TTS语音播报等功能。
This commit is contained in:
110
app/services/command_service.py
Normal file
110
app/services/command_service.py
Normal file
@@ -0,0 +1,110 @@
|
||||
"""
|
||||
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,
|
||||
command_type: str | None = None,
|
||||
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)
|
||||
|
||||
if command_type:
|
||||
query = query.where(CommandLog.command_type == command_type)
|
||||
count_query = count_query.where(CommandLog.command_type == command_type)
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user