From a0ef997c98bcb52a51d1a50949361a8f8e61f1a9 Mon Sep 17 00:00:00 2001 From: wyc <1104749580@qq.com> Date: Mon, 13 Jul 2026 15:17:28 +0800 Subject: [PATCH] =?UTF-8?q?linux=20=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Local_Server/Data_Source/Data_Source.cpp | 141 ++++++++++-------- 1 file changed, 75 insertions(+), 66 deletions(-) diff --git a/module/Local_Server/Data_Source/Data_Source.cpp b/module/Local_Server/Data_Source/Data_Source.cpp index 138b4ed..da847d7 100644 --- a/module/Local_Server/Data_Source/Data_Source.cpp +++ b/module/Local_Server/Data_Source/Data_Source.cpp @@ -3,7 +3,8 @@ #include "../server/Global.h" #include "Database.h" #include -Psc::serial::Serial* create_serial(std::string_view serial_name, + +Psc::serial::Serial *create_serial(std::string_view serial_name, Baud_Rate_Type baud_rate) { auto serial = new Psc::serial::Serial; serial->set_serial_name(serial_name); @@ -15,16 +16,16 @@ Psc::serial::Serial* create_serial(std::string_view serial_name, serial->set_buffer_byte_size(10 * 1024); if (!serial->open()) { std::cerr << "createSerial " + std::string(serial_name) + ":" + - std::to_string(baud_rate) + " 打开串口失败!\n"; + std::to_string(baud_rate) + " 打开串口失败!\n"; Psc::fail_fast(); return nullptr; - } - else { + } else { // std::cerr << "createSerial " + serial_name + ":" + // std::to_string(baud_rate) + " 打开串口成功!\n"; } return serial; } + std::shared_ptr create_from_json(const JSON *that_json) { std::string type = that_json->get_string("type"); std::shared_ptr ret = create_data_source_from_type(type); @@ -35,18 +36,21 @@ std::shared_ptr create_from_json(const JSON *that_json) { // } return ret; } + std::shared_ptr Data_Source::that() { return shared_from_this(); } + bool Data_Source::registered() const { auto all_source = Global::instance()->mode_acs.data_source_config.map.list(); - for (auto& item : all_source) { + for (auto &item: all_source) { if (item.get() == this) { return true; } } return false; } + Psc::JSON Data_Source::get_state() { Psc::JSON ret = Psc::JSON::object(); ret.append_list(get_custom_state_json().children); @@ -54,6 +58,7 @@ Psc::JSON Data_Source::get_state() { ret.append({"ds: value_statistics(byte)", value_statistics}); return ret; } + Data_Source::Data_Source() { // parse_format = std::make_shared(); // 写入到配置文件是懒加载 其他保存时他跟着保存 @@ -65,14 +70,16 @@ Data_Source::Data_Source() { } }; } + std::string Data_Source::thread_key() const { // return "Data_Source_Handle_Thread:[" + key + "]"; return key + "_DS_HT"; } -void File_Data_Source::before_handle_msg(std::shared_ptr& msg) { + +void File_Data_Source::before_handle_msg(std::shared_ptr &msg) { auto cur = std::dynamic_pointer_cast(msg); if (cur != nullptr) { - auto& pre = last_prase_msg; + auto &pre = last_prase_msg; if (!pre) { pre = cur; return; @@ -83,11 +90,12 @@ void File_Data_Source::before_handle_msg(std::shared_ptr& msg) { } } } + std::vector readLines(std::string_view path) { std::vector lines; auto path_string = std::string(path); std::filesystem::path p = - std::filesystem::path((const char8_t*)path_string.c_str()); + std::filesystem::path((const char8_t *) path_string.c_str()); // std::filesystem::path p = std::filesystem::u8path(path); std::ifstream istream(p); if (!istream) { @@ -119,8 +127,7 @@ std::vector readLines(std::string_view path) { // if (ch == '\r' && stream.peek() == '\n') { // stream.get(); // 吃掉 \n // } - } - else { + } else { line += ch; } if (line.size() == 1000) { @@ -134,11 +141,12 @@ std::vector readLines(std::string_view path) { } std::ostringstream oss; oss << path_string + " 总行数:" + std::to_string(lines.size()) + " 有效行数:" + - std::to_string(lines.size()) + "\n" - << std::flush; + std::to_string(lines.size()) + "\n" + << std::flush; std::cout << oss.str() << std::flush; return lines; } + std::string extractID(std::string_view logLine) { size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格 if (lastSpacePos != std::string::npos) { @@ -146,6 +154,7 @@ std::string extractID(std::string_view logLine) { } return std::string(logLine); } + time_t convert_to_timestamp(std::string_view str) { // 创建一个结构体 tm 来存储解析后的时间 std::tm timeStruct = {}; @@ -163,6 +172,7 @@ time_t convert_to_timestamp(std::string_view str) { } return timestamp; } + std::tuple extractID2(std::string_view logLine) { size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格 time_t t = convert_to_timestamp(logLine.substr(0, 19)); @@ -174,14 +184,16 @@ std::tuple extractID2(std::string_view logLine) { } return {std::string(logLine), t}; } + std::string bin_format(std::string_view hex, time_t t) { std::tm *currentTime = std::localtime(&t); auto sec = currentTime->tm_hour * 3600 + currentTime->tm_min * 60 + - currentTime->tm_sec; + currentTime->tm_sec; SSR::MLAT_timestamp a(sec, 0); return create_Binary_Format_memory(hex, 0, &a); } -std::optional File_Data_Source::get_raw_line(int& ret_index) { + +std::optional File_Data_Source::get_raw_line(int &ret_index) { if (index == 0 && part_infos.empty()) { ret_index = -1; return std::nullopt; @@ -190,14 +202,13 @@ std::optional File_Data_Source::get_raw_line(int& ret_index) { if (index == part_infos.size()) { if (play_mode == Play_Mode::loop) { index = 0; - } - else { + } else { if (!have_report_play_back_all_success) { std::ostringstream oss; oss << SSR::get_current_date() << " " << SSR::get_current_time() - << " [进程:" << std::this_thread::get_id() << "] " - << file_path + " 全部加载成功!\n" - << std::flush; + << " [进程:" << std::this_thread::get_id() << "] " + << file_path + " 全部加载成功!\n" + << std::flush; std::cout << oss.str() << std::flush; have_report_play_back_all_success = true; } @@ -208,6 +219,7 @@ std::optional File_Data_Source::get_raw_line(int& ret_index) { ret_index = index + 1; return part_infos[index++]; } + // 1A 33 1A 1A F1 FB 87 73 7E 7F a8001d81a87543b0a80000 std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, int index, std::string_view log_info) { @@ -215,12 +227,10 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, // 2025-01-06 07:20:17 [info] dc2541e65001401a3119b2dd1c7af67132201 if (data_type == File_Data_Type::BIN) { return std::string(log_info); - } - else if (data_type == File_Data_Type::BIN_Text) { + } else if (data_type == File_Data_Type::BIN_Text) { auto info = extractID(log_info); ret = hex2mem(info); - } - else if (data_type == File_Data_Type::AVR) { + } else if (data_type == File_Data_Type::AVR) { auto [info, time] = extractID2(log_info); ret = bin_format(info, time); } @@ -231,18 +241,16 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, std::string info(log_info); std::string result = ds->last_char; ds->last_char = ""; - for (char c : info) { + for (char c: info) { if (std::isxdigit(c)) { // 会判断字符 c 是否是十六进制数字字符 result += c; - } - else if (std::isspace(c)) { + } else if (std::isspace(c)) { // - } - else { + } else { std::ostringstream oss; - oss << "存在非法字符[" << c << "]: value:" << (int)c << " in:" - << log_info << LOG_POS; + oss << "存在非法字符[" << c << "]: value:" << (int) c << " in:" + << log_info << LOG_POS; server_logger->c_debug("非法字符:", {}, oss.str()); return ""; } @@ -254,29 +262,25 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, result.pop_back(); } ret = hex2mem(result); - } - else if (data_type == File_Data_Type::BIN_Blank_One_Line_With_Escape) { - auto size = log_info.size(); + } else if (data_type == File_Data_Type::BIN_Blank_One_Line_With_Escape) { + auto size = log_info.size(); if (size % 2 != 0) { return hex2mem(std::string(log_info.substr(0, size - 1))); } return hex2mem(std::string(log_info)); - } - else if (data_type == File_Data_Type::BIN_Blank_One_Line_No_Escape) { + } else if (data_type == File_Data_Type::BIN_Blank_One_Line_No_Escape) { auto size = log_info.size(); std::string data; if (size % 2 != 0) { data = std::string(log_info.substr(0, size - 1)); - } - else { + } else { data = std::string(log_info); } return SSR::packet_to_escape_format(hex2mem(data)); - } - else if (data_type == File_Data_Type::SIMPLE_BIN_Blank) { + } else if (data_type == File_Data_Type::SIMPLE_BIN_Blank) { std::string info(log_info); std::string result; - for (char c : info) { + for (char c: info) { if (c != ' ') { result += c; } @@ -286,8 +290,8 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, int size = result.size(); if (size != 2 * 2 && size != 7 * 2 && size != 14 * 2) { std::cout << "line_offset:[" << index << "] " << "大小" << size - << " 不为偶数,或不为2,7,14 不能转换成二进制格式:" << result - << std::endl; + << " 不为偶数,或不为2,7,14 不能转换成二进制格式:" << result + << std::endl; return ""; } std::string head("\x1a"); @@ -296,29 +300,28 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type, std::string signal_level("\xFF"); if (size == 2 * 2) { type = R"(1)"; - } - else if (size == 7 * 2) { + } else if (size == 7 * 2) { type = R"(2)"; - } - else if (size == 14 * 2) { + } else if (size == 14 * 2) { type = R"(3)"; } ret = SSR::packet_to_escape_format(head + type + time_stamp + signal_level + - hex2mem(result)); + hex2mem(result)); // std::cout << ret.size() << std::endl; - } - else { + } else { std::cout << "未知的回放类型 " << static_cast(data_type) << std::endl; Psc::fail_fast(); } return ret; } + asio::awaitable File_Data_Source::_close() { std::lock_guard g(mtx); index = 0; part_infos.clear(); co_return; } + asio::awaitable File_Data_Source::_open() { std::lock_guard g(mtx); auto path = get_true_file_path(); @@ -329,17 +332,17 @@ asio::awaitable File_Data_Source::_open() { } if (data_type == File_Data_Type::BIN) { part_infos = readBinaryFileAsString(path, 1000); - } - else { + } else { part_infos = readLines(path); } state = "已加载" + to_string(part_infos.size()) + "长度数据!"; co_return; } + std::vector File_Data_Source::readBinaryFileAsString(std::string_view filepath, size_t part_size) { // 打开文件(以二进制模式) - std::ifstream file(filepath, std::ios::binary); + std::ifstream file(filepath.data(), std::ios::binary); if (!file) { std::cerr << "无法打开文件: " << filepath << std::endl; return {}; // 文件打开失败,返回空vector @@ -369,7 +372,8 @@ std::vector File_Data_Source::readBinaryFileAsString(std::string_vi file.close(); return parts; } -void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) { + +void File_Data_Source::origin_data_transform_mode_data(std::string &mode_data) { std::lock_guard g(mtx); assert(mode_data.size() == 0); int ret_index = 0; @@ -379,8 +383,7 @@ void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) { ? get_true_from_raw_line(this, data_type, ret_index, log_info.value()) : ""; - } - else { + } else { int num = 0; std::string ret; std::optional log_info = get_raw_line(ret_index); @@ -399,6 +402,7 @@ void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) { mode_data = ret; } } + void File_Data_Source::handle_mode_s(std::shared_ptr msg) { if (!msg) return; cache_list.push_back(std::make_shared(std::move(msg))); @@ -406,7 +410,7 @@ void File_Data_Source::handle_mode_s(std::shared_ptr msg) { if (pre_cache == nullptr) { pre_cache = cache_list.front(); cache_list.pop_front(); - SSR::Play_Back_Time_Point& start = player_clock.start_time; + SSR::Play_Back_Time_Point &start = player_clock.start_time; start.day_num = pre_cache->day_num; start.day_sec = pre_cache->msg->mlat_timestamp.daysec; } @@ -427,44 +431,48 @@ void File_Data_Source::handle_mode_s(std::shared_ptr msg) { } } } -void Dll_Data_Source::origin_data_transform_mode_data(std::string& mode_data) { + +void Dll_Data_Source::origin_data_transform_mode_data(std::string &mode_data) { auto start = std::chrono::steady_clock::now(); if (read_func_ptr == nullptr) return; - auto buf = reinterpret_cast(buffer.data()); + auto buf = reinterpret_cast(buffer.data()); auto len = read_func_ptr(buf, buffer_size); vs.update(len); std::string data(buf, len); if (data.empty()) { auto end = std::chrono::steady_clock::now(); auto cost = - std::chrono::duration_cast(end - start) - .count(); + std::chrono::duration_cast(end - start) + .count(); // std::cout << "Dll_Data_Source::read cost: " << cost << " us\n"; return; } auto ret = get_true_from_raw_line(this, data_type, -1, data); auto end = std::chrono::steady_clock::now(); auto cost = std::chrono::duration_cast(end - start) - .count(); + .count(); // std::cout << "Dll_Data_Source::read cost: " << cost << " us\n"; mode_data = ret; } + asio::awaitable Shared_Memory_Data_Source::_open() { sm = std::make_unique(); sm->init(shared_memory_name, shared_memory_size); co_return; } + asio::awaitable Shared_Memory_Data_Source::_close() { sm.reset(); co_return; } + void Shared_Memory_Data_Source::origin_data_transform_mode_data( - std::string& mode_data) { + std::string &mode_data) { auto size = sm->shm.size(); size_t length = size; std::string data; data.resize(size); - bool ok = sm->read((uint8_t*)data.data(), length); + bool ok = sm->read((uint8_t *) data.data(), length); if (!ok) { mode_data = ""; } @@ -477,9 +485,10 @@ void Shared_Memory_Data_Source::origin_data_transform_mode_data( auto ret = get_true_from_raw_line(this, data_type, -1, data); mode_data = ret; } + void Data_Source_Config::server(Global *g) { - auto& svr = g->svr; - auto& api = g->api; + auto &svr = g->svr; + auto &api = g->api; std::string name = "data_source"; // 返回所有 JSON 数据的 API svr.Post(api + "get_data_source_connect", [this](HTTP_Param) { @@ -522,7 +531,7 @@ void Data_Source_Config::server(Global *g) { std::shared_ptr ds = create_data_source_from_type(t); ds->from_json(data); HTTP_REQUIRE_VALUE(enable, data->try_get_bool("enable")) - (void)enable; + (void) enable; HTTP_REQUIRE_TRUE(map.insert(index, ds), "index") // std::cout << params.to_json_string() << std::endl; g->mode_acs.source_feed_relation_config.set_need_refresh();