From 7f6997721537794a02c3a4f824a24a39fb2a1cdf Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Tue, 3 Jun 2025 15:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0--=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=BB=9F=E8=AE=A1=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/events.py | 23 +++++++++++++++++++++++ app/core/config.py | 8 ++++---- app/crud/event.py | 19 ++++++++++++++++++- app/services/event_sync_service.py | 3 ++- app/services/websocket_service.py | 1 - 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/api/v1/endpoints/events.py b/app/api/v1/endpoints/events.py index f1e2001..50d5262 100644 --- a/app/api/v1/endpoints/events.py +++ b/app/api/v1/endpoints/events.py @@ -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 + }) diff --git a/app/core/config.py b/app/core/config.py index c45f1ce..e6813de 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -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" # 测试数据库 diff --git a/app/crud/event.py b/app/crud/event.py index d0bd9af..0c773d0 100644 --- a/app/crud/event.py +++ b/app/crud/event.py @@ -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}") diff --git a/app/services/event_sync_service.py b/app/services/event_sync_service.py index 41bed27..e8c14a9 100644 --- a/app/services/event_sync_service.py +++ b/app/services/event_sync_service.py @@ -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"] # 新增机器人消息 diff --git a/app/services/websocket_service.py b/app/services/websocket_service.py index 017d2a6..8988916 100644 --- a/app/services/websocket_service.py +++ b/app/services/websocket_service.py @@ -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)