Compare commits
3 Commits
CigaretteS
...
main
Author | SHA1 | Date |
---|---|---|
|
a8d4a359c4 | 7 months ago |
|
c10ad02d1a | 7 months ago |
|
cc8ce53abc | 1 year ago |
@ -0,0 +1,68 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"string": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"complex": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"future": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"mutex": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"semaphore": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"stop_token": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"thread": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"typeindex": "cpp",
|
||||||
|
"typeinfo": "cpp"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
/* This file was generated by qmake with the info from <root>/src/mqtt/mqtt.pro. */
|
||||||
|
#ifdef __cplusplus /* create empty PCH in C mode */
|
||||||
|
#include <QtCore/QtCore>
|
||||||
|
#include <QtNetwork/QtNetwork>
|
||||||
|
#endif
|
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,97 @@
|
|||||||
|
#include "threadSendMqtt.h"
|
||||||
|
#include <WinSock2.h>
|
||||||
|
#include <WS2tcpip.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <qtcpsocket.h>
|
||||||
|
|
||||||
|
// 相关头文件
|
||||||
|
#include <QFile> // 文件操作
|
||||||
|
#include <QJsonObject> // JSON对象
|
||||||
|
#include <QJsonArray> // JSON数组
|
||||||
|
#include <QJsonDocument> // JSON文档
|
||||||
|
#include <QJsonParseError> // JSON异常捕捉
|
||||||
|
|
||||||
|
void threadSendMqtt::init(SyncQueue<_MqttSendInfo>* p_MQTT_Info_queue, std::string ip_, int port_) {
|
||||||
|
ip = QString::fromStdString(ip_);
|
||||||
|
port = port_;
|
||||||
|
qDebug() << "Mqtt ip:" << ip << "| Mqtt port:" << port;
|
||||||
|
Local_MQTT_Info_queue = p_MQTT_Info_queue;
|
||||||
|
}
|
||||||
|
void threadSendMqtt::start_work()
|
||||||
|
{
|
||||||
|
//start(HighestPriority);
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
const QString content = QDateTime::currentDateTime().toString()
|
||||||
|
+ QLatin1String(" Received Topic: ")
|
||||||
|
+ topic.name()
|
||||||
|
+ QLatin1String(" Message: ")
|
||||||
|
+ message
|
||||||
|
+ QLatin1Char('\n');
|
||||||
|
//ui->editLog->insertPlainText(content);
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void threadSendMqtt::run()
|
||||||
|
{
|
||||||
|
if (!connectTCP())
|
||||||
|
qDebug() << "Mqtt connect error!";
|
||||||
|
|
||||||
|
while (isLoop) {
|
||||||
|
_MqttSendInfo TCPSendInfo;
|
||||||
|
Local_MQTT_Info_queue->take(TCPSendInfo);
|
||||||
|
num++;
|
||||||
|
sendData(&TCPSendInfo, num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void threadSendMqtt::sendData(_MqttSendInfo* TCPSendInfo, int Num) {
|
||||||
|
QJsonObject addressObject;
|
||||||
|
// 方式二:赋值
|
||||||
|
addressObject["street"] = "123 Main St.";
|
||||||
|
addressObject["city"] = "Anytown";
|
||||||
|
addressObject["country"] = "USA";
|
||||||
|
|
||||||
|
|
||||||
|
if (m_client->state() == QMqttClient::Connected) {
|
||||||
|
QJsonDocument jsonDoc(addressObject);
|
||||||
|
QByteArray jsonBytes = jsonDoc.toJson();
|
||||||
|
auto result = m_client->publish(QMqttTopicName("topic"), jsonBytes, 0, true);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <iostream>
|
||||||
|
#include <QThread>
|
||||||
|
#include <QtMqtt/qmqttclient.h>
|
||||||
|
#include "common.h"
|
||||||
|
#include "SyncQueue.h"
|
||||||
|
|
||||||
|
class _MqttSendInfo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int MsgID;
|
||||||
|
QString timecost;
|
||||||
|
|
||||||
|
_MqttSendInfo()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class threadSendMqtt : public QThread {
|
||||||
|
public:
|
||||||
|
QString ip;
|
||||||
|
int port;
|
||||||
|
int num = 0;
|
||||||
|
|
||||||
|
threadSendMqtt(QObject* o = nullptr) :QThread(o)
|
||||||
|
{
|
||||||
|
isLoop = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
~threadSendMqtt()
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
_MqttSendInfo TCPSendInfo;
|
||||||
|
Local_MQTT_Info_queue->put(TCPSendInfo);
|
||||||
|
quit();
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
void stop();
|
||||||
|
bool connectTCP();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void run();
|
||||||
|
|
||||||
|
public:
|
||||||
|
void init(SyncQueue<_MqttSendInfo>* p_MQTT_Info_queue, std::string ip_, int port_);
|
||||||
|
void start_work();
|
||||||
|
void sendData(_MqttSendInfo* TCPSendInfo, int Num);
|
||||||
|
|
||||||
|
public:
|
||||||
|
SyncQueue<_MqttSendInfo>* Local_MQTT_Info_queue;
|
||||||
|
std::atomic_bool isLoop = { 0 };
|
||||||
|
QMqttClient* m_client = NULL;
|
||||||
|
};
|
Loading…
Reference in New Issue