From 7748793546d8af396f0ca1fe07e036721aaf335c Mon Sep 17 00:00:00 2001 From: cxh Date: Thu, 5 Jun 2025 09:24:58 +0800 Subject: [PATCH] temp --- .vscode/settings.json | 6 ++++ CMakeLists.txt | 22 ++++++++++-- config/config.toml | 6 ++++ include/LedMqttSubscriber.h | 29 +++++++++++++++ src/LedMqttSubscriber.cpp | 71 +++++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 include/LedMqttSubscriber.h create mode 100644 src/LedMqttSubscriber.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b4db9b9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "condition_variable": "cpp", + "future": "cpp" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index ea50639..75d8438 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,8 +4,11 @@ project(led_control) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -# 设置头文件目录 -include_directories(include) +# 包含头文件目录 +include_directories(${CMAKE_SOURCE_DIR}/include) + +# 库文件目录 +link_directories(${CMAKE_SOURCE_DIR}/lib) # 查找src目录下所有cpp文件 file(GLOB SOURCES "src/*.cpp") @@ -13,8 +16,21 @@ file(GLOB SOURCES "src/*.cpp") # 生成可执行文件 add_executable(led_app ${SOURCES}) -# 链接需要的库,示例链接pthread和toml11(假设toml11是头文件库,不用链接) +# 链接需要的库 find_package(Threads REQUIRED) target_link_libraries(led_app PRIVATE Threads::Threads) +# 连接 paho mqtt 和依赖库 +target_link_libraries(led_app + paho-mqttpp3 + paho-mqtt3as + ssl + crypto +) + +# 运行时动态库路径设置 +set_target_properties(led_app PROPERTIES + BUILD_RPATH ${CMAKE_SOURCE_DIR}/lib +) + # 如果你toml11是以静态库形式存在,或有其他依赖库,记得这里添加 diff --git a/config/config.toml b/config/config.toml index 007942f..ea0c870 100644 --- a/config/config.toml +++ b/config/config.toml @@ -1,3 +1,9 @@ [serial] port = "/dev/ttyS0" baudrate = 115200 +[mqtt] +host = "192.168.4.196:11883" +username = "zxwl" +password = "zxwl1234@" +topic = "/zxwl/vehicle/V060001/info" +qos = 1 \ No newline at end of file diff --git a/include/LedMqttSubscriber.h b/include/LedMqttSubscriber.h new file mode 100644 index 0000000..ef85fa4 --- /dev/null +++ b/include/LedMqttSubscriber.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +class MqttClient +{ +public: + MqttClient(const std::string &configPath); + bool start(); + +private: + std::string broker; + std::string clientId; + std::string username; + std::string password; + std::string topic; + int qos; + + class Callback : public virtual mqtt::callback + { + public: + void message_arrived(mqtt::const_message_ptr msg) override; + }; + + mqtt::async_client *client = nullptr; + mqtt::connect_options connOpts; + Callback callback; +}; diff --git a/src/LedMqttSubscriber.cpp b/src/LedMqttSubscriber.cpp new file mode 100644 index 0000000..1262d35 --- /dev/null +++ b/src/LedMqttSubscriber.cpp @@ -0,0 +1,71 @@ +#include "LedMqttSubscriber.h" +#include "toml.hpp" +#include +#include +#include +#include + +// 生成随机clientId辅助函数 +std::string generateRandomClientId(const std::string &prefix = "client_") +{ + // 用时间戳+随机数生成一个简单的唯一字符串 + auto now = std::chrono::system_clock::now().time_since_epoch().count(); + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(1000, 9999); + + std::stringstream ss; + ss << prefix << now << "_" << dis(gen); + return ss.str(); +} + +MqttClient::MqttClient(const std::string &configPath) +{ + try + { + auto data = toml::parse(configPath); + auto mqttConfig = toml::find(data, "mqtt"); + + broker = toml::find(mqttConfig, "host"); + clientId = generateRandomClientId(); + username = toml::find_or(mqttConfig, "username", ""); + password = toml::find_or(mqttConfig, "password", ""); + topic = toml::find(mqttConfig, "topic"); + qos = toml::find_or(mqttConfig, "qos", 1); + + client = new mqtt::async_client(broker, clientId); + connOpts.set_clean_session(true); + if (!username.empty()) + connOpts.set_user_name(username); + if (!password.empty()) + connOpts.set_password(password); + + client->set_callback(callback); + } + catch (const std::exception &e) + { + std::cerr << "加载 MQTT 配置失败: " << e.what() << std::endl; + } +} + +bool MqttClient::start() +{ + try + { + client->connect(connOpts)->wait(); + client->subscribe(topic, qos)->wait(); + std::cout << "已订阅主题:" << topic << std::endl; + return true; + } + catch (const mqtt::exception &e) + { + std::cerr << "MQTT 启动失败: " << e.what() << std::endl; + return false; + } +} + +void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) +{ + std::cout << "接收到消息: " << msg->to_string() << std::endl; +}