添加--添加获取机器人统计信息接口

This commit is contained in:
haotian 2025-06-03 15:02:00 +08:00
parent 60a05121cb
commit 7f69977215
5 changed files with 47 additions and 7 deletions

View File

@ -3,6 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Body
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.database import get_db
from app.crud.event import event
from app.models.models import Robot
from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery, BackStageEvent, BackStageEventDto, BackStageEventDetail, EditTemperatureDto, OcrAlertMessage, OcrAlertMessageDto
from app.util.httpResponse import BaseResponse
@ -183,6 +184,28 @@ async def get_alert_count(
):
alert_count = await event.get_alert_count(db)
return BaseResponse(code=200, msg="success", data=alert_count)
@router.get("/events/robotST", response_model=BaseResponse[dict])
async def get_robot_st(
db: AsyncSession = Depends(get_db)
):
# 机器人总数
t_count = await event.get_robot_count(db, status=[])
# 在线总数
online_count = await event.get_robot_count(db, status=[Robot.onlineStatus=="1", Robot.status=="0"])
# 离线总数
offline_count = await event.get_robot_count(db, status=[Robot.onlineStatus=="2", Robot.status=="0"])
# 故障总数
disable_count = await event.get_robot_count(db, status=[Robot.status=="1"])
return BaseResponse(code=200, msg="success",
data = {
"t_count": t_count,
"online_count": online_count,
"offline_count": offline_count,
"disable_count": disable_count
})

View File

@ -2,12 +2,12 @@ from pydantic_settings import BaseSettings
class Settings(BaseSettings):
# 数据库配置
# DB_HOST: str = "14.103.162.172"
DB_HOST: str = "10.0.0.17"
DB_HOST: str = "14.103.162.172"
# DB_HOST: str = "10.0.0.17"
DB_PORT: int = 3306
DB_USER: str = "root"
# DB_PASSWORD: str = "dnxxkj"
DB_PASSWORD: str = "root"
DB_PASSWORD: str = "dnxxkj"
# DB_PASSWORD: str = "root"
DB_NAME: str = "kangda"
# DB_NAME: str = "kangda_test" # 测试数据库

View File

@ -3,7 +3,7 @@ from sqlalchemy import select, and_, or_, update, bindparam, func
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.crud.base import CRUDBase
from app.models.models import Event, Image, Temperature, Message
from app.models.models import Event, Image, Temperature, Message, Robot
from app.schemas.event import EventUpdate, EventQuery, BackStageEvent, BackStageEventDto, BackStageEventDetail, EditTemperatureDto, OcrAlertMessage, OcrAlertMessageDto
from pydantic import VERSION as PYDANTIC_VERSION
@ -339,6 +339,23 @@ class CRUDEvent(CRUDBase[Event, EventUpdate, EventUpdate]):
return result.scalar()
except Exception as e:
print(f"{e}")
async def get_robot_count(
self,
db: AsyncSession,
*, # 关键字参数分隔符,代表后面的参数一定要用关键字指定
status: list
):
try:
query_stmt = (
select(func.count(Robot.robotId))
.where(*status)
)
result = await db.execute(query_stmt)
return result.scalar()
except Exception as e:
print(f"{e}")

View File

@ -48,7 +48,7 @@ class EventSyncService:
# 同步机器人状态
async def sync_robot_status(self, message: dict):
"""同步机器人状态"""
print(f"开始同步机器人状态")
# 机器人列表为空时请求数据库
if self.robot_dict is None:
@ -58,6 +58,7 @@ class EventSyncService:
if message["robotId"] in self.robot_dict:
# 修改在线状态
if self.robot_dict[message["robotId"]].onlineStatus != message["onlineStatus"]:
print(f"开始同步机器人状态")
await self._update_robot_online_status(self.robot_dict[message["robotId"]])
self.robot_dict[message["robotId"]].onlineStatus = message["onlineStatus"]
# 新增机器人消息

View File

@ -82,7 +82,6 @@ class WebSocketClient:
# 机器人状态消息
elif self._process_robot_status(message_dict):
print("处理机器人状态消息...")
await self.event_sync_service.sync_robot_status(message_dict)
# await run_sync_event(t)