diff --git a/app/services/event_sync_service.py b/app/services/event_sync_service.py index 0c5df66..3b7d843 100644 --- a/app/services/event_sync_service.py +++ b/app/services/event_sync_service.py @@ -59,8 +59,9 @@ class EventSyncService: # 修改在线状态 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"] + await self._update_robot_online_status(self.robot_dict[message["robotId"]]) + # 新增机器人消息 else: await self._add_robot(message) @@ -88,13 +89,19 @@ class EventSyncService: """更新机器人在线状态""" try: async with async_session() as session: + # print(robot.onlineStatus) update_stmt = ( update(Robot).where(Robot.robotId == robot.robotId).values( - onlineStatus = robot.onlineStatus + onlineStatus = robot.onlineStatus, + updateTime = datetime.now() ) ) + # print(repr(update_stmt)) await session.execute(update_stmt) - await session.commit() + if await session.commit() is None: # 判断提交结果 + print("更新机器人状态成功") + else: + print("更新机器人状态未生效") except Exception as e: print(f"更新机器人在线状态失败: {e}") diff --git a/app/services/websocket_service.py b/app/services/websocket_service.py index 9e88ad7..78cc9a9 100644 --- a/app/services/websocket_service.py +++ b/app/services/websocket_service.py @@ -25,6 +25,9 @@ class WebSocketClient: timestamp = int(datetime.now().timestamp() * 1000) user_id = f"{self.ws_config.get('account')}_{timestamp}_{self.ws_config.get('tenantInfoId')}" return f"wss://{self.ws_config.get('url')}{user_id}" + + # 测试用websocket地址 + # return "ws://10.0.0.202:8788" async def save_message(self, message: str): """保存接收到的消息到文件""" @@ -69,7 +72,7 @@ class WebSocketClient: """处理接收到的消息""" try: # 保存消息 - await self.save_message(message) + # await self.save_message(message) # 尝试解析JSON消息 message_dict = json.loads(message) @@ -82,7 +85,7 @@ class WebSocketClient: # 机器人状态消息 elif self._process_robot_status(message_dict): - # print("处理机器人状态消息...") + print("处理机器人状态消息...") # print(f"{datetime.now()}收到消息: {message}") # 只打印前100个字符 await self.event_sync_service.sync_robot_status(message_dict) diff --git a/change_log.md b/change_log.md index 1ea564b..6796ae8 100644 --- a/change_log.md +++ b/change_log.md @@ -49,3 +49,5 @@ - 修改 requirement.txt文件,删除不必要的头文件, 修改数据库配置. - 更新 更新paddleocr模型 +- 修改 run_websocket中同步机器人状态逻辑. +- 添加 测试用websocket地址 diff --git a/logs/websocket/websocket.pid b/logs/websocket/websocket.pid deleted file mode 100644 index c9ac1a0..0000000 --- a/logs/websocket/websocket.pid +++ /dev/null @@ -1 +0,0 @@ -433729 diff --git a/start_sync_robot.sh b/start_sync_robot.sh index f3048ef..61c2916 100644 --- a/start_sync_robot.sh +++ b/start_sync_robot.sh @@ -17,4 +17,6 @@ fi nohup python run_sync_robot.py > "$LOG_FILE" 2>&1 & # 保存进程id -echo $! > "$LOG_DIR/robot.pid" \ No newline at end of file +echo $! > "$LOG_DIR/robot.pid" +echo "WebSocket客户端已启动,日志保存在: $LOG_FILE" +echo "进程ID: $(cat $LOG_DIR/robot.pid)" \ No newline at end of file