Kaynağa Gözat

feat(cache): the expiration time for information such as multi-functional cards is 5 minutes

Casper Dai 2 yıl önce
ebeveyn
işleme
43a97d8f8a
2 değiştirilmiş dosya ile 41 ekleme ve 14 silme
  1. 36 13
      src/utils/adapter/monitor.js
  2. 5 1
      src/views/external/mesh/index.vue

+ 36 - 13
src/utils/adapter/monitor.js

@@ -13,15 +13,14 @@ import {
 } from './constant'
 import {
   GET_POWER_STATUS,
-  GET_RECEIVER_TOPOLOGY,
   GET_RECEIVER_INFO,
   parseCachePower,
   getPowerStatusByMessage,
-  getReceivingCardTopologyByMessage,
   getReceivingCardStatusByMessage
 } from './nova'
 
 const TIMEOUT_MILLISECOND = 120000
+const EXPIRED_MILLISECOND = 300000
 
 const deviceCache = new Map()
 const cbMap = new Map()
@@ -106,14 +105,6 @@ export function startMonitor () {
           ...data.data
         })
       }
-    } else if (message.function === GET_RECEIVER_TOPOLOGY) {
-      const data = getReceivingCardTopologyByMessage(message)
-      if (data.success) {
-        emit(id, 'topology', {
-          timestamp,
-          ...data.data
-        })
-      }
     } else {
       injectMap.get(id)?.forEach(cb => {
         cb(message)
@@ -143,13 +134,46 @@ function getCacheById (id) {
         status: Status.LOADING,
         receivers: []
       },
-      topology: null,
       screen: null
     })
   }
   return cache
 }
 
+function getFreshCacheById (id) {
+  const cache = deviceCache.get(id)
+  if (cache) {
+    let update = false
+    if (cache[ThirdPartyDevice.MULTI_FUNCTION_CARD].status > Status.LOADING && (Date.now() - cache[ThirdPartyDevice.MULTI_FUNCTION_CARD].timestamp > EXPIRED_MILLISECOND)) {
+      cache[ThirdPartyDevice.MULTI_FUNCTION_CARD] = {
+        status: Status.LOADING,
+        switchStatus: Power.LOADING,
+        powers: []
+      }
+      update = true
+    }
+    if (cache.screen && (Date.now() - cache.screen.timestamp > EXPIRED_MILLISECOND)) {
+      cache.screen = null
+      update = true
+    }
+    if (update) {
+      cbMap.get(id)?.forEach(cb => {
+        cb(cache)
+      })
+    }
+    return cache
+  }
+  return getCacheById(id)
+}
+
+export function resetCacheById (id) {
+  console.log('monitor', id, '->', 'reset cache')
+  const cache = deviceCache.get(id)
+  if (cache) {
+    deviceCache.delete(id)
+  }
+}
+
 function checkMultiCard (id, multiCard) {
   switch (multiCard.status) {
     case Status.LOADING:
@@ -212,8 +236,8 @@ export function addListener (id, cb) {
     return
   }
   console.log('monitor add', id, '->', 'success')
+  const cache = getFreshCacheById(id)
   cbSet.add(cb)
-  const cache = getCacheById(id)
   checkMultiCard(id, cache[ThirdPartyDevice.MULTI_FUNCTION_CARD])
   cb(cache)
 }
@@ -231,7 +255,6 @@ function doUnsubscribe () {
           )
           cbMap.delete(id)
           injectMap.delete(id)
-          deviceCache.delete(id)
         })
         client.unsubscribe(topics)
         unsubscribeIds = new Set()

+ 5 - 1
src/views/external/mesh/index.vue

@@ -231,6 +231,7 @@ import {
   SensorToThirdPartyMap,
   ThirdPartyToCameraMap
 } from '@/constant'
+import { resetCacheById } from '@/utils/adapter'
 import {
   getMesh,
   addNodeToMesh,
@@ -694,8 +695,11 @@ export default {
     checkMesh (links, nodes) {
       console.log(links, nodes)
       const nodeMap = {}
-      nodes.forEach(({ id }) => {
+      nodes.forEach(({ id, nodeType, instanceId }) => {
         nodeMap[id] = 1
+        if (nodeType === ThirdPartyDevice.BOX && instanceId) {
+          resetCacheById(instanceId)
+        }
       })
       const invalidLinks = []
       const invalidPorts = []