From 112912730c477a64f7b27086794a3bc30e6787c7 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期二, 03 十一月 2026 13:22:14 +0800
Subject: [PATCH] feat: 设备实时报警订阅与监控/巡检页面优化
---
src/views/environmentAccess/deviceRemoteMonitoring/index.vue | 64 ++++++++++++++++++++++++++++----
1 files changed, 56 insertions(+), 8 deletions(-)
diff --git a/src/views/environmentAccess/deviceRemoteMonitoring/index.vue b/src/views/environmentAccess/deviceRemoteMonitoring/index.vue
index b8ebe0c..1dbd6db 100644
--- a/src/views/environmentAccess/deviceRemoteMonitoring/index.vue
+++ b/src/views/environmentAccess/deviceRemoteMonitoring/index.vue
@@ -3,6 +3,9 @@
<div class="page-head">
<div><h2>璁惧杩滅▼鐩戞帶</h2><p>瀹炴椂閲囬泦娓╁害銆佸帇鍔涖�佹尟鍔ㄤ笌鑳借�楋紝寮傚父鑷姩鍛婅骞舵敮鎸佽繙绋嬪惎鍋溿��</p></div>
<div class="head-actions">
+ <el-tag :type="pushState === 'connected' ? 'success' : 'warning'" effect="plain">
+ 鍛婅鎺ㄩ�亄{ pushState === 'connected' ? '宸茶繛鎺�' : '杩炴帴涓�' }}
+ </el-tag>
<el-tag type="success" effect="dark">{{ onlineCount }} 鍙板湪绾�</el-tag>
<el-tag v-if="alarmCount" type="danger" effect="dark">{{ alarmCount }} 鏉″憡璀�</el-tag>
<el-button :loading="loading" type="primary" @click="collect">绔嬪嵆閲囬泦</el-button>
@@ -33,7 +36,12 @@
<span>閲囬泦锛歿{ item.collectTime || '鏆傛棤鏁版嵁' }}</span>
<el-popconfirm :title="`${item.controlStatus === 'running' ? '鍋滄' : '鍚姩'}璇ヨ澶囷紵`" @confirm="toggle(item)">
<template #reference>
- <el-button :type="item.controlStatus === 'running' ? 'danger' : 'success'" size="small" plain>
+ <el-button
+ :type="item.controlStatus === 'running' ? 'danger' : 'success'"
+ :loading="!!controlLoading[item.deviceId]"
+ size="small"
+ plain
+ >
{{ item.controlStatus === 'running' ? '杩滅▼鍋滄' : '杩滅▼鍚姩' }}
</el-button>
</template>
@@ -47,15 +55,21 @@
</template>
<script setup>
-import { computed, onBeforeUnmount, onMounted, ref } from "vue";
-import { collectTelemetry, controlDevice, listDeviceAlarms, listDeviceMonitor } from "@/api/environmentAccess/deviceMonitor";
+import { computed, onBeforeUnmount, onMounted, reactive, ref } from "vue";
+import { ElMessage, ElNotification } from "element-plus";
+import {
+ collectTelemetry, controlDevice, listDeviceAlarms, listDeviceMonitor, subscribeDeviceAlarms,
+} from "@/api/environmentAccess/deviceMonitor";
const devices = ref([]);
const alarms = ref([]);
const loading = ref(false);
+const pushState = ref("connecting");
+const controlLoading = reactive({});
const onlineCount = computed(() => devices.value.filter((i) => i.status !== "offline").length);
const alarmCount = computed(() => devices.value.filter((i) => ["warning", "alarm"].includes(i.status)).length);
let timer;
+let alarmSubscription;
const payload = (res) => res?.data?.records || res?.data || res?.records || [];
async function load() {
@@ -66,16 +80,50 @@
alarms.value = payload(alarmRes);
} finally { loading.value = false; }
}
-async function collect() { await collectTelemetry(); await load(); }
+async function collect() {
+ await collectTelemetry();
+ await load();
+ ElMessage.success("瀹炴椂鍙傛暟閲囬泦瀹屾垚");
+}
async function toggle(item) {
- await controlDevice(item.deviceId, item.controlStatus === "running" ? "stop" : "start");
- await collect();
+ const action = item.controlStatus === "running" ? "stop" : "start";
+ controlLoading[item.deviceId] = true;
+ try {
+ const response = await controlDevice(item.deviceId, action);
+ const result = response?.data || {};
+ item.controlStatus = result.controlStatus || (action === "start" ? "running" : "stopped");
+ ElMessage.success(result.message || (action === "start" ? "璁惧鍚姩鎴愬姛" : "璁惧鍋滄鎴愬姛"));
+ await load();
+ } finally {
+ controlLoading[item.deviceId] = false;
+ }
+}
+function receiveAlarm(alarm) {
+ const deviceIndex = devices.value.findIndex((item) => item.deviceId === alarm.deviceId);
+ if (deviceIndex >= 0) devices.value.splice(deviceIndex, 1, { ...devices.value[deviceIndex], ...alarm });
+ const alarmIndex = alarms.value.findIndex((item) => item.deviceId === alarm.deviceId);
+ if (alarmIndex >= 0) alarms.value.splice(alarmIndex, 1, alarm);
+ else alarms.value.unshift(alarm);
+ ElNotification({
+ title: `${alarm.status === "alarm" ? "璁惧鎶ヨ" : "璁惧棰勮"} 路 ${alarm.deviceName || alarm.deviceId}`,
+ message: alarm.alarmContent || "璁惧鍙傛暟瓒呰繃璁惧畾闃堝��",
+ type: alarm.status === "alarm" ? "error" : "warning",
+ duration: 10000,
+ position: "top-right",
+ });
}
const value = (v) => v === null || v === undefined ? "--" : Number(v).toFixed(2);
const statusText = (v) => ({ normal: "姝e父", warning: "棰勮", alarm: "鎶ヨ", offline: "绂荤嚎" }[v] || "鏈煡");
const statusType = (v) => ({ normal: "success", warning: "warning", alarm: "danger", offline: "info" }[v] || "info");
-onMounted(() => { load(); timer = window.setInterval(load, 30000); });
-onBeforeUnmount(() => window.clearInterval(timer));
+onMounted(() => {
+ load();
+ alarmSubscription = subscribeDeviceAlarms(receiveAlarm, (state) => { pushState.value = state; });
+ timer = window.setInterval(load, 30000);
+});
+onBeforeUnmount(() => {
+ window.clearInterval(timer);
+ alarmSubscription?.close();
+});
</script>
<style scoped lang="scss">
--
Gitblit v1.9.3