chenyao
2025-02-14 58aeb1b4b7fef38bfbba99231c46d1c6572e8712
src/views/hooks/droneFly.ts
@@ -5,6 +5,7 @@
import IndexedDBService from '@/utils/indexDB'
import CreateFrustum from '@/utils/cesium/frustum/CreateFrustum'
import { useMyStore } from '@/store'
import { getTwoPointInfo } from '@/api/manage'
const { getEntityById, removeById, globalCesium, addLeftClickEvent, removeLeftClickEvent, flyTo } = cesiumOperation()
// 无人机实体
@@ -12,6 +13,10 @@
// 激光实体
let laserEntity = null
// 两点之间线实体
let twoLineEntity = null
let myAppEntity = null;
export function droneFly () {
  const store = useMyStore()
  let deviceInfo = {} // 无人机信息
@@ -30,6 +35,16 @@
  let lastTime = null
  let viewInfoFrustum = null
  // 手机设备定位
  let me_latitude = 0;
  let me_longitude = 0;
  let me_altitude = 0;
  // 扫射定位
  let measure_target_longitude = 0;
  let measure_target_latitude = 0;
  let measure_target_altitude = 0;
  async function initDock(data:any, sn:any, dockSn:any, workspaceId:any, ssLinePath:any, viewDroneInfo:any) {
    // 获取workspaceId
    wId = workspaceId
@@ -38,7 +53,7 @@
    // 机场信息
    dockInfo = data.dockInfo[`${dockSn}`]
    viewDroneInfo = viewDroneInfo
    console.log('打印我想看的',viewDroneInfo.longitude)
    console.log('导致地图不显示原因1',viewDroneInfo.longitude)
    // 当前飞行任务创建时间
    if (data.currentType === EDeviceTypeName.Aircraft && deviceInfo) {
      const droneLngLat = {
@@ -64,16 +79,12 @@
            dockInfo,
            ssLinePath
          )
          addFlyLaser(
            viewDroneInfo.longitude,
            viewDroneInfo.latitude,
            deviceInfo.height,
            deviceInfo.payloads[0],
            deviceInfo.attitude_head,
            deviceInfo,
            dockInfo,
            ssLinePath
          )
          let domain = sessionStorage.getItem('domain')
          if (domain !== '0') {
            addFlyLaser(
              deviceInfo.payloads[0]
            )
          }
          setelCamera(
            viewDroneInfo.longitude,
            viewDroneInfo.latitude,
@@ -95,7 +106,6 @@
      } else {
        if (deviceInfo.mode_code && [3, 4, 5, 9, 10].includes(deviceInfo.mode_code)) {
          initCreateFrustum(deviceInfo, viewDroneInfo)
          console.log('4444', viewDroneInfo.longitude)
          addFlyGltf(
            viewDroneInfo.longitude,
            viewDroneInfo.latitude,
@@ -106,16 +116,12 @@
            dockInfo,
            ssLinePath
          )
          addFlyLaser(
            viewDroneInfo.longitude,
            viewDroneInfo.latitude,
            deviceInfo.height,
            deviceInfo.payloads[0],
            deviceInfo.attitude_head,
            deviceInfo,
            dockInfo,
            ssLinePath
          )
          let domain = sessionStorage.getItem('domain')
          if (domain !== '0') {
            addFlyLaser(
              deviceInfo.payloads[0]
            )
          }
        }
      }
      const imgRightBlue = new URL('@/assets/images/arrow-right-pink.png', import.meta.url).href
@@ -188,18 +194,24 @@
  const imgLaser = new URL('@/assets/images/laser.png', import.meta.url).href
  // 增加激光扫射点
  function addFlyLaser(lng:any, lat:any, alt:any, payloads:any, attitude_head:any, deviceInfo:any, dockInfo:any, ssLinePath:any) {
    console.log('哒哒哒', payloads)
  function addFlyLaser(payloads:any) {
    const startPosition = globalCesium.Cartesian3.fromDegrees(
      me_longitude, me_latitude, 0
    )
    const position = globalCesium.Cartesian3.fromDegrees(
      payloads.measure_target_longitude, payloads.measure_target_latitude, payloads.measure_target_altitude
    )
    measure_target_longitude = payloads.measure_target_longitude;
    measure_target_latitude = payloads.measure_target_latitude;
    measure_target_altitude = payloads.measure_target_altitude;
    if (laserEntity && laserEntity != null) {
      laserEntity.position = new globalCesium.CallbackProperty(function () {
        return position
      }, false)
      laserEntity.orientation = new globalCesium.CallbackProperty(function () {
        return orientation
      }, false)
      // laserEntity.orientation = new globalCesium.CallbackProperty(function () {
      //   return orientation
      // }, false)
  
      laserEntity.show = true
  
@@ -217,6 +229,20 @@
        }
      })
    }
    removeById('route_two_line')
    twoLineEntity = window.cesiumViewer.entities.add({
      id: 'route_two_line',
      polyline: {
        positions: new globalCesium.CallbackProperty(() => [startPosition, position], false),
        width: 5,
        material: new globalCesium.PolylineDashMaterialProperty({
            color: globalCesium.Color.YELLOW, // 虚线颜色
            dashLength: 6.0, // 虚线段的长度
        }),
        zIndex: 1,
        clampToGround: false,
      }
    })
    // 增加激光扫射信息
    laserEntity.label = new globalCesium.LabelGraphics({
      text: `目标海拔:${Math.ceil(payloads.measure_target_altitude)}m\n测距距离:${Math.round(
@@ -286,7 +312,6 @@
  async function createDroneEntityLabel (entity:any, deviceInfo:any, dockInfo:any, ssLinePath:any) {
    deviceInfo = deviceInfo
    if (currentTaskCreateTime === null) return
    console.log('77777', deviceInfo?.basic_osd?.longitude)
    if (!deviceInfo?.longitude && !deviceInfo?.latitude) return
    if (deviceInfo.mode_code !== 5) {
      currentTaskCreateTime = null
@@ -538,7 +563,96 @@
      heading: attitude_head,
    })
  }
  // 获取手机定位和激光发射定位的高度和距离
  function getTwoHeightWidth(workspaceId:String) {
    getTwoPointInfo(workspaceId,me_latitude,me_longitude,measure_target_latitude,measure_target_longitude).then((res:any) => {
      const heightDifference = res.data.heightDifference
      const distance = res.data.distance
      // 增加激光扫射信息
      myAppEntity.label = new globalCesium.LabelGraphics({
        text: `高度:${Math.ceil(heightDifference)}m\n距离:${Math.round(
          Math.ceil(distance),
        )}m`,
        font: '13px monospace',
        showBackground: true,
        horizontalOrigin: globalCesium.HorizontalOrigin.CENTER,
        verticalOrigin: globalCesium.VerticalOrigin.BOTTOM,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
        pixelOffset: new globalCesium.Cartesian2(0, -40),
        show: true,
      })
    })
  }
  const meMapSrc = new URL('@/assets/images/me.png', import.meta.url).href
  // 获取手机设备定位
  function getPhoneLocation(value:Boolean, workspaceId:String) {
    if (window.cesiumViewer == null) return
    // 创建一个箭头实体
    removeById('me_dw')
    myAppEntity = window.cesiumViewer.entities.add({
      id: 'me_dw',
      position: globalCesium.Cartesian3.fromDegrees(0, 0, 0), // 初始位置
      billboard: {
        image: meMapSrc, // 箭头图片路径
        width: 30,
        height: 30,
        alignedAxis: globalCesium.Cartesian3.UNIT_Z, // 对齐轴
      },
      // orientation: new globalCesium.CallbackProperty(() => {
      //   return globalCesium.Transforms.headingPitchRollQuaternion(
      //     myAppEntity.position.getValue(window.cesiumViewer.clock.currentTime),
      //     new globalCesium.HeadingPitchRoll(heading, 0, 0) // 设置朝向
      //   );
      // }, false),
    });
    let heading = 0; // 初始朝向
    // 获取设备朝向
    // if (window.DeviceOrientationEvent) {
    //   window.addEventListener('deviceorientation', (event) => {
    //     if (event.alpha !== null) {
    //       heading = globalCesium.Math.toRadians(event.alpha); // 将 alpha 转换为弧度
    //     }
    //   });
    // } else {
    //   console.log('手机不支持.');
    // }
    // 获取设备位置
    if (navigator.geolocation) {
      navigator.geolocation.watchPosition((position) => {
        me_longitude = position.coords.longitude;
        me_latitude = position.coords.latitude;
        me_altitude = position.coords.altitude || 0;
        // 更新箭头实体的位置
        myAppEntity.position = globalCesium.Cartesian3.fromDegrees(me_longitude, me_latitude, me_altitude);
        if (value) {
          // 将相机定位到设备位置 只有点击图标才可以执行这里
          window.cesiumViewer.camera.setView({
            destination: globalCesium.Cartesian3.fromDegrees(me_longitude, me_latitude, me_altitude + 1000), // 相机高度
            // orientation: {
            //   heading: heading, // 相机朝向
            //   pitch: globalCesium.Math.toRadians(-90), // 俯视角度
            //   roll: 0,
            // },
          });
        }
        // 查看两条线直接距离和高度
        // getTwoHeightWidth(workspaceId)
      }, (error) => {
        console.error('定位有误:', error);
      });
    } else {
      console.log('手机不支持.');
    }
  }
  return {
    initDock
    initDock,
    getPhoneLocation,
  }
}