This commit is contained in:
2026-08-05 11:59:45 +08:00
parent 18e64dfe22
commit 587c1d6d01
22 changed files with 2517 additions and 3049 deletions
+38 -61
View File
@@ -1,10 +1,9 @@
#include "Data_Source.h"
#include <codecvt>
#include <string_view>
#include "../server/Global.h"
#include "Database.h"
#include <string_view>
Psc::serial::Serial* create_serial(std::string_view serial_name,
Baud_Rate_Type baud_rate) {
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);
serial->set_baud_rate(baud_rate);
@@ -14,8 +13,7 @@ Psc::serial::Serial* create_serial(std::string_view serial_name,
serial->set_flow_control(Psc::serial::FlowControl::HardwareControl);
serial->set_buffer_byte_size(10 * 1024);
if (!serial->open()) {
std::cerr << "createSerial " + std::string(serial_name) + ":" +
std::to_string(baud_rate) + " 打开串口失败!\n";
std::cerr << "createSerial " + std::string(serial_name) + ":" + std::to_string(baud_rate) + " 打开串口失败!\n";
Psc::fail_fast();
return nullptr;
}
@@ -25,14 +23,10 @@ Psc::serial::Serial* create_serial(std::string_view serial_name,
}
return serial;
}
std::shared_ptr<Data_Source> create_from_json(const JSON* that_json) {
std::shared_ptr<Data_Source> create_from_json(const JSON* that_json, bool not_exist_use_default_value) {
std::string type = that_json->get_string("type");
std::shared_ptr<Data_Source> ret = create_data_source_from_type(type);
ret->from_json(that_json);
// if (ret->enable)
// {
// ret->open();
// }
ret->from_json(that_json, not_exist_use_default_value);
return ret;
}
std::shared_ptr<Data_Source> Data_Source::that() {
@@ -87,8 +81,7 @@ void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
std::vector<std::string> readLines(std::string_view path) {
std::vector<std::string> lines;
auto path_string = std::string(path);
std::filesystem::path p =
std::filesystem::path((const char8_t*)path_string.c_str());
std::filesystem::path p = std::filesystem::path((const char8_t*)path_string.c_str());
// std::filesystem::path p = std::filesystem::u8path(path);
std::ifstream istream(p);
if (!istream) {
@@ -134,9 +127,8 @@ std::vector<std::string> readLines(std::string_view path) {
lines.push_back(line);
}
std::ostringstream oss;
oss << path_string + " 总行数:" + std::to_string(lines.size()) + " 有效行数:" +
std::to_string(lines.size()) + "\n"
<< std::flush;
oss << path_string + " 总行数:" + std::to_string(lines.size()) + " 有效行数:" + std::to_string(lines.size()) +
"\n" << std::flush;
std::cout << oss.str() << std::flush;
return lines;
}
@@ -168,17 +160,13 @@ std::tuple<std::string, time_t> extractID2(std::string_view logLine) {
size_t lastSpacePos = logLine.rfind(" "); // 查找最后一个空格
time_t t = convert_to_timestamp(logLine.substr(0, 19));
if (lastSpacePos != std::string::npos) {
return {
std::string(logLine.substr(lastSpacePos + 1)),
t
}; // 从最后一个空格之后提取字符串
return {std::string(logLine.substr(lastSpacePos + 1)), t}; // 从最后一个空格之后提取字符串
}
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;
auto sec = currentTime->tm_hour * 3600 + currentTime->tm_min * 60 + currentTime->tm_sec;
SSR::MLAT_timestamp a(sec, 0);
return create_Binary_Format_memory(hex, 0, &a);
}
@@ -196,9 +184,8 @@ std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
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;
}
@@ -210,8 +197,7 @@ std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
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) {
std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type, int index, std::string_view log_info) {
std::string ret;
// 2025-01-06 07:20:17 [info] dc2541e65001401a3119b2dd1c7af67132201
if (data_type == File_Data_Type::BIN) {
@@ -242,8 +228,7 @@ std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type,
}
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 "";
}
@@ -287,8 +272,7 @@ 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");
@@ -304,8 +288,7 @@ std::string get_true_from_raw_line(Data_Source* ds, File_Data_Type data_type,
else if (size == 14 * 2) {
type = R"(3)";
}
ret = SSR::packet_to_escape_format(head + type + time_stamp + signal_level +
hex2mem(result));
ret = SSR::packet_to_escape_format(head + type + time_stamp + signal_level + hex2mem(result));
// std::cout << ret.size() << std::endl;
}
else {
@@ -337,8 +320,7 @@ asio::awaitable<void> File_Data_Source::_open() {
state = "已加载" + to_string(part_infos.size()) + "长度数据!";
co_return;
}
std::vector<std::string> File_Data_Source::readBinaryFileAsString(std::string_view filepath,
size_t part_size) {
std::vector<std::string> File_Data_Source::readBinaryFileAsString(std::string_view filepath, size_t part_size) {
// 打开文件(以二进制模式)
std::ifstream file(filepath.data(), std::ios::binary);
if (!file) {
@@ -376,10 +358,7 @@ void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
int ret_index = 0;
if (play_mode != Play_Mode::analysis) {
std::optional<std::string> log_info = get_raw_line(ret_index);
mode_data = log_info.has_value()
? get_true_from_raw_line(this, data_type, ret_index,
log_info.value())
: "";
mode_data = log_info.has_value() ? get_true_from_raw_line(this, data_type, ret_index, log_info.value()) : "";
}
else {
int num = 0;
@@ -400,10 +379,12 @@ 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<SSR::Mode_S_Msg> msg) {
if (!msg) return;
void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg>& msg) {
if (!msg)
return;
cache_list.push_back(std::make_shared<Cache_Msg>(std::move(msg)));
if (cache_list.empty()) return;
if (cache_list.empty())
return;
if (pre_cache == nullptr) {
pre_cache = cache_list.front();
cache_list.pop_front();
@@ -413,7 +394,8 @@ void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
}
auto sys_time = player_clock.get_cur_time_point();
while (true) {
if (cache_list.empty()) break;
if (cache_list.empty())
break;
auto pre = pre_cache;
auto cur = cache_list.front();
cur->day_num = pre->day_num;
@@ -430,23 +412,21 @@ void File_Data_Source::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) {
}
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;
if (read_func_ptr == nullptr)
return;
auto buf = reinterpret_cast<char*>(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<std::chrono::microseconds>(end - start)
.count();
auto cost = std::chrono::duration_cast<std::chrono::microseconds>(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<std::chrono::microseconds>(end - start)
.count();
auto cost = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
// std::cout << "Dll_Data_Source::read cost: " << cost << " us\n";
mode_data = ret;
}
@@ -459,8 +439,7 @@ asio::awaitable<void> Shared_Memory_Data_Source::_close() {
sm.reset();
co_return;
}
void Shared_Memory_Data_Source::origin_data_transform_mode_data(
std::string& mode_data) {
void Shared_Memory_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
auto size = sm->shm.size();
size_t length = size;
std::string data;
@@ -503,36 +482,37 @@ void Data_Source_Config::server(Global* g) {
}
res->setBody(warp(ret).to_json_string());
});
svr.Post(api + "get_mode_s_data_source_config", [this](HTTP_Param) {
res->setBody(warp(to_json()).to_json_string());
});
svr.Post(api + "get_mode_s_data_source_config",
[this](HTTP_Param) { res->setBody(warp(to_json()).to_json_string()); });
svr.Post(api + svr.update + name, [this, g](HTTP_Param) {
CHECK_JSON_PARAM
std::cout << "Data_Source:" << req->path() << that_json->to_json_string();
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
HTTP_REQUIRE_VALUE(ds, map.get(key))
ds->from_json(&params);
ds->from_json(&params, true);
g->save();
g->mode_acs.source_feed_relation_config.set_need_refresh();
res->setBody(warp(ds->to_json()).to_json_string());
});
svr.Post(api + svr.insert + name, [g, this](HTTP_Param) {
CHECK_JSON_PARAM
std::cout << "Data_Source:" << req->path() << that_json->to_json_string();
HTTP_REQUIRE_VALUE(index, params.try_get_number<int>("index"))
HTTP_REQUIRE_PTR(data, params.get("data"))
HTTP_REQUIRE_VALUE(t, data->try_get_string("type"))
std::cout << params.to_json_string() << std::endl;
std::shared_ptr<Data_Source> ds = create_data_source_from_type(t);
ds->from_json(data);
ds->from_json(data, true);
HTTP_REQUIRE_VALUE(enable, data->try_get_bool("enable"))
(void)enable;
HTTP_REQUIRE_TRUE(map.insert(index, ds), "index")
// std::cout << params.to_json_string() << std::endl;
HTTP_REQUIRE_TRUE(map.insert(index, ds), "index");
g->mode_acs.source_feed_relation_config.set_need_refresh();
Config::save();
res->setBody(warp(to_json()).to_json_string());
});
svr.Post(api + svr.remove + name, [g, this](HTTP_Param) {
CHECK_JSON_PARAM
std::cout << "Data_Source:" << req->path() << that_json->to_json_string();
HTTP_REQUIRE_VALUE(index, params.try_get_number<int>("index"))
HTTP_REQUIRE_VALUE(ds, map.try_get(index))
ds->async_stop();
@@ -558,6 +538,3 @@ void Data_Source_Config::server(Global* g) {
g->mode_acs.source_feed_relation_config.set_need_refresh();
});
}