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 |  157 +++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 118 insertions(+), 39 deletions(-)

diff --git a/src/views/left/KGSimOption/RealTimeSimulation.vue b/src/views/left/KGSimOption/RealTimeSimulation.vue
index 4dc917b..14c3105 100644
--- a/src/views/left/KGSimOption/RealTimeSimulation.vue
+++ b/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;
 
@@ -237,7 +239,6 @@
 
   EventBus.emit("close-selectArea");
 
-  // 鏄剧ず鍔犺浇涓彁绀�
   const loadingMessage = ElMessage({
     type: "info",
     message: "姝e湪鍚姩妯℃嫙...",
@@ -246,58 +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);
+
+    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瀛樺偍鍒皃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);
+  }
 }
 
 // 鍋滄杞鍑芥暟
@@ -305,8 +378,14 @@
   if (pollingInterval) {
     clearInterval(pollingInterval);
     pollingInterval = null;
-    console.log("杞宸插仠姝�");
   }
+
+  if (fastPollingInterval) {
+    clearInterval(fastPollingInterval);
+    fastPollingInterval = null;
+  }
+
+  console.log("杞宸插仠姝�");
 }
 
 EventBus.on("close-time", () => {

--
Gitblit v1.9.3