fix: 修正 RTSP 為 5 秒抓一次 device
This commit is contained in:
parent
ba7fda3d07
commit
bb549311df
@ -1,9 +1,5 @@
|
|||||||
// src/apis/rtsp/index.js
|
// src/apis/rtsp/index.js
|
||||||
import {
|
import { POST_SET_RTSP_ENABLE, POST_GET_RTSP_DEVICE } from "./api";
|
||||||
POST_SET_RTSP_ENABLE,
|
|
||||||
POST_SET_SAMBA_DIRECTORY,
|
|
||||||
POST_GET_RTSP_DEVICE,
|
|
||||||
} from "./api";
|
|
||||||
import instance from "@/util/request";
|
import instance from "@/util/request";
|
||||||
import apihandler from "@/util/apihandler";
|
import apihandler from "@/util/apihandler";
|
||||||
|
|
||||||
|
@ -96,6 +96,7 @@ export default {
|
|||||||
message: "",
|
message: "",
|
||||||
rtspDevices: [],
|
rtspDevices: [],
|
||||||
selectedMainId: null, // 目前選中的設備 main_id
|
selectedMainId: null, // 目前選中的設備 main_id
|
||||||
|
pollTimer: null, // ← 用於記錄輪詢計時器
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@ -109,6 +110,10 @@ export default {
|
|||||||
async mounted() {
|
async mounted() {
|
||||||
await this.loadRtspDevices();
|
await this.loadRtspDevices();
|
||||||
},
|
},
|
||||||
|
beforeUnmount() {
|
||||||
|
// 離開頁面時確實清掉輪詢
|
||||||
|
this.stopPolling();
|
||||||
|
},
|
||||||
watch: {
|
watch: {
|
||||||
selectedBtn: {
|
selectedBtn: {
|
||||||
handler(newVal) {
|
handler(newVal) {
|
||||||
@ -163,6 +168,31 @@ export default {
|
|||||||
this.monitorUrl = d.rtsp_url || DEFAULT_MONITOR_URL;
|
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() {
|
async startDetection() {
|
||||||
if (!this.selectedMainId) {
|
if (!this.selectedMainId) {
|
||||||
@ -177,6 +207,9 @@ export default {
|
|||||||
const found = this.rtspDevices.find((d) => d.main_id === keepId);
|
const found = this.rtspDevices.find((d) => d.main_id === keepId);
|
||||||
if (found) this.selectDevice(found);
|
if (found) this.selectDevice(found);
|
||||||
this.message = this.$t("rtsp.startSuccess");
|
this.message = this.$t("rtsp.startSuccess");
|
||||||
|
|
||||||
|
// ★ 啟動後開始每 5 秒抓一次
|
||||||
|
this.startPolling();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
this.message = this.$t("rtsp.startFail");
|
this.message = this.$t("rtsp.startFail");
|
||||||
@ -199,6 +232,9 @@ export default {
|
|||||||
const found = this.rtspDevices.find((d) => d.main_id === keepId);
|
const found = this.rtspDevices.find((d) => d.main_id === keepId);
|
||||||
if (found) this.selectDevice(found);
|
if (found) this.selectDevice(found);
|
||||||
this.message = this.$t("rtsp.stopSuccess");
|
this.message = this.$t("rtsp.stopSuccess");
|
||||||
|
|
||||||
|
// ★ 停止後結束輪詢
|
||||||
|
this.stopPolling();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
this.message = this.$t("rtsp.stopFail");
|
this.message = this.$t("rtsp.stopFail");
|
||||||
|
Loading…
Reference in New Issue
Block a user