缩减代码

This commit is contained in:
2026-08-09 22:00:30 +08:00
parent 11e7affc75
commit 256c41fa9c
28 changed files with 157 additions and 1024 deletions
+15 -138
View File
@@ -1,7 +1,6 @@
#include "Config.h"
#include "Config_Default.h"
#include <algorithm>
#include <array>
#include <exception>
#include <filesystem>
#include <mutex>
@@ -10,14 +9,8 @@
#include <utility>
#include "Global.h"
#include "Psc_Cpp_Core/Base/Check.h"
#include "Psc_Cpp_Core/Net_Adapter/Net_Adapter.h"
#undef interface
std::string default_config_directory;
namespace {
constexpr std::string_view config_section_names[] = {"version", "dsp", "drogon", "http_monitor", "mlat", "web_server", "map_resources", "map_view", "map_models", "device", "mode_acs", "log", "console", "external_database", "cesium_graphics"};
constexpr std::string_view config_section_name(Config_Section section) {
return config_section_names[static_cast<std::size_t>(section)];
}
std::filesystem::path config_directory() {
if (!default_config_directory.empty()) {
return default_config_directory;
@@ -116,7 +109,6 @@ Psc::JSON config_section_value(Global& global, Config_Section section) {
return global.map_model_config.read([](const auto& value) {
return value.to_base_json();
});
case Config_Section::device: return global.device_config.to_base_json();
case Config_Section::mode_acs: return global.mode_acs.to_base_json();
case Config_Section::log: return global.log_config.to_base_json();
case Config_Section::console: return global.console_config.to_base_json();
@@ -127,11 +119,10 @@ Psc::JSON config_section_value(Global& global, Config_Section section) {
std::terminate();
}
}
std::string get_config_directory() {
return config_directory().string();
}
Psc::JSON Config::load() {
std::filesystem::create_directories(config_directory());
std::error_code ignored_error;
std::filesystem::remove(config_directory() / "device.json", ignored_error);
const auto defaults = Psc::parse_json(std::string_view(reinterpret_cast<const char*>(ecap_default_config_json), sizeof(ecap_default_config_json)));
auto result = Psc::JSON::object();
for (std::size_t index = 0; index < static_cast<std::size_t>(Config_Section::count); ++index) {
@@ -141,20 +132,19 @@ Psc::JSON Config::load() {
std::cout << get_current_date_string() << " 启动加载配置目录: 【" << config_directory().string() << "" << std::endl;
return result;
}
static std::vector<std::string> read_string_array(const Psc::JSON* json, std::string_view key) {
auto field = json->get(key);
if (field == nullptr || field->valueType != Psc::Array) {
throw Psc::json_assign_error(std::make_error_code(std::errc::invalid_argument), std::string(key));
Psc::JSON Web_Server_Config::to_base_json() const {
return Psc::JSON::object({{"webapp", webapp}, {"listen_port", listen_port}});
}
void Web_Server_Config::from_base_json(const Psc::JSON* that_json, bool not_exist_use_default_value) {
std::error_code error;
auto result = Psc::assign_field_ec(webapp, that_json, "webapp", error);
if(!not_exist_use_default_value)
Psc::check_json_assign_field(result, error, that_json, "webapp");
if(that_json != nullptr && that_json->get("listen_port") != nullptr) {
error.clear();
result = Psc::assign_field_ec(listen_port, that_json, "listen_port", error);
Psc::check_json_assign_field(result, error, that_json, "listen_port");
}
std::vector<std::string> ret;
ret.reserve(field->children.size());
for (const auto& child : field->children) {
if (child.valueType != Psc::String) {
throw Psc::json_assign_error(std::make_error_code(std::errc::invalid_argument), std::string(key));
}
ret.emplace_back(child.val);
}
return ret;
}
static std::vector<std::string> read_optional_string_array(const Psc::JSON* json, std::string_view key) {
auto field = json->get(key);
@@ -459,11 +449,8 @@ void Map_View_Config::from_base_json(const Psc::JSON* that_json, bool not_exist_
map2d.from_base_json(that_json->get("map2d"), not_exist_use_default_value);
map3d.from_base_json(that_json->get("map3d"), not_exist_use_default_value);
camera.from_base_json(that_json->get("camera"), not_exist_use_default_value);
const auto height_key = that_json->get("base_station_fly_to_height_offset_meters")
? "base_station_fly_to_height_offset_meters"
: "baseStationFlyToHeightOffsetMeters";
base_station_fly_to_height_offset_meters =
std::max(100.0, read_optional_double_field(that_json, height_key, base_station_fly_to_height_offset_meters));
std::max(100.0, read_optional_double_field(that_json, "base_station_fly_to_height_offset_meters", base_station_fly_to_height_offset_meters));
}
Psc::JSON Map_View_Config::to_base_json() const {
auto ret = Psc::JSON::object();
@@ -552,116 +539,6 @@ void Mode_ACS_Config::server(Global* g) {
data_source_config.server(g);
data_feed_config.server(g);
}
void Device_Config::server(Global* g) {
auto& svr = g->svr;
auto& api = g->api;
svr.Post(api + "get_device_config", [this](HTTP_Param) { res->setBody(warp(to_base_json()).to_json_string()); });
svr.Post(api + "get_net_adapter_list", [this](HTTP_Param) {
JSON ret = JSON::object();
auto arr = JSON::array();
for (auto& it : Psc::get_net_adapter_info_map()) {
arr.append(it.second.to_Json());
}
ret.append({"list", arr});
res->setBody(warp(ret).to_json_string());
});
svr.Post(api + "set_device_config", [this](HTTP_Param) {
CHECK_JSON_PARAM
JSON old_device_config;
{
old_device_config = to_base_json();
}
HTTP_REQUIRE_PTR(net, params.get("net"))
Net_Config device, wifi;
bool has_device = false;
bool has_wifi = false;
for (const auto& it : net->children) {
HTTP_REQUIRE_VALUE(net_key, it.try_get_string("key"))
if (net_key == "device") {
device.from_base_json(&it, true);
has_device = true;
}
if (net_key == "wifi") {
wifi.from_base_json(&it, true);
has_wifi = true;
}
}
HTTP_REQUIRE_TRUE(has_device && has_wifi, "net")
bool it1 = check_ip_netmask_gateway(device.ip, device.netmask, device.gateway);
bool it2 = check_ip_netmask_gateway(wifi.ip, wifi.netmask, wifi.gateway);
bool p1 = check_port(device.port);
bool p2 = check_port(wifi.port);
if (!it1 || !it2 || !p1 || !p2) {
server_logger->error("", {}, LOG_POS + " ip port! " + device.ip + ":" + std::to_string(device.port));
res->setBody(warp(old_device_config).to_json_string());
return;
}
#if WIN32
bool use = false;
#else
bool use = true;
#endif
{
auto& w = *this;
*w.list.get("device").value() = device;
*w.list.get("wifi").value() = wifi;
}
Global::save(Config_Section::device);
if (use) {
std::string interface;
{
std::string path = get_exe_dir() + "/interface";
std::ifstream ifs(path);
if (!ifs.is_open()) {
server_logger->error("", {}, LOG_POS + " 无法打开文件! " + path);
res->setBody(warp(old_device_config).to_json_string());
return;
}
std::stringstream buffer;
buffer << ifs.rdbuf();
interface = buffer.str();
}
static auto set = [](std::string& interface, std::string_view token, std::string_view value) {
std::string var_name = "${" + std::string(token) + "}";
auto size = var_name.size();
size_t pos = interface.find(var_name);
while (pos != std::string::npos) {
interface.replace(pos, size, value);
pos = interface.find(var_name);
}
};
set(interface, "ip", device.ip);
set(interface, "gateway", device.gateway);
set(interface, "netmask", device.netmask);
set(interface, "wifi_ip", wifi.ip);
set(interface, "wifi_gateway", wifi.gateway);
set(interface, "wifi_netmask", wifi.netmask);
std::cout << interface << std::endl;
std::string path = "/etc/network/interfaces";
std::ofstream outfile(path);
if (!outfile.is_open()) {
server_logger->error("", {}, LOG_POS + " 无法打开文件!" + path);
res->setBody(warp(old_device_config).to_json_string());
return;
}
outfile << interface;
outfile.close();
std::string bash_path = get_exe_dir() + "/restart_net_service.sh";
if (!std::filesystem::exists(bash_path)) {
server_logger->error("", {}, LOG_POS + " !" + bash_path);
res->setBody(warp(old_device_config).to_json_string());
return;
}
int result = system(bash_path.c_str());
if (result != 0) {
server_logger->error("", {}, LOG_POS + " 脚本执行失败!" + bash_path);
res->setBody(warp(old_device_config).to_json_string());
return;
}
}
res->setBody(warp(params).to_json_string());
});
}
void Config::save() {
auto& global = *Global::instance();
for (std::size_t index = 0; index < static_cast<std::size_t>(Config_Section::count); ++index) {