songshankun
2023-11-20 3a3cc473c33cb4a97399ace76a1b35e9ffd68525
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
36
37
38
39
40
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(() => {
    return deviceRes?.value?.data as Devices
  })
 
  // 当前设备在缺少工艺参数的时候是否允许下发生产
  const currentDeviceAllowNoParams = computed(() => {
    const currentDeviceInfo = deviceInfo.value.deviceList?.find((ele) => {
      return ele.deviceID === deviceInfo.value.currentDeviceID
    })
    return !currentDeviceInfo?.needSetProcessParams
  })
 
  /**
   * 如果任务状态是进行中, 则轮询 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, currentDeviceAllowNoParams }
})