fix:地图仪表盘功能模块实现优化

This commit is contained in:
2025-06-19 13:48:44 +08:00
parent 2c7e37f41a
commit 68ebde55a4
@@ -87,6 +87,23 @@ interface RealDevice {
lastMaintenance: string;
nextMaintenance: string;
modeCode: number;
emergencyStopState: number;
alarmState: number;
// 新增官方协议字段
putterState: number;
coverState: number;
silentMode: number;
supplementLightState: number;
batteryStoreMode: number;
homePositionIsValid: number;
safeLandHeight: number;
isAlternateConfigured: number;
airConditionerState: number;
switchTime: number;
devicePaired: number;
subDeviceSn: string;
deviceOnlineStatus: number;
firstPowerOn: number;
};
airportStatus?: {
flightTaskStep: number;
@@ -252,6 +269,9 @@ const DeviceMapDashboard: React.FC = () => {
const markersRef = useRef<Map<string, any>>(new Map());
const mapContainerRef = useRef<HTMLDivElement>(null);
// 全局鼠标位置跟踪
const [globalMousePosition, setGlobalMousePosition] = useState<{ x: number; y: number }>({ x: 0, y: 0 });
// WebSocket相关
const wsRef = useRef<WebSocket | null>(null);
const [wsConnected, setWsConnected] = useState(false);
@@ -369,6 +389,23 @@ const DeviceMapDashboard: React.FC = () => {
lastMaintenance: '',
nextMaintenance: '',
modeCode: 0,
emergencyStopState: 0,
alarmState: 0,
// 新增官方协议字段
putterState: 0,
coverState: 0,
silentMode: 0,
supplementLightState: 0,
batteryStoreMode: 0,
homePositionIsValid: 0,
safeLandHeight: 0,
isAlternateConfigured: 0,
airConditionerState: 0,
switchTime: 0,
devicePaired: 0,
subDeviceSn: '',
deviceOnlineStatus: 0,
firstPowerOn: 0,
}
})));
} else {
@@ -396,8 +433,13 @@ const DeviceMapDashboard: React.FC = () => {
if (!hasRecentData) return DeviceStatus.OFFLINE;
// 检查告警状态
if (device.metrics.errorCount > 0 || device.metrics.warningCount > 0) {
// 检查紧急停止状态
if (device.metrics.emergencyStopState === 1) {
return DeviceStatus.ERROR;
}
// 检查报警状态
if (device.metrics.alarmState === 1) {
return DeviceStatus.ERROR;
}
@@ -411,12 +453,27 @@ const DeviceMapDashboard: React.FC = () => {
return DeviceStatus.MAINTENANCE;
}
// 检查定位质量
if (device.metrics.positionQuality < 3) {
return DeviceStatus.MAINTENANCE;
}
// 检查环境条件(风速过大)
if (device.metrics.windSpeed > 10) {
return DeviceStatus.MAINTENANCE;
}
// 检查降雨状态
if (device.metrics.rainfall > 0) {
return DeviceStatus.MAINTENANCE;
}
// 检查无人机是否在机库中
if (device.metrics.droneInDock === 1) {
return DeviceStatus.STANDBY;
}
// 检查模式代码(飞行任务)
// 检查工作模式代码(飞行任务)
if (device.metrics.modeCode > 0) {
return DeviceStatus.TASK;
}
@@ -533,6 +590,23 @@ const DeviceMapDashboard: React.FC = () => {
lastMaintenance: device.lastMaintenance || '',
nextMaintenance: device.nextMaintenance || '',
modeCode: 0,
emergencyStopState: 0,
alarmState: 0,
// 新增官方协议字段
putterState: 0,
coverState: 0,
silentMode: 0,
supplementLightState: 0,
batteryStoreMode: 0,
homePositionIsValid: 0,
safeLandHeight: 0,
isAlternateConfigured: 0,
airConditionerState: 0,
switchTime: 0,
devicePaired: 0,
subDeviceSn: '',
deviceOnlineStatus: 0,
firstPowerOn: 0,
}
}));
};
@@ -555,9 +629,18 @@ const DeviceMapDashboard: React.FC = () => {
// 处理设备悬停
const handleDeviceHover = (device: RealDevice, event: React.MouseEvent) => {
// 使用全局鼠标位置或事件位置
const mouseX = globalMousePosition.x || event.clientX;
const mouseY = globalMousePosition.y || event.clientY;
console.log('handleDeviceHover - 鼠标位置:', { mouseX, mouseY });
// 使用智能定位
const smartPosition = getSmartCardPosition(mouseX, mouseY);
setHoveredDevice(device);
setShowDeviceCard(true);
setDeviceCardPosition({ x: event.clientX, y: event.clientY });
setDeviceCardPosition(smartPosition);
};
// 处理设备离开
@@ -611,6 +694,9 @@ const DeviceMapDashboard: React.FC = () => {
// 刷新设备数据
const handleRefresh = () => {
setLoading(true);
// 清除所有现有标记
clearAllMarkers();
setTimeout(() => {
const newDevices = generateMockDevices();
setDevices(convertMockDevicesToRealDevices(newDevices));
@@ -624,6 +710,13 @@ const DeviceMapDashboard: React.FC = () => {
fetchDeviceList();
initWebSocket();
// 添加全局鼠标移动监听器
const handleGlobalMouseMove = (event: MouseEvent) => {
setGlobalMousePosition({ x: event.clientX, y: event.clientY });
};
document.addEventListener('mousemove', handleGlobalMouseMove);
return () => {
if (wsRef.current) {
wsRef.current.close();
@@ -637,6 +730,7 @@ const DeviceMapDashboard: React.FC = () => {
if (tokenCache.timer) {
clearTimeout(tokenCache.timer);
}
document.removeEventListener('mousemove', handleGlobalMouseMove);
};
}, []);
@@ -811,7 +905,7 @@ const DeviceMapDashboard: React.FC = () => {
// 清除不存在的标记
currentMarkers.forEach((marker, deviceSn) => {
if (!newMarkers.find(m => m.device.deviceSn === deviceSn)) {
marker.remove();
safeRemoveMarker(marker);
currentMarkers.delete(deviceSn);
}
});
@@ -823,45 +917,27 @@ const DeviceMapDashboard: React.FC = () => {
if (existingMarker) {
// 更新现有标记
existingMarker.setPosition(position);
existingMarker.setIcon({
type: 'image',
image: createMarkerIcon(color, status),
size: [32, 32],
anchor: 'bottom-center'
});
try {
if (typeof existingMarker.setPosition === 'function') {
existingMarker.setPosition(position);
}
if (typeof existingMarker.setIcon === 'function') {
existingMarker.setIcon({
type: 'image',
image: createMarkerIcon(color, status),
size: [32, 32],
anchor: 'bottom-center'
});
}
} catch (error) {
console.warn('更新标记失败:', error);
}
} else {
// 创建新标记
const newMarker = new window.AMap.Marker({
position,
icon: {
type: 'image',
image: createMarkerIcon(color, status),
size: [32, 32],
anchor: 'bottom-center'
},
offset: [0, -16],
title: `${device.deviceName} (${DEVICE_STATUS_CONFIG[status].text})`
});
// 添加点击事件
newMarker.on('click', () => {
handleDeviceClick(device, {} as React.MouseEvent);
});
// 添加鼠标事件
newMarker.on('mouseover', () => {
setHoveredDevice(device);
setShowDeviceCard(true);
});
newMarker.on('mouseout', () => {
setHoveredDevice(null);
setShowDeviceCard(false);
});
newMarker.setMap(mapRef.current);
currentMarkers.set(device.deviceSn, newMarker);
const newMarker = safeCreateMarker(device, position, color, status);
if (newMarker) {
currentMarkers.set(device.deviceSn, newMarker);
}
}
});
}, [devices, mapMarkers]);
@@ -1013,6 +1089,7 @@ const DeviceMapDashboard: React.FC = () => {
const isFixed = eventData.position_state?.is_fixed || 0;
const rtkNumber = eventData.position_state?.rtk_number || 0;
const gpsNumber = eventData.position_state?.gps_number || 0;
const isCalibration = eventData.position_state?.is_calibration || 0;
// 提取网络信息
const networkQuality = eventData.network_state?.quality || 0;
@@ -1023,14 +1100,36 @@ const DeviceMapDashboard: React.FC = () => {
const droneInDock = eventData.drone_in_dock || 0;
const modeCode = eventData.mode_code || 0;
// 提取存储信息
const storageTotal = eventData.storage?.total || 0;
const storageUsed = eventData.storage?.used || 0;
const diskUsage = storageTotal > 0 ? (storageUsed / storageTotal) * 100 : 0;
// 提取告警状态
const alarmState = eventData.alarm_state || 0;
// 提取设备部件状态
const putterState = eventData.putter_state || 0;
const coverState = eventData.cover_state || 0;
const silentMode = eventData.silent_mode || 0;
const supplementLightState = eventData.supplement_light_state || 0;
const emergencyStopState = eventData.emergency_stop_state || 0;
const batteryStoreMode = eventData.battery_store_mode || 0;
const homePositionIsValid = eventData.home_position_is_valid || 0;
// 提取备用降落点信息
const alternateLandPoint = eventData.alternate_land_point;
const safeLandHeight = alternateLandPoint?.safe_land_height || 0;
const isAlternateConfigured = alternateLandPoint?.is_configured || 0;
// 提取空调状态
const airConditioner = eventData.air_conditioner;
const airConditionerState = airConditioner?.air_conditioner_state || 0;
const switchTime = airConditioner?.switch_time || 0;
// 提取子设备信息
const subDevice = eventData.sub_device;
const devicePaired = subDevice?.device_paired || 0;
const subDeviceSn = subDevice?.device_sn || '';
const deviceOnlineStatus = subDevice?.device_online_status || 0;
// 提取报警状态
const alarmState = eventData.alarm_state || 0;
// 提取首次开机时间
const firstPowerOn = eventData.first_power_on || 0;
setDevices(prevDevices =>
prevDevices.map(device =>
@@ -1058,17 +1157,34 @@ const DeviceMapDashboard: React.FC = () => {
// 无人机状态
droneInDock,
modeCode,
// 存储数据
diskUsage,
// 告警状态
errorCount: alarmState > 0 ? (device.metrics.errorCount || 0) + 1 : device.metrics.errorCount || 0,
warningCount: emergencyStopState > 0 ? (device.metrics.warningCount || 0) + 1 : device.metrics.warningCount || 0,
// 设备部件状态
putterState,
coverState,
silentMode,
supplementLightState,
emergencyStopState,
batteryStoreMode,
homePositionIsValid,
// 备用降落点
safeLandHeight,
isAlternateConfigured,
// 空调状态
airConditionerState,
switchTime,
// 子设备状态
devicePaired,
subDeviceSn,
deviceOnlineStatus,
// 报警状态
alarmState,
// 首次开机时间
firstPowerOn,
// 信号强度(基于网络质量)
signalStrength: networkQuality,
// 数据速率(基于网络速率)
dataRate: networkRate,
// 更新时间
uptime: Date.now() - (eventData.first_power_on || Date.now())
uptime: firstPowerOn > 0 ? Date.now() - firstPowerOn : 0
},
lastUpdateTime: new Date().toISOString()
}
@@ -1083,7 +1199,10 @@ const DeviceMapDashboard: React.FC = () => {
humidity,
positionQuality,
networkQuality,
droneInDock
droneInDock,
emergencyStopState,
alarmState,
modeCode
});
};
@@ -1430,6 +1549,117 @@ const DeviceMapDashboard: React.FC = () => {
return false;
};
// 安全的地图标记管理方法
const safeRemoveMarker = (marker: any) => {
try {
if (typeof marker.remove === 'function') {
marker.remove();
return true;
} else if (typeof marker.setMap === 'function') {
marker.setMap(null);
return true;
} else if (typeof marker.destroy === 'function') {
marker.destroy();
return true;
} else {
console.warn('无法移除标记,方法不可用');
return false;
}
} catch (error) {
console.warn('移除标记失败:', error);
return false;
}
};
// 智能定位悬停卡片
const getSmartCardPosition = (mouseX: number, mouseY: number) => {
const cardWidth = 280;
const cardHeight = 200;
const padding = 20;
let left = mouseX + 10;
let top = mouseY - 10;
// 检查右边界
if (left + cardWidth > window.innerWidth - padding) {
left = mouseX - cardWidth - 10;
}
// 检查下边界
if (top + cardHeight > window.innerHeight - padding) {
top = mouseY - cardHeight - 10;
}
// 确保不超出左边界和上边界
left = Math.max(padding, left);
top = Math.max(padding, top);
return { x: left, y: top };
};
const safeCreateMarker = (device: RealDevice, position: [number, number], color: string, status: DeviceStatus) => {
try {
const newMarker = new window.AMap.CircleMarker({
center: position,
radius: 8,
fillColor: color,
fillOpacity: 0.8,
strokeColor: '#fff',
strokeWeight: 2,
strokeOpacity: 1,
title: `${device.deviceName} (${DEVICE_STATUS_CONFIG[status].text})`
});
// 添加点击事件
if (typeof newMarker.on === 'function') {
newMarker.on('click', () => {
handleDeviceClick(device, {} as React.MouseEvent);
});
newMarker.on('mouseover', (event: any) => {
console.log('地图标记mouseover事件:', event);
console.log('全局鼠标位置:', globalMousePosition);
// 使用全局鼠标位置
const mouseX = globalMousePosition.x;
const mouseY = globalMousePosition.y;
console.log('使用的鼠标位置:', { mouseX, mouseY });
// 使用智能定位
const smartPosition = getSmartCardPosition(mouseX, mouseY);
console.log('智能定位结果:', smartPosition);
setHoveredDevice(device);
setShowDeviceCard(true);
setDeviceCardPosition(smartPosition);
});
newMarker.on('mouseout', () => {
setHoveredDevice(null);
setShowDeviceCard(false);
});
}
if (typeof newMarker.setMap === 'function') {
newMarker.setMap(mapRef.current);
}
return newMarker;
} catch (error) {
console.warn('创建标记失败:', error);
return null;
}
};
const clearAllMarkers = () => {
const currentMarkers = markersRef.current;
currentMarkers.forEach((marker) => {
safeRemoveMarker(marker);
});
currentMarkers.clear();
};
return (
<div style={{ height: '100vh', position: 'relative', overflow: 'hidden' }}>
{/* 顶部控制栏 */}
@@ -1568,6 +1798,20 @@ const DeviceMapDashboard: React.FC = () => {
检查地图
</Button>
<Button
icon={<InfoCircleOutlined />}
onClick={() => {
console.log('悬停卡片调试信息:');
console.log('- 全局鼠标位置:', globalMousePosition);
console.log('- 卡片位置:', deviceCardPosition);
console.log('- 悬停设备:', hoveredDevice);
console.log('- 显示卡片:', showDeviceCard);
message.info('悬停卡片调试信息已输出到控制台');
}}
>
调试悬停
</Button>
<Button
icon={<EnvironmentOutlined />}
onClick={() => {
@@ -1654,8 +1898,8 @@ const DeviceMapDashboard: React.FC = () => {
<div
style={{
position: 'absolute',
left: deviceCardPosition.x + 10,
top: deviceCardPosition.y - 10,
left: deviceCardPosition.x,
top: deviceCardPosition.y,
zIndex: 1001,
background: '#fff',
border: '1px solid #d9d9d9',
@@ -1663,9 +1907,16 @@ const DeviceMapDashboard: React.FC = () => {
padding: '12px',
boxShadow: '0 4px 12px rgba(0,0,0,0.15)',
minWidth: '220px',
maxWidth: '280px'
maxWidth: '280px',
pointerEvents: 'none', // 防止卡片阻挡鼠标事件
transition: 'opacity 0.2s ease-in-out'
}}
>
{/* 调试信息 */}
<div style={{ fontSize: '10px', color: '#999', marginBottom: '4px', borderBottom: '1px solid #f0f0f0', paddingBottom: '4px' }}>
位置: ({deviceCardPosition.x}, {deviceCardPosition.y}) | 鼠标: ({globalMousePosition.x}, {globalMousePosition.y})
</div>
<div style={{ marginBottom: '8px' }}>
<Text strong style={{ fontSize: '14px' }}>{hoveredDevice.deviceName}</Text>
<Tag
@@ -1685,6 +1936,18 @@ const DeviceMapDashboard: React.FC = () => {
{hoveredDevice.metrics.droneInDock !== undefined && (
<div>机库: {hoveredDevice.metrics.droneInDock === 1 ? '在库' : '离库'}</div>
)}
{hoveredDevice.metrics.emergencyStopState === 1 && (
<div style={{ color: '#ff4d4f' }}>⚠️ 紧急停止已触发</div>
)}
{hoveredDevice.metrics.alarmState === 1 && (
<div style={{ color: '#ff4d4f' }}>🚨 有报警</div>
)}
{hoveredDevice.metrics.windSpeed > 10 && (
<div style={{ color: '#fa8c16' }}>💨 风速过大</div>
)}
{hoveredDevice.metrics.rainfall > 0 && (
<div style={{ color: '#fa8c16' }}>🌧️ 有降雨</div>
)}
{hoveredDevice.location && (
<div>位置: {hoveredDevice.location.latitude.toFixed(4)}, {hoveredDevice.location.longitude.toFixed(4)}</div>
)}
@@ -1872,6 +2135,18 @@ const DeviceMapDashboard: React.FC = () => {
</div>
<div style={{ fontSize: '12px', color: '#999', marginTop: '4px' }}>
电量: {device.metrics.batteryCapacity}% | 网络: {device.metrics.networkQuality}% | 温度: {device.metrics.temperature}°C
{device.metrics.emergencyStopState === 1 && (
<span style={{ color: '#ff4d4f', marginLeft: '8px' }}>⚠️ 紧急停止</span>
)}
{device.metrics.alarmState === 1 && (
<span style={{ color: '#ff4d4f', marginLeft: '8px' }}>🚨 报警</span>
)}
{device.metrics.windSpeed > 10 && (
<span style={{ color: '#fa8c16', marginLeft: '8px' }}>💨 大风</span>
)}
{device.metrics.rainfall > 0 && (
<span style={{ color: '#fa8c16', marginLeft: '8px' }}>🌧️ 降雨</span>
)}
{device.lastUpdateTime && (
<span style={{ marginLeft: '8px' }}>
{new Date(device.lastUpdateTime).toLocaleTimeString()}
@@ -2176,7 +2451,7 @@ const DeviceMapDashboard: React.FC = () => {
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>风速</div>
<div style={{ fontSize: '18px', fontWeight: 'bold' }}>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.windSpeed > 10 ? '#ff4d4f' : '#52c41a' }}>
{selectedDevice.metrics.windSpeed}m/s
</div>
</div>
@@ -2184,7 +2459,7 @@ const DeviceMapDashboard: React.FC = () => {
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>降雨量</div>
<div style={{ fontSize: '18px', fontWeight: 'bold' }}>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.rainfall > 0 ? '#ff4d4f' : '#52c41a' }}>
{selectedDevice.metrics.rainfall}mm
</div>
</div>
@@ -2192,6 +2467,61 @@ const DeviceMapDashboard: React.FC = () => {
</Row>
</div>
{/* 设备状态信息 */}
<div style={{ marginBottom: '16px' }}>
<Title level={5}>设备状态</Title>
<Row gutter={[8, 8]}>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>紧急停止</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.emergencyStopState === 1 ? '#ff4d4f' : '#52c41a' }}>
{selectedDevice.metrics.emergencyStopState === 1 ? '已触发' : '正常'}
</div>
</div>
</Col>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>报警状态</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.alarmState === 1 ? '#ff4d4f' : '#52c41a' }}>
{selectedDevice.metrics.alarmState === 1 ? '有报警' : '正常'}
</div>
</div>
</Col>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>机盖状态</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.coverState === 1 ? '#52c41a' : '#ff4d4f' }}>
{selectedDevice.metrics.coverState === 1 ? '开启' : '关闭'}
</div>
</div>
</Col>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>静音模式</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.silentMode === 1 ? '#52c41a' : '#8c8c8c' }}>
{selectedDevice.metrics.silentMode === 1 ? '开启' : '关闭'}
</div>
</div>
</Col>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>补光灯</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.supplementLightState === 1 ? '#52c41a' : '#8c8c8c' }}>
{selectedDevice.metrics.supplementLightState === 1 ? '开启' : '关闭'}
</div>
</div>
</Col>
<Col span={12}>
<div style={{ textAlign: 'center', padding: '8px', background: '#f5f5f5', borderRadius: '4px' }}>
<div style={{ fontSize: '12px', color: '#666' }}>空调状态</div>
<div style={{ fontSize: '18px', fontWeight: 'bold', color: selectedDevice.metrics.airConditionerState === 1 ? '#52c41a' : '#8c8c8c' }}>
{selectedDevice.metrics.airConditionerState === 1 ? '开启' : '关闭'}
</div>
</div>
</Col>
</Row>
</div>
{/* 无人机特有信息 */}
{selectedDevice.deviceType === 1 && (
<>