diff --git a/src/core/System.cpp b/src/core/System.cpp index 580bb46..a50a927 100644 --- a/src/core/System.cpp +++ b/src/core/System.cpp @@ -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::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::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()); } -} \ No newline at end of file +} diff --git a/src/network/HTTPClient.cpp b/src/network/HTTPClient.cpp index 2db3273..362d6c8 100644 --- a/src/network/HTTPClient.cpp +++ b/src/network/HTTPClient.cpp @@ -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; } -} \ No newline at end of file +}