mqtthandler.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef MQTTHANDLER_H
  2. #define MQTTHANDLER_H
  3. #include <QObject>
  4. #include <QtMqtt/QMqttClient>
  5. #include <QtMqtt/QMqttSubscription>
  6. #include <QTextCodec>
  7. #include <QThread>
  8. #include <QTimer>
  9. #include <QTextToSpeechEngine>
  10. #include <QtMultimedia/QMediaPlayer>
  11. #include <QAudioOutput>
  12. #include <curl/curl.h>
  13. #include <QStandardPaths>
  14. #include <m_json.h>
  15. #include <m_loger.h>
  16. #include <main.h>
  17. #include <QtConcurrent/QtConcurrent>
  18. #include <QFutureWatcher>
  19. class MqttHandler : public QThread
  20. {
  21. Q_OBJECT
  22. public:
  23. QByteArray m_client_dataBuf; //数据缓存区
  24. int m_client_rxByte = 0; //接收字节数
  25. explicit MqttHandler(QObject *parent = nullptr);
  26. ~MqttHandler();
  27. void run() override {
  28. exec();
  29. }
  30. Q_INVOKABLE void connectToBroker();
  31. Q_INVOKABLE void disconnectFromBroker();
  32. Q_INVOKABLE void publish_m(const QByteArray &message);
  33. Q_INVOKABLE void subscribe_m(const QString &m_topic);
  34. Q_INVOKABLE void unsubscribe_m(const QString &m_topic);
  35. Q_INVOKABLE bool checkLink(){
  36. if(m_client->state()==QMqttClient::Disconnected){
  37. return false;
  38. }else{
  39. return true;
  40. }
  41. }
  42. Q_INVOKABLE QStringList netMessage()
  43. {
  44. QStringList net_list;
  45. net_list.append(QString::number(param::net_alive));
  46. net_list.append(param::net_name);
  47. net_list.append(param::net_description);
  48. net_list.append(param::net_mac);
  49. net_list.append(param::net_ipv4);
  50. net_list.append(param::ftp_main_ip);
  51. net_list.append(QString::number(param::dat_f_main));
  52. net_list.append(QString::number(param::datupload_remain/1000));
  53. return net_list;
  54. }
  55. Q_INVOKABLE void sendUpdateApp(){
  56. emit appdownload();
  57. }
  58. Q_INVOKABLE void sendUpdatesBoard(){
  59. emit updateStm32(); //下载rbl文件
  60. emit dataReceived(mqtt_json.CmdUpdate()); //发送升级指令
  61. }
  62. signals:
  63. void connected();
  64. void disconnected();
  65. void mqttclient_readSuccess(int recByte);
  66. void dataReceived(const QByteArray &message);
  67. void autotakephoto(bool stat);
  68. void appdownload(); //apk文件下载信号
  69. void updateStm32(); //rbl文件下载信号
  70. void configChange();
  71. void mediaplay();
  72. void sysReboot();
  73. public slots:
  74. private slots:
  75. // Your existing slots...
  76. void onConnected();
  77. void onDisconnected();
  78. void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic);
  79. void reConnect();
  80. void playSound();
  81. private:
  82. QMediaPlayer* mediaPlayer;
  83. QAudioOutput* maudioOutput;
  84. QTextCodec *tc; // 编码
  85. QMqttClient *m_client;
  86. QUrl url;
  87. JsonFunc mqtt_json;
  88. };
  89. class CurlHandler : public QThread
  90. {
  91. Q_OBJECT
  92. public:
  93. CurlHandler(const QString &url, const QString &outputPath)
  94. : url(url), outputPath(outputPath) {
  95. username = "cbd_user";
  96. password = "YFkj@6O2O";
  97. }
  98. void seturl(const QString &nurl)
  99. {
  100. url = nurl;
  101. }
  102. void setfilepath(const QString &npath){
  103. outputPath = npath;
  104. }
  105. void settype(const QString &tp){
  106. update_type = tp;
  107. }
  108. protected:
  109. void run() override {
  110. CURL *curl = curl_easy_init();
  111. QFile localFile(outputPath);
  112. if (localFile.open(QIODevice::WriteOnly)) {
  113. if(curl){
  114. emit downloadStart(update_type);
  115. curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData());
  116. // 设置用户名和密码
  117. curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).toUtf8().constData());
  118. // 设置回调函数,将下载的数据写入本地文件
  119. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
  120. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &localFile);
  121. // 设置进度回调函数
  122. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
  123. //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressCallback);
  124. curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progressCallback);
  125. curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
  126. res = curl_easy_perform(curl);
  127. if (res == CURLE_OK) {
  128. Logger::instance()->log("File downloaded and saved.");
  129. emit downloadFinished(true,update_type);
  130. } else {
  131. emit downloadFinished(false,update_type);
  132. qDebug() << "Download failed:" << curl_easy_strerror(res);
  133. }
  134. curl_easy_cleanup(curl);
  135. }
  136. else{
  137. qDebug() << "Failed to open local file for writing.";
  138. }
  139. localFile.close();
  140. curl_global_cleanup();
  141. }else{
  142. qDebug() << "Failed to open local file for writing.";
  143. }
  144. }
  145. public slots:
  146. void uploadFile(const QString &url){
  147. QString username = param::ftp_main_uid;
  148. QString password = param::ftp_main_pwd;
  149. }
  150. signals:
  151. void downloadStart(QString mtype);
  152. void downloadFinished(bool result,QString mtype);
  153. void uploadFinished(bool result);
  154. void sglProgress(double val,double timeLeft,double speed); //进度信号
  155. private:
  156. CURLcode res;
  157. QString update_type;
  158. QString url;
  159. QString outputPath;
  160. QString username;
  161. QString password;
  162. QFile localFile;
  163. static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
  164. size_t totalSize = size * nmemb;
  165. QFile* file = static_cast<QFile*>(userp);
  166. if (file) {
  167. file->write(static_cast<const char*>(contents), totalSize);
  168. }
  169. return totalSize;
  170. }
  171. // 下载进度回调函数
  172. static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
  173. {
  174. Q_UNUSED(ultotal);
  175. Q_UNUSED(ulnow);
  176. // 获取对象实例
  177. CurlHandler* curl_handler = static_cast<CurlHandler*>(clientp);
  178. // 计算下载百分比
  179. double percent = 0.0;
  180. double speed = 0.0;
  181. double timeLeft = 0.0;
  182. if (dltotal > 0) {
  183. percent = (double)dlnow / (double)dltotal*100.0;
  184. speed = speed = (double)dlnow / 1024.0; // 下载速度(KB/s)
  185. timeLeft = ((double)dltotal - (double)dlnow) / (speed * 1024.0)*100; // 剩余时间(秒)
  186. }
  187. //qDebug() <<"Downloaded:" << dlnow <<"-" <<dltotal<<"-"<<percent<<"-"<<timeLeft;
  188. if (speed > 0) {
  189. emit curl_handler->sglProgress(percent,timeLeft,speed);
  190. }
  191. // 计算剩余时间
  192. double remainingTime = 0.0;
  193. // 更新进度条或进行其他处理
  194. // if (dlnow > 0) {
  195. // remainingTime = ((dltotal - dlnow) / (dlnow / 1000.0)); // 假设下载速度为每秒字节数
  196. // }
  197. //Logger::instance()->log("Downloaded:"+QString::number(percent)+"%"+"Remaining Time:"+QString::number(remainingTime)+"seconds");
  198. return 0;
  199. }
  200. };
  201. #endif // MQTTHANDLER_H