常规更新

This commit is contained in:
2026-07-14 18:10:23 +08:00
parent 4fd18631d3
commit 9c29878bb9
5 changed files with 1212 additions and 1248 deletions
+6 -15
View File
@@ -1,19 +1,14 @@
#pragma once
#include <string_view>
#include "../External_Database/export.h"
#include <map>
#include <memory>
#include <optional>
#include <string>
#include "SSR/Aircraft_Info.h"
class Aircraft : public SSR::Aircraft_Info {
public:
explicit Aircraft(std::string_view icao);
// 静态信息按 ICAO24 加载一次;航线信息仅在 callsign 变化时重新加载。
// map 的键是外部数据源名称。
std::map<std::string, std::shared_ptr<External_Database_Row>>
@@ -22,7 +17,6 @@ public:
callsign_external_database_info;
bool external_database_info_loaded{};
std::optional<std::string> last_external_database_callsign;
[[nodiscard]] std::shared_ptr<External_Database_Row>
external_database_row(std::string_view resource_name) const {
auto key = std::string(resource_name);
@@ -34,27 +28,26 @@ public:
const auto iter = external_database_info.find(key);
return iter == external_database_info.end() ? nullptr : iter->second;
}
void refresh_external_database_info();
Psc::JSON toJson() {
refresh_external_database_info();
Psc::JSON ret = Psc::JSON::object();
Ret_J(icao) ret.append({"每秒位置数", get_pest()});
Ret_J(icao)
ret.append({"每秒位置数", get_pest()});
ret.append({"飞机位置轨迹点数", air_pos_track_list.size()});
ret.append({"地面位置轨迹点数", surface_pos_track_list.size()});
ret.append({"信号强度", signal_level});
auto opt_pos = air_pos_track_list.last();
ret.append({"当前位置消息", opt_pos.has_value() ? opt_pos->to_json() : ""});
auto cpr_state_opt = BDS05_airborne_position.value;
ret.append(
{"cpr解码器状态", cpr_state_opt.has_value()
{
"cpr解码器状态", cpr_state_opt.has_value()
? cpr_state_opt.value().current_state_json()
: ""});
: ""
});
ret.append({"时间", Psc::utc_2_local_time(timestamp)});
ret.append({"多点定位数据源", mlat});
auto external_info = Psc::JSON::object();
for (const auto& [resource_key, row] : external_database_info) {
external_info.append({resource_key, row->to_json()});
@@ -63,11 +56,9 @@ public:
external_info.append({resource_key, row->to_json()});
}
ret.append({"external_database_info", external_info});
push_all_bds_info(ret);
return ret;
}
#ifdef Cache_Some_Mode_S
std::list<std::shared_ptr<Mode_S_Msg>> cache;
size_t total_num = 0;
+33 -41
View File
@@ -3,7 +3,6 @@
#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) {
auto serial = new Psc::serial::Serial;
@@ -19,13 +18,13 @@ Psc::serial::Serial *create_serial(std::string_view serial_name,
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<Data_Source> create_from_json(const JSON* that_json) {
std::string type = that_json->get_string("type");
std::shared_ptr<Data_Source> ret = create_data_source_from_type(type);
@@ -36,11 +35,9 @@ std::shared_ptr<Data_Source> create_from_json(const JSON *that_json) {
// }
return ret;
}
std::shared_ptr<Data_Source> 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) {
@@ -50,15 +47,14 @@ bool Data_Source::registered() const {
}
return false;
}
Psc::JSON Data_Source::get_state() {
Psc::JSON ret = Psc::JSON::object();
ret.append({"state", Psc::to_string(state)});
ret.append_list(get_custom_state_json().children);
ret.append({"ds: read_speedbyte", read_speed});
ret.append({"ds: value_statisticsbyte", value_statistics});
return ret;
}
Data_Source::Data_Source() {
// parse_format = std::make_shared<Input_Format>();
// 写入到配置文件是懒加载 其他保存时他跟着保存
@@ -70,12 +66,10 @@ 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<SSR::Mode_Msg>& msg) {
auto cur = std::dynamic_pointer_cast<SSR::Mode_Msg>(msg);
if (cur != nullptr) {
@@ -90,7 +84,6 @@ 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);
@@ -127,7 +120,8 @@ std::vector<std::string> readLines(std::string_view path) {
// if (ch == '\r' && stream.peek() == '\n') {
// stream.get(); // 吃掉 \n
// }
} else {
}
else {
line += ch;
}
if (line.size() == 1000) {
@@ -146,7 +140,6 @@ std::vector<std::string> readLines(std::string_view path) {
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) {
@@ -154,7 +147,6 @@ std::string extractID(std::string_view logLine) {
}
return std::string(logLine);
}
time_t convert_to_timestamp(std::string_view str) {
// 创建一个结构体 tm 来存储解析后的时间
std::tm timeStruct = {};
@@ -172,7 +164,6 @@ time_t convert_to_timestamp(std::string_view str) {
}
return timestamp;
}
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));
@@ -184,7 +175,6 @@ std::tuple<std::string, time_t> 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 +
@@ -192,7 +182,6 @@ std::string bin_format(std::string_view hex, time_t t) {
SSR::MLAT_timestamp a(sec, 0);
return create_Binary_Format_memory(hex, 0, &a);
}
std::optional<std::string> File_Data_Source::get_raw_line(int& ret_index) {
if (index == 0 && part_infos.empty()) {
ret_index = -1;
@@ -202,7 +191,8 @@ std::optional<std::string> 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()
@@ -219,7 +209,6 @@ std::optional<std::string> 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) {
@@ -227,10 +216,12 @@ 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);
}
@@ -245,9 +236,11 @@ std::string get_true_from_raw_line(Data_Source *ds, File_Data_Type data_type,
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;
@@ -262,22 +255,26 @@ 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) {
}
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) {
@@ -300,28 +297,29 @@ 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));
// std::cout << ret.size() << std::endl;
} else {
}
else {
std::cout << "未知的回放类型 " << static_cast<int>(data_type) << std::endl;
Psc::fail_fast();
}
return ret;
}
asio::awaitable<void> File_Data_Source::_close() {
std::lock_guard g(mtx);
index = 0;
part_infos.clear();
co_return;
}
asio::awaitable<void> File_Data_Source::_open() {
std::lock_guard g(mtx);
auto path = get_true_file_path();
@@ -332,13 +330,13 @@ asio::awaitable<void> 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<std::string> File_Data_Source::readBinaryFileAsString(std::string_view filepath,
size_t part_size) {
// 打开文件(以二进制模式)
@@ -372,7 +370,6 @@ std::vector<std::string> File_Data_Source::readBinaryFileAsString(std::string_vi
file.close();
return parts;
}
void File_Data_Source::origin_data_transform_mode_data(std::string& mode_data) {
std::lock_guard g(mtx);
assert(mode_data.size() == 0);
@@ -383,7 +380,8 @@ 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<std::string> log_info = get_raw_line(ret_index);
@@ -402,7 +400,6 @@ 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;
cache_list.push_back(std::make_shared<Cache_Msg>(std::move(msg)));
@@ -431,7 +428,6 @@ 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;
@@ -454,18 +450,15 @@ void Dll_Data_Source::origin_data_transform_mode_data(std::string &mode_data) {
// std::cout << "Dll_Data_Source::read cost: " << cost << " us\n";
mode_data = ret;
}
asio::awaitable<void> Shared_Memory_Data_Source::_open() {
sm = std::make_unique<Psc::SM_RingBuffer>();
sm->init(shared_memory_name, shared_memory_size);
co_return;
}
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) {
auto size = sm->shm.size();
@@ -485,7 +478,6 @@ 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;
+47 -64
View File
@@ -15,18 +15,14 @@
#include <utility>
#include <vector>
#include <string_view>
class Global;
class Invalid_Http_Param final : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
[[noreturn]] inline void throw_invalid_http_param(const char* detail) {
throw Invalid_Http_Param(detail);
}
#define CHECK_JSON_PARAM \
auto o_params = Psc::try_parse_json(req->body().data()); \
if (!o_params.has_value()) { \
@@ -34,47 +30,41 @@ public:
} \
auto &params = o_params.value(); \
auto that_json = &params;
#define HTTP_REQUIRE_VALUE(NAME, EXPRESSION) \
auto o_http_##NAME = (EXPRESSION); \
if (!o_http_##NAME.has_value()) { \
throw_invalid_http_param(#NAME); \
} \
auto NAME = std::move(o_http_##NAME).value();
#define HTTP_REQUIRE_PTR(NAME, EXPRESSION) \
auto NAME = (EXPRESSION); \
if ((NAME) == nullptr) { \
throw_invalid_http_param(#NAME); \
}
#define HTTP_REQUIRE_TRUE(EXPRESSION, DETAIL) \
if (!(EXPRESSION)) { \
throw_invalid_http_param(DETAIL); \
}
template <typename Value_Type> class Ordered_List {
template <typename Value_Type>
class Ordered_List {
public:
void swap(size_t i, size_t j) {
std::lock_guard g(mtx);
if (i >= _list.size() || j >= _list.size())
return;
std::swap(_list[i], _list[j]);
}
void push_back(Value_Type value) {
std::lock_guard g(mtx);
_list.push_back(value); // 添加默认值
}
void insert(int i, Value_Type value) {
std::lock_guard g(mtx);
_list.insert(_list.begin() + i, value);
}
Value_Type get(size_t i) { return _list[i]; }
Value_Type get(size_t i) {
return _list[i];
}
void remove(size_t i) {
std::lock_guard g(mtx);
if (i >= _list.size())
@@ -85,15 +75,15 @@ public:
std::lock_guard g(mtx);
return this->_list;
}
void clear() { list().clear(); }
void clear() {
list().clear();
}
protected:
std::mutex mtx;
std::vector<Value_Type> _list;
};
template <typename Key_Type, typename Value_Type> class Ordered_Map {
template <typename Key_Type, typename Value_Type>
class Ordered_Map {
public:
std::optional<Value_Type> get(Key_Type key) {
std::lock_guard g(mtx);
@@ -107,24 +97,19 @@ public:
std::lock_guard g(mtx);
if (i >= _list.size() || j >= _list.size())
return false;
std::swap(_list[i], _list[j]);
// 更新 map 中的 value,假设你可以从 Value 中获取 key
Key_Type key_i = _list[i]->key;
Key_Type key_j = _list[j]->key;
_map[key_i] = _list[i];
_map[key_j] = _list[j];
return true;
}
void push_back(Value_Type value) {
std::lock_guard g(mtx);
_list.push_back(value); // 添加默认值
_map[value->key] = value; // 插入到 map
}
bool insert(int i, Value_Type value) {
std::lock_guard g(mtx);
if (i < 0 || static_cast<size_t>(i) > _list.size())
@@ -133,16 +118,15 @@ public:
_map[value->key] = value;
return true;
}
Value_Type get(size_t i) { return _list[i]; }
Value_Type get(size_t i) {
return _list[i];
}
std::optional<Value_Type> try_get(size_t i) {
std::lock_guard g(mtx);
if (i >= _list.size())
return std::nullopt;
return _list[i];
}
bool remove(size_t i) {
std::lock_guard g(mtx);
if (i >= _list.size())
@@ -164,13 +148,11 @@ public:
list().clear();
_map.clear();
}
protected:
std::mutex mtx;
std::vector<Value_Type> _list;
std::map<Key_Type, Value_Type> _map;
};
class Mode_AC_Statistic_Data {
public:
Psc::JSON to_json() {
@@ -192,7 +174,6 @@ public:
add_inter();
length_error_num++;
}
protected:
std::atomic<size_t> length_error_num{};
void add_inter() {
@@ -210,7 +191,6 @@ protected:
time_t utc{};
// Psc::Speed_Statistics speed;
};
class Statistic_Data {
public:
time_t utc{};
@@ -224,7 +204,6 @@ public:
}
Statistic_Data() = default;
};
class DF_Statistic_Data {
public:
time_t utc{};
@@ -237,7 +216,6 @@ public:
Ret_J(total_num);
Ret_J(crc_error_num);
// Ret_J(speed);
std::string t = "null";
if (total_num != 0) {
t = std::to_string(static_cast<double>(crc_error_num) /
@@ -250,10 +228,15 @@ public:
}
return ret;
}
void add() { add_inter(); }
void add_crc_error() { crc_error_num++; }
void add_sub_part(std::string_view key) { add_sub_part_inter(key); }
void add() {
add_inter();
}
void add_crc_error() {
crc_error_num++;
}
void add_sub_part(std::string_view key) {
add_sub_part_inter(key);
}
// Psc::Speed_Statistics speed;
protected:
void add_inter() {
@@ -266,7 +249,6 @@ protected:
pre_second_num++;
// speed.update(1);
}
void add_sub_part_inter(std::string_view key) {
auto t = get_create_statistic_data(key);
t->total_num++;
@@ -288,14 +270,11 @@ protected:
t = &statistic_map[key_string];
return t;
}
std::map<std::string, Statistic_Data> statistic_map;
};
class Mode_S_Statistic_Data {
public:
// Psc::Speed_Statistics speed;
// Psc::Speed_Statistics total_speed() const {
// Psc::Speed_Statistics total_speed{};
// for (auto& cur : DF_Statistic_Data_map) {
@@ -307,12 +286,9 @@ public:
size_t total_num = get_total_num();
size_t crc_error_num = get_crc_error_num();
Psc::JSON ret = Psc::JSON::object();
ret.append({"每秒数量", get_total_pre_second_num()});
Ret_J(length_error_num);
Ret_J(total_num);
std::string t = "null";
if (total_num != 0) {
t = std::to_string(static_cast<double>(crc_error_num) /
@@ -321,28 +297,38 @@ public:
}
Ret_J(crc_error_num);
ret.append({"crc_error_rate", t});
Psc::JSON df_sub_type = Psc::JSON::object();
for (auto& cur : DF_Statistic_Data_map) {
df_sub_type.append({Psc::to_string(cur.first) + " [" +
df_sub_type.append({
Psc::to_string(cur.first) + " [" +
std::to_string(static_cast<int>(cur.first)) + "]",
cur.second.to_json()});
cur.second.to_json()
});
}
Psc::JSON cpr_error_o = Psc::JSON::object();
for (auto& cur : cpr_error) {
cpr_error_o.append({Psc::to_string(cur.first) + " [" +
std::to_string(static_cast<int>(cur.first)) + "]",
Psc::to_string(cur.second)});
auto num = cur.second;
std::string rate_str = std::format("{:.2f}%", static_cast<double>(num) / base * 100.0);
auto& type = cur.first;
auto key = Psc::to_string(type);
auto val = Psc::to_string(static_cast<size_t>(cur.second)) + " " + rate_str;
cpr_error_o.append({key, val});
}
ret.append({"DF子类型", df_sub_type});
ret.append({"CPR统计信息", cpr_error_o});
return ret;
}
void add_length_error(std::string_view key) { length_error_num++; }
void add_cpr_error(SSR::CPR_Error_Type cet) { ++cpr_error[cet]; }
void add_length_error(std::string_view key) {
length_error_num++;
}
void add_cpr_error(SSR::CPR_Error_Type cet) {
if (cet == SSR::CPR_Error_Type::Normal) {
base++;
}
else {
++cpr_error[cet];
}
}
DF_Statistic_Data*
get_create_df_statistic_data(const SSR::Downlink_Format& key) {
DF_Statistic_Data* t;
@@ -354,7 +340,6 @@ public:
t = &DF_Statistic_Data_map[key];
return t;
}
size_t get_total_pre_second_num() {
size_t ret = 0;
for (auto& cur : DF_Statistic_Data_map) {
@@ -362,7 +347,6 @@ public:
}
return ret;
}
size_t get_total_num() {
size_t ret = 0;
for (auto& cur : DF_Statistic_Data_map) {
@@ -377,13 +361,12 @@ public:
}
return ret;
}
protected:
std::atomic<size_t> length_error_num{};
std::map<SSR::Downlink_Format, DF_Statistic_Data> DF_Statistic_Data_map;
std::map<SSR::CPR_Error_Type, size_t> cpr_error;
std::map<SSR::CPR_Error_Type, double> cpr_error;
double base = 0;
};
template <typename T> class Immutable : public T {};
template <typename T>
class Immutable : public T {};
#endif
-2
View File
@@ -102,9 +102,7 @@ Global::Global() {
SSR::P_S msg) {
auto src = std::dynamic_pointer_cast<Data_Source>(msg->source);
Mode_S_Statistic_Data* ss = &src->mode_s_statistic;
if (t != SSR::CPR_Error_Type::Normal) {
ss->add_cpr_error(t);
}
SSR::mode_s_logger->debug("CPR/" + to_string(t), {}, log);
};
// 处理错误