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.
This commit is contained in:
@@ -176,8 +176,8 @@ bool CustomRobot::SetReportFreq(int32_t interval, int32_t duration) {
|
|||||||
|
|
||||||
bool CustomRobot::initializeMqtt() {
|
bool CustomRobot::initializeMqtt() {
|
||||||
try {
|
try {
|
||||||
std::string broker = config_->mqtt_broker;
|
std::string broker = config_.mqtt_broker;
|
||||||
int port = config_->mqtt_port;
|
int port = config_.mqtt_port;
|
||||||
std::string clientId = generateRandomClientId();
|
std::string clientId = generateRandomClientId();
|
||||||
|
|
||||||
mqttClient_ = std::make_unique<MqttClient>(broker, port, clientId);
|
mqttClient_ = std::make_unique<MqttClient>(broker, port, clientId);
|
||||||
@@ -192,7 +192,7 @@ bool CustomRobot::initializeMqtt() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string controlTopic = config_->topic_prefix + "/" + config_->topic_cmd;
|
std::string controlTopic = config_.topic_prefix + "/" + config_.topic_cmd;
|
||||||
|
|
||||||
mqttClient_->subscribe(controlTopic, 1);
|
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);
|
// 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 (topic == controlTopic) {
|
||||||
// if (message.contains("command")) {
|
// if (message.contains("command")) {
|
||||||
@@ -225,7 +225,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay
|
|||||||
// response["status"] = "executed";
|
// response["status"] = "executed";
|
||||||
// response["command"] = command;
|
// response["command"] = command;
|
||||||
// response["timestamp"] = std::time(nullptr);
|
// 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);
|
// mqttClient_->publishJson(statusTopic, response);
|
||||||
|
|
||||||
// // Publish updated status
|
// // Publish updated status
|
||||||
@@ -238,7 +238,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay
|
|||||||
// response["status"] = "executed";
|
// response["status"] = "executed";
|
||||||
// response["command"] = command;
|
// response["command"] = command;
|
||||||
// response["timestamp"] = std::time(nullptr);
|
// 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);
|
// mqttClient_->publishJson(statusTopic, response);
|
||||||
|
|
||||||
// // Publish updated status
|
// // Publish updated status
|
||||||
@@ -250,7 +250,7 @@ void CustomRobot::onMqttMessage(const std::string& topic, const std::string& pay
|
|||||||
// response["status"] = "error";
|
// response["status"] = "error";
|
||||||
// response["message"] = "Unknown command: " + command;
|
// response["message"] = "Unknown command: " + command;
|
||||||
// response["timestamp"] = std::time(nullptr);
|
// 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);
|
// mqttClient_->publishJson(statusTopic, response);
|
||||||
|
|
||||||
// // Publish updated status
|
// // Publish updated status
|
||||||
@@ -270,7 +270,7 @@ void CustomRobot::onMqttConnection(bool connected) {
|
|||||||
nlohmann::json status;
|
nlohmann::json status;
|
||||||
status["status"] = "online";
|
status["status"] = "online";
|
||||||
status["timestamp"] = std::time(nullptr);
|
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);
|
mqttClient_->publishJson(statusTopic, status, 1, true);
|
||||||
|
|
||||||
publishStatus();
|
publishStatus();
|
||||||
@@ -296,7 +296,7 @@ void CustomRobot::publishStatus() {
|
|||||||
status["controller_connected"] = controller_->isConnected();
|
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);
|
mqttClient_->publishJson(statusTopic, status, 1, true);
|
||||||
|
|
||||||
LOG_DEBUG("Published status to topic: " + statusTopic);
|
LOG_DEBUG("Published status to topic: " + statusTopic);
|
||||||
@@ -307,7 +307,7 @@ void CustomRobot::publishStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string CustomRobot::generateRandomClientId() const {
|
std::string CustomRobot::generateRandomClientId() const {
|
||||||
std::string baseId = config_->mqtt_client_id;
|
std::string baseId = config_.mqtt_client_id;
|
||||||
|
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 gen(rd());
|
std::mt19937 gen(rd());
|
||||||
|
|||||||
Reference in New Issue
Block a user