songshankun
2023-11-02 7e7424b2662e5b27dcc1c0d37f43e909e0b15ee1
src/stores/devices.ts
@@ -1,23 +1,35 @@
import { ref, computed } from 'vue'
import { computed, onUnmounted } from 'vue'
import { defineStore } from 'pinia'
import type { Devices } from '@/api/device'
import { getDeviceList } from '@/api'
import { useRequest } from 'vue-hooks-plus'
import type { Devices } from '@/api/device'
export const useDevicesStore = defineStore('counter', () => {
  const devices = ref<Devices>()
  const deviceIDList = computed(() => devices?.value?.deviceIDList ?? [])
export const useDevicesStore = defineStore('device', () => {
  const deviceInfo = computed(() => {
    return deviceRes?.value?.data as Devices
  })
  function getDevicesInfo() {
    getDeviceList().then(
      (res) => {
        devices.value = res?.data
      },
      (err) => {
        console.error(err)
        devices.value = undefined
      }
    )
  /**
   * 如果任务状态是进行中, 则轮询 plc 取进度
   */
  const {
    data: deviceRes,
    run: startDevicePolling,
    cancel: cancelDevicePolling
  } = useRequest(getDeviceList, {
    manual: true,
    pollingInterval: 6000,
    pollingWhenHidden: false
  })
  function startPollingDevice() {
    cancelDevicePolling()
    startDevicePolling()
  }
  return { devices, deviceIDList, getDevicesInfo }
  onUnmounted(() => {
    cancelDevicePolling()
  })
  return { deviceInfo, startPollingDevice }
})