From 2f387619c1cf834c058ac77a3fd4cabc42e5d4de Mon Sep 17 00:00:00 2001
From: guonan <guonan201020@163.com>
Date: 星期三, 25 六月 2025 15:09:30 +0800
Subject: [PATCH] 提交初版实时模拟

---
 src/components/tools/LayerTree.vue         |    2 
 src/store/simulation.js                    |    3 
 src/api/trApi.js                           |   36 +++++++++--
 src/components/menu/TimeLine.vue           |   42 ++++++++-----
 src/components/monifangzhen/schemeCard.vue |   55 ++++++++++++-----
 5 files changed, 95 insertions(+), 43 deletions(-)

diff --git a/src/api/trApi.js b/src/api/trApi.js
index 53d8f50..1f5eb9a 100644
--- a/src/api/trApi.js
+++ b/src/api/trApi.js
@@ -38,7 +38,7 @@
 export async function getSimDataById(id) {
   try {
     const res = await instance.get(`/simu/selectPage?id=${id}`);
-    return res.data; 
+    return res.data;
   } catch (error) {
     console.error("Error fetching data:", error);
     throw error; // 鎶涘嚭閿欒锛岃璋冪敤鏂瑰彲浠ユ崟鑾�
@@ -117,22 +117,44 @@
   }
 }
 
-// 閫氳繃鎺ュ彛鍘昏姹俲son锛屽皢璇锋眰鐨刯son瑙f瀽鑾峰彇娉ョ煶娴佸弬鏁�
-export async function fetchWaterSimulationData(serviceInfo) {
+// // 閫氳繃鎺ュ彛鍘昏姹俲son锛屽皢璇锋眰鐨刯son瑙f瀽鑾峰彇娉ョ煶娴佸弬鏁�
+// export async function fetchWaterSimulationData(serviceInfo) {
+//   try {
+//     const response = await fetch(`/simu/${serviceInfo}/layer.json`); // 鍙戣捣璇锋眰
+//     // const response = await fetch(`/simu/c2h1dc/layer.json`); // 鍙戣捣璇锋眰
+//     if (!response.ok) {
+//       throw new Error(`HTTP error! status: ${response.status}`);
+//     }
+//     const jsonData = await response.json(); // 瑙f瀽 JSON 鏁版嵁
+//     // console.log(jsonData, "jsonjsonjsonjson");
+//     return parseWaterSimulationData(jsonData); // 璋冪敤瑙f瀽鍑芥暟
+//   } catch (error) {
+//     console.error("璇锋眰鎴栬В鏋愭暟鎹椂鍑洪敊:", error);
+//     return null;
+//   }
+// }
+
+export async function fetchWaterSimulationData(serviceInfo, timestamp = null) {
   try {
-    const response = await fetch(`/simu/${serviceInfo}/layer.json`); // 鍙戣捣璇锋眰
-    // const response = await fetch(`/simu/c2h1dc/layer.json`); // 鍙戣捣璇锋眰
+    // 鏍规嵁鏄惁鎻愪緵鏃堕棿鎴虫潵鍐冲畾鏂囦欢鍚�
+    let fileName = timestamp ? timestamp : 'layer.json';
+    const url = `/simu/${serviceInfo}/${fileName}`;
+
+    const response = await fetch(url); // 鍙戣捣璇锋眰
+
+    console.log(url,'aaaaaaaaa')
+
     if (!response.ok) {
       throw new Error(`HTTP error! status: ${response.status}`);
     }
     const jsonData = await response.json(); // 瑙f瀽 JSON 鏁版嵁
-    // console.log(jsonData, "jsonjsonjsonjson");
-    return parseWaterSimulationData(jsonData); // 璋冪敤瑙f瀽鍑芥暟
+    return parseWaterSimulationData(jsonData); // 璋冪敤瑙f瀽鍑芥暟澶勭悊鏁版嵁
   } catch (error) {
     console.error("璇锋眰鎴栬В鏋愭暟鎹椂鍑洪敊:", error);
     return null;
   }
 }
+
 // 鑾峰彇姘翠綅姘存繁
 export async function getFlowRate(data) {
   // console.log(data,'鍙戦�佺殑鏁版嵁锛�');
diff --git a/src/components/menu/TimeLine.vue b/src/components/menu/TimeLine.vue
index 177f159..b3f87e6 100644
--- a/src/components/menu/TimeLine.vue
+++ b/src/components/menu/TimeLine.vue
@@ -152,7 +152,7 @@
 import { useSimStore } from "@/store/simulation";
 import { storeToRefs } from "pinia";
 const simStore = useSimStore();
-const { selectedScheme, frameNum } = storeToRefs(simStore);
+const { selectedScheme, frameNum, layerDate } = storeToRefs(simStore);
 
 const emit = defineEmits([
   "timeUpdate",
@@ -191,7 +191,7 @@
 const isWaterPrimitiveCreated = ref(false);
 let playInterval = null;
 let timeStepInfo = null;
-let rainTotalInfo = ([]);
+let rainTotalInfo = [];
 const isRainEnabled = ref(false);
 const rainParams = reactive({
   rainSize: 0.5,
@@ -349,10 +349,10 @@
 
   const rainfallList = data.rainfalls;
   console.log("鏈�缁堢殑 rainfallList:", rainfallList);
-  rainTotalInfo.value = rainfallList
-  calculateTimeStep(rainTotalInfo.value)
+  rainTotalInfo.value = rainfallList;
+  calculateTimeStep(rainTotalInfo.value);
   // 浣跨敤绀轰緥
- timeStepInfo = calculateTimeStep(rainTotalInfo.value);
+  timeStepInfo = calculateTimeStep(rainTotalInfo.value);
 
   // 鎻愬彇 intensity 鍊�
   rainFallValues.value = rainfallList.map((r) => r.intensity);
@@ -431,13 +431,13 @@
 
 function calculateTimeStep(dataArray) {
   if (!dataArray || dataArray.length < 2) {
-    console.warn('鏁版嵁涓嶈冻锛屾棤娉曡绠楁椂闂存闀�');
+    console.warn("鏁版嵁涓嶈冻锛屾棤娉曡绠楁椂闂存闀�");
     return null;
   }
 
   // 瑙f瀽鏃堕棿瀛楃涓蹭负 Date 瀵硅薄
   function parseTime(timeStr) {
-    return new Date(timeStr.replace(' ', 'T')); // 鍏煎 ISO 鏍煎紡
+    return new Date(timeStr.replace(" ", "T")); // 鍏煎 ISO 鏍煎紡
   }
 
   const firstTime = parseTime(dataArray[0].time);
@@ -455,7 +455,9 @@
     const next = parseTime(dataArray[i + 1].time);
     const step = Math.abs(next - current) / (1000 * 60 * 60); // 姣 -> 灏忔椂
     if (Math.abs(step - timeStepHours) > 0.01) {
-      console.warn(`鍦ㄧ储寮� ${i} 澶勫彂鐜颁簡涓嶅悓鐨勬椂闂存闀�: ${step.toFixed(2)} 灏忔椂`);
+      console.warn(
+        `鍦ㄧ储寮� ${i} 澶勫彂鐜颁簡涓嶅悓鐨勬椂闂存闀�: ${step.toFixed(2)} 灏忔椂`
+      );
     }
   }
 
@@ -481,7 +483,9 @@
   const currentTimestamp = timeToTimestamp(currentData.time);
 
   // 宸茶繃鍘荤殑鏃堕棿锛堝皬鏃讹級
-  const elapsedTimeInHours = parseFloat((currentTimestamp - initialTimestamp) / (1000 * 60 * 60));
+  const elapsedTimeInHours = parseFloat(
+    (currentTimestamp - initialTimestamp) / (1000 * 60 * 60)
+  );
 
   console.log(`鎸佺画浜� ${elapsedTimeInHours} 灏忔椂`);
 
@@ -516,13 +520,13 @@
 
   // 杈撳嚭鍏抽敭淇℃伅
   console.table({
-    '褰撳墠鏃堕棿': currentData.time,
-    '绱鏃堕暱 D(t) (h)': D.toFixed(2),
-    '闆ㄥ己闃堝�� IR(t) (mm/h)': IR.toFixed(2),
-    '褰撳墠闄嶉洦寮哄害 I(t) (mm/h)': intensity.toFixed(2),
-    '褰撳墠闃舵缂栧彿': stage,
-    '鏈�澶ч樁娈电紪鍙�': maxStage,
-    '鏄惁瑙﹀彂娉ョ煶娴�': stage >= 5 ? '鏄�' : '鍚�'
+    褰撳墠鏃堕棿: currentData.time,
+    "绱鏃堕暱 D(t) (h)": D.toFixed(2),
+    "闆ㄥ己闃堝�� IR(t) (mm/h)": IR.toFixed(2),
+    "褰撳墠闄嶉洦寮哄害 I(t) (mm/h)": intensity.toFixed(2),
+    褰撳墠闃舵缂栧彿: stage,
+    鏈�澶ч樁娈电紪鍙�: maxStage,
+    鏄惁瑙﹀彂娉ョ煶娴�: stage >= 5 ? "鏄�" : "鍚�",
   });
 
   // 鏍规嵁鏈�澶ч樁娈佃缃鑹�
@@ -757,12 +761,16 @@
   try {
     // 褰撳墠鏂规鐨勬墍鏈変俊鎭�
     const schemeInfo = selectedScheme.value;
+    const jsonFetch = ref(null);
     serviceInfo = schemeInfo.serviceName;
     if (selectedScheme.value.type == 2) {
       speedShow.value = false;
+      jsonFetch.value = layerDate.value;
+      // serviceInfo = layerDate.value;
     } else {
       getRainfallData();
       speedShow.value = true;
+      jsonFetch.value = null;
     }
 
     // console.log('鑾峰彇鍒扮殑 serviceName:', serviceInfo);
@@ -772,7 +780,7 @@
       waterTimestamps: timestamps,
       watersMaxHeight,
       watersMinHeight,
-    } = await fetchWaterSimulationData(serviceInfo);
+    } = await fetchWaterSimulationData(serviceInfo, jsonFetch.value);
     console.log(
       "褰撳墠鏂规涓嬬殑鏈�澶ф按浣嶆繁搴﹀拰鏈�灏忔按浣嶆繁搴�",
       watersMaxHeight,
diff --git a/src/components/monifangzhen/schemeCard.vue b/src/components/monifangzhen/schemeCard.vue
index a25acd1..92c915a 100644
--- a/src/components/monifangzhen/schemeCard.vue
+++ b/src/components/monifangzhen/schemeCard.vue
@@ -74,7 +74,6 @@
   selectedId.value = id;
 }
 
-
 function formatTime(time) {
   return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
 }
@@ -93,6 +92,9 @@
 function close() {
   messageShow.value = false;
 }
+
+// 瀹炴椂妯℃嫙浜斿垎閽熻姹備竴娆$殑瀹氭椂鍣�
+const realTimeSimInterval = ref(null);
 
 async function startPlay(item) {
   if (item.status === 2) {
@@ -127,7 +129,7 @@
     return;
   }
 
-  // 璋冪敤姹傝В鍣紙涓嶅湪瀹炴椂妯℃嫙鐨勬儏鍐典笅锛�
+  // 鏂板缓鏂规锛屾病鏈� status 鍜� serviceName 涓� type != 2
   if (!item.status && !item.serviceName && item.type !== 2) {
     try {
       await getSimStart(item.id);
@@ -143,23 +145,21 @@
     return;
   }
 
-  // 瀹炴椂妯℃嫙
+  // 澶勭悊 type == 2 鐨勬儏鍐碉紙瀹炴椂妯℃嫙锛�
   if (item.type === 2) {
-    try {
-      // 瀹炴椂妯℃嫙璋冪敤姹傝В鍣ㄤ細鐩存帴鍦ㄦ帴鍙d腑杩斿洖缁撴灉
-      const ress = await getSimStart(item.id);
-      const res = await getSimDataById(item.id);
-      item.serviceName = res.data[0]?.serviceName || null;
-      simStore.setSelectedScheme(item);
-      getScheme();
-
-      if (ress.code === 200) {
-        initeWaterPrimitiveView();
-        emit("start");
-      }
-    } catch (e) {
-      console.error("瀹炴椂妯℃嫙鑾峰彇妯℃嫙鏁版嵁澶辫触锛�", e);
+    // 娓呴櫎宸叉湁瀹氭椂鍣紝闃叉閲嶅鍚姩
+    if (realTimeSimInterval.value) {
+      clearInterval(realTimeSimInterval.value);
     }
+
+    // 鍗冲埢鎵ц涓�娆�
+    await executeRealTimeSimulation(item);
+
+    // 姣忛殧 5 鍒嗛挓鎵ц涓�娆�
+    realTimeSimInterval.value = setInterval(() => {
+      executeRealTimeSimulation(item);
+    }, 5 * 60 * 1000); // 5鍒嗛挓
+
     return;
   }
 
@@ -167,7 +167,28 @@
   simStore.setSelectedScheme(item);
 }
 
+// 灏佽瀹炴椂妯℃嫙鐨勫紓姝ユ搷浣�
+async function executeRealTimeSimulation(item) {
+  try {
+    const ress = await getSimStart(item.id);
 
+    console.log(ress, "resssssssss");
+    const res = await getSimDataById(item.id);
+
+    item.serviceName = res.data[0]?.serviceName || null;
+    simStore.setSelectedScheme(item);
+    getScheme();
+
+    if (ress.code === 200) {
+      simStore.layerDate = ress.data;
+      console.log(simStore.layerDate,'aaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbb')
+      initeWaterPrimitiveView();
+      emit("start");
+    }
+  } catch (e) {
+    console.error("瀹炴椂妯℃嫙鑾峰彇妯℃嫙鏁版嵁澶辫触锛�", e);
+  }
+}
 
 function handleBack(value) {
   if (value === false) {
diff --git a/src/components/tools/LayerTree.vue b/src/components/tools/LayerTree.vue
index d209e48..425de2b 100644
--- a/src/components/tools/LayerTree.vue
+++ b/src/components/tools/LayerTree.vue
@@ -76,7 +76,7 @@
   try {
     TerrainLayer = await earthCtrl.factory.createTerrainLayer({
       sourceType: "ctb",
-      url: "http://106.120.22.26:9103/gisserver/ctsserver/sunhugoudem",
+      url: "http://106.120.22.26:9103/gisserver/ctsserver/sunhugoudem84",
       requestVertexNormals: true,
     });
     treeMap.set("鍦板舰鏁版嵁", TerrainLayer);
diff --git a/src/store/simulation.js b/src/store/simulation.js
index ea5a36a..b18bbdd 100644
--- a/src/store/simulation.js
+++ b/src/store/simulation.js
@@ -2,6 +2,7 @@
 import { defineStore } from 'pinia'
 import { ref } from 'vue'
 export const useSimStore = defineStore('simulation', () => {
+    const layerDate = ref("")
     // 甯ф暟
     const frameNum = ref(0)
     // 鐩綍鏍戦�変腑
@@ -46,7 +47,6 @@
         selectedScheme.value = scheme
         rainFalls.value = JSON.parse(scheme.data).rainfalls
         intensityUnit.value = JSON.parse(scheme.data).intensityUnit
-        console.log(intensityUnit.value, 'shceme')
     }
     const clearSelectedScheme = () => {
         selectedScheme.value = null
@@ -196,6 +196,7 @@
         userSelectedLayers,
         devices,
         frameNum,
+        layerDate,
 
         // 鏂规鐩稿叧鏂规硶
         setSchemCard,

--
Gitblit v1.9.3