2024-07-23 21:11:36 +08:00
|
|
|
|
<template>
|
2024-07-24 16:45:05 +08:00
|
|
|
|
<div class="w-100 batteryBar">
|
2024-08-08 16:34:57 +08:00
|
|
|
|
<!-- 进度条显示剩余电量 -->
|
2024-08-15 15:37:46 +08:00
|
|
|
|
<el-progress class="z90" :percentage="batteryRemaining" :show-text="false" :stroke-width="3"
|
|
|
|
|
:color="statusColor"></el-progress>
|
2024-08-08 16:34:57 +08:00
|
|
|
|
|
|
|
|
|
<!-- Tooltip 组件,用于显示电量或续航时间 -->
|
|
|
|
|
<tooltip v-if="showTooltip" class="z90" :horizontalPosition="`${batteryRemaining}%`" :backgroundColor="statusColor">
|
|
|
|
|
<template v-if="showBattery">
|
|
|
|
|
{{ batteryRemainingPower }} mAh
|
|
|
|
|
</template>
|
|
|
|
|
<template v-else>
|
|
|
|
|
{{ endurance }} 分钟
|
|
|
|
|
</template>
|
2024-07-24 16:45:05 +08:00
|
|
|
|
</tooltip>
|
2024-08-08 16:34:57 +08:00
|
|
|
|
|
|
|
|
|
<!-- 当返航剩余电量大于 5 时,显示返航提示 -->
|
2024-08-15 15:37:46 +08:00
|
|
|
|
<tooltip v-if="rtlRemaining > 0" class="z90" :horizontalPosition="`${rtlRemaining}%`" backgroundColor="#ff3333">
|
2024-07-24 16:45:05 +08:00
|
|
|
|
返
|
2024-07-23 21:11:36 +08:00
|
|
|
|
</tooltip>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2024-08-06 16:10:50 +08:00
|
|
|
|
import Tooltip from '@/components/Tooltip'
|
2024-08-15 15:37:46 +08:00
|
|
|
|
import geodist from 'geodist'
|
2024-07-23 21:11:36 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'BatteryStatus',
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
2024-08-08 16:34:57 +08:00
|
|
|
|
showBattery: true, // 用于控制显示电量或续航时间
|
|
|
|
|
interval: null // 用于存储定时器 ID
|
2024-07-23 21:11:36 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2024-07-24 16:45:05 +08:00
|
|
|
|
computed: {
|
2024-07-24 18:16:03 +08:00
|
|
|
|
statusColor () {
|
|
|
|
|
// 获取剩余电量百分比和返航电量百分比
|
|
|
|
|
const remaining = this.batteryRemaining
|
|
|
|
|
const rtl = this.rtlRemaining
|
|
|
|
|
// 计算剩余电量与返航电量的差值
|
|
|
|
|
const percentage = remaining - rtl
|
2024-08-08 16:34:57 +08:00
|
|
|
|
// 将百分比值限制在 0 到 100 范围内
|
2024-07-24 18:16:03 +08:00
|
|
|
|
const normalizedPercentage = Math.max(0, Math.min(100, percentage))
|
|
|
|
|
// 定义起始和结束的色相值 (0 - 360)
|
|
|
|
|
const startHue = 200 // 代表 #00C8C8
|
|
|
|
|
const endHue = 0 // 代表 #FF0000
|
|
|
|
|
// 根据百分比计算色相
|
|
|
|
|
const hue = Math.round(startHue + (endHue - startHue) * (100 - normalizedPercentage) / 100)
|
|
|
|
|
// 设置饱和度和亮度(可以根据需要调整)
|
|
|
|
|
const saturation = 90 // 饱和度保持不变
|
|
|
|
|
const lightness = 50 // 亮度保持不变
|
2024-08-08 16:34:57 +08:00
|
|
|
|
// 返回 HSL 颜色值
|
2024-07-24 18:16:03 +08:00
|
|
|
|
return `hsl(${hue}, ${saturation}%, ${lightness}%)`
|
|
|
|
|
},
|
2024-08-08 16:34:57 +08:00
|
|
|
|
// 电池剩余电量值 (mAh)
|
2024-07-24 16:45:05 +08:00
|
|
|
|
batteryRemainingPower () {
|
|
|
|
|
if (this.plane && this.plane.planeState) {
|
|
|
|
|
const battCapacity = this.plane.planeState.battCapacity
|
|
|
|
|
const batteryRemaining = this.plane.planeState.batteryRemaining
|
|
|
|
|
if (battCapacity !== undefined && batteryRemaining !== undefined) {
|
|
|
|
|
return (battCapacity / 100) * batteryRemaining
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 18:16:03 +08:00
|
|
|
|
return 0
|
2024-07-24 16:45:05 +08:00
|
|
|
|
},
|
2024-08-08 16:34:57 +08:00
|
|
|
|
// 剩余飞行时间 (分钟)
|
|
|
|
|
endurance () {
|
|
|
|
|
if (this.plane && this.plane.planeState && this.plane.planeState.currentBattery > 2) {
|
|
|
|
|
return Math.floor(this.batteryRemainingPower / 1000 / this.plane.planeState.currentBattery * 60)
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
},
|
|
|
|
|
// 控制 Tooltip 显示的条件
|
|
|
|
|
showTooltip () {
|
|
|
|
|
return this.batteryRemainingPower || this.endurance
|
|
|
|
|
},
|
|
|
|
|
// 剩余电量 (百分比)
|
2024-07-24 16:45:05 +08:00
|
|
|
|
batteryRemaining () {
|
|
|
|
|
if (this.plane && this.plane.planeState && this.plane.planeState.batteryRemaining !== undefined) {
|
|
|
|
|
const remaining = this.plane.planeState.batteryRemaining
|
|
|
|
|
if (remaining < 0) {
|
|
|
|
|
return 0
|
|
|
|
|
} else if (remaining > 100) {
|
|
|
|
|
return 100
|
|
|
|
|
} else {
|
|
|
|
|
return Number(remaining)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0
|
2024-07-24 18:16:03 +08:00
|
|
|
|
},
|
2024-08-08 16:34:57 +08:00
|
|
|
|
// 返航所需电量 (百分比)
|
2024-07-24 18:16:03 +08:00
|
|
|
|
rtlRemaining () {
|
2024-08-15 15:37:46 +08:00
|
|
|
|
let rtlTime = 0 // 返航所需时间(秒)
|
|
|
|
|
if (this.planePosition && this.homePosition && this.wpnavSpeed) {
|
|
|
|
|
const planeLngLat = { lat: this.planePosition[1], lon: this.planePosition[0] }
|
|
|
|
|
const homeLngLat = { lat: this.homePosition.lat, lon: this.homePosition.lng }
|
|
|
|
|
const horizontalDistance = geodist(planeLngLat, homeLngLat, { unit: 'meters' }) // 水平距离
|
|
|
|
|
const verticalDistance = Math.abs(this.planePosition[2] - this.homePosition.alt) // 垂直距离
|
|
|
|
|
const rtlDistance = horizontalDistance + verticalDistance // 返航总距离 水平+高度
|
|
|
|
|
rtlTime = Math.floor(Number(rtlDistance) / Number(this.wpnavSpeed)) // 返航需要时间 单位:秒
|
2024-07-24 18:16:03 +08:00
|
|
|
|
}
|
2024-08-15 15:37:46 +08:00
|
|
|
|
if (rtlTime <= 0) {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
const currentBattery = this.plane.planeState.currentBattery // 电流 (安培)
|
|
|
|
|
const batteryCapacity = this.plane.planeState.battCapacity // 电池总容量 (mAh)
|
|
|
|
|
// 计算返航所需电量(mAh),假设电流与电量是线性关系
|
|
|
|
|
const rtlPowerRequired = rtlTime * currentBattery / 3.6 // 返航所需电量 (mAh)
|
|
|
|
|
// 计算返航所需电量占总电量的百分比
|
|
|
|
|
const rtlPowerPercentage = (rtlPowerRequired / batteryCapacity) * 100
|
|
|
|
|
// 将百分比限制在 0 到 100 范围内
|
|
|
|
|
return Math.max(0, Math.min(100, rtlPowerPercentage))
|
|
|
|
|
},
|
|
|
|
|
// 返航点
|
|
|
|
|
homePosition () {
|
|
|
|
|
if (this.plane && this.plane.planeState) {
|
|
|
|
|
return this.plane.planeState.homePosition
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
|
|
|
|
// 飞机当前位置
|
|
|
|
|
planePosition () {
|
|
|
|
|
if (this.plane && this.plane.planeState) {
|
|
|
|
|
return this.plane.planeState.position[0]
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
},
|
|
|
|
|
// 飞机返航速度
|
|
|
|
|
wpnavSpeed () {
|
|
|
|
|
if (this.plane && this.plane.planeState) {
|
|
|
|
|
return this.plane.planeState.wpnavSpeed
|
|
|
|
|
}
|
|
|
|
|
return null
|
2024-07-24 16:45:05 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2024-07-23 21:11:36 +08:00
|
|
|
|
props: {
|
|
|
|
|
plane: {
|
2024-08-08 16:34:57 +08:00
|
|
|
|
type: Object,
|
2024-07-23 21:11:36 +08:00
|
|
|
|
deep: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
components: {
|
|
|
|
|
Tooltip
|
2024-07-24 16:45:05 +08:00
|
|
|
|
},
|
2024-08-08 16:34:57 +08:00
|
|
|
|
methods: {
|
|
|
|
|
/**
|
|
|
|
|
* 剩余电量与剩余续航时间轮播
|
|
|
|
|
*/
|
|
|
|
|
startInterval () {
|
|
|
|
|
this.interval = setInterval(() => {
|
|
|
|
|
this.showBattery = !this.showBattery
|
|
|
|
|
}, 3000)
|
|
|
|
|
},
|
|
|
|
|
clearInterval () {
|
|
|
|
|
if (this.interval) {
|
|
|
|
|
clearInterval(this.interval)
|
|
|
|
|
this.interval = null
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
checkDisplayConditions () {
|
|
|
|
|
this.clearInterval()
|
|
|
|
|
// 如果存在剩余电量和剩余续航时间,则启动轮播
|
|
|
|
|
if (this.batteryRemainingPower > 0 && this.endurance > 0) {
|
|
|
|
|
this.startInterval()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
batteryRemainingPower () {
|
|
|
|
|
this.checkDisplayConditions()
|
|
|
|
|
},
|
|
|
|
|
endurance (val) {
|
|
|
|
|
this.checkDisplayConditions()
|
|
|
|
|
if (val === 0) {
|
|
|
|
|
this.showBattery = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted () {
|
|
|
|
|
// 在组件挂载时检查显示条件
|
|
|
|
|
this.checkDisplayConditions()
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy () {
|
|
|
|
|
// 在组件销毁前清除定时器
|
|
|
|
|
this.clearInterval()
|
2024-07-23 21:11:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
@import "@/styles/theme.scss";
|
|
|
|
|
|
|
|
|
|
.batteryBar {
|
2024-07-24 16:45:05 +08:00
|
|
|
|
position: absolute;
|
2024-07-23 21:11:36 +08:00
|
|
|
|
}
|
|
|
|
|
</style>
|