提取出 Data_Source_Handler

This commit is contained in:
2026-06-23 14:01:23 +08:00
parent 8ca05115ec
commit afd4bf8e69
8 changed files with 380 additions and 389 deletions
+38 -70
View File
@@ -1,18 +1,8 @@
#pragma once
#include <utility>
#include "Core/Base/SM_RingBuffer.h"
#include "Core/Serial/Serial.h"
#include "Core/Statistics/Frequency_Limit.h"
#include "Core/Statistics/Statistics.h"
#include "Core/transmit_protocol/core/statistics.h"
#include "Database.h"
#include "Local_Server/global_include.h"
#include "global.h"
#include "PlayBack.h"
#include "Data_Source_Handler.h"
namespace Psc {
class SM_RingBuffer;
@@ -72,75 +62,47 @@ protected:
std::vector<bool> test_v;
};
class Data_Source : public DataBase, public std::enable_shared_from_this<Data_Source> {
class Data_Source : public std::enable_shared_from_this<Data_Source>, public Data_Source_Handler{
public:
~Data_Source() override = default;
virtual std::string read() { return ""; }
std::shared_ptr<Data_Source> that();
virtual ucoro::awaitable<std::string> read_coro() = 0;
virtual Psc::JSON get_custom_state_json() = 0;
Psc::JSON get_state();
std::string last_char; // 用于处理奇数字节的情况
// std::shared_ptr<Input_Format> parse_format;
Psc::Speed_Statistics read_speed;
Psc::Value_Statistics value_statistics;
Data_Source() {
// parse_format = std::make_shared<Input_Format>();
// 写入到配置文件是懒加载 其他保存时他跟着保存
base_station.handle_when_updated = [this](SSR::HULC_Status_Message msg) {
if (this->update_form_gps) {
lat = msg.get_latitude();
lon = msg.get_longitude();
alt = msg.Alt;
}
};
}
Data_Source();
std::shared_ptr<SSR::Mode_Msg> last_prase_msg = nullptr;
virtual ucoro::awaitable<void> handle_in_loop_coro(){co_return;}
virtual void handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg);
virtual void pre_handle(const std::string &data);
virtual std::shared_ptr<SSR::Msg> create_msg(const std::shared_ptr<SSR::Data_Source_Interface>& source, const std::string& packet);
virtual void update_msg_day_time(std::shared_ptr<SSR::Msg> msg);
virtual void handle_mode_acs_source();
virtual ucoro::awaitable<void> handle_in_loop_coro();
virtual ucoro::awaitable<std::string> recv_coro();
virtual ucoro::awaitable<void> source_step_coro();
void process_mode_acs_data(const std::string& mode_data);
void refresh_data_feed_key_list();
void push_to_feed(const std::shared_ptr<SSR::Msg> &msg);
std::atomic<bool> enable{};
std::string type;
std::atomic<bool> base_station_show{};
std::atomic<bool> aircraft_show{};
std::atomic<bool> show_icao = true;
std::atomic<bool> show_call_sign = false;
std::atomic<bool> show_fly_status = false;
std::atomic<bool> keep_mode = true;
double lat{};
double lon{};
double alt{};
std::atomic<bool> update_form_gps{};
std::string color = "#1677ff";
int aircraft_pixel_size{};
Mode_AC_Statistic_Data mode_ac_statistic;
Mode_S_Statistic_Data mode_s_statistic;
Frequency_Limit statistic_fl;
virtual void handle_in_loop() {};
Frequency_Limit statistic_fl;
std::string thread_key() const;
void test_and_attach_thread();
void test_and_stop_thread() const;
@@ -230,9 +192,6 @@ protected:
struct TCP_Client_Data_Source : Data_Source {
void handle_in_loop() override {
ucoro::sync_await(handle_in_loop_coro());
}
ucoro::awaitable<void> handle_in_loop_coro() override {
co_await cli.tick_coro();
co_return;
@@ -273,10 +232,8 @@ struct TCP_Client_Data_Source : Data_Source {
Get_J(port);
}
std::string read() override {
return ucoro::sync_await(recv_coro());
}
ucoro::awaitable<std::string> recv_coro() override {
ucoro::awaitable<std::string> read_coro() override {
co_return co_await cli.read_coro();
}
};
@@ -288,7 +245,7 @@ struct Serial_Data_Source : Data_Source {
}
Serial_Data_Source() { this->type = "Serial_Data_Source"; }
bool _open() override {
serial = new Psc::serial::Serial;
serial = std::make_unique<Psc::serial::Serial_Coro>();
serial->set_serial_name(port_name);
serial->set_baud_rate(baud_rate);
serial->set_parity(Psc::serial::Parity::NoParity);
@@ -304,8 +261,20 @@ struct Serial_Data_Source : Data_Source {
}
return ok;
}
void _close() override { serial->close(); }
std::string read() override { return serial->read(); }
void _close() override { if (serial) serial->close(); }
ucoro::awaitable<void> handle_in_loop_coro() override {
if (serial) {
co_await serial->tick_coro();
}
co_return;
}
ucoro::awaitable<std::string> read_coro() override {
if (!serial) {
co_return "";
}
co_return co_await serial->read_coro();
}
Psc::JSON to_json() override {
Psc::JSON ret = Data_Source::to_json();
Ret_J(port_name);
@@ -316,11 +285,10 @@ struct Serial_Data_Source : Data_Source {
Data_Source::from_json(that_json);
Get_J(port_name) Get_J(baud_rate);
}
Psc::serial::Serial *serial{};
std::unique_ptr<Psc::serial::Serial_Coro> serial{};
~Serial_Data_Source() override {
if (serial) {
serial->close();
delete serial;
}
}
};
@@ -353,9 +321,7 @@ struct File_Data_Source : public Data_Source {
}
File_Data_Source() { this->type = "File_Data_Source"; }
~File_Data_Source() override = default;
void before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) override;
struct Cache_Msg {
explicit Cache_Msg(std::shared_ptr<SSR::Mode_S_Msg> msg) : msg(std::move(msg)) {}
@@ -389,9 +355,10 @@ struct File_Data_Source : public Data_Source {
Ret_J(play_mode);
return ret;
}
std::string read() override;
void handle_mode_acs_source() override;
ucoro::awaitable<void> source_step_coro() override;
ucoro::awaitable<std::string> read_coro() override;
void handle_mode_s(std::shared_ptr<SSR::Mode_S_Msg> msg) override;
protected:
std::optional<std::string> get_raw_line(int &ret_index);
@@ -462,7 +429,8 @@ struct Dll_Data_Source : Data_Source {
state = "加载成功";
return true;
}
std::string read() override;
ucoro::awaitable<std::string> read_coro();
};
@@ -485,8 +453,8 @@ struct Shared_Memory_Data_Source : Data_Source {
}
bool _open() override;
void _close() override;
std::string read() override;
ucoro::awaitable<std::string> read_coro() override;
protected:
std::unique_ptr<Psc::SM_RingBuffer> sm;
std::string shared_memory_name;