急停 发送的模式切换 接收障碍物信息 纵览界面 返航和任务表格数据 优化 状态界面加入人员跟随和 系统模式 还未对接系统模式 任务监控界面加入障碍物

This commit is contained in:
mmc
2026-11-13 20:29:49 +08:00
parent e77d956434
commit f057e0b72c
10 changed files with 537 additions and 109 deletions

View File

@@ -256,6 +256,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
);
const liveObstacleEntities = new Map();
const liveObstacleLastTsRef = useRef(0);
// ==============================
// 统一相机飞行调度器
@@ -396,39 +397,119 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}, [baseMapType, tiandituUnavailable]);
const computeDistanceScale = () => {
const viewer = viewerRef.current;
const MIN_SCALE = 0.35;
const MAX_SCALE = 2.5;
const REF_HEIGHT = 50; // 相机高度 50m 时 scale = 1(基准尺寸)
const h = viewer.camera.positionCartographic?.height ?? REF_HEIGHT;
let s = REF_HEIGHT / Math.max(h, 1); // 反比
if (s < MIN_SCALE) s = MIN_SCALE;
if (s > MAX_SCALE) s = MAX_SCALE;
return s;
};
function updateLiveObstacleBoxes(
message,
) {
const viewer = viewerRef.current;
//for (const [, entity] of liveObstacleEntities) {
// viewer.entities.remove(entity);
//}
//liveObstacleEntities.clear();
// 让 billboard / label 图标随相机高度(地图远近)动态缩放
// 经验映射:相机越高(地图越远)scale 越小,相机越低(地图越近)scale 越大
// 相机高度 20m → scale 1.5;50m → 1.0;200m → 0.5;上限/下限 clamp
if (!viewer || !Cesium) return;
console.log(message, "障碍物信息")
const payload = message;
const objects = Array.isArray(payload) ? payload : (payload?.objects ?? []);
const objects = Array.isArray(payload)
? payload
: Array.isArray(payload?.objects)
? payload.objects
: [];
console.log(objects, "障碍物信息2222")
// 全量替换:先把地图上所有 live-obstacle 实体按 id 前缀全部移除
// 不依赖 liveObstacleEntities Map(避免历史孤儿实体残留)
const toRemove = [];
viewer.entities.values.forEach((ent) => {
if (ent && ent.id && String(ent.id).startsWith('live-obstacle-')) {
toRemove.push(ent);
}
});
toRemove.forEach((ent) => viewer.entities.remove(ent));
liveObstacleEntities.clear();
if (objects.length === 0) return;
if (objects.length === 0) {
console.log("没有障碍物")
viewer.scene.requestRender();
return;
}
// 记录第一个有效障碍物的位置,用于视角跳转
let firstPos = null;
objects.forEach((obj, index) => {
console.log(obj, "障碍物信息1111")
const lat = Number(obj.latitude ?? obj.lat);
const lon = Number(obj.longitude ?? obj.lng ?? obj.lon);
const dis = obj?.distance;
// 根据类型设置默认尺寸和颜色
const type = (obj.class_name ?? obj.name ?? '').toLowerCase();
const isPerson = type.includes('person') || type.includes('行人') || type.includes('人');
const isVehicle = type.includes('car') || type.includes('车辆') || type.includes('车') || type.includes('truck');
const isFence = type.includes('fence') || type.includes('围挡') || type.includes('障碍');
// 优先按 class_id 判定类型;未提供时再 fallback 到 class_name 字符串
const CLASS_ID_MAP = {
0: 'bus',
1: 'car',
2: 'motorcycle',
3: 'person',
4: 'truck',
};
const classIdRaw = obj.class_id ?? obj.classId ?? obj.class;
const parsedClassId = Number(classIdRaw);
const kindByClassId = Number.isInteger(parsedClassId) && CLASS_ID_MAP[parsedClassId]
? CLASS_ID_MAP[parsedClassId]
: null;
const rawName = (obj.class_name ?? obj.name ?? '').toLowerCase();
const kindByName = rawName.includes('person') || rawName.includes('行人') || rawName.includes('人') ? 'person'
: rawName.includes('motorcycle') || rawName.includes('摩托车') || rawName.includes('电驴') ? 'motorcycle'
: rawName.includes('bus') || rawName.includes('公交') || rawName.includes('大巴') ? 'bus'
: rawName.includes('truck') || rawName.includes('货车') || rawName.includes('卡车') ? 'truck'
: rawName.includes('car') || rawName.includes('车辆') || rawName.includes('车') ? 'car'
: rawName.includes('fence') || rawName.includes('围挡') || rawName.includes('障碍') ? 'fence'
: 'unknown';
const sx = Number(obj.size_x ?? obj.sizeX ?? (isPerson ? 0.6 : isVehicle ? 1.8 : 1.2));
const sy = Number(obj.size_y ?? obj.sizeY ?? (isPerson ? 0.6 : isVehicle ? 4.2 : 1.2));
const sz = Number(obj.size_z ?? obj.sizeZ ?? (isPerson ? 1.7 : isVehicle ? 1.5 : 1.5));
const obstacleKind = kindByClassId || kindByName;
const isPerson = obstacleKind === 'person';
const isVehicle = ['car', 'truck', 'bus', 'motorcycle'].includes(obstacleKind);
const isFence = obstacleKind === 'fence';
// 尺寸兜底:过滤 NaN / 负数 / 0,避免 Cesium BoxOutlineGeometry 抛 DeveloperError
const safeSize = (val, fallback) => {
const n = Number(val);
return Number.isFinite(n) && n > 0 ? n : fallback;
};
// 不同类别的默认尺寸(长 x 宽 x 高,单位米)
const KIND_DEFAULT_SIZE = {
person: { sx: 0.6, sy: 0.6, sz: 1.7 },
motorcycle: { sx: 0.8, sy: 2.2, sz: 1.6 },
car: { sx: 1.8, sy: 4.5, sz: 1.5 },
truck: { sx: 2.5, sy: 7.0, sz: 3.0 },
bus: { sx: 2.6, sy: 10.0, sz: 3.2 },
fence: { sx: 1.2, sy: 1.2, sz: 1.5 },
unknown: { sx: 1.2, sy: 1.2, sz: 1.5 },
};
const defaults = KIND_DEFAULT_SIZE[obstacleKind] || KIND_DEFAULT_SIZE.unknown;
const sx = safeSize(obj.size_x ?? obj.sizeX, defaults.sx);
const sy = safeSize(obj.size_y ?? obj.sizeY, defaults.sy);
const sz = safeSize(obj.size_z ?? obj.sizeZ, defaults.sz);
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return;
const id = `live-obstacle-${obj.track_id ?? obj.id ?? index}`;
const id = `live-obstacle-${index}`;
const position = Cesium.Cartesian3.fromDegrees(lon, lat, sz / 2);
if (!firstPos) firstPos = { lon, lat };
@@ -438,21 +519,21 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(Number(obj.angle ?? obj.yaw ?? 0)), 0, 0)
);
// 设置视觉差异
let boxColor = Cesium.Color.RED;
let typeEmoji = '⚠️';
let iconImage = null; // 用于存储图标
if (isPerson) {
boxColor = Cesium.Color.YELLOW;
typeEmoji = '🚶';
iconImage = 'https://img.icons8.com/color/96/walking--v1.png'; // 行人图标
} else if (isVehicle) {
boxColor = Cesium.Color.CYAN;
typeEmoji = '🚗';
iconImage = 'https://img.icons8.com/color/96/car--v1.png'; // 车辆图标
}
const labelText = `${typeEmoji} ${obj.class_name ?? '障碍物'} #${obj.track_id ?? obj.id ?? index} (${sx.toFixed(1)}x${sy.toFixed(1)}m)`;
// 不同类别的颜色、图标和文字
const KIND_STYLE = {
person: { color: Cesium.Color.YELLOW, emoji: '🚶', label: '行人', icon: 'https://img.icons8.com/color/96/walking--v1.png', width: 24 },
motorcycle: { color: Cesium.Color.ORANGE, emoji: '🛵', label: '摩托车', icon: 'https://img.icons8.com/color/96/motorcycle--v1.png', width: 28 },
car: { color: Cesium.Color.CYAN, emoji: '🚗', label: '小车', icon: 'https://img.icons8.com/color/96/car--v1.png', width: 32 },
truck: { color: Cesium.Color.LIME, emoji: '🚚', label: '货车', icon: 'https://img.icons8.com/color/96/delivery-truck--v1.png', width: 36 },
bus: { color: Cesium.Color.VIOLET, emoji: '🚌', label: '公交', icon: 'https://img.icons8.com/color/96/bus--v1.png', width: 36 },
unknown: { color: Cesium.Color.RED, emoji: '⚠️', label: '障碍物', icon: null, width: 32 },
};
const style = KIND_STYLE[obstacleKind] || KIND_STYLE.unknown;
const boxColor = style.color;
const typeEmoji = style.emoji;
const iconImage = style.icon;
const labelText = `${typeEmoji} ${style.label} (${sx.toFixed(1)}x${sy.toFixed(1)}m) ${dis ? `\n距离: ${dis.toFixed(1)}m` : ''}`;
const billboardSize = style.width;
let entity = viewer.entities.getById(id);
@@ -462,30 +543,32 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
position,
orientation,
// 外部包围盒,显示物理占用空间
box: {
dimensions: new Cesium.Cartesian3(sx, sy, sz),
material: boxColor.withAlpha(0.2),
outline: true,
outlineColor: boxColor,
outlineWidth: 1,
},
//box: {
// dimensions: new Cesium.Cartesian3(sx, sy, sz),
// material: boxColor.withAlpha(0.2),
// outline: true,
// outlineColor: boxColor,
// outlineWidth: 1,
//},
// 内部图标,直观显示物体类型
billboard: iconImage ? {
image: iconImage,
width: isPerson ? 24 : 32,
height: isPerson ? 24 : 32,
width: billboardSize,
height: billboardSize,
heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
scale: new Cesium.CallbackProperty(computeDistanceScale, false),
} : undefined,
label: {
text: labelText,
font: 'bold 14px sans-serif',
fillColor: Cesium.Color.WHITE,
showBackground: true,
backgroundColor: boxColor.withAlpha(0.85),
pixelOffset: new Cesium.Cartesian2(0, -40),
disableDepthTestDistance: Number.POSITIVE_INFINITY,
},
//label: {
// text: labelText,
// font: 'bold 14px sans-serif',
// fillColor: Cesium.Color.WHITE,
// showBackground: true,
// backgroundColor: boxColor.withAlpha(0.85),
// pixelOffset: new Cesium.Cartesian2(0, -40),
// disableDepthTestDistance: Number.POSITIVE_INFINITY,
// //scale: new Cesium.CallbackProperty(computeDistanceScale, false),
//},
});
} else {
entity.position = position;
@@ -497,27 +580,29 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
}
if (entity.billboard && iconImage) {
entity.billboard.image = iconImage;
entity.billboard.scale = new Cesium.CallbackProperty(computeDistanceScale, false);
}
if (entity.label) {
entity.label.text = labelText;
entity.label.backgroundColor = boxColor.withAlpha(0.85);
entity.label.scale = new Cesium.CallbackProperty(computeDistanceScale, false);
}
}
liveObstacleEntities.set(id, entity);
});
// 自动跳转到第一个障碍物
if (firstPos) {
flyToView(viewer, 100, {
destination: Cesium.Cartesian3.fromDegrees(firstPos.lon, firstPos.lat, 60),
orientation: {
heading: Cesium.Math.toRadians(0),
pitch: Cesium.Math.toRadians(-40),
roll: 0
},
duration: 2.0
}, 'live-obstacle-jump');
}
//if (firstPos) {
// flyToView(viewer, 100, {
// destination: Cesium.Cartesian3.fromDegrees(firstPos.lon, firstPos.lat, 60),
// orientation: {
// heading: Cesium.Math.toRadians(0),
// pitch: Cesium.Math.toRadians(-40),
// roll: 0
// },
// duration: 2.0
// }, 'live-obstacle-jump');
//}
viewer.scene.requestRender();
}
@@ -567,10 +652,79 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
// return () => clearTimeout(timer);
//}, [stationId]);
//useEffect(() => {
// const viewer = viewerRef.current;
// if (!viewer || !Cesium) return;
// let count = 0;
// const mockTimer = setInterval(() => {
// count++;
// let mockMsg;
// // 奇数次:模拟有障碍物
// if (count % 2 === 1) {
// mockMsg = {
// objects: [
// {
// track_id: "obs-v-001",
// latitude: 31.2989,
// longitude: 120.5853,
// size_x: 1.8,
// size_y: 4.2,
// size_z: 1.5,
// angle: 30,
// class_name: "车辆",
// distance: 15.5
// },
// {
// track_id: "obs-p-002",
// latitude: 31.2992,
// longitude: 120.5856,
// size_x: 0.6,
// size_y: 0.6,
// size_z: 1.7,
// angle: 0,
// class_name: "行人",
// distance: 8.2
// },
// {
// track_id: "obs-t-003",
// latitude: 31.2987,
// longitude: 120.5850,
// size_x: 2.5,
// size_y: 7.0,
// size_z: 3.0,
// angle: 90,
// class_name: "货车",
// distance: 22.6
// }
// ]
// };
// console.log("模拟发送:有障碍物", mockMsg);
// } else {
// // 偶数次:模拟没有障碍物
// mockMsg = {
// objects: []
// };
// console.log("模拟发送:没有障碍物");
// }
// updateLiveObstacleBoxes(mockMsg);
// }, 3000); // 每隔3秒发送一次
// return () => {
// clearInterval(mockTimer);
// };
//}, [stationId]);
const handleDetectFn = (res) => {
if (!res) return;
liveObstacleLastTsRef.current = Date.now();
updateLiveObstacleBoxes(res);
};
@@ -648,7 +802,6 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
};
const clearDrawingPath = () => {
const viewer = viewerRef.current;
if (!viewer) return;
@@ -724,6 +877,7 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
pixelOffset: new Cesium.Cartesian2(0, 0),
color: ringColor,
disableDepthTestDistance: Number.POSITIVE_INFINITY,
scale: new Cesium.CallbackProperty(computeDistanceScale, false),
heightReference: Cesium.HeightReference.NONE,
},
});
@@ -3891,6 +4045,28 @@ const CesiumMap = React.forwardRef(({ ifClear, ifClearPlanLine, ifClearNavLine,
handleDetectFn(detectReportInfo)
}, [detectReportInfo])
// // 超时兜底:2s 没收到新的障碍物消息,自动清掉(防止上游停推时残留)
useEffect(() => {
const TIMEOUT = 2000;
const timer = setInterval(() => {
const now = Date.now();
if (liveObstacleLastTsRef.current > 0 && now - liveObstacleLastTsRef.current > TIMEOUT) {
const v = viewerRef.current;
if (!v || !v.scene) return;
// 按 id 前缀扫掉所有 live-obstacle 实体(不依赖 Map 记录)
const toRemove = [];
v.entities.values.forEach((ent) => {
if (ent && ent.id && String(ent.id).startsWith('live-obstacle-')) toRemove.push(ent);
});
toRemove.forEach((ent) => v.entities.remove(ent));
liveObstacleEntities.clear();
liveObstacleLastTsRef.current = 0;
v.scene.requestRender();
}
}, 500);
return () => clearInterval(timer);
}, []);
// 暴露 ref 方法
useImperativeHandle(ref, () => ({
captureScreenshot,