songshankun
2023-11-03 35b1ce8f65b78fcdfdd56481b29ed8d74a8a6621
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
import { computed, onUnmounted, ref } from 'vue'
import { defineStore } from 'pinia'
import { apiSetCurrentDevice, getDeviceList } from '@/api'
import type { SetCurrentDeviceParams } from '@/api'
import { useRequest } from 'vue-hooks-plus'
import type { Devices } from '@/api/device'
import { DEVICE_INFO_POLLING_DURATION } from '@/common/constants'
import { ElMessage } from 'element-plus'
 
export const useDevicesStore = defineStore('device', () => {
  const deviceInfo = computed(() => {
    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 }
})