forked from drone/command-center-dashboard

罗广辉
2025-04-21 2800fa4f32f3900509cb4d6eefaf2bfaf54efdd7
src/views/SignMachineNest/SignMachineNest.vue
@@ -1,99 +1,71 @@
<template>
  <MachineLeft></MachineLeft>
  <MachineRight></MachineRight>
   <MachineLeft></MachineLeft>
   <MachineRight></MachineRight>
</template>
<script setup>
import MachineLeft from '@/views/SignMachineNest/MachineLeft/MachineLeft.vue'
import MachineRight from '@/views/SignMachineNest/MachineRight/MachineRight.vue';
import { useConnectWebSocket } from '../../utils/websocket/connect-websocket';
import { getWebsocketUrl } from '@/websocket/util/config';
import { EBizCode } from '@/utils/staticData/enums.js';
import { EModeCode } from '@/utils/staticData/device.js';
import { useStore } from 'vuex';
import { getDeviceDetail, getFlightStatistics } from '@/api/home/machineNest';
const store = useStore();
let connectWs = ref(null);
import MachineRight from '@/views/SignMachineNest/MachineRight/MachineRight.vue'
import { useStore } from 'vuex'
import { getDeviceDetail, getEventList, getFlightStatistics } from '@/api/home/machineNest'
import { useSingleDroneMap } from '@/hooks/useSingleDroneMap/useSingleDroneMap'
import { useDroneWS } from '@/hooks/useDroneWS'
import _ from 'lodash'
import { getAreaCodeApi } from '@/api/home'
const store = useStore()
let connectWs = ref(null)
// 单个机巢信息
const singleUavHome = computed(() => store.state.home.singleUavHome);
const singleUavHome = computed(() => store.state.home.singleUavHome)
let workspaceId = ref('')
let { wsInfo } = useDroneWS(workspaceId) //ws信息,是一个ref对象
const dockSn = computed(() => singleUavHome.value.device_sn) //机巢sn
const droneSn = computed(() => wsInfo.value?.device_osd?.data?.sn) //无人机sn
const singleTotal = ref({})
const dockDetails = ref('')
provide('dockSn', dockSn)
provide('droneSn', droneSn)
provide('wsInfo', wsInfo)
provide('dockDetails', dockDetails)
provide('singleTotal', singleTotal)
let osdVisible = ref({});
// 单机巢初始化及事件撒点
const { init, initEventLayer, initDroneEntity } = useSingleDroneMap({
   eventApi: getEventList,
   eventApiParams: {
      device_sn: singleUavHome.value.device_sn,
   },
})
let workspaceId = ref('');
// 进入单个机巢开始连接ws
const createWsConntect = () => {
  let webSorketUrl = getWebsocketUrl() + '&workspace-id=' + workspaceId.value;
  // 监听ws 消息
  connectWs.value = useConnectWebSocket(messageHandler, webSorketUrl);
};
const messageHandler = (result) => {
  let payload = JSON.parse(result) // 为了兼容聊天消息
  if (!payload) return
  switch (payload.biz_code) {
    case EBizCode.GatewayOsd: {
      store.commit('setGatewayInfo', payload.data)
      break
    }
    case EBizCode.DeviceOsd: {
      store.commit('setDeviceInfo', payload)
      store.commit('setWsMessage', payload)
      break
    }
    case EBizCode.DockOsd: {
      store.commit('setDockOnfo', payload.data)
      break
    }
    default:
          break
  }
};
// 获取单个机巢信息
const getSingleDetails = () => {
  getDeviceDetail(singleUavHome.value.device_sn).then((res) => {
    if (res.data.code !== 0) return;
    const result = res.data.data;
    const child = result.children;
    // 对应store 里面数据结构
    osdVisible.value.sn = child?.device_sn || ''
    osdVisible.value.callsign = child?.nickname || '--'
    osdVisible.value.model = EModeCode.Disconnected || ''
    osdVisible.value.visible = true
    osdVisible.value.gateway_sn = result?.device_sn || ''
    osdVisible.value.is_dock = true
    osdVisible.value.gateway_callsign = result?.callsign || '--'
    osdVisible.value.payloads = child?.payloads_list || []
    osdVisible.value.device_domain = child.domain || 0
    osdVisible.value.device_sub_type = child.sub_type || 1
    osdVisible.value.device_type = child.type || 0
    // osdVisible.value.latest_wayline_job = result?.latest_wayline_job || {}
    store.commit('setOsdVisibleInfo', osdVisible.value);
    store.commit('setSelectedWorkSpaceId', result.workspace_id);
    workspaceId.value = result.workspace_id;
    createWsConntect();
  });
};
   getDeviceDetail(singleUavHome.value.device_sn).then(res => {
      const result = res.data.data
      dockDetails.value = result
    const storageObj  = _.pick(result, ['latitude', 'longitude']) || {}
      store.commit('setSingleUavHome',{...singleUavHome.value, ...storageObj})
      initDroneEntity({
         lng: result.longitude,
         lat: result.latitude,
         status: result.status,
      })
      workspaceId.value = result.workspace_id
   })
}
// 获取机巢统计数据 提供给左右侧组件使用
const getMachineData = () => {
  getFlightStatistics(singleUavHome.value.device_sn).then(res => {
    if (res.data.code !== 0) return;
    const result = res.data.data;
    console.log(result);
    console.log(result);
    store.commit('setSingleTotal', result);
  })
};
   getFlightStatistics(singleUavHome.value.device_sn).then(res => {
      if (res.data.code !== 0) return
      singleTotal.value = res.data.data
   })
}
onMounted(() => {
  getSingleDetails();
  getMachineData();
});
   initEventLayer()
   getSingleDetails()
   getMachineData()
})
onUnmounted(() => {
  connectWs?.value?.close();
});
   connectWs?.value?.close()
})
</script>