Casper Dai 2 роки тому
батько
коміт
07dd9581f4

+ 16 - 45
src/views/device/power/components/DevicePowerDecreaseTask.vue

@@ -1,15 +1,10 @@
 <script>
-import {
-  ThirdPartyDevice,
-  PowerAction
-} from '@/constant'
+import { PowerAction } from '@/constant'
 import { publish } from '@/utils/mqtt'
 import {
-  Status,
+  RELAY_KEY,
   GET_MULTI_POWER_TIMING,
   SET_MULTI_POWER_TIMING,
-  addListener,
-  removeListener,
   addInjectListener,
   removeInjectListener
 } from '@/utils/adapter'
@@ -38,36 +33,19 @@ export default {
   data () {
     return {
       status: 0,
-      retryCount: 0,
-      cardInfo: null
+      retryCount: 0
     }
   },
   mounted () {
     this.$timer = -1
-    addListener(this.device.id, this.onMessage)
     addInjectListener(this.device.id, this.onInjectMessage)
+    this.onSync()
   },
   beforeDestroy () {
     clearTimeout(this.$timer)
-    removeListener(this.device.id, this.onMessage)
     removeInjectListener(this.device.id, this.onInjectMessage)
   },
   methods: {
-    onMessage (value) {
-      const multiCard = value[ThirdPartyDevice.MULTI_FUNCTION_CARD]
-      if (multiCard.status === Status.OK) {
-        if (!this.cardInfo) {
-          const { connectIndex, portIndex } = multiCard.powers[0]
-          const type = multiCard.powers.find(({ type }) => type === '屏体电源' || type === '默认')?.type || multiCard.powers.find(({ type }) => !!type)?.type
-          this.cardInfo = { type, connectIndex, portIndex }
-          if (type) {
-            this.onSync()
-          } else {
-            this.status = 4
-          }
-        }
-      }
-    },
     onInjectMessage (message) {
       if (message.messageId === this.$messageId) {
         this.$messageId = null
@@ -83,7 +61,7 @@ export default {
         switch (message.function) {
           case GET_MULTI_POWER_TIMING:
             clearTimeout(this.$timer)
-            this.sendTasks(data.data)
+            this.sendTasks(data.data.filter(({ connectIndex, portIndex }) => connectIndex !== RELAY_KEY && portIndex !== RELAY_KEY))
             break
           case SET_MULTI_POWER_TIMING:
             clearTimeout(this.$timer)
@@ -102,29 +80,22 @@ export default {
         clearTimeout(this.$timer)
       })
     },
-    createTasks () {
-      const type = this.cardInfo.type
-      return this.tasks.map(task => {
-        return {
-          ...task,
-          type
-        }
-      })
-    },
     sendTasks (tasks) {
-      const data = []
-      const { connectIndex, portIndex } = this.cardInfo
-      const currTasks = tasks.find(item => item.connectIndex === connectIndex && item.portIndex === portIndex)?.conditions
-      if (currTasks) {
-        const { change, data: newTasks } = this.checkAndTransfromTasks(currTasks)
-        if (change) {
-          this.status = 1
+      if (tasks.length) {
+        let needUpdate = false
+        const data = []
+        for (let i = 0; i < tasks.length; i++) {
+          const { enable, conditions, ...options } = tasks[i]
+          const { change, data: newTasks } = this.checkAndTransfromTasks(conditions)
+          needUpdate = needUpdate || change
           data.push({
-            connectIndex,
-            portIndex,
+            ...options,
             enable: true,
             conditions: newTasks.map(({ status, ...task }) => task)
           })
+        }
+        if (needUpdate) {
+          this.status = 1
           this.$timer = setTimeout(this.retry, 5000)
           this.sendTopic(SET_MULTI_POWER_TIMING, {
             sn: this.device.serialNumber,

+ 27 - 31
src/views/device/power/components/DevicePowerTask.vue

@@ -3,6 +3,7 @@ import { ThirdPartyDevice } from '@/constant'
 import { publish } from '@/utils/mqtt'
 import {
   Status,
+  RELAY_KEY,
   GET_MULTI_POWER_TIMING,
   SET_MULTI_POWER_TIMING,
   addListener,
@@ -73,9 +74,9 @@ export default {
           case GET_MULTI_POWER_TIMING:
             clearTimeout(this.$timer)
             if (this.retryCount > 0) {
-              this.checkTasks(data.data)
+              this.checkTasks(data.data.filter(({ connectIndex, portIndex }) => connectIndex !== RELAY_KEY && portIndex !== RELAY_KEY))
             } else {
-              this.sendTasks(data.data)
+              this.sendTasks(data.data.filter(({ connectIndex, portIndex }) => connectIndex !== RELAY_KEY && portIndex !== RELAY_KEY))
             }
             break
           case SET_MULTI_POWER_TIMING:
@@ -106,31 +107,28 @@ 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
-      if (currTasks) {
-        data.push({
-          connectIndex,
-          portIndex,
-          enable: true,
-          conditions: [
-            ...this.createTasks(),
-            ...currTasks.map(({ status, ...task }) => task)
-          ]
-        })
-      } else {
-        data.push({
-          connectIndex,
-          portIndex,
-          enable: true,
-          conditions: this.createTasks()
-        })
-      }
+      const newTasks = this.createTasks()
       this.$timer = setTimeout(this.retry, 5000)
       this.sendTopic(SET_MULTI_POWER_TIMING, {
         sn: this.device.serialNumber,
-        taskInfo: data
+        taskInfo: tasks.length
+          ? tasks.map(({ enable, conditions, ...options }) => {
+            return {
+              ...options,
+              enable: true,
+              conditions: [
+                ...newTasks,
+                ...conditions.map(({ status, ...task }) => task)
+              ]
+            }
+          })
+          : [{
+            connectIndex,
+            portIndex,
+            enable: true,
+            conditions: newTasks
+          }]
       }).then(messageId => {
         this.$messageId = messageId
         savePowerLogger({
@@ -152,14 +150,12 @@ export default {
       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])) {
+      const map = {}
+      this.tasks.forEach(({ flag }) => {
+        map[flag] = true
+      })
+      for (let i = 0; i < tasks.length; i++) {
+        if (tasks[i].conditions.some(({ flag }) => map[flag])) {
           this.status = 2
           return
         }