|
|
|
@ -1,8 +1,5 @@
|
|
|
|
|
#include "threadSendMqtt.h"
|
|
|
|
|
#include <WinSock2.h>
|
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <qtcpsocket.h>
|
|
|
|
|
|
|
|
|
|
// 相关头文件
|
|
|
|
|
#include <QFile> // 文件操作
|
|
|
|
@ -27,25 +24,19 @@ void threadSendMqtt::stop()
|
|
|
|
|
{
|
|
|
|
|
isLoop = false;
|
|
|
|
|
wait();
|
|
|
|
|
delete m_client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool threadSendMqtt::connectTCP() {
|
|
|
|
|
|
|
|
|
|
m_client = new QMqttClient(this);
|
|
|
|
|
m_client->setHostname(ip);
|
|
|
|
|
m_client->setPort(port);
|
|
|
|
|
//m_client->setUsername(userName);
|
|
|
|
|
//m_client->setPassword(userPassword);
|
|
|
|
|
m_client->connectToHost();
|
|
|
|
|
//connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
|
|
|
|
|
|
|
|
|
|
connect(m_client, &QMqttClient::connected, this, [this](void) {qDebug() << "Mqtt connected";});
|
|
|
|
|
connect(m_client, &QMqttClient::disconnected, this, [this](void) {qDebug() << "Mqtt disconnected"; });
|
|
|
|
|
|
|
|
|
|
connect(m_client, SIGNAL(messageSent(qint32)), this, SLOT(MQTT_DATASEND_SUCCESS(qint32)));//消息发送成功提示的槽函数绑定
|
|
|
|
|
|
|
|
|
|
connect(m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
|
|
|
|
|
bool threadSendMqtt::connectMqtt() {
|
|
|
|
|
connect(&m_client, &QMqttClient::connected, this, [this](void) {
|
|
|
|
|
qDebug() << "Mqtt connected";
|
|
|
|
|
});
|
|
|
|
|
connect(&m_client, &QMqttClient::disconnected, this, [this](void) {
|
|
|
|
|
qDebug() << "Mqtt disconnected";
|
|
|
|
|
});
|
|
|
|
|
connect(&m_client, &QMqttClient::errorChanged, this, [this](QMqttClient::ClientError error) {
|
|
|
|
|
qDebug() << error;
|
|
|
|
|
});
|
|
|
|
|
connect(&m_client, &QMqttClient::messageReceived, this, [this](const QByteArray &message, const QMqttTopicName &topic) {
|
|
|
|
|
const QString content = QDateTime::currentDateTime().toString()
|
|
|
|
|
+ QLatin1String(" Received Topic: ")
|
|
|
|
|
+ topic.name()
|
|
|
|
@ -54,30 +45,40 @@ bool threadSendMqtt::connectTCP() {
|
|
|
|
|
+ QLatin1Char('\n');
|
|
|
|
|
//ui->editLog->insertPlainText(content);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(m_client, &QMqttClient::pingResponseReceived, this, [this]() {
|
|
|
|
|
connect(&m_client, &QMqttClient::pingResponseReceived, this, [this]() {
|
|
|
|
|
const QString content = QDateTime::currentDateTime().toString()
|
|
|
|
|
+ QLatin1String(" PingResponse")
|
|
|
|
|
+ QLatin1Char('\n');
|
|
|
|
|
//ui->editLog->insertPlainText(content);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname);
|
|
|
|
|
//connect(ui->spinBoxPort, QOverload<int>::of(&QSpinBox::valueChanged), this, &MainWindow::setClientPort);
|
|
|
|
|
qDebug() << m_client.state();
|
|
|
|
|
if (m_client.state() == QMqttClient::Disconnected)
|
|
|
|
|
{
|
|
|
|
|
m_client.setHostname("192.168.3.6");
|
|
|
|
|
m_client.setClientId("123");
|
|
|
|
|
m_client.setPort(1883);
|
|
|
|
|
//m_client.setUsername(userName);
|
|
|
|
|
//m_client.setPassword(userPassword);
|
|
|
|
|
m_client.connectToHost();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void threadSendMqtt::run()
|
|
|
|
|
{
|
|
|
|
|
if (!connectTCP())
|
|
|
|
|
qDebug() << m_client.state();
|
|
|
|
|
if (!connectMqtt())
|
|
|
|
|
qDebug() << "Mqtt connect error!";
|
|
|
|
|
|
|
|
|
|
while (isLoop) {
|
|
|
|
|
_MqttSendInfo TCPSendInfo;
|
|
|
|
|
qDebug() << m_client.error();
|
|
|
|
|
qDebug() << m_client.state();
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
|
/*_MqttSendInfo TCPSendInfo;
|
|
|
|
|
Local_MQTT_Info_queue->take(TCPSendInfo);
|
|
|
|
|
num++;
|
|
|
|
|
sendData(&TCPSendInfo, num);
|
|
|
|
|
sendData(&TCPSendInfo, num);*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -89,9 +90,9 @@ void threadSendMqtt::sendData(_MqttSendInfo* TCPSendInfo, int Num) {
|
|
|
|
|
addressObject["country"] = "USA";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_client->state() == QMqttClient::Connected) {
|
|
|
|
|
if (m_client.state() == QMqttClient::Connected) {
|
|
|
|
|
QJsonDocument jsonDoc(addressObject);
|
|
|
|
|
QByteArray jsonBytes = jsonDoc.toJson();
|
|
|
|
|
auto result = m_client->publish(QMqttTopicName("topic"), jsonBytes, 0, true);
|
|
|
|
|
auto result = m_client.publish(QMqttTopicName("topic"), jsonBytes, 0, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|