随机生成mqtt client id
This commit is contained in:
parent
6a50b8f776
commit
c024a82b22
@ -323,7 +323,7 @@ private:
|
||||
for (int code : vehicle_error_code.getAllErrorCodes())
|
||||
{
|
||||
std::string fault_json = generateFaultJson(code, config.vid, getCurrentTimestampMs());
|
||||
if (!mqtt_client_.publish(fault_topic_, fault_json, 1)) // QoS=1
|
||||
if (!mqtt_client_.publish(fault_topic_, fault_json, 0))
|
||||
{
|
||||
RCLCPP_WARN(this->get_logger(), "Failed to publish fault code %d to MQTT", code);
|
||||
}
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <pthread.h>
|
||||
@ -227,6 +228,25 @@ private:
|
||||
rclcpp::Subscription<sweeper_interfaces::msg::Rtk>::SharedPtr gps_subscribe_;
|
||||
};
|
||||
|
||||
std::string generate_mqtt_client_id()
|
||||
{
|
||||
// 获取当前时间戳(以毫秒为单位)
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
|
||||
// 生成一个 4 位随机数
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1000, 9999);
|
||||
int random_num = dis(gen);
|
||||
|
||||
// 拼接成 client ID
|
||||
std::ostringstream oss;
|
||||
oss << "client_" << millis << "_" << std::setw(4) << std::setfill('0') << random_num;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
bool load_config(const std::string &path)
|
||||
{
|
||||
Json::Reader reader;
|
||||
@ -250,7 +270,7 @@ bool load_config(const std::string &path)
|
||||
// int port = m["inter_net_port"].asInt(); // 11883
|
||||
|
||||
g_conf.address = ip + ":" + std::to_string(port);
|
||||
g_conf.client_id = "sweeper_CLIENT_1532_GPS"; // 如需可改成 VID 等
|
||||
g_conf.client_id = generate_mqtt_client_id();
|
||||
g_conf.user = m["mqtt_user"].asString();
|
||||
g_conf.password = m["mqtt_password"].asString();
|
||||
g_conf.pub_gps_topic = m["pub_gps_topic"].asString();
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
#include <ctime>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include <pthread.h>
|
||||
@ -43,7 +44,7 @@ std::string mqtt_topic_remote;
|
||||
|
||||
// 新增:声明未定义的常量
|
||||
std::string ADDRESS;
|
||||
std::string CLIENTID_SUB = "sweeper200_sub"; // 客户端ID,提供默认值
|
||||
std::string CLIENTID_SUB; // 客户端ID,提供默认值
|
||||
|
||||
std::chrono::steady_clock::time_point last_message_time = std::chrono::steady_clock::now();
|
||||
constexpr auto QOS = 1;
|
||||
@ -53,6 +54,25 @@ char sub_buff[500];
|
||||
|
||||
car_ctrl car_ctrl_mes;
|
||||
|
||||
std::string generate_mqtt_client_id()
|
||||
{
|
||||
// 获取当前时间戳(以毫秒为单位)
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
|
||||
// 生成一个 4 位随机数
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1000, 9999);
|
||||
int random_num = dis(gen);
|
||||
|
||||
// 拼接成 client ID
|
||||
std::ostringstream oss;
|
||||
oss << "client_" << millis << "_" << std::setw(4) << std::setfill('0') << random_num;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
void sendResponse(MQTTClient client, const std::string &topic, long long seqNo, int code, std::string msg)
|
||||
{
|
||||
// 使用JSON库构建响应数据
|
||||
@ -428,7 +448,7 @@ void init_main()
|
||||
mqtt_topic_remote = root["mqtt"]["remote_topic"].asString();
|
||||
// 添加对sub_topic的初始化
|
||||
sub_topic = mqtt_topic_remote;
|
||||
CLIENTID_SUB = CLIENTID_SUB + "_" + mqtt_vid;
|
||||
CLIENTID_SUB = generate_mqtt_client_id();
|
||||
// ADDRESS = mqtt_inter_net_address + ":" + std::to_string(mqtt_inter_net_port);
|
||||
ADDRESS = mqtt_external_net_address + ":" + std::to_string(mqtt_external_net_port);
|
||||
cout << "ADDRESS: " << ADDRESS << endl;
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include "task_manager_node.hpp"
|
||||
#include "sweeper_interfaces/msg/sub.hpp"
|
||||
#include <signal.h> // 添加信号处理头文件
|
||||
#include <random>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -35,7 +36,7 @@ std::string sub_topic;
|
||||
|
||||
// 修复变量声明和初始化
|
||||
std::string ADDRESS;
|
||||
std::string CLIENTID_SUB = "sweeper200_task"; // 客户端ID
|
||||
std::string CLIENTID_SUB; // 客户端ID
|
||||
std::string destinationFilePath1 = "./gps_load_now.txt";
|
||||
std::chrono::steady_clock::time_point last_message_time = std::chrono::steady_clock::now();
|
||||
constexpr auto QOS = 0;
|
||||
@ -49,6 +50,25 @@ TaskStatus status_up = TaskStatus::PENDING;
|
||||
CurrentTask currentTask;
|
||||
std::mutex taskMutex;
|
||||
|
||||
std::string generate_mqtt_client_id()
|
||||
{
|
||||
// 获取当前时间戳(以毫秒为单位)
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
|
||||
// 生成一个 4 位随机数
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(1000, 9999);
|
||||
int random_num = dis(gen);
|
||||
|
||||
// 拼接成 client ID
|
||||
std::ostringstream oss;
|
||||
oss << "client_" << millis << "_" << std::setw(4) << std::setfill('0') << random_num;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
// 信号处理函数
|
||||
void signalHandler(int signum)
|
||||
{
|
||||
@ -732,7 +752,7 @@ void init_main()
|
||||
mqtt_topic_push_status = root["mqtt"]["mqtt_topic_push_status"].asString();
|
||||
// 添加对sub_topic的初始化
|
||||
sub_topic = mqtt_topic_sub_task;
|
||||
CLIENTID_SUB = CLIENTID_SUB + "_" + mqtt_vid;
|
||||
CLIENTID_SUB = generate_mqtt_client_id();
|
||||
// ADDRESS = mqtt_inter_net_address + ":" + std::to_string(mqtt_inter_net_port);
|
||||
ADDRESS = mqtt_external_net_address + ":" + std::to_string(mqtt_external_net_port);
|
||||
cout << "ADDRESS: " << ADDRESS << endl;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user