This commit is contained in:
2026-06-24 09:54:17 +08:00
parent 05dec135ed
commit 351fc5c453
7 changed files with 252 additions and 483 deletions
+16 -56
View File
@@ -1,20 +1,10 @@
#include "Data_Source.h"
#include <algorithm>
#include <asio/post.hpp>
#include <asio/thread_pool.hpp>
#include <codecvt>
#include <thread>
#include "../server/Global.h"
#include "Database.h"
asio::thread_pool& data_source_process_pool() {
const auto hardware_threads = std::thread::hardware_concurrency();
const auto worker_count = std::max(2u, hardware_threads > 2 ? hardware_threads - 2 : hardware_threads);
static asio::thread_pool pool(worker_count);
return pool;
}
Psc::serial::Serial* create_serial(const std::string& serial_name, Baud_Rate_Type baud_rate) {
auto serial = new Psc::serial::Serial;
serial->set_serial_name(serial_name);
@@ -53,6 +43,16 @@ std::shared_ptr<Data_Source> create_from_json(const JSON* that_json) {
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) {
if (item.get() == this) {
return true;
}
}
return false;
}
Psc::JSON Data_Source::get_state() {
Psc::JSON ret = Psc::JSON::object();
ret.append_list(get_custom_state_json().children);
@@ -78,13 +78,6 @@ std::string Data_Source::thread_key() const {
return key + "_DS_HT";
}
void Data_Source::test_and_attach_thread() {
if (enable && open()) {
data_source_loop_requested_.store(true, std::memory_order_release);
}
}
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) {
@@ -109,34 +102,6 @@ void File_Data_Source::before_handle_msg(std::shared_ptr<SSR::Mode_Msg>& msg) {
void Data_Source::test_and_stop_thread() {
data_source_loop_requested_.store(false, std::memory_order_release);
data_source_loop_generation_.fetch_add(1, std::memory_order_acq_rel);
}
bool Data_Source::data_source_loop_should_run() const {
return data_source_loop_requested_.load(std::memory_order_acquire);
}
std::uint64_t Data_Source::data_source_loop_generation() const {
return data_source_loop_generation_.load(std::memory_order_acquire);
}
bool Data_Source::try_mark_data_source_loop_running() {
bool expected = false;
return data_source_loop_running_.compare_exchange_strong(
expected,
true,
std::memory_order_acq_rel,
std::memory_order_acquire);
}
void Data_Source::mark_data_source_loop_stopped() {
data_source_loop_running_.store(false, std::memory_order_release);
}
std::vector<std::string> readLines(const std::string& path) {
std::vector<std::string> lines;
@@ -625,13 +590,10 @@ void Data_Source_Config::server(Global* g) {
HTTP_REQUIRE_VALUE(key, params.try_get_string("key"))
HTTP_REQUIRE_VALUE(ds, map.get(key))
// 直接关闭
ds->test_and_stop_thread();
ds->enable = false;
ds->close();
ds->from_json(&params);
if (ds->enable) {
ds->open();
ds->test_and_attach_thread();
}
wake_data_feed_thread();
g->save();
g->mode_acs.source_feed_relation_config.set_need_refresh();
});
@@ -645,13 +607,10 @@ void Data_Source_Config::server(Global* g) {
std::shared_ptr<Data_Source> ds = create_data_source_from_type(t);
ds->from_json(data);
HTTP_REQUIRE_VALUE(enable, data->try_get_bool("enable"))
if (enable)
{
ds->open();
ds->test_and_attach_thread();
}
(void)enable;
HTTP_REQUIRE_TRUE(map.insert(index, ds), "index")
wake_data_feed_thread();
//std::cout << params.to_json_string() << std::endl;
g->mode_acs.source_feed_relation_config.set_need_refresh();
Config::save();
@@ -662,10 +621,11 @@ void Data_Source_Config::server(Global* g) {
CHECK_JSON_PARAM
HTTP_REQUIRE_VALUE(index, params.try_get_number<int>("index"))
HTTP_REQUIRE_VALUE(ds, map.try_get(index))
ds->test_and_stop_thread();
ds->enable = false;
ds->close();
HTTP_REQUIRE_TRUE(map.remove(index), "index")
wake_data_feed_thread();
g->save();
res->setBody(warp(to_json()).to_json_string());
g->mode_acs.source_feed_relation_config.set_need_refresh();