From e3c214ee1ca16e76aeabede2420f33982fe68937 Mon Sep 17 00:00:00 2001 From: Sucan126 <632190820@qq.com> Date: Mon, 8 Sep 2025 19:55:56 +0800 Subject: [PATCH] Refactor MQTT configuration access in CustomRobot class. Updated member variable access from pointer dereferencing to direct access for improved clarity and consistency in the codebase. --- src/custom_robot.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/custom_robot.cpp b/src/custom_robot.cpp index 6f4101f..b570c6e 100644 --- a/src/custom_robot.cpp +++ b/src/custom_robot.cpp @@ -176,8 +176,8 @@ bool CustomRobot::SetReportFreq(int32_t interval, int32_t duration) { bool CustomRobot::initializeMqtt() { try { - std::string broker = config_->mqtt_broker; - int port = config_->mqtt_port; + std::string broker = config_.mqtt_broker; + int port = config_.mqtt_port; std::string clientId = generateRandomClientId(); mqttClient_ = std::make_unique(broker, port, clientId); @@ -192,7 +192,7 @@ bool CustomRobot::initializeMqtt() { return false; } - std::string controlTopic = config_->topic_prefix + "/" + config_->topic_cmd; + std::string controlTopic = config_.topic_prefix + "/" + config_.topic_cmd; mqttClient_->subscribe(controlTopic, 1); @@ -211,7 +211,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay // nlohmann::json message = nlohmann::json::parse(payload); - // std::string controlTopic = config_->topic_prefix + "/" + config_->topic_cmd; + // std::string controlTopic = config_.topic_prefix + "/" + config_.topic_cmd; // if (topic == controlTopic) { // if (message.contains("command")) { @@ -225,7 +225,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay // response["status"] = "executed"; // response["command"] = command; // response["timestamp"] = std::time(nullptr); - // std::string statusTopic = config_->topic_prefix + "/" + config_->topic_state; + // std::string statusTopic = config_.topic_prefix + "/" + config_.topic_state; // mqttClient_->publishJson(statusTopic, response); // // Publish updated status @@ -238,7 +238,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay // response["status"] = "executed"; // response["command"] = command; // response["timestamp"] = std::time(nullptr); - // std::string statusTopic = config_->topic_prefix + "/" + config_->topic_state; + // std::string statusTopic = config_.topic_prefix + "/" + config_.topic_state; // mqttClient_->publishJson(statusTopic, response); // // Publish updated status @@ -250,7 +250,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay // response["status"] = "error"; // response["message"] = "Unknown command: " + command; // response["timestamp"] = std::time(nullptr); - // std::string statusTopic = config_->topic_prefix + "/" + config_->topic_state; + // std::string statusTopic = config_.topic_prefix + "/" + config_.topic_state; // mqttClient_->publishJson(statusTopic, response); // // Publish updated status @@ -270,7 +270,7 @@ void CustomRobot::onMqttConnection(bool connected) { nlohmann::json status; status["status"] = "online"; status["timestamp"] = std::time(nullptr); - std::string statusTopic = config_->topic_prefix + "/" + config_->topic_state; + std::string statusTopic = config_.topic_prefix + "/" + config_.topic_state; mqttClient_->publishJson(statusTopic, status, 1, true); publishStatus(); @@ -296,7 +296,7 @@ void CustomRobot::publishStatus() { status["controller_connected"] = controller_->isConnected(); } - std::string statusTopic = config_->topic_prefix + "/" + config_->topic_state; + std::string statusTopic = config_.topic_prefix + "/" + config_.topic_state; mqttClient_->publishJson(statusTopic, status, 1, true); LOG_DEBUG("Published status to topic: " + statusTopic); @@ -307,7 +307,7 @@ void CustomRobot::publishStatus() { } std::string CustomRobot::generateRandomClientId() const { - std::string baseId = config_->mqtt_client_id; + std::string baseId = config_.mqtt_client_id; std::random_device rd; std::mt19937 gen(rd());