guonan
2025-07-03 f37845dd0a787dd42bf6c72e923433f30fcd8cc3
src/views/left/KGSimOption/RealTimeSimulation.vue
@@ -81,7 +81,8 @@
import { useSimStore } from "@/store/simulation.js";
import { EventBus } from "@/eventBus"; // 引入事件总线
import { getDeviceInfoSHG, getYLJData } from "@/api/hpApi";
import { getSimStart, getSimDataById } from "@/api/trApi";
import { getSimStart, getSimDataById, getSimresult } from "@/api/trApi";
import { ControlSchemeType } from "@/assets/js/lib-pixelstreamingfrontend.esm";
// 获取 Store 实例
const simAPIStore = SimAPIStore();
@@ -219,14 +220,15 @@
// 实时模拟定时器
let pollingInterval = null;
// 用于记录上次数据条数
let lastDataLength = 0;
let pollingTimer = null; // 用于保存定时器引用
async function startPlay() {
  // 开始模拟前需要先保存方案
  updateSelectedGauges();
  formData.geom = props.selectedArea;
  // 保存方案
  const resApi = await simAPIStore.addSimCheme(formData);
  const schemeId = resApi.data?.data?.id;
@@ -235,7 +237,8 @@
    return;
  }
  // 显示加载中提示
  EventBus.emit("close-selectArea");
  const loadingMessage = ElMessage({
    type: "info",
    message: "正在启动模拟...",
@@ -244,60 +247,130 @@
  });
  try {
    // 调用求解器并初始化模拟
    const resStart = await getSimStart(schemeId);
    await getSimStart(schemeId);
    // 请求完成后关闭加载提示
    loadingMessage.close();
    if (resStart.code === 200) {
      const res = await getSimDataById(schemeId);
      simStore.setSelectedScheme(res.data[0]);
      simStore.layerDate = resStart.data;
      initeWaterPrimitiveView();
    // 定义一个函数用于轮询获取数据
    const pollForResult = async () => {
      try {
        startSimulate(); // 这里可能会报错
      } catch (error) {
        console.error("调用 startSimulate 出错:", error);
      }
        const res = await getSimresult(schemeId);
        console.log(res.data, "实时模拟 - 轮询结果");
      // 开始轮询任务:每 5 分钟调用一次 getSimStart 并更新方案数据
      startPolling(schemeId);
    } else {
      ElMessage.error(resStart.message || "调用求解器失败");
    }
        if (res.code === 200 && res.data.length > 0) {
          // 成功拿到数据
          loadingMessage.close();
          handleNewData(res.data, schemeId);
          startPolling(schemeId);
          // ✅ 清除定时器
          if (pollingTimer) {
            clearTimeout(pollingTimer);
            pollingTimer = null;
          }
        } else {
          // 数据无效,继续轮询
          pollingTimer = setTimeout(pollForResult, 10 * 1000);
        }
      } catch (error) {
        console.error("请求模拟结果失败", error);
        pollingTimer = setTimeout(pollForResult, 10 * 1000); // 请求出错也继续轮询
      }
    };
    // 首次延迟 2 分钟开始轮询
    pollingTimer = setTimeout(async () => {
      await pollForResult(); // 开始第一次轮询
    }, 3 * 60 * 1000); // 3分钟后第一次请求
  } catch (error) {
    loadingMessage.close();
    ElMessage.error("请求失败:" + (error.message || "未知错误"));
    console.error("调用 getSimStart 出错:", error);
  }
  // EventBus.emit("close-selectArea");
    if (pollingTimer) {
      clearTimeout(pollingTimer);
      pollingTimer = null;
    }
  }
}
// 启动轮询函数
// 定时五分钟请求
function startPolling(schemeId) {
  stopPolling(); // 避免重复启动
  stopPolling(); // 确保不会重复启动
  pollingInterval = setInterval(async () => {
    try {
      const resStart = await getSimStart(schemeId);
      const res = await getSimresult(schemeId);
      if (resStart.code === 200) {
        const res = await getSimDataById(schemeId);
        simStore.setSelectedScheme(res.data[0]); // 更新方案数据
        simStore.layerDate = resStart.data; // 更新 layer 数据
      if (res.data && res.data.length > 0) {
        if (res.data.length === lastDataLength) {
          console.log("主轮询:无新数据,切换为 10 秒高频轮询");
        console.log("轮询获取最新数据成功");
      } else {
        console.warn("轮询请求失败:", resStart.message);
          clearInterval(pollingInterval);
          pollingInterval = null;
          startFastPolling(schemeId); // 启动高频轮询
        } else {
          handleNewData(res.data, schemeId);
        }
      }
    } catch (error) {
      console.error("轮询请求异常:", error);
      console.error("轮询获取模拟结果失败", error);
    }
  }, 5 * 60 * 1000); // 每 5 分钟执行一次
  }, 5.6 * 60 * 1000); // 每 5.5 分钟执行一次
}
let fastPollingInterval = null;
// 如果五分钟没拿到最新的数据,则开启十秒钟调用一次,拿到最新的数据就停止
function startFastPolling(schemeId) {
  fastPollingInterval = setInterval(async () => {
    try {
      const res = await getSimresult(schemeId);
      if (res.data && res.data.length > 0) {
        if (res.data.length !== lastDataLength) {
          console.log("高频轮询:检测到新数据,恢复主轮询");
          clearInterval(fastPollingInterval);
          fastPollingInterval = null;
          handleNewData(res.data, schemeId);
          startPolling(schemeId); // 重新启动主轮询
        }
      }
    } catch (error) {
      console.error("高频轮询获取模拟结果失败", error);
    }
  }, 10 * 1000); // 每 10 秒执行一次
}
// 拿取最新的layer.json存储到pinia中
async function handleNewData(dataArray, schemeId) {
  // 拿服务名称
  const res = await getSimDataById(schemeId);
  simStore.setSelectedScheme(res.data[0]); // 更新方案数据
  const latestItem = dataArray[dataArray.length - 1];
  const currentLength = dataArray.length;
  if (currentLength <= lastDataLength) {
    console.log("本轮无新数据(长度未变化)");
    return;
  }
  // 更新标识
  lastDataLength = currentLength;
  // 执行更新逻辑
  console.log("检测到新数据,更新中...");
  console.log(latestItem, "last");
  simStore.layerDate = latestItem;
  initeWaterPrimitiveView();
  try {
    startSimulate();
  } catch (error) {
    console.error("调用 startSimulate 出错:", error);
  }
}
// 停止轮询函数
@@ -305,8 +378,14 @@
  if (pollingInterval) {
    clearInterval(pollingInterval);
    pollingInterval = null;
    console.log("轮询已停止");
  }
  if (fastPollingInterval) {
    clearInterval(fastPollingInterval);
    fastPollingInterval = null;
  }
  console.log("轮询已停止");
}
EventBus.on("close-time", () => {