zhongrj
2025-03-28 4ff3eee77b41466d08117970970d01be6e48ffe1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { showNotify } from 'vant';
import { onBeforeUnmount, onMounted, ref } from 'vue'
import EventBus from '@/event-bus/'
import { EBizCode, EBizCodeMessage } from '@/types'
import { ControlSource } from '@/types/device'
import {
  ControlSourceChangeInfo,
  ControlSourceChangeType,
  DrcModeExitNotifyMessage,
  DrcStatusNotifyMessage,
  FlyToPointMessage,
  TakeoffToPointMessage
} from '@/types/drone-control'
import { useMyStore } from '@/store'
export interface UseDroneControlWsEventParams {
}
const store = useMyStore()
 
export function useDroneControlWsEvent (sn: string, payloadSn: string, funcs?: UseDroneControlWsEventParams) {
  const droneControlSource = ref(ControlSource.A)
  const payloadControlSource = ref(ControlSource.B)
  function onControlSourceChange (data: ControlSourceChangeInfo) {
    if (data.type === ControlSourceChangeType.Flight && data.sn === sn) {
      droneControlSource.value = data.control_source
      store.commit('SET_DRONE_CONTROL_SOURCE', droneControlSource.value)
      // ElMessage.info(`飞行控制改为 ${droneControlSource.value}`)
      return
    }
    if (data.type === ControlSourceChangeType.Payload && data.sn === payloadSn) {
      payloadControlSource.value = data.control_source
      // ElMessage.info(`负载控制改为 ${payloadControlSource.value}.`)
    }
  }
 
  function handleProgress (key: string, message: string, error: number) {
    if (error !== 0) {
      showNotify({ type: 'danger', message: key })
    } else {
      showNotify({ type: 'danger', message: key })
    }
  }
 
  function handleDroneControlWsEvent (payload: any) {
    if (!payload) {
      return
    }
 
    switch (payload.biz_code) {
      case EBizCode.ControlSourceChange: {
        onControlSourceChange(payload.data)
        break
      }
      case EBizCode.FlyToPointProgress: {
        const { sn: deviceSn, result, message: msg } = payload.data as FlyToPointMessage
        if (deviceSn !== sn) return
        handleProgress(EBizCodeMessage[EBizCode.FlyToPointProgress], `设备(编码: ${deviceSn}) ${msg}`, result)
        break
      }
      case EBizCode.TakeoffToPointProgress: {
        const { sn: deviceSn, result, message: msg } = payload.data as TakeoffToPointMessage
        if (deviceSn !== sn) return
        handleProgress(EBizCodeMessage[EBizCode.TakeoffToPointProgress], `设备(编码: ${deviceSn}) ${msg}`, result)
        break
      }
      case EBizCode.JoystickInvalidNotify: {
        const { sn: deviceSn, result, message: msg } = payload.data as DrcModeExitNotifyMessage
        if (deviceSn !== sn) return
        handleProgress(EBizCodeMessage[EBizCode.JoystickInvalidNotify], `设备(编码: ${deviceSn}) ${msg}`, result)
        break
      }
      case EBizCode.DrcStatusNotify: {
        const { sn: deviceSn, result, message: msg } = payload.data as DrcStatusNotifyMessage
        // handleProgress(EBizCodeMessage[EBizCode.DrcStatusNotify], `device(sn: ${deviceSn}) ${msg}`, result)
 
        break
      }
    }
    // eslint-disable-next-line no-unused-expressions
    // console.log('payload.biz_code', payload.data)
  }
 
  onMounted(() => {
    EventBus.on('droneControlWs', handleDroneControlWsEvent)
  })
 
  onBeforeUnmount(() => {
    EventBus.off('droneControlWs', handleDroneControlWsEvent)
  })
 
  return {
    droneControlSource: droneControlSource,
    payloadControlSource: payloadControlSource
  }
}