fix(playback): 增强回放WebSocket钩子,确保首次接收AMR数据后初始化机器人图元

This commit is contained in:
xudan 2025-10-30 00:18:16 +08:00
parent 4a45782d5e
commit 8e7f00ec50
2 changed files with 31 additions and 8 deletions

View File

@ -44,7 +44,10 @@ export interface UsePlaybackWebSocketReturn {
changeSpeed: (speed: number) => void; // Placeholder for speed control
}
export function usePlaybackWebSocket(editorService: ShallowRef<EditorService | undefined>): UsePlaybackWebSocketReturn {
export function usePlaybackWebSocket(
editorService: ShallowRef<EditorService | undefined>,
onFirstAmrData?: () => void,
): UsePlaybackWebSocketReturn {
const client = shallowRef<WebSocket | null>(null);
const isConnected = ref(false);
const isPlaying = ref(false);
@ -54,6 +57,7 @@ export function usePlaybackWebSocket(editorService: ShallowRef<EditorService | u
const currentSceneId = ref<string | null>(null);
const sceneJson = ref<string | null>(null);
const locationData = ref<any[]>([]); // 新增:库位数据的响应式引用
let hasReceivedAmrData = false; // 新增:用于确保 onFirstAmrData 只调用一次的标志
const { calculateCenterPoint, jumpToPosition } = useViewState();
@ -104,6 +108,11 @@ export function usePlaybackWebSocket(editorService: ShallowRef<EditorService | u
(msg.data as RobotRealtimeInfo[]).forEach((robot) => {
latestRobotData.set(robot.id, robot);
});
// [关键修复] 仅在第一次收到AMR数据时触发回调
if (!hasReceivedAmrData) {
hasReceivedAmrData = true;
onFirstAmrData?.();
}
} else if (msg.type === 'LOCATION') {
// 新增:处理库位数据
locationData.value = msg.data;

View File

@ -107,7 +107,10 @@ const leftSiderEl = shallowRef<HTMLElement>();
const isPlaybackControllerVisible = ref<boolean>(true);
const selectedDate = ref<Dayjs>();
const playback = usePlaybackWebSocket(editor);
const playback = usePlaybackWebSocket(editor, async () => {
// []
await editor.value?.initRobots();
});
watch(mode, async (newMode) => {
if (newMode === 'live') {
@ -125,8 +128,8 @@ watch(mode, async (newMode) => {
watch(playback.sceneJson, async (newJson) => {
if (newJson) {
await editor.value?.load(newJson);
// []
await editor.value?.initRobots();
// [] onFirstAmrData
// await editor.value?.initRobots();
}
});
@ -205,8 +208,8 @@ watch(playback.currentTime, (newTimestamp) => {
watch(playback.sceneJson, async (newJson) => {
if (newJson) {
await editor.value?.load(newJson);
// []
await editor.value?.initRobots();
// [] onFirstAmrData
// await editor.value?.initRobots();
}
});
@ -258,7 +261,7 @@ const readScene = async () => {
* 采用 requestAnimationFrame 优化高频数据渲染确保动画流畅且不阻塞UI线程
*/
const monitorScene = async () => {
console.log(current.value?.id);
console.log('[实时模式] 开始监控场景, current:', current.value?.id);
client.value?.close();
// 使
const sidWithFloor = isMultiFloor.value ? `${props.sid}/${currentFloorIndex.value + 1}` : props.sid;
@ -278,6 +281,7 @@ const monitorScene = async () => {
* @param updates 需要更新的机器人数据数组
*/
const batchUpdateRobots = (updates: Array<{ id: string; data: RobotRealtimeInfo }>) => {
console.log('[实时模式] batchUpdateRobots 被调用,更新数量:', updates.length);
if (!editor.value || updates.length === 0) return;
// pen
@ -299,6 +303,7 @@ const monitorScene = async () => {
} = data;
// 1.
console.log('[实时模式] 更新机器人业务数据:', id, { x, y, active, angle });
editor.value?.updateRobot(id, {
...rest,
isCharging: isCharging as any,
@ -338,6 +343,7 @@ const monitorScene = async () => {
penUpdatePayload.y = y - 60;
penUpdatePayload.visible = true;
penUpdatePayload.locked = LockState.None;
console.log('[实时模式] 设置机器人可见性:', id, { x: x - 60, y: y - 60, visible: true });
}
if (angle != null) {
penUpdatePayload.rotate = -angle + 180;
@ -423,6 +429,7 @@ const monitorScene = async () => {
*/
ws.onmessage = (e) => {
const data = JSON.parse(e.data || '{}');
console.log('[实时模式] 收到WebSocket数据:', data);
// type=99
if (data.type === 99) {
@ -431,6 +438,7 @@ const monitorScene = async () => {
} else {
//
const robotData = data as RobotRealtimeInfo;
console.log('[实时模式] 收到机器人数据:', robotData.id, { x: robotData.x, y: robotData.y, active: robotData.active });
latestRobotData.set(robotData.id, robotData);
}
};
@ -541,9 +549,14 @@ onMounted(() => {
onMounted(async () => {
console.log('Current route in movement-supervision.vue:', window.location.href);
if (mode.value === 'live') {
console.log('[实时模式] 初始化实时模式');
await readScene();
// []
console.log('[实时模式] 初始化机器人图元');
await editor.value?.initRobots();
await monitorScene();
} else {
console.log('[回放模式] 初始化回放模式');
playback.connect(props.sid);
// []
const debugDate = dayjs('2025-09-28 09:00:00');
@ -551,7 +564,8 @@ onMounted(async () => {
playback.seek(debugDate.valueOf());
}
await editor.value?.initRobots();
// [] onFirstAmrData
// await editor.value?.initRobots();
storageLocationService.value?.startMonitoring({ interval: 1 });
//