ts/src/views/Satellite/hooks/satellite.ts

183 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-12-09 06:44:52 +00:00
import SatelliteEntity from '@/js/SatelliteEntity'
import { difference } from 'lodash'
2025-01-06 01:18:58 +00:00
import { useEntity } from '@/hooks/entity'
2025-02-19 08:06:46 +00:00
import { useTree } from '@/utils/tree'
import { getSatellite } from '@/api/Satellite'
2024-12-09 06:44:52 +00:00
// import CreateFrustum from '@/js/Sensor'
interface ISatellite {
name: string
2025-01-20 01:42:19 +00:00
id: string
2024-12-09 06:44:52 +00:00
tle: string
}
interface IBaseFilterParam {
treeData: Array<any>
params: Array<string | number>
paramName: string
}
2025-02-19 08:06:46 +00:00
const { filterTreeNodeByField } = useTree()
2024-12-09 06:44:52 +00:00
const satelliteList = ref<ISatellite[]>([])
2025-02-19 08:06:46 +00:00
const checkedKeys = ref<Array<string | number>>([])
2024-12-09 06:44:52 +00:00
2025-02-19 08:06:46 +00:00
const { satelliteMap, showOrHideLoad, satellitePayloadShowMap } = useEntity()
2025-01-20 01:42:19 +00:00
const showPoint = ref(true)
function showPointUnderSat(id?: string) {
if (id) {
satelliteMap.get(id).underPoint = true
} else {
;[...satelliteMap.values()].forEach(satellite => {
satellite.underPoint = showPoint.value
})
}
}
2024-12-09 06:44:52 +00:00
export function useSatellite() {
function addSatellites(ids: Array<string | number>) {
const addIds = difference(ids, [...satelliteMap.keys()])
const removeIds = difference([...satelliteMap.keys()], ids)
removeIds.forEach(id => {
removeSatellite(id)
})
2024-12-09 06:44:52 +00:00
if (addIds.length > 0) {
const nodes = filterTreeNodeByField({
treeData: satelliteList.value,
params: addIds,
paramName: 'id',
})
nodes.forEach(node => {
const entity = addSatellite(node)
satelliteMap.set(node.id, entity)
2025-01-20 01:42:19 +00:00
if (showPoint.value) {
showPointUnderSat(node.id)
}
2024-12-09 06:44:52 +00:00
})
}
}
// 创建satellite entity 实例
2025-02-19 08:06:46 +00:00
function addSatellite({ id, tle }: ISatellite) {
2024-12-09 06:44:52 +00:00
const satellite = new SatelliteEntity(tle)
const cesiumSateEntity = satellite.createSatelliteEntity()
// const result = viewer.entities.add(cesiumSateEntity)
2025-01-13 08:45:08 +00:00
setTimeout(() => {
2025-01-20 01:42:19 +00:00
// satellite.sensorType = Math.random() > 0.5 ? 'conic' : 'rectangle'
2025-02-19 08:06:46 +00:00
const satPayload = satellitePayloadShowMap.get(id)
if (satPayload && satPayload.detectingPayload) {
satPayload.detectingPayload.angle &&
(satellite.sensorAngle = satPayload.detectingPayload.angle)
satPayload.detectingPayload.show && (satellite.sensor = true)
}
2025-01-13 08:45:08 +00:00
}, 1000)
2025-01-20 01:42:19 +00:00
2024-12-09 06:44:52 +00:00
// viewer.clock.multiplier = 100
2025-01-13 08:45:08 +00:00
return satellite
2024-12-09 06:44:52 +00:00
// let heading = 0
// let pitch = 0
// let roll = 0
// let hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll)
// let orientation = Cesium.Quaternion.fromHeadingPitchRoll(hpr)
// // 创建视锥体
// console.log(
// viewer.scene.canvas.clientWidth / viewer.scene.canvas.clientHeight,
// 'viewer.scene.canvas.clientWidth / viewer.scene.canvas.clientHeight'
// )
// let createFrustum = new CreateFrustum({
// position: origin,
// orientation: orientation,
// fov: 30,
// near: 0.1,
// far: 10000,
// aspectRatio:
// viewer.scene.canvas.clientWidth / viewer.scene.canvas.clientHeight,
// })
}
function removeSatellite(id: string | number) {
if (satelliteMap.has(id)) {
const satellite = satelliteMap.get(id)
2025-01-13 08:45:08 +00:00
satellite.destroy()
2024-12-09 06:44:52 +00:00
satelliteMap.delete(id)
}
}
2025-02-19 08:06:46 +00:00
return {
satelliteList,
checkedKeys,
getSatelliteList,
addSatellites,
showPoint,
showPointUnderSat,
}
2024-12-09 06:44:52 +00:00
}
2025-02-19 08:06:46 +00:00
// function filterTreeNodeByField({
// treeData,
// params,
// paramName,
// }: IBaseFilterParam): Array<ISatellite> {
// return treeData.reduce((acc, node) => {
// if (params.includes(node[paramName]) && !node.children) {
// acc.push(node)
// }
// if (node.children) {
// acc = acc.concat(
// filterTreeNodeByField({
// treeData: node.children,
// params,
// paramName,
// })
// )
// }
// return acc
// }, [])
// }
async function getSatelliteList() {
const checked = JSON.parse(JSON.stringify(checkedKeys.value))
const sateRes = await getSatellite()
const { data, code } = sateRes
if (code === '200') {
checkedKeys.value = []
satelliteList.value = data
getAllNodesToPayload()
nextTick(() => {
checkedKeys.value = checked
})
}
// console.log('data,code', data, code)
}
function getAllNodesToPayload() {
// const mbList = getLeafNodeIds(satelliteList.value, 'dataId')
satelliteList.value.forEach(satellite => {
if (satellite.extendInfo) {
const { detectingPayload, communicationPayload } = satellite.extendInfo
const detectingShow =
satellitePayloadShowMap.get(satellite.id)?.detectingPayload?.show ||
false
const communicationShow =
satellitePayloadShowMap.get(satellite.id)?.communicationPayload?.show ||
false
satellitePayloadShowMap.set
satellitePayloadShowMap.set(satellite.id, {
detectingPayload: detectingPayload
? { ...detectingPayload, show: detectingShow }
: null,
communicationPayload: communicationPayload
? { ...communicationPayload, show: communicationShow }
: null,
})
2024-12-09 06:44:52 +00:00
}
2025-02-19 08:06:46 +00:00
})
console.log('satellitePayloadShowMap', satellitePayloadShowMap)
2024-12-09 06:44:52 +00:00
}