修改 mock_server.py 和 HTTPDataSource.cpp 文件,匹配机场位置接口
This commit is contained in:
parent
383f2caf1a
commit
4f1e7e4eba
@ -85,6 +85,6 @@ echo "2. Extract: tar xzf ${PACKAGE_NAME}"
|
|||||||
echo "3. Verify checksum: sha256sum -c ${PROJECT_NAME}.sha256"
|
echo "3. Verify checksum: sha256sum -c ${PROJECT_NAME}.sha256"
|
||||||
echo "4. Stop service: systemctl stop collision-avoidance"
|
echo "4. Stop service: systemctl stop collision-avoidance"
|
||||||
echo "5. Backup old binary"
|
echo "5. Backup old binary"
|
||||||
echo "6. Copy new binary: cp bin/${PROJECT_NAME} /usr/local/bin/"
|
echo "6. Copy new binary: cp bin/${PROJECT_NAME} /opt/collision_avoidance/bin/"
|
||||||
echo "7. Start service: systemctl start collision-avoidance"
|
echo "7. Start service: systemctl start collision-avoidance"
|
||||||
echo "8. Check status: systemctl status collision-avoidance"
|
echo "8. Check status: systemctl status collision-avoidance"
|
||||||
@ -291,15 +291,30 @@ bool HTTPDataSource::parseVehicleResponse(const std::string& response, std::vect
|
|||||||
try {
|
try {
|
||||||
json j = json::parse(response);
|
json j = json::parse(response);
|
||||||
|
|
||||||
if (!j.is_array()) {
|
// 检查顶层结构
|
||||||
Logger::error("Invalid vehicle response format: expected array");
|
if (!j.contains("status") || !j.contains("data")) {
|
||||||
|
Logger::error("Invalid vehicle response format: missing status or data field");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查状态码
|
||||||
|
int status = j["status"].get<int>();
|
||||||
|
if (status != 200) {
|
||||||
|
Logger::error("Vehicle response error, status: " + std::to_string(status));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取数据数组
|
||||||
|
const auto& data = j["data"];
|
||||||
|
if (!data.is_array()) {
|
||||||
|
Logger::error("Invalid vehicle response format: data is not an array");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
vehicles.clear();
|
vehicles.clear();
|
||||||
vehicles.reserve(j.size());
|
vehicles.reserve(data.size());
|
||||||
|
|
||||||
for (const auto& item : j) {
|
for (const auto& item : data) {
|
||||||
Vehicle veh;
|
Vehicle veh;
|
||||||
|
|
||||||
// 解析必需字段
|
// 解析必需字段
|
||||||
@ -315,12 +330,7 @@ bool HTTPDataSource::parseVehicleResponse(const std::string& response, std::vect
|
|||||||
veh.geo.longitude = item["longitude"].get<double>();
|
veh.geo.longitude = item["longitude"].get<double>();
|
||||||
|
|
||||||
// 时间戳转换
|
// 时间戳转换
|
||||||
int64_t time = item["time"].get<int64_t>();
|
veh.timestamp = item["time"].get<uint64_t>();
|
||||||
if (time < 0) {
|
|
||||||
Logger::warning("收到负数时间戳: ", time, ",设置为0");
|
|
||||||
time = 0;
|
|
||||||
}
|
|
||||||
veh.timestamp = static_cast<uint64_t>(time);
|
|
||||||
|
|
||||||
// 解析可选字段
|
// 解析可选字段
|
||||||
if (item.contains("direction")) {
|
if (item.contains("direction")) {
|
||||||
@ -353,15 +363,34 @@ bool HTTPDataSource::parseAircraftResponse(const std::string& response, std::vec
|
|||||||
try {
|
try {
|
||||||
json j = json::parse(response);
|
json j = json::parse(response);
|
||||||
|
|
||||||
if (!j.is_array()) {
|
// 检查顶层结构
|
||||||
Logger::error("Invalid aircraft response format: expected array");
|
if (!j.contains("status") || !j.contains("data")) {
|
||||||
|
Logger::error("Invalid aircraft response format: missing status or data field");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查状态码
|
||||||
|
int status = j["status"].get<int>();
|
||||||
|
if (status != 200) {
|
||||||
|
Logger::error("Aircraft response error, status: " + std::to_string(status));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取数据数组
|
||||||
|
const auto& data = j["data"];
|
||||||
|
if (!data.is_array()) {
|
||||||
|
Logger::error("Invalid aircraft response format: data is not an array");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
aircraft.clear();
|
aircraft.clear();
|
||||||
aircraft.reserve(j.size());
|
aircraft.reserve(data.size());
|
||||||
|
|
||||||
for (const auto& item : j) {
|
// 获取当前时间戳,用于临时替代
|
||||||
|
uint64_t current_timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
||||||
|
|
||||||
|
for (const auto& item : data) {
|
||||||
Aircraft ac;
|
Aircraft ac;
|
||||||
|
|
||||||
// 解析必需字段
|
// 解析必需字段
|
||||||
@ -376,13 +405,9 @@ bool HTTPDataSource::parseAircraftResponse(const std::string& response, std::vec
|
|||||||
ac.geo.latitude = item["latitude"].get<double>();
|
ac.geo.latitude = item["latitude"].get<double>();
|
||||||
ac.geo.longitude = item["longitude"].get<double>();
|
ac.geo.longitude = item["longitude"].get<double>();
|
||||||
|
|
||||||
// 时间戳转换
|
// 时间戳处理:暂时使用本地时间戳
|
||||||
int64_t time = item["time"].get<int64_t>();
|
// 注意:机场给的时间戳可能是 double 格式,我们暂时不使用它
|
||||||
if (time < 0) {
|
ac.timestamp = current_timestamp;
|
||||||
Logger::warning("收到负数时间戳: ", time, ",设置为0");
|
|
||||||
time = 0;
|
|
||||||
}
|
|
||||||
ac.timestamp = static_cast<uint64_t>(time);
|
|
||||||
|
|
||||||
// 解析可选字段
|
// 解析可选字段
|
||||||
if (item.contains("altitude")) {
|
if (item.contains("altitude")) {
|
||||||
|
|||||||
@ -776,7 +776,11 @@ def get_flight_positions():
|
|||||||
}
|
}
|
||||||
response_data.append(api_aircraft)
|
response_data.append(api_aircraft)
|
||||||
|
|
||||||
return jsonify(response_data)
|
return jsonify({
|
||||||
|
"status": 200,
|
||||||
|
"msg": "当前航空器实时位置数据",
|
||||||
|
"data": response_data
|
||||||
|
})
|
||||||
|
|
||||||
def switch_traffic_light_state():
|
def switch_traffic_light_state():
|
||||||
"""统一处理红绿灯状态切换"""
|
"""统一处理红绿灯状态切换"""
|
||||||
@ -827,11 +831,19 @@ def get_vehicle_positions():
|
|||||||
vehicle["time"] = int(current_time * 1000)
|
vehicle["time"] = int(current_time * 1000)
|
||||||
last_vehicle_update_time = current_time
|
last_vehicle_update_time = current_time
|
||||||
|
|
||||||
return jsonify(vehicle_data)
|
return jsonify({
|
||||||
|
"status": 200,
|
||||||
|
"msg": "当前车辆实时位置数据",
|
||||||
|
"data": vehicle_data
|
||||||
|
})
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error in get_vehicle_positions: {str(e)}")
|
print(f"Error in get_vehicle_positions: {str(e)}")
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({
|
||||||
|
"status": 500,
|
||||||
|
"msg": str(e),
|
||||||
|
"data": None
|
||||||
|
}), 500
|
||||||
|
|
||||||
@app.route('/getTrafficLightSignals', methods=['GET', 'OPTIONS'])
|
@app.route('/getTrafficLightSignals', methods=['GET', 'OPTIONS'])
|
||||||
def get_traffic_light_signals():
|
def get_traffic_light_signals():
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user