修改 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 "4. Stop service: systemctl stop collision-avoidance"
|
||||
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 "8. Check status: systemctl status collision-avoidance"
|
||||
@ -291,15 +291,30 @@ bool HTTPDataSource::parseVehicleResponse(const std::string& response, std::vect
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
vehicles.clear();
|
||||
vehicles.reserve(j.size());
|
||||
vehicles.reserve(data.size());
|
||||
|
||||
for (const auto& item : j) {
|
||||
for (const auto& item : data) {
|
||||
Vehicle veh;
|
||||
|
||||
// 解析必需字段
|
||||
@ -315,12 +330,7 @@ bool HTTPDataSource::parseVehicleResponse(const std::string& response, std::vect
|
||||
veh.geo.longitude = item["longitude"].get<double>();
|
||||
|
||||
// 时间戳转换
|
||||
int64_t time = item["time"].get<int64_t>();
|
||||
if (time < 0) {
|
||||
Logger::warning("收到负数时间戳: ", time, ",设置为0");
|
||||
time = 0;
|
||||
}
|
||||
veh.timestamp = static_cast<uint64_t>(time);
|
||||
veh.timestamp = item["time"].get<uint64_t>();
|
||||
|
||||
// 解析可选字段
|
||||
if (item.contains("direction")) {
|
||||
@ -353,15 +363,34 @@ bool HTTPDataSource::parseAircraftResponse(const std::string& response, std::vec
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 解析必需字段
|
||||
@ -376,13 +405,9 @@ bool HTTPDataSource::parseAircraftResponse(const std::string& response, std::vec
|
||||
ac.geo.latitude = item["latitude"].get<double>();
|
||||
ac.geo.longitude = item["longitude"].get<double>();
|
||||
|
||||
// 时间戳转换
|
||||
int64_t time = item["time"].get<int64_t>();
|
||||
if (time < 0) {
|
||||
Logger::warning("收到负数时间戳: ", time, ",设置为0");
|
||||
time = 0;
|
||||
}
|
||||
ac.timestamp = static_cast<uint64_t>(time);
|
||||
// 时间戳处理:暂时使用本地时间戳
|
||||
// 注意:机场给的时间戳可能是 double 格式,我们暂时不使用它
|
||||
ac.timestamp = current_timestamp;
|
||||
|
||||
// 解析可选字段
|
||||
if (item.contains("altitude")) {
|
||||
|
||||
@ -745,7 +745,7 @@ def get_flight_positions():
|
||||
"""获取当前航空器位置信息"""
|
||||
if request.method == 'OPTIONS':
|
||||
return '', 204
|
||||
|
||||
|
||||
if not check_auth():
|
||||
return jsonify({
|
||||
"status": 401,
|
||||
@ -776,7 +776,11 @@ def get_flight_positions():
|
||||
}
|
||||
response_data.append(api_aircraft)
|
||||
|
||||
return jsonify(response_data)
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
"msg": "当前航空器实时位置数据",
|
||||
"data": response_data
|
||||
})
|
||||
|
||||
def switch_traffic_light_state():
|
||||
"""统一处理红绿灯状态切换"""
|
||||
@ -827,11 +831,19 @@ def get_vehicle_positions():
|
||||
vehicle["time"] = int(current_time * 1000)
|
||||
last_vehicle_update_time = current_time
|
||||
|
||||
return jsonify(vehicle_data)
|
||||
return jsonify({
|
||||
"status": 200,
|
||||
"msg": "当前车辆实时位置数据",
|
||||
"data": vehicle_data
|
||||
})
|
||||
|
||||
except Exception as 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'])
|
||||
def get_traffic_light_signals():
|
||||
|
||||
Loading…
Reference in New Issue
Block a user