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 | 202 +++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 183 insertions(+), 19 deletions(-) diff --git a/src/views/left/KGSimOption/RealTimeSimulation.vue b/src/views/left/KGSimOption/RealTimeSimulation.vue index 3ca2832..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 { 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({ @@ -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