Refactor MQTT callback function names for consistency. Changed MessageCallback and ConnectionCallback to MessageCb and ConnectionCb in mqtt.hpp, mqtt.cpp, and custom_robot.cpp to improve code clarity and maintainability.
This commit is contained in:
@@ -178,8 +178,8 @@ bool CustomRobot::initializeMqtt() {
|
||||
|
||||
mqttClient_ = std::make_unique<MqttClient>(broker, port, clientId);
|
||||
using namespace std::placeholders;
|
||||
mqttClient_->setMessageCallback(std::bind(&CustomRobot::onMqttMessage, this, _1, _2));
|
||||
mqttClient_->setConnectionCallback(std::bind(&CustomRobot::onMqttConnection, this, _1));
|
||||
mqttClient_->setMessageCb(std::bind(&CustomRobot::onMqttMessage, this, _1, _2));
|
||||
mqttClient_->setConnectionCb(std::bind(&CustomRobot::onMqttConnection, this, _1));
|
||||
|
||||
mqttClient_->startMessageProcessor();
|
||||
|
||||
|
||||
30
src/mqtt.cpp
30
src/mqtt.cpp
@@ -13,13 +13,11 @@ MqttClient::MqttClient(const std::string& broker, int port, const std::string& c
|
||||
ss << "tcp://" << broker << ":" << port;
|
||||
serverURI_ = ss.str();
|
||||
|
||||
// 初始化连接选项
|
||||
connOpts_ = MQTTClient_connectOptions_initializer;
|
||||
connOpts_.keepAliveInterval = 20;
|
||||
connOpts_.cleansession = 1;
|
||||
connOpts_.reliable = 0; // 异步发送
|
||||
|
||||
// 创建 MQTT 客户端
|
||||
int rc = MQTTClient_create(&client_, serverURI_.c_str(), clientId_.c_str(),
|
||||
MQTTCLIENT_PERSISTENCE_NONE, nullptr);
|
||||
if (rc != MQTTCLIENT_SUCCESS) {
|
||||
@@ -27,7 +25,6 @@ MqttClient::MqttClient(const std::string& broker, int port, const std::string& c
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置回调函数
|
||||
MQTTClient_setCallbacks(client_, this, connectionLostCallback,
|
||||
messageArrivedCallback, deliveryCompleteCallback);
|
||||
}
|
||||
@@ -74,13 +71,11 @@ bool MqttClient::connect(const std::string& username, const std::string& passwor
|
||||
return false;
|
||||
}
|
||||
|
||||
// 重置连接选项
|
||||
connOpts_ = MQTTClient_connectOptions_initializer;
|
||||
connOpts_.keepAliveInterval = 20;
|
||||
connOpts_.cleansession = 1;
|
||||
connOpts_.reliable = 0;
|
||||
|
||||
// 设置用户名和密码
|
||||
if (!username_.empty()) {
|
||||
connOpts_.username = username_.c_str();
|
||||
if (!password_.empty()) {
|
||||
@@ -192,12 +187,12 @@ bool MqttClient::publishJson(const std::string& topic, const nlohmann::json& jso
|
||||
return publish(topic, json.dump(), qos, retain);
|
||||
}
|
||||
|
||||
void MqttClient::setMessageCallback(MessageCallback callback) {
|
||||
messageCallback_ = callback;
|
||||
void MqttClient::setMessageCb(MessageCb cb) {
|
||||
messageCb_ = cb;
|
||||
}
|
||||
|
||||
void MqttClient::setConnectionCallback(ConnectionCallback callback) {
|
||||
connectionCallback_ = callback;
|
||||
void MqttClient::setConnectionCb(ConnectionCb cb) {
|
||||
connectionCb_ = cb;
|
||||
}
|
||||
|
||||
void MqttClient::startMessageProcessor() {
|
||||
@@ -237,9 +232,9 @@ void MqttClient::processMessageQueue() {
|
||||
messageQueue_.pop();
|
||||
lock.unlock();
|
||||
|
||||
if (messageCallback_) {
|
||||
if (messageCb_) {
|
||||
try {
|
||||
messageCallback_(message.topic, message.payload);
|
||||
messageCb_(message.topic, message.payload);
|
||||
} catch (const std::exception& e) {
|
||||
LOG_ERROR("Exception in message callback: " + std::string(e.what()));
|
||||
}
|
||||
@@ -250,7 +245,6 @@ void MqttClient::processMessageQueue() {
|
||||
}
|
||||
}
|
||||
|
||||
// C 风格回调函数实现
|
||||
int MqttClient::messageArrivedCallback(void* context, char* topicName, int topicLen, MQTTClient_message* message) {
|
||||
MqttClient* client = static_cast<MqttClient*>(context);
|
||||
|
||||
@@ -270,7 +264,7 @@ int MqttClient::messageArrivedCallback(void* context, char* topicName, int topic
|
||||
MQTTClient_freeMessage(&message);
|
||||
MQTTClient_free(topicName);
|
||||
|
||||
return 1; // 成功处理消息
|
||||
return 1;
|
||||
}
|
||||
|
||||
void MqttClient::connectionLostCallback(void* context, char* cause) {
|
||||
@@ -282,14 +276,14 @@ void MqttClient::connectionLostCallback(void* context, char* cause) {
|
||||
}
|
||||
|
||||
void MqttClient::deliveryCompleteCallback(void* context, MQTTClient_deliveryToken dt) {
|
||||
// 消息发送完成回调,目前不需要特殊处理
|
||||
|
||||
}
|
||||
|
||||
void MqttClient::handleConnectionLost() {
|
||||
connected_ = false;
|
||||
|
||||
if (connectionCallback_) {
|
||||
connectionCallback_(false);
|
||||
if (connectionCb_) {
|
||||
connectionCb_(false);
|
||||
}
|
||||
|
||||
if (shouldReconnect_ && !reconnecting_) {
|
||||
@@ -303,8 +297,8 @@ void MqttClient::handleConnectionEstablished() {
|
||||
reconnecting_ = false;
|
||||
LOG_INFO("MQTT connection established");
|
||||
|
||||
if (connectionCallback_) {
|
||||
connectionCallback_(true);
|
||||
if (connectionCb_) {
|
||||
connectionCb_(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user