xieb
2023-09-13 3667807a7b7418efc090ee3fa6a6b734bc3080bf
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
import { ref, onMounted, onBeforeUnmount } from 'vue'
import EventBus from '/@/event-bus/'
import {
  DRC_METHOD,
  DRCHsiInfo,
  DRCOsdInfo,
  DRCDelayTimeInfo,
  DrcResponseInfo,
} from '/@/types/drc'
 
export function useDroneControlMqttEvent (sn: string) {
  const drcInfo = ref('')
  const hsiInfo = ref('')
  const osdInfo = ref('')
  const delayInfo = ref('')
  const errorInfo = ref('')
 
  function handleHsiInfo (data: DRCHsiInfo) {
    hsiInfo.value = `method: ${DRC_METHOD.HSI_INFO_PUSH}\r\n ${JSON.stringify(data)}\r\n `
  }
 
  function handleOsdInfo (data: DRCOsdInfo) {
    osdInfo.value = `method: ${DRC_METHOD.OSD_INFO_PUSH}\r\n ${JSON.stringify(data)}\r\n `
  }
 
  function handleDelayTimeInfo (data: DRCDelayTimeInfo) {
    delayInfo.value = `method: ${DRC_METHOD.DELAY_TIME_INFO_PUSH}\r\n ${JSON.stringify(data)}\r\n `
  }
 
  function handleDroneControlErrorInfo (data: DrcResponseInfo) {
    if (!data.result) {
      return
    }
    errorInfo.value = `Drc error code: ${data.result}, seq: ${data.output?.seq}`
  }
 
  function handleDroneControlMqttEvent (payload: any) {
    if (!payload || !payload.method) {
      return
    }
 
    switch (payload.method) {
      case DRC_METHOD.HSI_INFO_PUSH: {
        handleHsiInfo(payload.data)
        break
      }
      case DRC_METHOD.OSD_INFO_PUSH: {
        handleOsdInfo(payload.data)
        break
      }
      case DRC_METHOD.DELAY_TIME_INFO_PUSH: {
        handleDelayTimeInfo(payload.data)
        break
      }
      case DRC_METHOD.DRONE_EMERGENCY_STOP:
      case DRC_METHOD.DRONE_CONTROL: {
        handleDroneControlErrorInfo(payload.data)
        break
      }
    }
    drcInfo.value = hsiInfo.value + osdInfo.value + delayInfo.value
  }
 
  onMounted(() => {
    EventBus.on('droneControlMqttInfo', handleDroneControlMqttEvent)
  })
 
  onBeforeUnmount(() => {
    EventBus.off('droneControlMqttInfo', handleDroneControlMqttEvent)
  })
 
  return {
    drcInfo: drcInfo,
    errorInfo: errorInfo
  }
}