mqtthandler.h 7.3 KB

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