1
This commit is contained in:
parent
055fe9c626
commit
ba07a6fe15
@ -1,16 +1,13 @@
|
||||
#include "mqtt_client.hpp"
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include <chrono>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
extern std::atomic<bool> g_running;
|
||||
|
||||
MQTTClient::MQTTClient(const MQTTConfig &config)
|
||||
: config_(config), connected_(false)
|
||||
{
|
||||
initializeClient();
|
||||
}
|
||||
MQTTClient::MQTTClient(const MQTTConfig& config) : config_(config), connected_(false) { initializeClient(); }
|
||||
|
||||
MQTTClient::~MQTTClient()
|
||||
{
|
||||
@ -53,8 +50,7 @@ void MQTTClient::connect()
|
||||
void MQTTClient::disconnect()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!connected_)
|
||||
return;
|
||||
if (!connected_) return;
|
||||
|
||||
try
|
||||
{
|
||||
@ -63,8 +59,7 @@ void MQTTClient::disconnect()
|
||||
// 等待短时间,检查 g_running
|
||||
for (int i = 0; i < 10 && g_running; ++i)
|
||||
{
|
||||
if (disc_tok->is_complete())
|
||||
break;
|
||||
if (disc_tok->is_complete()) break;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
@ -81,15 +76,13 @@ void MQTTClient::disconnect()
|
||||
}
|
||||
|
||||
connected_ = false;
|
||||
if (on_disconnect_)
|
||||
on_disconnect_();
|
||||
if (on_disconnect_) on_disconnect_();
|
||||
}
|
||||
|
||||
void MQTTClient::force_disconnect()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!connected_)
|
||||
return;
|
||||
if (!connected_) return;
|
||||
|
||||
try
|
||||
{
|
||||
@ -103,20 +96,18 @@ void MQTTClient::force_disconnect()
|
||||
}
|
||||
|
||||
connected_ = false;
|
||||
if (on_disconnect_)
|
||||
on_disconnect_();
|
||||
if (on_disconnect_) on_disconnect_();
|
||||
}
|
||||
|
||||
void MQTTClient::publish(const std::string& topic, const std::string& payload, int qos)
|
||||
{
|
||||
if (qos == -1)
|
||||
qos = config_.qos;
|
||||
if (qos == -1) qos = config_.qos;
|
||||
|
||||
try
|
||||
{
|
||||
client_->publish(topic, payload.data(), payload.size(), qos, false); // 异步,不阻塞
|
||||
// 如果 topic 不包含 "heartbeat",才打印日志
|
||||
if (topic.find("heartbeat") == std::string::npos)
|
||||
if (topic.find("status") == std::string::npos)
|
||||
{
|
||||
LOG_INFO("[MQTT] Published message to topic: " + topic);
|
||||
}
|
||||
@ -125,15 +116,13 @@ void MQTTClient::publish(const std::string &topic, const std::string &payload, i
|
||||
{
|
||||
LOG_ERROR("[MQTT] Publish failed: " + std::string(e.what()));
|
||||
connected_ = false;
|
||||
if (on_disconnect_)
|
||||
on_disconnect_();
|
||||
if (on_disconnect_) on_disconnect_();
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTClient::subscribe(const std::string& topic, int qos)
|
||||
{
|
||||
if (qos == -1)
|
||||
qos = config_.qos;
|
||||
if (qos == -1) qos = config_.qos;
|
||||
|
||||
try
|
||||
{
|
||||
@ -144,8 +133,7 @@ void MQTTClient::subscribe(const std::string &topic, int qos)
|
||||
{
|
||||
LOG_ERROR("[MQTT] Subscribe failed: " + std::string(e.what()));
|
||||
connected_ = false;
|
||||
if (on_disconnect_)
|
||||
on_disconnect_();
|
||||
if (on_disconnect_) on_disconnect_();
|
||||
}
|
||||
}
|
||||
|
||||
@ -158,10 +146,7 @@ void MQTTClient::switchServer(const MQTTConfig &newConfig)
|
||||
connect();
|
||||
}
|
||||
|
||||
bool MQTTClient::isConnected() const
|
||||
{
|
||||
return connected_;
|
||||
}
|
||||
bool MQTTClient::isConnected() const { return connected_; }
|
||||
|
||||
void MQTTClient::setConnectCallback(ConnectCallback cb) { on_connect_ = cb; }
|
||||
void MQTTClient::setDisconnectCallback(DisconnectCallback cb) { on_disconnect_ = cb; }
|
||||
@ -171,20 +156,17 @@ void MQTTClient::connection_lost(const std::string &cause)
|
||||
{
|
||||
connected_ = false;
|
||||
LOG_WARN("[MQTT] Connection lost: " + cause);
|
||||
if (on_disconnect_)
|
||||
on_disconnect_();
|
||||
if (on_disconnect_) on_disconnect_();
|
||||
}
|
||||
|
||||
void MQTTClient::connected(const std::string& cause)
|
||||
{
|
||||
connected_ = true;
|
||||
LOG_INFO("[MQTT] Reconnected: " + cause);
|
||||
if (on_connect_)
|
||||
on_connect_();
|
||||
if (on_connect_) on_connect_();
|
||||
}
|
||||
|
||||
void MQTTClient::message_arrived(mqtt::const_message_ptr msg)
|
||||
{
|
||||
if (on_message_)
|
||||
on_message_(msg->get_topic(), msg->get_payload_str());
|
||||
if (on_message_) on_message_(msg->get_topic(), msg->get_payload_str());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user