chenyao
2025-02-24 a0f008e1b176ab0c844bc4721f282e12d18f855c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<template>
  <div class="ptz-control">
    <div class="btn-group">
      <div class="btn-item" :class="isPtz?'actived-blue':''" @touchstart="ptzClick">
        <img src="@/assets/images/yt.png" />
      </div>
      <div class="btn-item" @touchstart="onTakeCameraPhoto">
        <img src="@/assets/images/photo.png" />
      </div>
      <div class="btn-item" v-if="!isCameraRecording" @touchstart="onStartCameraRecording">
        <img src="@/assets/images/sx-open.png" />
      </div>
      <div class="btn-item" v-else @touchstart="onStopCameraRecording">
        <img src="@/assets/images/sx-close.png"/>
      </div>
      <div class="btn-item" @touchstart="onZoom(true)">
        <img src="@/assets/images/fangda.png" />
      </div>
      <div class="btn-item" @touchstart="onZoom(false)">
        <img src="@/assets/images/suoxiao.png" />
      </div>
      <!-- <div class="btn-item">
        <img v-if="!isBigVideo" src="@/assets/images/fang.png" @touchstart="changeModelVideo(true)" />
        <img v-else src="@/assets/images/suo.png" @touchstart="changeModelVideo(false)" />
      </div> -->
    </div>
    <div class="circle-box" v-show="isPtz">
      <div class="top" @click="ytClick('up')"></div>
      <div class="left" @click="ytClick('left')"></div>
      <div class="bottom" @click="ytClick('down')"></div>
      <div class="right" @click="ytClick('right')"></div>
      <div class="reset-center" @click="onResetGimbal(0)">
        <el-icon><Refresh /></el-icon>
      </div>
      <div class="blue-bgc"></div>
    </div>
  </div>
</template>
 
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue';
import { ControlSource, EModeCode } from '@/types/device'
import { CameraMode } from '@/types/live-stream'
import { ref, onMounted, onBeforeUnmount, nextTick, reactive, computed, watch } from 'vue';
import { postPayloadCommands, postPayloadAuth } from '@/api/drone/payload'
import { switchLivestream } from '@/api/drone'
import { usePayloadControl } from './use-payload-control'
import { useDroneControlWsEvent } from '@/utils/websocket/drone-ws-control'
import cesiumOperation from '@/utils/cesium-tsa.js';
import { useMyStore } from '@/store'
import { fa } from 'element-plus/es/locale';
 
const { loadLAYER } = cesiumOperation()
 
const {
  checkPayloadAuth,
  authPayload,
  resetGimbal,
  switchCameraMode,
  takeCameraPhoto,
  startCameraRecording,
  stopCameraRecording,
  changeCameraFocalLength,
} = usePayloadControl()
 
const props = defineProps({
  sn: {
    type: String,
    required: true,
  },
  osdVisible: {
    type: Object,
    required: true,
  },
  cesiumViewe: {
    type: Object,
    required: true,
  }
});
const emitEvents = defineEmits(['changeModelVideo'])
 
// 放大和缩小
// let isBigVideo = ref<Boolean>(false)
 
// 录像
let isCameraRecording = ref<Boolean>(false)
// 
let isActionComplete = ref<Boolean>(false)
 
let payloadCameraMode = ref<String>('wide')
 
// 缩放监听更新值
let zoom_factor_watch = ref(0)
// 缩放初始值
let zoom_factor_start = ref(0)
// 缩放变化值
let zoom_factor = ref(0)
// is_First 记录第一次
let is_First = ref<Boolean>(false)
 
// 是否显示云台图标
let isPtz = ref(false)
 
// 显示高德地图矢量还是影像(2D/3D)
// let is2d = ref(true)
 
let yaw_speed = ref((payloadCameraMode.value == 'wide'? 1 : Math.ceil(zoom_factor.value))*10) // 云台横向角度
let pitch_speed = ref((payloadCameraMode.value == 'wide'? 1 : Math.ceil(zoom_factor.value))*10) // 云台纵向角度
 
let resultProps = reactive<any>(props.osdVisible.children.payloads_list[0])
// console.log(resultProps, '现在勒')
const store = useMyStore()
// 负载控制
const payloadSelectInfo = reactive<any>({
  value: resultProps.payload_sn,
  controlSource: resultProps.control_source,
  options: [],
  payloadIndex: resultProps.payload_index,
  camera: '', // 当前负载osd信息
})
 
watch(() => store.state.wsMessage, (newValue, oldValue) => {
  if (newValue && newValue.cameras) {
    // console.log('监控值', newValue.cameras)
    zoom_factor_watch.value = Math.ceil(newValue.cameras[0].zoom_factor)
    if (!is_First.value) {
      zoom_factor.value = zoom_factor_watch.value
      zoom_factor_start.value = zoom_factor_watch.value
      is_First.value = true
    }
    
    payloadSelectInfo.camera = newValue.cameras.find(
      (item:any) =>
        item.payload_index === payloadSelectInfo.payloadIndex,
    )
  } else {
    payloadSelectInfo.camera = undefined
  }
},
  deep: true,
})
// ws 消息通知
const { droneControlSource, payloadControlSource } = useDroneControlWsEvent(props.sn, payloadSelectInfo.value)
watch(() => payloadControlSource, (controlSource) => {
  payloadSelectInfo.controlSource = 'A'// controlSource.value
}, {
  immediate: true,
  deep: true
})
 
 
// 切换是否出现云台上下左右图
const ptzClick = () => {
  isPtz.value = !isPtz.value
}
 
// 拍照
const onTakeCameraPhoto =  () => {
  if (!checkPayloadAuth(payloadSelectInfo.controlSource)) {
    return
  }
 
  if (isActionComplete.value) {
    ElMessage.warning('当前相机动作暂未完成,请勿点击!!')
    return
  }
  if (payloadSelectInfo.camera?.recording_state) {
    ElMessage.warning('当前摄像头正在录制视频,请先结束视频录制!!')
    return
  }
 
  if (payloadSelectInfo.camera?.camera_mode !== CameraMode.Photo) {
    const res = switchCamera(CameraMode.Photo)
    res.then((response:any) => {
      if (!response) return
 
      if (payloadSelectInfo.camera?.camera_mode !== CameraMode.Photo) {
        curTaskCameraPhoto(2000)
      } else {
        curTaskCameraPhoto(0)
      }
    })
 
    return
  }
 
  curTaskCameraPhoto(0)
}
const curTaskCameraPhoto =  (time:any) => {
  isActionComplete.value = true
 
  ElMessage({
    message: '请稍后,正在拍摄中。。。',
    duration: 2000,
  })
  setTimeout(() => {
    let cameraType = ref('wide')
    if (payloadCameraMode.value === 'zoom') {
      cameraType.value = payloadCameraMode
    }
    console.log('1111',cameraType.value)
    takeCameraPhoto(
      props.osdVisible.dockSn,
      payloadSelectInfo.payloadIndex,
      cameraType.value
    ).then((photoRes:any) => {
      if (!photoRes) {
        return
      }
 
      isActionComplete.value = false
    })
  }, time)
}
const switchCamera =  (camera_mode:any) => {
  if (!checkPayloadAuth(payloadSelectInfo.controlSource)) {
    return
  }
  return switchCameraMode(props.osdVisible.dockSn, {
    payload_index: payloadSelectInfo.payloadIndex,
    camera_mode: camera_mode,
    video_type: payloadCameraMode.value,
  })
}
 
// 相机操作
const onAuthPayload = ()  => {
  //负载控制
  authPayload(props.osdVisible.dockSn, payloadSelectInfo.payloadIndex).then(
    (payloadRes:any) => {
      if (payloadRes.code !== 0)
        return ElMessage.error('获得有效载荷控制失败,请稍后重试!!')
      payloadControlSource.value = ControlSource.A
      ElMessage.success('成功获得有效载荷控制')
    },
  )
}
// 开始录像
const onStartCameraRecording = () => {
  if (!checkPayloadAuth(payloadSelectInfo.controlSource)) {
    return
  }
  if (isActionComplete.value) {
    ElMessage.warning('当前相机动作暂未完成,请勿点击!!')
    return
  }
 
  if (payloadSelectInfo.camera?.recording_state) {
    return ElMessage.warning(
      '当前摄像头正在录制视频,请勿重复点击!!',
    )
  }
 
  if (payloadSelectInfo.camera?.camera_mode !== CameraMode.Video) {
    const res = switchCamera(CameraMode.Video)
    res.then((response) => {
      if (!response) return
      if (payloadSelectInfo.camera?.camera_mode !== CameraMode.Video) {
        curStartCameraRecording(2000)
      } else {
        curStartCameraRecording(0)
      }
    })
 
    return
  }
 
  curStartCameraRecording(0)
}
 
  const curStartCameraRecording = (time:any) => {
    isActionComplete.value = true
 
    ElMessage({
      message: '请稍后,正在开启视频录制。。。',
      duration: 2000,
    })
 
    setTimeout(() => {
      startCameraRecording(props.osdVisible.dockSn, payloadSelectInfo.payloadIndex)
        .then((res) => {
          if (res.code !== 0)
            return ElMessage.warning('开始录制失败,请稍后重试!!')
          ElMessage.success('开始录制成功')
          // 开始录像
          isCameraRecording.value = true
          isActionComplete.value = false
        }).catch((e) => {
          isActionComplete.value = false
        })
    }, time)
  }
 
// 停止录像
const onStopCameraRecording = () => {
  if (!checkPayloadAuth(payloadSelectInfo.controlSource)) {
    return
  }
  if (isActionComplete.value) {
    ElMessage.warning('当前相机动作暂未完成,请勿点击!!')
    return
  }
  isActionComplete.value = true
  stopCameraRecording(props.osdVisible.dockSn, payloadSelectInfo.payloadIndex)
    .then((res) => {
      if (res.code !== 0)
        return ElMessage.warning('停止录制失败,请稍后重试!!')
      ElMessage.success('停止录制成功')
      // 停止录像
      isCameraRecording.value = false
      isActionComplete.value = false
    })
    .catch((e) => {
      isActionComplete.value = false
    })
}
    
 
// 缩放
const onZoom = (value:any) => {
  if (value) {
    zoom_factor.value += 1
    if (zoom_factor.value >= 200) {
      zoom_factor.value = 200
    }
  } else {
    zoom_factor.value -= 1
    if (zoom_factor.value <= 2) {
      zoom_factor.value = 2
    }
    console.log('9999', zoom_factor.value)
  }
  switchLivestream({
    video_id: `${props.osdVisible.dockSn}/${payloadSelectInfo.payloadIndex}/normal-0`,
    video_type: 'zoom',
  }).then((res:any) => {
    if (res.data.code != 0) return
    // 只考虑 广角:wide模式
    changeCameraFocalLength(props.osdVisible.dockSn, {
        payload_index: payloadSelectInfo.payloadIndex,
        camera_type: 'zoom',
        zoom_factor: zoom_factor.value,
      }, value)
  })
}
 
// const onChangeD = () => {
//   is2d.value = !is2d.value
//   store.commit('SET_MAP_SETTING_MODE', is2d.value?2:3);
//   loadLAYER();
// }
 
// 重置云台角度
const onResetGimbal = async (resetMode:any) => {
  try {
    await resetGimbal(props.osdVisible.dockSn, {
      payload_index: payloadSelectInfo.payloadIndex,
      reset_mode: resetMode,
    })
    await changeCameraFocalLength(props.osdVisible.dockSn, {
      payload_index: payloadSelectInfo.payloadIndex,
      camera_type: 'zoom',
      zoom_factor: zoom_factor_start.value,
    })
    // this.zoomMultiple = 8 // 设置步长
  } catch (err) {
    console.log(err)
  }
}
// 记得加loading
// 云台上 下 左 右
const ytClick = async (value:String) => {
  if (payloadCameraMode.value == 'zoom' && zoom_factor.value > 35 && (value == 'up' || value == 'right')) { pitch_speed.value = 350 }
  if (payloadCameraMode.value == 'zoom' && zoom_factor.value > 35 && (value == 'left' || value == 'down')) { pitch_speed.value = -350 }
  if (value == 'up' || value == 'down') {
    postPayloadCommands(props.osdVisible.dockSn, {cmd: 'camera_screen_drag',
    data: {
      locked: false,
      payload_index: payloadSelectInfo.payloadIndex,
      pitch_speed: value == 'up' ? pitch_speed.value : -pitch_speed.value,
      yaw_speed: 0,
    }})
  } else if ((value == 'left' || value == 'right')) {
    postPayloadCommands(props.osdVisible.dockSn, {cmd: 'camera_screen_drag',
    data: {
      locked: false,
      payload_index: payloadSelectInfo.payloadIndex,
      pitch_speed: 0,
      yaw_speed: value == 'right' ? pitch_speed.value : -pitch_speed.value,
    }})
  } 
}
 
// 视频大小的缩放
// const changeModelVideo = (value:Boolean) => {
//   isBigVideo.value = value
//   emitEvents('changeModelVideo', value)
// }
 
</script>
 
<style lang="scss" scoped>
.ptz-control {
  .btn-group {
    position: absolute;
    right: 0.2rem;
    bottom: 0rem;
    .btn-item {
      width: 2rem;
      height: 2rem;
      border-radius: 3px;
      background-color: rgba(0, 0, 0, 0.5);
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      cursor: pointer;
      margin-bottom: 0.5rem;
      pointer-events: all;
      img {
        width: 1.6rem;
        height: 1.6rem;
      }
    }
    .actived-blue {
      background-image: none;
      background-color: rgba(23, 124, 198, 0.7);
    }
  }
  .circle-box {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 20%;
    left: 1%;
 
    .top,
    .left,
    .bottom,
    .right {
      width: 10px;
      height: 10px;
      position: absolute;
      cursor: pointer;
      z-index: 2;
    }
 
    .top {
      top: -2px;
      left: 50%;
      transform: translateX(-50%);
      border-top: 10px solid transparent;
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
      border-bottom: 10px solid #fff;
 
      &:hover {
        &~.blue-bgc {
          border-top: 50px solid #409eff;
        }
      }
    }
 
    .left {
      top: 50%;
      left: -2px;
      transform: translateY(-50%);
      border-top: 10px solid transparent;
      border-right: 10px solid #fff;
      border-left: 10px solid transparent;
      border-bottom: 10px solid transparent;
 
      &:hover {
        &~.blue-bgc {
          border-left: 50px solid #409eff;
        }
      }
    }
 
    .bottom {
      bottom: -2px;
      left: 50%;
      transform: translateX(-50%);
      border-top: 10px solid #fff;
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
      border-bottom: 10px solid transparent;
 
      &:hover {
        &~.blue-bgc {
          border-bottom: 50px solid #409eff;
        }
      }
    }
 
    .right {
      top: 50%;
      right: -2px;
      transform: translateY(-50%);
      border-top: 10px solid transparent;
      border-right: 10px solid transparent;
      border-left: 10px solid #fff;
      border-bottom: 10px solid transparent;
 
      &:hover {
        &~.blue-bgc {
          border-right: 50px solid #409eff;
        }
      }
    }
 
    .reset-center {
      position: absolute;
      width: 40%;
      height: 40%;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      background-color: rgb(63, 66, 68);
      z-index: 2;
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
 
      i {
        color: #fff;
        font-size: 20px;
        font-weight: bolder;
      }
 
      &:hover {
        background-color: #409eff;
      }
    }
 
    .blue-bgc {
      width: 100px;
      height: 100px;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
      border-top: 50px solid transparent;
      border-right: 50px solid transparent;
      border-left: 50px solid transparent;
      border-bottom: 50px solid transparent;
    }
  }
}
</style>