From 8d5bf92bc62a417d2d37862c28ceee9752289dc4 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Fri, 20 Dec 2024 14:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E6=8E=89=E5=91=BD=E5=90=8D=E7=A9=BA?= =?UTF-8?q?=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/SystemConfig.cpp | 68 +++++-------------------------------- src/config/SystemConfig.h | 31 +++++------------ 2 files changed, 18 insertions(+), 81 deletions(-) diff --git a/src/config/SystemConfig.cpp b/src/config/SystemConfig.cpp index f7e32a2..b6151a1 100644 --- a/src/config/SystemConfig.cpp +++ b/src/config/SystemConfig.cpp @@ -1,73 +1,23 @@ -#include "config/SystemConfig.h" +#include "SystemConfig.h" #include "utils/Logger.h" #include -namespace config { - void SystemConfig::load(const std::string& filename) { try { std::ifstream file(filename); if (!file.is_open()) { - throw std::runtime_error("Cannot open config file: " + filename); + throw std::runtime_error("Failed to open config file: " + filename); } - + nlohmann::json j; file >> j; - - // 加载机场信息 - airport.name = j["airport"]["name"]; - airport.iata = j["airport"]["iata"]; - airport.icao = j["airport"]["icao"]; - airport.reference_point.latitude = j["airport"]["reference_point"]["latitude"]; - airport.reference_point.longitude = j["airport"]["reference_point"]["longitude"]; - airport.coordinate_points = j["airport"]["coordinate_points"].get>(); - - // 加载数据源配置 - data_source.host = j["data_source"]["host"]; - data_source.port = j["data_source"]["port"]; - data_source.aircraft_path = j["data_source"]["aircraft_path"]; - data_source.vehicle_path = j["data_source"]["vehicle_path"]; - data_source.traffic_light_path = j["data_source"]["traffic_light_path"]; - data_source.refresh_interval_ms = j["data_source"]["refresh_interval_ms"]; - data_source.timeout_ms = j["data_source"]["timeout_ms"]; - data_source.read_timeout_ms = j["data_source"]["read_timeout_ms"]; - - // 加载 WebSocket 配置 - websocket.port = j["websocket"]["port"]; - websocket.max_connections = j["websocket"]["max_connections"]; - websocket.ping_interval_ms = j["websocket"]["ping_interval_ms"]; - websocket.position_update.aircraft_interval_ms = j["websocket"]["position_update"]["aircraft_interval_ms"]; - websocket.position_update.vehicle_interval_ms = j["websocket"]["position_update"]["vehicle_interval_ms"]; - - // 加载碰撞检测配置 - collision_detection.update_interval_ms = j["collision_detection"]["update_interval_ms"]; - // 加载碰撞预测配置 - collision_detection.prediction.time_window = j["collision_detection"]["prediction"]["time_window"]; - collision_detection.prediction.vehicle_size = j["collision_detection"]["prediction"]["vehicle_size"]; - collision_detection.prediction.aircraft_size = j["collision_detection"]["prediction"]["aircraft_size"]; - collision_detection.prediction.min_unmanned_speed = j["collision_detection"]["prediction"]["min_unmanned_speed"]; - - // 加载日志配置 - logging.level = j["logging"]["level"]; - logging.file = j["logging"]["file"]; - logging.max_size_mb = j["logging"]["max_size_mb"]; - logging.max_files = j["logging"]["max_files"]; - logging.console_output = j["logging"]["console_output"]; - - // 加载调试配置 - debug.enable_mock_data = j["debug"]["enable_mock_data"]; - debug.save_raw_data = j["debug"]["save_raw_data"]; - debug.profile_performance = j["debug"]["profile_performance"]; - - // 加载告警配置 - warning.warning_interval_ms = j["warning"]["warning_interval_ms"]; - warning.log_interval_ms = j["warning"]["log_interval_ms"]; + // 使用 nlohmann/json 的自动转换 + from_json(j, *this); + + Logger::info("Loaded system configuration from ", filename); } catch (const std::exception& e) { - Logger::error("Failed to load system config: ", e.what()); - throw; + throw std::runtime_error("Failed to load config: " + std::string(e.what())); } -} - -} // namespace config \ No newline at end of file +} \ No newline at end of file diff --git a/src/config/SystemConfig.h b/src/config/SystemConfig.h index 05eb520..a488392 100644 --- a/src/config/SystemConfig.h +++ b/src/config/SystemConfig.h @@ -1,12 +1,11 @@ #pragma once #include +#include #include class System; // 前向声明 -namespace config { - struct CoordinatePoint { std::string point; double latitude; @@ -15,20 +14,16 @@ struct CoordinatePoint { class SystemConfig { public: - // 获取单例实例 static SystemConfig& instance() { static SystemConfig instance; return instance; } - - // 加载配置文件 + void load(const std::string& filename); - - // 删除拷贝构造和赋值操作 + SystemConfig(const SystemConfig&) = delete; SystemConfig& operator=(const SystemConfig&) = delete; - - // 允许 System 类访问构造函数 + friend class System; struct Airport { @@ -39,8 +34,6 @@ public: double latitude; double longitude; } reference_point; - - // 使用前向声明的坐标点结构体 std::vector coordinate_points; } airport; @@ -72,7 +65,7 @@ public: double time_window; double vehicle_size; double aircraft_size; - double min_unmanned_speed; // 无人车最低速度 (m/s) + double min_unmanned_speed; } prediction; } collision_detection; @@ -91,14 +84,13 @@ public: } debug; struct Warning { - int warning_interval_ms; // 超时告警间隔 - int log_interval_ms; // 日志记录间隔 + int warning_interval_ms; + int log_interval_ms; } warning; private: - // 移动构造函数到 private 区域 SystemConfig() = default; -}; +}; // JSON 序列化支持 NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(CoordinatePoint, point, latitude, longitude); @@ -112,9 +104,4 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::CollisionDetection, update_inte NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Logging, level, file, max_size_mb, max_files, console_output); NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Debug, enable_mock_data, save_raw_data, profile_performance); NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig::Warning, warning_interval_ms, log_interval_ms); -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig, airport, data_source, websocket, collision_detection, logging, debug, warning); - -} // namespace config - -using config::SystemConfig; -using config::CoordinatePoint; \ No newline at end of file +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SystemConfig, airport, data_source, websocket, collision_detection, logging, debug, warning); \ No newline at end of file