diff --git a/Cigarette/Thread/threadReceive.cpp b/Cigarette/Thread/threadReceive.cpp index 54dab302..b77bca57 100644 --- a/Cigarette/Thread/threadReceive.cpp +++ b/Cigarette/Thread/threadReceive.cpp @@ -11,8 +11,8 @@ void threadReceive::stop() { isLoop = false; BOOL bDontLinger = FALSE; - mSocket->abort(); - filemSocket->abort(); + mSocket.abort(); + filemSocket.abort(); // wait(); } @@ -20,27 +20,25 @@ void threadReceive::init(std::string IP, int port) { ip = QString::fromStdString(IP); - mSocket = new QUdpSocket(this); - mSocket->bind(port + NumberOfSupportedCameras * 2 + 1, QUdpSocket::ShareAddress); - connect(mSocket, &QUdpSocket::readyRead, this, &threadReceive::processPendingDatagram); + mSocket.bind(port + NumberOfSupportedCameras * 2 + 1, QUdpSocket::ShareAddress); + connect(&mSocket, &QUdpSocket::readyRead, this, &threadReceive::processPendingDatagram); - filemSocket = new QUdpSocket(this); - filemSocket->bind(port + NumberOfSupportedCameras * 2 + 2, QUdpSocket::ShareAddress); - connect(filemSocket, &QUdpSocket::readyRead, this, &threadReceive::fileprocessPendingDatagram); + filemSocket.bind(port + NumberOfSupportedCameras * 2 + 2, QUdpSocket::ShareAddress); + connect(&filemSocket, &QUdpSocket::readyRead, this, &threadReceive::fileprocessPendingDatagram); } void threadReceive::processPendingDatagram() { // 拥有等待的数据报 - while (mSocket->hasPendingDatagrams()) + while (mSocket.hasPendingDatagrams()) { QByteArray datagram; // 让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据 - datagram.resize(mSocket->pendingDatagramSize()); + datagram.resize(mSocket.pendingDatagramSize()); // 接收数据报,将其存放到datagram中 - mSocket->readDatagram(datagram.data(), datagram.size()); + mSocket.readDatagram(datagram.data(), datagram.size()); QString data = datagram; emit sendMsgToCigratte(data); @@ -52,14 +50,14 @@ void threadReceive::fileprocessPendingDatagram() QString filename; std::fstream file; // 拥有等待的数据报 - while (filemSocket->hasPendingDatagrams()) + while (filemSocket.hasPendingDatagrams()) { QByteArray datagram; // 让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据 - datagram.resize(filemSocket->pendingDatagramSize()); + datagram.resize(filemSocket.pendingDatagramSize()); // 接收数据报,将其存放到datagram中 - filemSocket->readDatagram(datagram.data(), datagram.size()); + filemSocket.readDatagram(datagram.data(), datagram.size()); QString data = datagram; QString str = data.mid(0, 4); diff --git a/Cigarette/Thread/threadReceive.h b/Cigarette/Thread/threadReceive.h index b6ffbb66..cc6714dc 100644 --- a/Cigarette/Thread/threadReceive.h +++ b/Cigarette/Thread/threadReceive.h @@ -40,6 +40,6 @@ signals: public: std::atomic_bool isLoop = { 0 }; - QUdpSocket* mSocket; - QUdpSocket* filemSocket; + QUdpSocket mSocket; + QUdpSocket filemSocket; }; diff --git a/Cigarette/Thread/threadSendMqtt.cpp b/Cigarette/Thread/threadSendMqtt.cpp index e4c18ab6..f5f5fd53 100644 --- a/Cigarette/Thread/threadSendMqtt.cpp +++ b/Cigarette/Thread/threadSendMqtt.cpp @@ -1,8 +1,5 @@ #include "threadSendMqtt.h" -#include -#include #include -#include // 相关头文件 #include // 文件操作 @@ -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::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); } } diff --git a/Cigarette/Thread/threadSendMqtt.h b/Cigarette/Thread/threadSendMqtt.h index 70cf2981..b44efd04 100644 --- a/Cigarette/Thread/threadSendMqtt.h +++ b/Cigarette/Thread/threadSendMqtt.h @@ -39,7 +39,7 @@ public: wait(); } void stop(); - bool connectTCP(); + bool connectMqtt(); protected: void run(); @@ -52,5 +52,5 @@ public: public: SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue; std::atomic_bool isLoop = { 0 }; - QMqttClient* m_client = NULL; + QMqttClient m_client; };