Kaynağa Gözat

fix: receiving card status cache

Casper Dai 1 yıl önce
ebeveyn
işleme
9ffd76af7a

+ 8 - 4
src/components/service/FullLink/index.vue

@@ -61,13 +61,14 @@
 </template>
 
 <script>
-import { ThirdPartyDevice } from '@/constant'
+import { ThirdPartyDevice } from '@/constant.js'
 import {
   Status,
-  Power
+  Power,
+  getReceivingCardStatusFromServer
 } from '@/utils/adapter'
-import { getThirdPartyDevicesByThirdPartyDevice } from '@/api/mesh'
-import { getDevice } from '@/api/device'
+import { getThirdPartyDevicesByThirdPartyDevice } from '@/api/mesh.js'
+import { getDevice } from '@/api/device.js'
 
 const LinkItems = Object.freeze([
   {
@@ -408,6 +409,9 @@ export default {
           by
         }
         this.onUpdateLed()
+        if (this.linkDeviceMap[ThirdPartyDevice.RECEIVING_CARD] > Status.NONE) {
+          getReceivingCardStatusFromServer(this.device.id)
+        }
         this.loaded = true
         return true
       }).finally(() => {

+ 45 - 19
src/utils/adapter/monitor.js

@@ -22,8 +22,8 @@ import {
   getReceivingCardStatusByMessage
 } from './nova.js'
 
-export const TIMEOUT_MILLISECOND = 120000
-export const EXPIRED_MILLISECOND = 300000
+export const TIMEOUT_MILLISECOND = 300000
+export const EXPIRED_MILLISECOND = 600000
 
 const deviceCache = new Map()
 const cbMap = new Map()
@@ -169,6 +169,13 @@ function getFreshCacheById (id) {
       }
       update = true
     }
+    if (cache[ThirdPartyDevice.RECEIVING_CARD].status > Status.LOADING && (Date.now() - cache[ThirdPartyDevice.RECEIVING_CARD].timestamp > EXPIRED_MILLISECOND)) {
+      cache[ThirdPartyDevice.MULTI_FUNCTION_CARD] = {
+        status: Status.LOADING,
+        receivers: []
+      }
+      update = true
+    }
     if (cache.screen && (Date.now() - cache.screen.timestamp > EXPIRED_MILLISECOND)) {
       cache.screen = null
       update = true
@@ -191,15 +198,16 @@ export function resetCacheById (id) {
   }
 }
 
-function checkMultiCard (id, multiCard) {
-  switch (multiCard.status) {
+function checkStatus (id, type, timeout = TIMEOUT_MILLISECOND) {
+  const data = getCacheById(id)[type]
+  switch (data.status) {
     case Status.LOADING:
-      console.log('monitor', id, '->', 'add task')
-      addTask(id)
+      console.log('monitor', id, '->', 'add task', type)
+      addTask(id, type)
       break
     case Status.OK:
-      if (Date.now() - multiCard.timestamp > TIMEOUT_MILLISECOND) {
-        multiCard.status = Status.WARNING
+      if (timeout && Date.now() - data.timestamp > timeout) {
+        data.status = Status.WARNING
       }
       break
     default:
@@ -255,7 +263,8 @@ export function addListener (id, cb) {
   console.log('monitor add', id, '->', 'success')
   const cache = getFreshCacheById(id)
   cbSet.add(cb)
-  checkMultiCard(id, cache[ThirdPartyDevice.MULTI_FUNCTION_CARD])
+  checkStatus(id, ThirdPartyDevice.MULTI_FUNCTION_CARD)
+  checkStatus(id, ThirdPartyDevice.RECEIVING_CARD)
   cb(cache)
 }
 
@@ -335,7 +344,14 @@ let queue = new Set()
 let fetching = false
 let started = false
 
-function addTask (id) {
+function addTask (id, type) {
+  // 暂只支持多功能卡
+  switch (type) {
+    case ThirdPartyDevice.MULTI_FUNCTION_CARD:
+      break
+    default:
+      return
+  }
   if (fetching) {
     if (queue.has(id)) {
       console.log('task exsit', id)
@@ -442,16 +458,26 @@ function startTask () {
 }
 
 export function getReceivingCardStatusFromServer (id) {
-  getReceivingCardStatusReport(id, { background: true, custom: true }).then(res => {
-    if (res.data && getCacheById(id)[ThirdPartyDevice.RECEIVING_CARD].status !== Status.LOADING) {
-      const message = JSON.parse(res.data)
-      const data = getReceivingCardStatusByMessage(message)
-      if (data.success) {
-        emit(id, ThirdPartyDevice.RECEIVING_CARD, {
-          timestamp: message.timestamp,
-          ...data.data
-        })
+  getReceivingCardStatusReport(id, { custom: true }).then(res => {
+    if (!res.data) {
+      return
+    }
+    const message = JSON.parse(res.data)
+    const { timestamp } = getCacheById(id)[ThirdPartyDevice.RECEIVING_CARD]
+    console.log(timestamp, message.timestamp, message.timestamp <= timestamp)
+    if (timestamp && message.timestamp <= timestamp) {
+      return
+    }
+    const data = getReceivingCardStatusByMessage(message)
+    if (data.success) {
+      const info = {
+        timestamp: message.timestamp,
+        ...data.data
+      }
+      if (Date.now() - message.timestamp > TIMEOUT_MILLISECOND) {
+        info.status = Status.WARNING
       }
+      emit(id, ThirdPartyDevice.RECEIVING_CARD, info)
     }
   })
 }

+ 1 - 1
src/utils/mqtt.js

@@ -73,7 +73,7 @@ function start () {
   const client = createClient({
     will: {
       topic: WillTopic,
-      payload: 'Connection Closed abnormally..!',
+      payload: `${store.getters.account} -> Connection closed abnormally!`,
       qos: 0,
       retain: false
     }