From f37845dd0a787dd42bf6c72e923433f30fcd8cc3 Mon Sep 17 00:00:00 2001
From: guonan <guonan201020@163.com>
Date: 星期四, 03 七月 2025 15:40:09 +0800
Subject: [PATCH] 实时模拟

---
 src/views/left/KGSimOption/RealTimeSimulation.vue |  208 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 186 insertions(+), 22 deletions(-)

diff --git a/src/views/left/KGSimOption/RealTimeSimulation.vue b/src/views/left/KGSimOption/RealTimeSimulation.vue
index 0b34776..14c3105 100644
--- a/src/views/left/KGSimOption/RealTimeSimulation.vue
+++ b/src/views/left/KGSimOption/RealTimeSimulation.vue
@@ -73,15 +73,21 @@
   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 { getDeviceInfoSHG, getYLJData } from "@/api/hpApi";
+import { getSimStart, getSimDataById, getSimresult } from "@/api/trApi";
+import { ControlSchemeType } from "@/assets/js/lib-pixelstreamingfrontend.esm";
 
 // 鑾峰彇 Store 瀹炰緥
-const simStore = SimAPIStore();
+const simAPIStore = SimAPIStore();
+
+const simStore = useSimStore();
 
 // 琛ㄥ崟鏁版嵁
 const formData = reactive({
@@ -116,8 +122,8 @@
 // 鑾峰彇鎵�鏈夐洦閲忚鏁版嵁锛堟潵鑷帴鍙o級
 const getRainListAll = () => {
   // 闆ㄩ噺璁$被鍨媔d
-  const ids = "1874719366287368194";
-  getDeviceInfoShg(ids).then((res) => {
+  const ids = "1917487171642212354";
+  getDeviceInfoSHG(ids).then((res) => {
     rainListNoFilter.value = res.data.pageData;
     // 鏍规嵁褰撳墠閫夋嫨鐨勫尯鍩熻嚜鍔ㄨ繃婊�
     updateShgListByArea();
@@ -200,12 +206,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 +218,179 @@
 // 娉ㄥ叆妯℃嫙鎿嶄綔鏂规硶
 const { startSimulate, endSimulate } = inject("simulateActions");
 
+// 瀹炴椂妯℃嫙瀹氭椂鍣�
+let pollingInterval = null;
+// 鐢ㄤ簬璁板綍涓婃鏁版嵁鏉℃暟
+let lastDataLength = 0;
+
+let pollingTimer = 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);
+
+  const resApi = await simAPIStore.addSimCheme(formData);
+  const schemeId = resApi.data?.data?.id;
+
+  if (!schemeId) {
+    ElMessage.error("鏂规淇濆瓨澶辫触锛屾湭鑾峰彇鍒版湁鏁� ID");
+    return;
+  }
+
   EventBus.emit("close-selectArea");
-  initeWaterPrimitiveView();
-  startSimulate();
+
+  const loadingMessage = ElMessage({
+    type: "info",
+    message: "姝e湪鍚姩妯℃嫙...",
+    duration: 0,
+    offset: 80,
+  });
+
+  try {
+    await getSimStart(schemeId);
+
+    // 瀹氫箟涓�涓嚱鏁扮敤浜庤疆璇㈣幏鍙栨暟鎹�
+    const pollForResult = async () => {
+      try {
+        const res = await getSimresult(schemeId);
+        console.log(res.data, "瀹炴椂妯℃嫙 - 杞缁撴灉");
+
+        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);
+
+    if (pollingTimer) {
+      clearTimeout(pollingTimer);
+      pollingTimer = null;
+    }
+  }
 }
+
+// 瀹氭椂浜斿垎閽熻姹�
+function startPolling(schemeId) {
+  stopPolling(); // 纭繚涓嶄細閲嶅鍚姩
+
+  pollingInterval = setInterval(async () => {
+    try {
+      const res = await getSimresult(schemeId);
+
+      if (res.data && res.data.length > 0) {
+        if (res.data.length === lastDataLength) {
+          console.log("涓昏疆璇細鏃犳柊鏁版嵁锛屽垏鎹负 10 绉掗珮棰戣疆璇�");
+
+          clearInterval(pollingInterval);
+          pollingInterval = null;
+
+          startFastPolling(schemeId); // 鍚姩楂橀杞
+        } else {
+          handleNewData(res.data, schemeId);
+        }
+      }
+    } catch (error) {
+      console.error("杞鑾峰彇妯℃嫙缁撴灉澶辫触", error);
+    }
+  }, 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瀛樺偍鍒皃inia涓�
+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);
+  }
+}
+
+// 鍋滄杞鍑芥暟
+function stopPolling() {
+  if (pollingInterval) {
+    clearInterval(pollingInterval);
+    pollingInterval = null;
+  }
+
+  if (fastPollingInterval) {
+    clearInterval(fastPollingInterval);
+    fastPollingInterval = null;
+  }
+
+  console.log("杞宸插仠姝�");
+}
+
+EventBus.on("close-time", () => {
+  stopPolling();
+});
 
 const toggleDetails = () => {
   isCollapsed.value = !isCollapsed.value;
@@ -240,6 +399,11 @@
 const futurePredictions = () => {
   console.log("鏈潵棰勬祴鎸夐挳琚偣鍑�");
 };
+
+onUnmounted(() => {
+  EventBus.off("close-time");
+  stopPolling();
+});
 </script>
 
 <style scoped>

--
Gitblit v1.9.3