This commit is contained in:
2026-07-27 15:17:13 +08:00
parent 8370ff9e79
commit c99b16d000
15 changed files with 1169 additions and 428 deletions
@@ -1,7 +1,7 @@
#include "Data_Source_Handler.h"
#include <string_view>
#include "Data_Source.h"
#include "Local_Server/server/Global.h"
#include <string_view>
#include "Local_Server/server/io_coro.h"
using namespace Psc;
std::shared_ptr<Data_Source> ds(Data_Source_Handler* dsh) {
@@ -13,18 +13,16 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
std::cout << "first:" << mem2hex(std::string(packet)) << std::endl;
return nullptr;
}
std::string error_len = source->key + " " + LOG_POS_SIMPLE +
std::string(" ") + "mode_s_error_length";
if (packet.size() < 2) return nullptr;
std::string error_len = source->key + " " + LOG_POS_SIMPLE + std::string(" ") + "mode_s_error_length";
if (packet.size() < 2)
return nullptr;
auto mt = packet[1];
if (mt == SSR::Msg::AC) {
if (packet.size() != SSR::Msg::AC_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(
error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex +
" should:" + std::to_string(SSR::Msg::AC_len));
SSR::mode_s_logger->c_debug(error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::AC_len));
mode_ac_statistic.add_length_error();
return nullptr;
}
@@ -34,13 +32,10 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
if (mt == SSR::Msg::S7) {
if (packet.size() != SSR::Mode_S_Msg::S7_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(
error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex +
" should:" + std::to_string(SSR::Msg::S7_len));
mode_s_statistic.add_length_error(
"[length error] Msg::S7 handle_mode_s_source");
SSR::mode_s_logger->c_debug(error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::S7_len));
mode_s_statistic.add_length_error("[length error] Msg::S7 handle_mode_s_source");
return nullptr;
}
return std::make_shared<SSR::Mode_S_Msg>(source, packet);
@@ -48,13 +43,10 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
if (mt == SSR::Msg::S14) {
if (packet.size() != SSR::Msg::S14_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(
error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex +
" should:" + std::to_string(SSR::Msg::S14_len));
mode_s_statistic.add_length_error(
"[length error] Msg::S14 handle_mode_s_source");
SSR::mode_s_logger->c_debug(error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex + " should:" + std::to_string(SSR::Msg::S14_len));
mode_s_statistic.add_length_error("[length error] Msg::S14 handle_mode_s_source");
return nullptr;
}
return std::make_shared<SSR::Mode_S_Msg>(source, packet);
@@ -63,13 +55,10 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
return nullptr; // 不知道如何解析跳过
if (packet.size() != SSR::Msg::Radarcape_status_len) {
auto packet_hex = mem2hex(std::string(packet));
SSR::mode_s_logger->c_debug(
error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) +
" hex:" + packet_hex +
" should:" + std::to_string(SSR::Msg::Radarcape_status_len));
mode_s_statistic.add_length_error(
"[length error] Msg::Radarcape_status_len handle_mode_s_source");
SSR::mode_s_logger->c_debug(error_len, {},
std::to_string(mt) + " size:" + std::to_string(packet.size()) + " hex:" +
packet_hex + " should:" + std::to_string(SSR::Msg::Radarcape_status_len));
mode_s_statistic.add_length_error("[length error] Msg::Radarcape_status_len handle_mode_s_source");
return nullptr;
}
return std::make_shared<SSR::Msg>(source, packet);
@@ -79,10 +68,9 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packe
auto packet_hex = mem2hex(std::string(packet));
// 找不到协议 size:23 hex:1A34195F0000001500B502FF06F423CE50000090000000
// should:5
SSR::mode_s_logger->c_debug(
error_len, {},
Psc::to_string(source->type) + " HULC size:" +
std::to_string(packet.size()) + " hex:" + packet_hex);
SSR::mode_s_logger->c_debug(error_len, {},
Psc::to_string(source->type) + " HULC size:" + std::to_string(packet.size()) +
" hex:" + packet_hex);
return nullptr;
}
return std::make_shared<SSR::Msg>(source, packet);
@@ -101,8 +89,8 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
}
origin_data_transform_mode_data(mode_data);
if (data_source_debug) {
std::cout << " after origin_data_transform_mode_data " << VAR_STR_2(name, mode_data.size()) <<
key << std::endl;
std::cout << " after origin_data_transform_mode_data " << VAR_STR_2(name, mode_data.size()) << key
<< std::endl;
}
static Value_Growth_Multi_T mt;
auto t = key;
@@ -124,17 +112,16 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
static bool mode_s_console = Global::instance()->console_config.mode_s_console;
static bool record_playback = Global::instance()->console_config.record_playback;
if (record_playback) {
pure_log(get_exe_dir() + "/playback/" + source->key + "_playback.dat",
mem2hex(mode_data, true, " ") + "\n");
pure_log(get_exe_dir() + "/playback/" + source->key + "_playback.dat", mem2hex(mode_data, true, " ") + "\n");
}
if (mode_s_console) {
std::cout << source->key + " read:[mode_s_serial]:" << mem2hex(mode_data)
<< std::endl;
std::cout << source->key + " read:[mode_s_serial]:" << mem2hex(mode_data) << std::endl;
}
auto handle_packet = [this, source, &ret](std::string& packet) {
ret++;
auto msg = create_msg(packet);
if (!msg) return;
if (!msg)
return;
source->push_to_feed(msg);
auto mt = msg->type;
bool mode_s = mt == SSR::Msg::S7 || mt == SSR::Msg::S14;
@@ -144,8 +131,7 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
else if (mt == SSR::Msg::Radarcape_status) {
auto radarcape_msg = SSR::create_Radarcape_STATUS_Message(packet);
std::cout << radarcape_msg.toJson().to_json_string() << std::endl;
SSR::mode_s_logger->debug("Radarcape_status/radarcape", {},
radarcape_msg.toJson().to_json_string());
SSR::mode_s_logger->debug("Radarcape_status/radarcape", {}, radarcape_msg.toJson().to_json_string());
}
else if (mode_s) {
// 拓展点
@@ -160,9 +146,7 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
else {
auto co = Coro::instance();
auto executor = co->process_data->get_executor();
asio::post(executor, [handle_packet, packet = std::move(packet)]() mutable {
handle_packet(packet);
});
asio::post(executor, [handle_packet, packet = std::move(packet)]() mutable { handle_packet(packet); });
}
};
SSR::Binary_Format_handle_buffer(source->buffer, mode_data, f);
@@ -170,8 +154,8 @@ size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data)
}
void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> mode_s_msg) {
auto source = ds(this);
std::string t = mode_s_msg->mlat_timestamp.to_memory() +
mode_s_msg->signal_level + Psc::hex2mem(mode_s_msg->msg_hex);
std::string t =
mode_s_msg->mlat_timestamp.to_memory() + mode_s_msg->signal_level + Psc::hex2mem(mode_s_msg->msg_hex);
auto p = mode_s_msg->packet.substr(2);
if (t != p) {
std::cout << mem2hex(t, true, " ") << std::endl;
@@ -183,12 +167,26 @@ void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> mode_s_
bool time_space_filter = cfg.time_space_filter.load();
bool speed_filter = cfg.speed_filter.load();
bool use_system_time = source->ignore_msg_time.load();
SSR::ADS_B_T::Constraint air_constraint{cfg.max_speed_m_s, cfg.air_pos_timeout, SSR::cpr_cb, time_space_filter, speed_filter, use_system_time};
SSR::ADS_B_T::Constraint surface_constraint{cfg.max_speed_m_s, cfg.surface_pos_timeout, SSR::cpr_cb, time_space_filter, speed_filter, use_system_time};
SSR::parse_mode_s_bin(source.get(), mode_s_msg,
source->base_station.get_pos(), air_constraint,
surface_constraint);
auto base_station_pos = source->base_station.get_pos();
if (!base_station_pos && source->base_station_has_valid_position.load()) {
base_station_pos = SSR::Position_3D{source->lat, source->lon, source->alt};
}
auto range_filter = cfg.aircraft_change_list_adsb_range_filter.load();
auto range_factor = cfg.aircraft_change_list_adsb_range_factor.load();
SSR::ADS_B_T::Constraint air_constraint{cfg.max_speed_m_s, cfg.air_pos_timeout, SSR::cpr_cb, time_space_filter,
speed_filter, use_system_time, base_station_pos, source->alt,
range_filter, range_factor};
SSR::ADS_B_T::Constraint surface_constraint{cfg.max_speed_m_s,
cfg.surface_pos_timeout,
SSR::cpr_cb,
time_space_filter,
speed_filter,
use_system_time,
base_station_pos,
source->alt,
range_filter,
range_factor};
SSR::parse_mode_s_bin(source.get(), mode_s_msg, base_station_pos, air_constraint, surface_constraint);
auto base = source->get_aircraft(mode_s_msg->icao);
if (base) {
auto derived = std::dynamic_pointer_cast<Aircraft>(base);
@@ -199,8 +197,7 @@ void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> mode_s_
mh.push(mode_s_msg);
auto tt = mh.get_all();
if (!tt.empty()) {
pure_log("@/logs/mlat.log",
"receive[" + std::to_string(tt.size()) + "]:\n");
pure_log("@/logs/mlat.log", "receive[" + std::to_string(tt.size()) + "]:\n");
for (Mlat_MSG& t : tt) {
std::ostringstream oss;
oss << std::fixed << std::setprecision(10);
@@ -223,12 +220,9 @@ void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> mode_s_
auto pos = o_pos.value();
source->alt = o_alt.value();
double x, y, z;
SSR::CPR::WGS84_LBH_to_XYZ(pos.lon, pos.lat, source->alt, x, y,
z);
oss << "\tpos[" << pos.lon << "," << pos.lat << ","
<< source->alt << "]" << std::endl;
oss << "\tXYZ[" << x << "," << y << "," << z << "]"
<< std::endl;
SSR::CPR::WGS84_LBH_to_XYZ(pos.lon, pos.lat, source->alt, x, y, z);
oss << "\tpos[" << pos.lon << "," << pos.lat << "," << source->alt << "]" << std::endl;
oss << "\tXYZ[" << x << "," << y << "," << z << "]" << std::endl;
}
}
}
@@ -262,23 +256,22 @@ void Data_Source_Handler::handle_HULC(std::string_view packet) {
auto msg = mem2hex(std::string(packet));
if (len != packet.size() - 4) {
SSR::mode_s_logger->debug("HULC/error_length", {},
"需要: " + std::to_string(len) +
" 当前: " + std::to_string(packet.size()) +
"需要: " + std::to_string(len) + " 当前: " + std::to_string(packet.size()) +
" hex: " + mem2hex(std::string(packet)));
return;
}
if (id == 1) {
// 状态消息
auto status_msg = SSR::create_HULC_Status_Message(packet);
bool gps_ok = status_msg.GPS_device_detected() && status_msg.GPS_valid() &&
status_msg.GPS_has_valid_fix();
if (gps_ok) {}
bool gps_ok = status_msg.GPS_device_detected() && status_msg.GPS_valid() && status_msg.GPS_has_valid_fix();
if (gps_ok) {
}
auto g = Global::instance();
base_station.set_msg(status_msg);
Log_Type type({}, {{"msg", std::string(msg)}});
SSR::mode_s_logger->debug("HULC/status", type,
status_msg.toJson().to_json_string());
if (gps_ok) {}
SSR::mode_s_logger->debug("HULC/status", type, status_msg.toJson().to_json_string());
if (gps_ok) {
}
}
else if (id == 24) {
SSR::mode_s_logger->debug("HULC/reply", {}, msg);
@@ -290,9 +283,9 @@ void Data_Source_Handler::handle_HULC(std::string_view packet) {
void Data_Source_Handler::refresh_data_feed_key_list() {
// std::cout << key << " refresh_data_feed_key_list" << std::endl;
std::vector<std::string> tmp;
for (auto& relation :
Global::instance()->mode_acs.source_feed_relation_config.map.list()) {
if (!relation->enable) continue;
for (auto& relation : Global::instance()->mode_acs.source_feed_relation_config.map.list()) {
if (!relation->enable)
continue;
if (relation->type == "One_to_One_Relation") {
auto t = dynamic_cast<One_to_One_Relation*>(relation.get());
// std::cout << VAR_STR_2(t->source_key, this->key) << "
@@ -302,19 +295,16 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
}
}
else if (relation->type == "First_Source_To_All_Feed_Relation") {
auto t =
dynamic_cast<First_Source_To_All_Feed_Relation*>(relation.get());
auto t = dynamic_cast<First_Source_To_All_Feed_Relation*>(relation.get());
std::shared_ptr<Data_Source> first = nullptr;
for (const auto& ds :
Global::instance()->mode_acs.data_source_config.map.list()) {
for (const auto& ds : Global::instance()->mode_acs.data_source_config.map.list()) {
if (ds->enable) {
first = ds;
break;
}
}
if (first->key == key) {
for (const auto& df :
Global::instance()->mode_acs.data_feed_config.map.list()) {
for (const auto& df : Global::instance()->mode_acs.data_feed_config.map.list()) {
if (df->enable) {
tmp.push_back(df->key);
}
@@ -337,10 +327,9 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
// 1. 删除 tmp 中没有的 cached_data_feed_key_list 元素
auto it = cached_data_feed_key_list.begin();
while (it != cached_data_feed_key_list.end()) {
if (std::find_if(tmp_info.begin(), tmp_info.end(),
[&](const Cached_Source_Info& info) {
return info.key == it->key;
}) == tmp_info.end()) {
if (std::find_if(tmp_info.begin(), tmp_info.end(), [&](const Cached_Source_Info& info) {
return info.key == it->key;
}) == tmp_info.end()) {
// 如果当前元素在 tmp 中找不到,删除它
it = cached_data_feed_key_list.erase(it);
}
@@ -350,11 +339,9 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
}
// 2. 创建 tmp 中有但 cached_data_feed_key_list 没有的元素
for (const auto& tmp_item : tmp_info) {
auto found = std::find_if(cached_data_feed_key_list.begin(),
cached_data_feed_key_list.end(),
[&](const Cached_Source_Info& cached_item) {
return cached_item.key == tmp_item.key;
});
auto found = std::find_if(
cached_data_feed_key_list.begin(), cached_data_feed_key_list.end(),
[&](const Cached_Source_Info& cached_item) { return cached_item.key == tmp_item.key; });
if (found == cached_data_feed_key_list.end()) {
// 如果 tmp_item 不在 cached_data_feed_key_list 中,添加它
cached_data_feed_key_list.push_back(tmp_item);
@@ -364,8 +351,7 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
}
}
}
std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
const std::shared_ptr<Data_Feed>& feed,
std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds, const std::shared_ptr<Data_Feed>& feed,
const std::shared_ptr<SSR::Msg>& msg) {
auto& type = msg->type;
auto fs = dynamic_cast<File_Data_Source*>(msg->source.get());
@@ -382,22 +368,22 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
// 计算出是否输出消息
std::optional<std::string> send_msg = std::nullopt;
if (type == SSR::Msg::HULC_Status) {
if (!use_status) return std::nullopt;
if (!use_status)
return std::nullopt;
auto data = static_cast<SSR::Msg*>(msg.get());
if (output_format == Output_Data_Format::BIN ||
output_format == Output_Data_Format::BIN_ID) {
if (output_format == Output_Data_Format::BIN || output_format == Output_Data_Format::BIN_ID) {
send_msg = SSR::packet_to_escape_format(data->packet);
}
}
else if (type == SSR::Msg::Radarcape_status) {
auto data = static_cast<SSR::Msg*>(msg.get());
if (output_format == Output_Data_Format::BIN ||
output_format == Output_Data_Format::BIN_ID) {
if (output_format == Output_Data_Format::BIN || output_format == Output_Data_Format::BIN_ID) {
send_msg = SSR::packet_to_escape_format(data->packet);
}
}
else if (type == SSR::Msg::AC) {
if (!use_mode_ac) return std::nullopt;
if (!use_mode_ac)
return std::nullopt;
auto data = static_cast<SSR::Mode_AC_Msg*>(msg.get());
std::string& msg_hex = data->msg_hex;
char signal_level = data->signal_level;
@@ -448,10 +434,12 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
sbs_out_put = true;
}
}
if (!sbs_out_put) return std::nullopt;
if (!sbs_out_put)
return std::nullopt;
send_msg = "";
SSR::SBS_MSG msg_base;
if (!aircraft) return std::nullopt;
if (!aircraft)
return std::nullopt;
msg_base.set(aircraft.get());
for (int msg_t = 1; msg_t <= 8; ++msg_t) {
send_msg->append(msg_base.to_msg(msg_t) + "\n");
@@ -463,11 +451,11 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
send_msg->append(msg_base.to_STA() + "\n");
return send_msg;
}
bool DF_11_17_18 =
df == SSR::Downlink_Format::All_Call_Reply_11 ||
bool DF_11_17_18 = df == SSR::Downlink_Format::All_Call_Reply_11 ||
df == SSR::Downlink_Format::Extended_Squitter_17 ||
df == SSR::Downlink_Format::Extended_Squitter_Non_Transponder_18;
if (mode_s_output_type == Mode_S_Output_Type::DF_11_17_18 && !DF_11_17_18) return std::nullopt;
if (mode_s_output_type == Mode_S_Output_Type::DF_11_17_18 && !DF_11_17_18)
return std::nullopt;
if (mode_s_output_type == Mode_S_Output_Type::NO_POS_Mode_S) {
if (aircraft && aircraft->pos().has_value()) {
return std::nullopt;
@@ -493,8 +481,7 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
send_msg = create_MLAT_AVR_format(msg_hex, &mlat_timestamp);
}
else {
std::cerr << "Unknown output format " << VAR_STR_1(output_format)
<< std::endl;
std::cerr << "Unknown output format " << VAR_STR_1(output_format) << std::endl;
return std::nullopt;
}
}
@@ -516,9 +503,9 @@ void Data_Source_Handler::push_to_feed(const std::shared_ptr<SSR::Msg>& msg) {
break;
}
std::shared_ptr<Data_Feed>& feed = opt_feed.value();
if (!feed->enable) continue;
if (msg->type == SSR::Mode_Msg::T::S7 ||
msg->type == SSR::Mode_Msg::T::S14) {
if (!feed->enable)
continue;
if (msg->type == SSR::Mode_Msg::T::S7 || msg->type == SSR::Mode_Msg::T::S14) {
// static Frequency_Limit fl;
// if (!fl.test()) {
// std::ostringstream oss;