From bb549311df2cc48d5bc8f19c39d9c088db00fa4a Mon Sep 17 00:00:00 2001 From: "MJM_2025_05\\polly" Date: Thu, 25 Sep 2025 17:38:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=20RTSP=20=E7=82=BA=20?= =?UTF-8?q?5=20=E7=A7=92=E6=8A=93=E4=B8=80=E6=AC=A1=20device?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/rtsp/index.js | 6 +----- src/views/rtsp/Rtsp.vue | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/apis/rtsp/index.js b/src/apis/rtsp/index.js index 443e888..a5b42eb 100644 --- a/src/apis/rtsp/index.js +++ b/src/apis/rtsp/index.js @@ -1,9 +1,5 @@ // src/apis/rtsp/index.js -import { - POST_SET_RTSP_ENABLE, - POST_SET_SAMBA_DIRECTORY, - POST_GET_RTSP_DEVICE, -} from "./api"; +import { POST_SET_RTSP_ENABLE, POST_GET_RTSP_DEVICE } from "./api"; import instance from "@/util/request"; import apihandler from "@/util/apihandler"; diff --git a/src/views/rtsp/Rtsp.vue b/src/views/rtsp/Rtsp.vue index 13e8f85..e8d5d26 100644 --- a/src/views/rtsp/Rtsp.vue +++ b/src/views/rtsp/Rtsp.vue @@ -96,6 +96,7 @@ export default { message: "", rtspDevices: [], selectedMainId: null, // 目前選中的設備 main_id + pollTimer: null, // ← 用於記錄輪詢計時器 }; }, computed: { @@ -109,6 +110,10 @@ export default { async mounted() { await this.loadRtspDevices(); }, + beforeUnmount() { + // 離開頁面時確實清掉輪詢 + this.stopPolling(); + }, watch: { selectedBtn: { handler(newVal) { @@ -163,6 +168,31 @@ export default { this.monitorUrl = d.rtsp_url || DEFAULT_MONITOR_URL; }, + // === 輪詢控制 === + startPolling() { + // 若已在輪詢就不重複啟動 + if (this.pollTimer) return; + + const tick = async () => { + const keepId = this.selectedMainId; + await this.loadRtspDevices(); + // 保持目前選中的設備不變(若仍存在) + const found = this.rtspDevices.find((d) => d.main_id === keepId); + if (found) this.selectDevice(found); + }; + + // 立即跑一次,之後每 5 秒跑一次 + tick(); + this.pollTimer = setInterval(tick, 5000); + }, + + stopPolling() { + if (this.pollTimer) { + clearInterval(this.pollTimer); + this.pollTimer = null; + } + }, + // 開始偵測 async startDetection() { if (!this.selectedMainId) { @@ -177,6 +207,9 @@ export default { const found = this.rtspDevices.find((d) => d.main_id === keepId); if (found) this.selectDevice(found); this.message = this.$t("rtsp.startSuccess"); + + // ★ 啟動後開始每 5 秒抓一次 + this.startPolling(); } catch (e) { console.error(e); this.message = this.$t("rtsp.startFail"); @@ -199,6 +232,9 @@ export default { const found = this.rtspDevices.find((d) => d.main_id === keepId); if (found) this.selectDevice(found); this.message = this.$t("rtsp.stopSuccess"); + + // ★ 停止後結束輪詢 + this.stopPolling(); } catch (e) { console.error(e); this.message = this.$t("rtsp.stopFail");