guonan
2025-06-26 4da56700113f235312e9fb64e49496c70fa12c1c
src/views/left/KGSimOption/RealTimeSimulation.vue
@@ -73,15 +73,20 @@
  inject,
  reactive,
  onMounted,
  onUnmounted,
} from "vue";
import { ElMessage } from "element-plus";
import { initeWaterPrimitiveView } from "@/utils/water";
import { SimAPIStore } from "@/store/simAPI";
import { useSimStore } from "@/store/simulation.js";
import { EventBus } from "@/eventBus"; // 引入事件总线
import { getDeviceInfoSHG, getYLJData } from "@/api/hpApi";
import { getSimStart, getSimDataById } from "@/api/trApi";
// 获取 Store 实例
const simStore = SimAPIStore();
const simAPIStore = SimAPIStore();
const simStore = useSimStore();
// 表单数据
const formData = reactive({
@@ -200,12 +205,10 @@
// 保存方案
const saveSim = async () => {
  try {
    getYLJData("1101160300070101").then((res) => {
      console.log(res, "resres");
    });
    // getYLJData("1101160300070101")
    updateSelectedGauges();
    formData.geom = props.selectedArea;
    await simStore.addSimCheme(formData);
    await simAPIStore.addSimCheme(formData);
    resetForm();
    EventBus.emit("close-selectArea");
  } catch (err) {}
@@ -214,24 +217,101 @@
// 注入模拟操作方法
const { startSimulate, endSimulate } = inject("simulateActions");
// 实时模拟定时器
let pollingInterval = null;
async function startPlay() {
  const selectedItems = filteredTableData.value.filter((item) => item.selected);
  if (selectedItems.length > 0) {
    console.log(
      "选中的项:",
      selectedItems.map((item) => item.name)
    );
  } else {
    console.log("未选中任何项");
  }
  console.log("当前选中的区域:", props.selectedArea);
  // 开始模拟前需要先保存方案
  updateSelectedGauges();
  formData.geom = props.selectedArea;
  await simStore.addSimCheme(formData);
  EventBus.emit("close-selectArea");
  initeWaterPrimitiveView();
  startSimulate();
  // 保存方案
  const resApi = await simAPIStore.addSimCheme(formData);
  const schemeId = resApi.data?.data?.id;
  if (!schemeId) {
    ElMessage.error("方案保存失败,未获取到有效 ID");
    return;
  }
  // 显示加载中提示
  const loadingMessage = ElMessage({
    type: "info",
    message: "正在启动模拟...",
    duration: 0,
    offset: 80,
  });
  try {
    // 调用求解器并初始化模拟
    const resStart = await getSimStart(schemeId);
    // 请求完成后关闭加载提示
    loadingMessage.close();
    if (resStart.code === 200) {
      const res = await getSimDataById(schemeId);
      simStore.setSelectedScheme(res.data[0]);
      simStore.layerDate = resStart.data;
      initeWaterPrimitiveView();
      try {
        startSimulate(); // 这里可能会报错
      } catch (error) {
        console.error("调用 startSimulate 出错:", error);
      }
      // 开始轮询任务:每 5 分钟调用一次 getSimStart 并更新方案数据
      startPolling(schemeId);
    } else {
      ElMessage.error(resStart.message || "调用求解器失败");
    }
  } catch (error) {
    loadingMessage.close();
    ElMessage.error("请求失败:" + (error.message || "未知错误"));
    console.error("调用 getSimStart 出错:", error);
  }
  // EventBus.emit("close-selectArea");
}
// 启动轮询函数
function startPolling(schemeId) {
  stopPolling(); // 避免重复启动
  pollingInterval = setInterval(async () => {
    try {
      const resStart = await getSimStart(schemeId);
      if (resStart.code === 200) {
        const res = await getSimDataById(schemeId);
        simStore.setSelectedScheme(res.data[0]); // 更新方案数据
        simStore.layerDate = resStart.data; // 更新 layer 数据
        console.log("轮询获取最新数据成功");
      } else {
        console.warn("轮询请求失败:", resStart.message);
      }
    } catch (error) {
      console.error("轮询请求异常:", error);
    }
  }, 5 * 60 * 1000); // 每 5 分钟执行一次
}
// 停止轮询函数
function stopPolling() {
  if (pollingInterval) {
    clearInterval(pollingInterval);
    pollingInterval = null;
    console.log("轮询已停止");
  }
}
EventBus.on("close-time", () => {
  stopPolling();
});
const toggleDetails = () => {
  isCollapsed.value = !isCollapsed.value;
@@ -240,6 +320,11 @@
const futurePredictions = () => {
  console.log("未来预测按钮被点击");
};
onUnmounted(() => {
  EventBus.off("close-time");
  stopPolling();
});
</script>
<style scoped>