增强车辆指令处理,确保在 ALERT 后补发 PARKING 指令,并优化 HTTPClient 以保持字段完整性

This commit is contained in:
sladro 2026-02-08 12:20:09 +08:00
parent f406782d4c
commit 3463926e98
2 changed files with 35 additions and 6 deletions

View File

@ -520,6 +520,20 @@ void System::processCollisions(
broadcastVehicleCommand(cmd);
controllableVehicles_.sendCommand(vehicleId, cmd);
// ALERT 后立即补发 PARKING 作为停止指令
if (cmd.type == CommandType::ALERT) {
VehicleCommand parkingCmd = cmd;
parkingCmd.type = CommandType::PARKING;
parkingCmd.reason = CommandReason::PARKING_SIDE;
parkingCmd.timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
broadcastVehicleCommand(parkingCmd);
controllableVehicles_.sendCommand(vehicleId, parkingCmd);
Logger::info("ALERT 后补发停车指令到车辆: ", vehicleId);
}
// 更新风险记录
currentVehiclesWithRisk.insert(vehicleId);
vehicleMaxRiskLevels[vehicleId] = risk.level;
@ -866,6 +880,20 @@ bool System::handleSafetyZoneRisk(const Vehicle& vehicle,
broadcastVehicleCommand(cmd);
controllableVehicles_.sendCommand(vehicle.id, cmd);
// 安全区触发 ALERT 时也补发 PARKING 作为停止指令
if (cmd.type == CommandType::ALERT) {
VehicleCommand parkingCmd = cmd;
parkingCmd.type = CommandType::PARKING;
parkingCmd.reason = CommandReason::PARKING_SIDE;
parkingCmd.timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
broadcastVehicleCommand(parkingCmd);
controllableVehicles_.sendCommand(vehicle.id, parkingCmd);
Logger::info("安全区 ALERT 后补发停车指令到车辆: ", vehicle.id);
}
CollisionRisk risk;
risk.id1 = vehicle.id;
risk.id2 = target ? target->id : "";
@ -947,4 +975,4 @@ void System::processPushedTrafficLightData(const nlohmann::json& di_data) {
} catch (const std::exception& e) {
Logger::error("处理推送的交通信号灯数据时出错: ", e.what());
}
}
}

View File

@ -97,10 +97,11 @@ bool HTTPClient::sendCommand(const std::string& host, int port, const std::strin
{"longitude", command.longitude}
};
if (command.type == CommandType::SIGNAL) {
request["signalState"] = getSignalStateString(command.signalState);
request["intersectionId"] = command.intersectionId;
}
// 字段完整性:即使非 SIGNAL 也保留字段,使用占位值避免后端校验失败
request["signalState"] =
command.type == CommandType::SIGNAL ? getSignalStateString(command.signalState) : "";
request["intersectionId"] =
command.type == CommandType::SIGNAL ? command.intersectionId : "";
// 后端当前校验要求该字段始终存在;对非 ALERT/WARNING 场景默认填 0
request["relativeSpeed"] = command.relativeSpeed;
@ -174,4 +175,4 @@ bool HTTPClient::sendCommand(const std::string& host, int port, const std::strin
Logger::error("Command failed with HTTP code: ", http_code, " response: ", response_buffer_);
return false;
}
}
}