std::string_view 的使用
This commit is contained in:
@@ -1,29 +1,28 @@
|
||||
#include "Data_Source_Handler.h"
|
||||
#include "Data_Source.h"
|
||||
#include "Local_Server/server/Global.h"
|
||||
#include <string_view>
|
||||
using namespace Psc;
|
||||
|
||||
std::shared_ptr<Data_Source> ds(Data_Source_Handler* dsh) {
|
||||
std::shared_ptr<Data_Source> ds(Data_Source_Handler *dsh) {
|
||||
return dynamic_cast<Data_Source*>(dsh)->that();
|
||||
}
|
||||
|
||||
std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(const std::string& packet) {
|
||||
std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(std::string_view packet) {
|
||||
auto source = ds(this);
|
||||
if (packet[0] != 0x1a) {
|
||||
std::cout << "first:" << mem2hex(packet) << std::endl;
|
||||
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;
|
||||
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:" + mem2hex(packet) +
|
||||
" hex:" + packet_hex +
|
||||
" should:" + std::to_string(SSR::Msg::AC_len));
|
||||
mode_ac_statistic.add_length_error();
|
||||
return nullptr;
|
||||
@@ -33,10 +32,11 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(const std::string& pac
|
||||
}
|
||||
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:" + mem2hex(packet) +
|
||||
" 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");
|
||||
@@ -46,10 +46,11 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(const std::string& pac
|
||||
}
|
||||
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:" + mem2hex(packet) +
|
||||
" 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");
|
||||
@@ -60,10 +61,11 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(const std::string& pac
|
||||
if (mt == SSR::Msg::Radarcape_status) {
|
||||
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:" + mem2hex(packet) +
|
||||
" 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");
|
||||
@@ -73,24 +75,24 @@ std::shared_ptr<SSR::Msg> Data_Source_Handler::create_msg(const std::string& pac
|
||||
}
|
||||
if (mt == SSR::Msg::HULC_Status) {
|
||||
if (packet.size() != SSR::Msg::HULC_len) {
|
||||
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:" + mem2hex(packet));
|
||||
std::to_string(packet.size()) + " hex:" + packet_hex);
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_shared<SSR::Msg>(source, packet);
|
||||
}
|
||||
// std::cout << "unknown_type:" << memory2hex(packet);
|
||||
rotating_log("unknown_type", mem2hex(packet));
|
||||
rotating_log("unknown_type", mem2hex(std::string(packet)));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t Data_Source_Handler::process_mode_acs_data(const std::string& origin_data) {
|
||||
size_t Data_Source_Handler::process_mode_acs_data(std::string_view origin_data) {
|
||||
// 先读取原始数据 注意多复制了一遍
|
||||
auto mode_data = origin_data;
|
||||
std::string mode_data(origin_data);
|
||||
origin_data_transform_mode_data(mode_data);
|
||||
size_t ret = 0;
|
||||
if (mode_data.empty()) {
|
||||
@@ -115,8 +117,7 @@ size_t Data_Source_Handler::process_mode_acs_data(const std::string& origin_data
|
||||
source->buffer, mode_data, [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;
|
||||
@@ -136,7 +137,6 @@ size_t Data_Source_Handler::process_mode_acs_data(const std::string& origin_data
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
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() +
|
||||
@@ -219,16 +219,15 @@ void Data_Source_Handler::handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> mode_s_
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Data_Source_Handler::handle_HULC(const std::string& packet) {
|
||||
void Data_Source_Handler::handle_HULC(std::string_view packet) {
|
||||
std::uint8_t len = SSR::get_len(packet);
|
||||
std::uint8_t id = SSR::get_id(packet);
|
||||
auto msg = mem2hex(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()) +
|
||||
" hex: " + mem2hex(packet));
|
||||
" hex: " + mem2hex(std::string(packet)));
|
||||
return;
|
||||
}
|
||||
if (id == 1) {
|
||||
@@ -239,7 +238,7 @@ void Data_Source_Handler::handle_HULC(const std::string& packet) {
|
||||
if (gps_ok) {}
|
||||
auto g = Global::instance();
|
||||
base_station.set_msg(status_msg);
|
||||
Log_Type type({}, {{"msg", 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) {}
|
||||
@@ -251,14 +250,12 @@ void Data_Source_Handler::handle_HULC(const std::string& packet) {
|
||||
SSR::mode_s_logger->debug("HULC/unknown_id", {}, msg);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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) << "
|
||||
@@ -330,8 +327,7 @@ void Data_Source_Handler::refresh_data_feed_key_list() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
|
||||
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;
|
||||
@@ -349,8 +345,7 @@ 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) {
|
||||
@@ -365,8 +360,7 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
|
||||
}
|
||||
}
|
||||
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;
|
||||
@@ -417,12 +411,10 @@ 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");
|
||||
@@ -438,8 +430,7 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
|
||||
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;
|
||||
@@ -472,7 +463,6 @@ std::optional<std::string> convert_to_send_format(Data_Source_Handler* ds,
|
||||
}
|
||||
return send_msg;
|
||||
}
|
||||
|
||||
void Data_Source_Handler::push_to_feed(const std::shared_ptr<SSR::Msg>& msg) {
|
||||
if (need_refresh_data_feed_key_list) {
|
||||
refresh_data_feed_key_list();
|
||||
@@ -489,8 +479,7 @@ 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 (!feed->enable) continue;
|
||||
if (msg->type == SSR::Mode_Msg::T::S7 ||
|
||||
msg->type == SSR::Mode_Msg::T::S14) {
|
||||
// static Frequency_Limit fl;
|
||||
@@ -521,7 +510,6 @@ void Data_Source_Handler::push_to_feed(const std::shared_ptr<SSR::Msg>& msg) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Psc::JSON Data_Source_Handler::get_all_connect_feed_status() {
|
||||
Psc::JSON ret = Psc::JSON::object();
|
||||
std::vector<Cached_Source_Info> list;
|
||||
@@ -543,7 +531,6 @@ Psc::JSON Data_Source_Handler::get_all_connect_feed_status() {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
Psc::JSON Data_Source_Handler::get_all_connect_feed() {
|
||||
Psc::JSON ret = Psc::JSON::array();
|
||||
std::vector<Cached_Source_Info> list;
|
||||
|
||||
Reference in New Issue
Block a user