添加同步单个eventId, 修改websocket判断同步条件
This commit is contained in:
parent
78254fa071
commit
2bce8140d5
@ -2,7 +2,7 @@ from fastapi import FastAPI, WebSocket
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from app.api.v1.api import api_router
|
||||
from app.core.config import settings
|
||||
from app.services.websocket_service import websocket_service
|
||||
# from app.services.websocket_service import websocket_service
|
||||
|
||||
app = FastAPI(
|
||||
title="温度数据采集系统",
|
||||
|
||||
@ -40,7 +40,7 @@ class Event(Base):
|
||||
resolution = Column(String(10))
|
||||
originX = Column(String(20))
|
||||
originY = Column(String(20))
|
||||
imgList = Column(String(20))
|
||||
imgList = Column(String(500))
|
||||
robotType = Column(String(5))
|
||||
eventFloor = Column(String(10))
|
||||
floorName = Column(String(10))
|
||||
|
||||
@ -35,6 +35,39 @@ class EventSyncService:
|
||||
await self._update_event_details(new_events)
|
||||
|
||||
print("事件同步完成")
|
||||
|
||||
|
||||
# 同步单个事件
|
||||
async def sync_event(self, eventId: str):
|
||||
"""同步单个事件"""
|
||||
print(f"开始同步事件: {eventId}")
|
||||
|
||||
# 获取事件详情, 为了兼容get_event_list_detail的参数格式
|
||||
event_details = self.kangda.get_event_list_detail([{"eventId": eventId}])
|
||||
|
||||
async with async_session() as session:
|
||||
for detail in event_details:
|
||||
if not detail:
|
||||
continue
|
||||
try:
|
||||
|
||||
# 更新事件信息
|
||||
new_event = Event(**detail)
|
||||
session.add(new_event)
|
||||
await session.commit()
|
||||
|
||||
# 保存图片信息
|
||||
image_list = await self._save_images(session, detail)
|
||||
|
||||
# 保存ocr温度信息
|
||||
await self._ocr_images(session, image_list)
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
print(f"事件: {eventId} 同步失败: {str(e)}")
|
||||
|
||||
print(f"事件: {eventId} 同步完成")
|
||||
|
||||
|
||||
|
||||
async def _get_new_events(self, event_list: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||
"""获取新事件列表"""
|
||||
@ -134,6 +167,8 @@ class EventSyncService:
|
||||
return image_list
|
||||
|
||||
async def _ocr_images(self, session: AsyncSession, image_list:Dict[str, Any]):
|
||||
if image_list is None:
|
||||
return
|
||||
|
||||
for image in image_list:
|
||||
result = self.ocr.image_inference(image.localPath)
|
||||
@ -162,12 +197,16 @@ async def run_sync():
|
||||
# 等待5分钟
|
||||
await asyncio.sleep(6000)
|
||||
|
||||
async def run_sync_once():
|
||||
async def run_sync_event(eventId: str):
|
||||
service = EventSyncService()
|
||||
try:
|
||||
await service.sync_events()
|
||||
await service.sync_event(eventId)
|
||||
except Exception as e:
|
||||
print(f"同步过程出错: {str(e)}")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(run_sync())
|
||||
# asyncio.run(run_sync())
|
||||
|
||||
asyncio.run(run_sync_event("c6bcc9191ded458f8c52b9e486cc4f60"))
|
||||
@ -5,7 +5,8 @@ from datetime import datetime
|
||||
import websockets
|
||||
import aiofiles
|
||||
from app.kangdaApi.parseConfig import parse_config
|
||||
from app.services.event_sync_service import run_sync_once
|
||||
from app.services.event_sync_service import run_sync_event
|
||||
from app.services.event_sync_service import EventSyncService
|
||||
|
||||
class WebSocketClient:
|
||||
def __init__(self):
|
||||
@ -15,7 +16,9 @@ class WebSocketClient:
|
||||
self.reconnect_delay = 5 # 重连延迟(秒)
|
||||
self.is_connected = False
|
||||
self.file_lock = asyncio.Lock() # 文件锁
|
||||
self.event_sync_service = EventSyncService()
|
||||
os.makedirs(self.message_dir, exist_ok=True)
|
||||
|
||||
|
||||
def get_ws_url(self) -> str:
|
||||
"""生成WebSocket连接URL"""
|
||||
@ -74,7 +77,10 @@ class WebSocketClient:
|
||||
# 检查是否需要触发事件同步
|
||||
if self._should_trigger_sync(message_dict):
|
||||
print("触发事件同步...")
|
||||
await run_sync_once()
|
||||
await self.event_sync_service.sync_event(message_dict.get("eventId"))
|
||||
|
||||
|
||||
# await run_sync_event(t)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
print("收到非JSON格式消息")
|
||||
@ -86,7 +92,8 @@ class WebSocketClient:
|
||||
try:
|
||||
# 示例:当消息类型为"event_update"时触发同步
|
||||
# 根据实际业务需求修改判断条件
|
||||
return message.get("type") == "event_update"
|
||||
return message.get("eventId", None) is not None
|
||||
# return message.("type") == "event_update"
|
||||
except Exception as e:
|
||||
print(f"检查触发条件时出错: {str(e)}")
|
||||
return False
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
from app.services.event_sync_service import run_sync
|
||||
from app.services.event_sync_service import run_sync, run_sync_event
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("启动事件同步服务...")
|
||||
asyncio.run(run_sync())
|
||||
# asyncio.run(run_sync())
|
||||
asyncio.run(run_sync_event("c6bcc9191ded458f8c52b9e486cc4f60"))
|
||||
Loading…
Reference in New Issue
Block a user