浏览代码

feat(power): retrying mechanism for batch timing tasks

Casper Dai 2 年之前
父节点
当前提交
8f2a1a0a44
共有 2 个文件被更改,包括 64 次插入7 次删除
  1. 57 5
      src/views/device/power/components/DevicePowerTask.vue
  2. 7 2
      src/views/device/power/index.vue

+ 57 - 5
src/views/device/power/components/DevicePowerTask.vue

@@ -27,14 +27,17 @@ export default {
   data () {
     return {
       status: 0,
+      retryCount: 1,
       cardInfo: null
     }
   },
   mounted () {
+    this.$timer = -1
     addListener(this.device.id, this.onMessage)
     addInjectListener(this.device.id, this.onInjectMessage)
   },
   beforeDestroy () {
+    clearTimeout(this.$timer)
     removeListener(this.device.id, this.onMessage)
     removeInjectListener(this.device.id, this.onInjectMessage)
   },
@@ -68,10 +71,15 @@ export default {
         }
         switch (message.function) {
           case GET_MULTI_POWER_TIMING:
-            this.status = 1
-            this.sendTasks(data.data)
+            clearTimeout(this.$timer)
+            if (this.retryCount > 0) {
+              this.checkTasks(data.data)
+            } else {
+              this.sendTasks(data.data)
+            }
             break
           case SET_MULTI_POWER_TIMING:
+            clearTimeout(this.$timer)
             this.status = 2
             break
           default:
@@ -80,8 +88,11 @@ export default {
       }
     },
     onSync () {
+      this.$timer = setTimeout(this.retry, 5000)
       this.sendTopic(GET_MULTI_POWER_TIMING, { sn: this.device.serialNumber }).then(messageId => {
         this.$messageId = messageId
+      }, () => {
+        clearTimeout(this.$timer)
       })
     },
     createTasks () {
@@ -94,6 +105,7 @@ export default {
       })
     },
     sendTasks (tasks) {
+      this.status = 1
       const data = []
       const { connectIndex, portIndex } = this.cardInfo
       const currTasks = tasks.find(item => item.connectIndex === connectIndex && item.portIndex === portIndex)?.conditions
@@ -115,7 +127,7 @@ export default {
           conditions: this.createTasks()
         })
       }
-      console.log(data)
+      this.$timer = setTimeout(this.retry, 5000)
       this.sendTopic(SET_MULTI_POWER_TIMING, {
         sn: this.device.serialNumber,
         taskInfo: data
@@ -130,8 +142,30 @@ export default {
             tasks: this.tasks
           })
         })
+      }, () => {
+        clearTimeout(this.$timer)
       })
     },
+    retry () {
+      this.retryCount += 1
+      this.status = 1
+      this.onSync()
+    },
+    checkTasks (tasks) {
+      const { connectIndex, portIndex } = this.cardInfo
+      const currTasks = tasks.find(item => item.connectIndex === connectIndex && item.portIndex === portIndex)?.conditions
+      if (currTasks) {
+        const map = {}
+        this.tasks.forEach(({ flag }) => {
+          map[flag] = true
+        })
+        if (currTasks.some(({ flag }) => map[flag])) {
+          this.status = 2
+          return
+        }
+      }
+      this.sendTasks(tasks)
+    },
     sendTopic (invoke, inputs) {
       const timestamp = `${Date.now()}`
       const messageId = `${invoke}_${timestamp}`
@@ -148,14 +182,32 @@ export default {
     }
   },
   render (h) {
+    if (this.status === 3 || this.status === 4) {
+      return h('el-tooltip', {
+        props: {
+          content: this.status === 3 ? '连接失败,请联系管理员' : '无可用端口,请联系管理员',
+          placement: 'left',
+          enterable: false
+        }
+      }, [
+        h('el-tag', {
+          staticClass: 'o-tag u-readonly',
+          props: {
+            size: 'medium',
+            'disable-transitions': true,
+            type: 'danger'
+          }
+        }, '失败')
+      ])
+    }
     return h('el-tag', {
       staticClass: 'o-tag u-readonly',
       props: {
         size: 'medium',
         'disable-transitions': true,
-        type: ['primary', 'warning', 'success', 'danger', 'danger'][this.status]
+        type: ['primary', 'warning', 'success'][this.status]
       }
-    }, ['同步中', '等待中', '成功', '失败', '无目标'][this.status])
+    }, ['等待', this.retryCount === 0 ? '同步中' : `重试(${this.retryCount})`, '成功'][this.status])
   }
 }
 </script>

+ 7 - 2
src/views/device/power/index.vue

@@ -79,6 +79,10 @@ export default {
           { label: '历史开关任务', on: this.onViewHistory },
           { label: '批量定时任务', on: this.onAddTimingTask }
         ],
+        filters: [
+          { key: 'name', type: 'search', placeholder: '设备名称' },
+          { type: 'refresh' }
+        ],
         cols: [
           { type: 'selection', selectable: this.canSelect },
           { prop: 'name', label: '设备名称', 'min-width': 60 },
@@ -179,10 +183,11 @@ export default {
         this.$refs.table?.getInst().toggleRowSelection(row)
       }
     },
-    getDevicesWithPower () {
+    getDevicesWithPower (params) {
       return getDevicesWithPower({
         activate: 1,
-        [this.tenant === this.$group.path ? 'tenant' : 'org']: this.$group.path
+        [this.tenant === this.$group.path ? 'tenant' : 'org']: this.$group.path,
+        ...params
       }).then(({ data }) => {
         const now = Date.now()
         return { data: data.map(device => {