haoxuan
2023-11-06 75c41cc8a2f25cff74e976cdc7851cbaddd97b3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { computed } from 'vue'
import { defineStore } from 'pinia'
import { getDeviceList } from '@/api'
import { useRequest } from 'vue-hooks-plus'
import type { Devices } from '@/api/device'
import { DEVICE_INFO_POLLING_DURATION } from '@/common/constants'
 
export const useDevicesStore = defineStore('device', () => {
  const deviceInfo = computed(() => {
    if (deviceInfo?.value) {
      deviceRes.value.data.currentDeviceID = 'wwwwwwww222222'
    }
    return deviceRes?.value?.data as Devices
  })
 
  /**
   * 如果任务状态是进行中, 则轮询 plc 取进度
   */
  const {
    data: deviceRes,
    run: startDevicePolling,
    cancel: cancelDevicePolling
  } = useRequest(getDeviceList, {
    manual: true,
    pollingInterval: DEVICE_INFO_POLLING_DURATION,
    pollingWhenHidden: false
  })
 
  function startPollingDevice() {
    cancelDevicePolling()
    startDevicePolling()
  }
 
  return { deviceInfo, startPollingDevice }
})