From ec8689c9f50e92fbc58553ca78440d64d5d0ffcc Mon Sep 17 00:00:00 2001 From: lnk Date: Wed, 25 Jun 2025 10:28:14 +0800 Subject: [PATCH] add front thread --- .vscode/settings.json | 12 +---- LFtid1056/dealMsg.cpp | 17 +++--- LFtid1056/main_thread.cpp | 110 +++++++++++++++++++++++++++++++------- 3 files changed, 101 insertions(+), 38 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 51004e6..bb879da 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -55,15 +55,5 @@ "C_Cpp_Runner.useLeakSanitizer": false, "C_Cpp_Runner.showCompilationTime": false, "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false, - "files.associations": { - "new": "cpp", - "mutex": "cpp", -<<<<<<< HEAD - "ostream": "cpp", - "thread": "cpp" -======= - "ostream": "cpp" ->>>>>>> 22c6b92 (add front demo in this project) - } + "C_Cpp_Runner.msvcSecureNoWarnings": false } \ No newline at end of file diff --git a/LFtid1056/dealMsg.cpp b/LFtid1056/dealMsg.cpp index 55a949a..5a83df4 100644 --- a/LFtid1056/dealMsg.cpp +++ b/LFtid1056/dealMsg.cpp @@ -10,16 +10,17 @@ #include "PQSMsg.h" #include "client2.h" #include "dealMsg.h" +#include +using namespace std; -SafeMessageQueue message_queue; // 全局消息队列 +SafeMessageQueue message_queue; // ȫϢ -void process_received_message(int client_index, const char* data, size_t length) { - // 实际的消息处理逻辑 - // 这里可以添加您的业务处理代码 - printf("Processing message from client %d, size: %zu\n", client_index, length); - - // 示例:解析消息并处理 - // 注意:根据您的协议实现具体的解析逻辑 +void process_received_message(string mac, const char* data, size_t length) { + // ʵʵϢ߼ + // ҵ + std::cout << "Active connections: " << mac << " size:" << length << std::endl; + // ʾϢ + // ע⣺Эʵ־Ľ߼ } diff --git a/LFtid1056/main_thread.cpp b/LFtid1056/main_thread.cpp index d5483e5..01d8faa 100644 --- a/LFtid1056/main_thread.cpp +++ b/LFtid1056/main_thread.cpp @@ -4,13 +4,13 @@ #include #include #include - - #include "PQSMsg.h" #include "client2.h" #include "dealMsg.h" #include "cloudfront/code/interface.h" + +using namespace std; #if 0 /* */ #define THREAD_CONNECTIONS 10 // ߳ @@ -37,6 +37,51 @@ thread_info_t thread_info[THREAD_CONNECTIONS]; // pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER; // ȫֻ extern SafeMessageQueue message_queue; +// ɲװ +std::vector generate_test_devices(int count) { + std::vector devices; + + for (int i = 1; i <= count; ++i) { + // װID + std::string dev_id = "D" + std::to_string(1000 + i).substr(1); // D001, D002, ..., D100 + std::string dev_name = "Device " + std::to_string(i); + + // ɲ + std::vector points = { + { + "P" + dev_id.substr(1) + "01", // ID P00101 + "Voltage " + dev_name, + dev_id, + 0.0, // ѹֵ + 0.0, + 100.0, + 80.0 + }, + { + "P" + dev_id.substr(1) + "02", // ID P00102 + "Current " + dev_name, + dev_id, + 0.0, // ֵ + 0.0, + 20.0, + 15.0 + } + }; + + // װ + devices.push_back({ + dev_id, + dev_name, + (i % 2 == 0) ? "Model-X" : "Model-Y", // ʹͺ + "00-B7-8D-A8-00-D6", // MACַ + 1, // ״̬ (1=) + points + }); + } + + return devices; +} + /* ̹߳ ̳߳*/ void* work_thread(void* arg) { int index = *(int*)arg; // ȡ߳ @@ -82,7 +127,33 @@ void* client_manager_thread(void* arg) { printf("Started client connections\n"); - start_client_connect(); + // + std::vector points1 = { + {"P001", "Main Voltage", "D001", 10.0, 0.0, 100.0, 0.0}, + {"P002", "Backup Voltage", "D001", 5.0, 0.0, 50.0, 0.0} + }; + + std::vector points2 = { + {"P101", "Generator Output", "D002", 20.0, 0.0, 200.0, 0.0} + }; + + // װб + std::vector devices = { + { + "D001", "Primary Device", "Model-X", "00-B7-8D-A8-00-D1", + 1, points1 + }, + { + "D002", "Backup Device", "Model-Y", "00-B7-8D-A8-00-D6", + 1, points2 + } + }; + + // 100װ + std::vector test_devices = generate_test_devices(100); + + // ͻ + start_client_connect(devices); printf("Stopped all client connections\n"); @@ -112,14 +183,16 @@ void* message_processor_thread(void* arg) { // ʵϢ߼ // ע⣺Ҫmsg.client_indexֿͻ // ɺͷڴ - printf("Processing message from client %d, length: %zu\n", - msg.client_index, msg.length); // ʵʵϢ - process_received_message(msg.client_index, msg.data, msg.length); + process_received_message(msg.mac, msg.data, msg.length); free(msg.data); } + else { + // Ϊʱߣ100΢ = 0.1룩 + usleep(100); + } } // ֹ߳ @@ -186,8 +259,7 @@ void restart_thread(int index) { /* ̴߳ */ int is_thread_alive(pthread_t tid) { - //return pthread_tryjoin_np(tid, NULL) == EBUSY; // EBUSYʾ߳ - return pthread_kill(tid, 0) == 0; // + return pthread_tryjoin_np(tid, NULL) == EBUSY; // EBUSYʾ߳ } /* */ @@ -220,16 +292,6 @@ int main() { free(index); } } - else if (i == 2){ - //ӿںmq - char* argv[] = { (char*)index,(char*)"-dcfg_stat_data", (char*)"-s1_1" }; - ThreadArgs* args = new ThreadArgs{3, argv}; - if (pthread_create(&thread_info[i].tid, NULL, cloudfrontthread, args) != 0) { - printf("Failed to create message processor thread %d\n", i); - delete args; // ߳ûɹֶͷ - free(index); - } - } else { // ߳ // ΪգʵӦп߳ @@ -265,10 +327,20 @@ int main() { } // socket״̬ - static int queue_monitor = 0; + static int queue_monitor = 0; + //static int count = 3; if (++queue_monitor >= 10) { // ÿ10뱨һ printf("Message queue size: %zu\n", message_queue.size()); queue_monitor = 0; + + /*std::vector test_devices = generate_test_devices(count); + count++; + for (const auto& device : test_devices) { + ClientManager::instance().add_device(device); + } + for (const auto& device : test_devices) { + ClientManager::instance().remove_device("D001"); + }*/ } }