guonan
2025-07-03 ca74058a77d7c9000a485502a2b53fbef5807ef5
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,6 +220,8 @@
// 实时模拟定时器
let pollingInterval = null;
// 用于记录上次数据条数
let lastDataLength = 0;
async function startPlay() {
  // 开始模拟前需要先保存方案
@@ -246,30 +249,27 @@
  });
  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();
    // 首次请求延迟 90s
    setTimeout(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.data.length > 0) {
          handleNewData(res.data, schemeId);
        }
        // 显示结果并开始轮询
        loadingMessage.close();
        startPolling(schemeId);
      } catch (error) {
        console.error("首次请求模拟结果失败", error);
        loadingMessage.close();
      }
    }, 3 * 60 * 1000); // 1.5 分钟后第一次请求
  } catch (error) {
    loadingMessage.close();
    ElMessage.error("请求失败:" + (error.message || "未知错误"));
@@ -277,27 +277,85 @@
  }
}
// 启动轮询函数
// 定时五分钟请求
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 +363,14 @@
  if (pollingInterval) {
    clearInterval(pollingInterval);
    pollingInterval = null;
    console.log("轮询已停止");
  }
  if (fastPollingInterval) {
    clearInterval(fastPollingInterval);
    fastPollingInterval = null;
  }
  console.log("轮询已停止");
}
EventBus.on("close-time", () => {