浏览代码

fix: 增加设备详情页实时数据3秒轮训

yanglzh 1 年之前
父节点
当前提交
8f6c3868b2
共有 2 个文件被更改,包括 20 次插入16 次删除
  1. 8 2
      src/views/iot/device/instance/detail.vue
  2. 12 14
      src/views/iot/iotmanager/dashboard.vue

+ 8 - 2
src/views/iot/device/instance/detail.vue

@@ -363,7 +363,7 @@
   </div>
 </template>
 <script lang="ts">
-import { toRefs, reactive, onMounted, ref, defineComponent, nextTick } from 'vue';
+import { toRefs, reactive, onMounted, ref, defineComponent, nextTick, onUnmounted } from 'vue';
 import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
 import functionCom from './component/function.vue';
 import 'vue3-json-viewer/dist/index.css';
@@ -430,6 +430,10 @@ export default defineComponent({
   },
   setup(props, context) {
 
+    let timer: any
+
+    onUnmounted(() => clearInterval(timer))
+
     const logqueryRef = ref();
 
     // 属性列表,查询保留小数位使用
@@ -509,7 +513,7 @@ export default defineComponent({
 
     function initData() {
       // 如果是嵌入的就是子设备,看子设备详情,否则看页面参数
-      api.instance.detail(props.deviceKey ||  route.params?.id).then((res: any) => {
+      api.instance.detail(props.deviceKey || route.params?.id).then((res: any) => {
         state.detail = res.data;
         state.developer_status = res.data.status;
         state.tableData.param.productKey = res.data.product.key;
@@ -533,6 +537,8 @@ export default defineComponent({
         getDeviceAssetMetadata()
 
         getrunData();
+        
+        timer = setInterval(getrunData, 3000)
 
         getDeviceTableData()
       });

+ 12 - 14
src/views/iot/iotmanager/dashboard.vue

@@ -79,7 +79,7 @@
 </template>
 
 <script lang="ts">
-import { toRefs, reactive, defineComponent, onMounted, ref, watch, nextTick, onActivated, getCurrentInstance } from 'vue';
+import { toRefs, reactive, defineComponent, onMounted, ref, watch, nextTick, onActivated, getCurrentInstance, onUnmounted } from 'vue';
 import * as echarts from 'echarts';
 import { useRouter, useRoute } from 'vue-router';
 import { useStore } from '/@/store/index';
@@ -102,6 +102,14 @@ export default defineComponent({
 	components: { EditDic, DetailDic },
 	setup() {
 
+		let timer1: any
+		let timer2: any
+
+		onUnmounted(() => {
+			clearInterval(timer1)
+			clearInterval(timer2)
+		})
+
 		const { proxy } = getCurrentInstance() as any;
 		const { alarm_type } = proxy.useDict('alarm_type');
 		const alarmTypeMap: any = {}
@@ -432,7 +440,7 @@ export default defineComponent({
 				state.homeOne[1].num1 = res.total - res.disable
 				state.homeOne[1].num2 = res.disable
 			})
-			
+
 			// 按告警级别统计
 			api.iotManage.deviceAlarmLevelCount('year', dayjs().format('YYYY')).then((res: any) => {
 				const list = (res.data || [])
@@ -470,20 +478,10 @@ export default defineComponent({
 		}
 
 		// 每隔3秒更新数据
-		setInterval(() => {
-			// 避免到其他页面也加载
-			if (route.path === '/iotmanager/dashboard') {
-				getLoopData()
-			}
-		}, 3000)
+		timer1 = setInterval(getLoopData, 3000)
 
 		// 每隔一分钟秒更新图形
-		setInterval(() => {
-			// 避免到其他页面也加载
-			if (route.path === '/iotmanager/dashboard') {
-				getChartData()
-			}
-		}, 60000)
+		timer2 = setInterval(getChartData, 60000)
 
 		const getOverviewData = () => {
 			getLoopData()