#ifndef MQTTHANDLER_H #define MQTTHANDLER_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class NetworkInfoHelper : public QObject { Q_OBJECT Q_PROPERTY(QNetworkInformation::Reachability reachability READ reachability NOTIFY reachabilityChanged) Q_PROPERTY(QNetworkInformation::TransportMedium transportMedium READ transportMedium NOTIFY transportMediumChanged) Q_PROPERTY(bool isBehindCaptivePortal READ isBehindCaptivePortal NOTIFY isBehindCaptivePortalChanged) Q_PROPERTY(bool isMetered READ isMetered NOTIFY isMeteredChanged) public: explicit NetworkInfoHelper(QObject *parent = nullptr); QNetworkInformation::Reachability reachability() const; QNetworkInformation::TransportMedium transportMedium() const; bool isBehindCaptivePortal() const; bool isMetered() const; signals: void reachabilityChanged(QNetworkInformation::Reachability newReachability); void transportMediumChanged(QNetworkInformation::TransportMedium current); void isBehindCaptivePortalChanged(bool state); void isMeteredChanged(bool isMetered); private: QNetworkInformation *m_networkInfo = nullptr; QNetworkInformation::Reachability m_initialReachability = QNetworkInformation::Reachability::Unknown; QNetworkInformation::TransportMedium m_initialTransportMedium = QNetworkInformation::TransportMedium::Unknown; bool m_initialIsBehindCaptivePortal = false; bool m_initialIsMetered = false; }; class MqttHandler : public QThread { Q_OBJECT public: QByteArray m_client_dataBuf; //数据缓存区 int m_client_rxByte = 0; //接收字节数 explicit MqttHandler(QObject *parent = nullptr); ~MqttHandler(); void run() override { exec(); } Q_INVOKABLE void connectToBroker(); Q_INVOKABLE void disconnectFromBroker(); Q_INVOKABLE void publish_m(const QByteArray &message); Q_INVOKABLE void subscribe_m(const QString &m_topic); Q_INVOKABLE void unsubscribe_m(const QString &m_topic); Q_INVOKABLE bool checkLink(){ if(m_client->state()==QMqttClient::Disconnected){ return false; }else{ return true; } } Q_INVOKABLE QStringList netMessage() { QStringList net_list; net_list.append(QString::number(param::net_alive)); net_list.append(param::net_name); net_list.append(param::net_description); net_list.append(param::net_mac); net_list.append(param::net_ipv4); net_list.append(param::ftp_main_ip); net_list.append(QString::number(param::dat_f_main)); net_list.append(QString::number(param::datupload_remain/1000)); return net_list; } Q_INVOKABLE void sendUpdateApp(){ emit appdownload(); } Q_INVOKABLE void sendUpdatesBoard(){ emit updateStm32(); //下载rbl文件 emit dataReceived(mqtt_json.CmdUpdate()); //发送升级指令 } Q_INVOKABLE QString cache_json_num(){ return QString::number(m_storage->JsonEntries_saved()); } Q_INVOKABLE QString cache_image_num(){ return QString::number(m_storage->ImageEntries_saved()); } Q_INVOKABLE QString cache_Json_Maxnum(){ return QString::number(m_storage->maxJsonEntries()); } Q_INVOKABLE QString cache_image_Maxnum(){ return QString::number(m_storage->maxImageEntries()); } Q_INVOKABLE int cache_image_enable(){ return m_storage->imageCacheEnabled(); } Q_INVOKABLE int cache_Json_enable(){ return m_storage->jsonCacheEnabled(); } // 提供接口(如果需要从 QML 调整配置) OfflineStorage* storage() { return m_storage; } signals: void connected(); void disconnected(); void mqttclient_readSuccess(int recByte); void dataReceived(const QByteArray &message); void messageSub(const QByteArray &message); void messagePub(const QByteArray &message); void autotakephoto(bool stat); void appdownload(); //apk文件下载信号 void updateStm32(); //rbl文件下载信号 void configChange(); void mediaplay(); void sysReboot(); void appInstall(); void signal_reupload_image(const QString &path); void signal_imei_change(const QString &new_imei); void signal_upload_log(const QString &path); public slots: void install_app(); void startReupload(); void storage_Picture(bool success,const QString &path); private slots: // Your existing slots... void onConnected(); void onDisconnected(); void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic); void reConnect(); void playSound(); private: QMediaPlayer* mediaPlayer; QAudioOutput* maudioOutput; QTextCodec *tc; // 编码 QMqttClient *m_client; OfflineStorage *m_storage; QUrl url; JsonFunc mqtt_json; int m_uploadBatchSize = 1; // 可配置 QHash> m_pendingAcks; // 消息ID -> 回调 QTimer *image_reupload; }; class CurlHandler : public QThread { Q_OBJECT public: CurlHandler(const QString &url, const QString &outputPath) : url(url), outputPath(outputPath) { username = "cbd_user"; password = "YFkj@6O2O"; } void seturl(const QString &nurl) { url = nurl; } void setfilepath(const QString &npath){ outputPath = npath; } void settype(const QString &tp){ update_type = tp; } protected: void run() override { CURL *curl = curl_easy_init(); QFile localFile(outputPath); if (localFile.open(QIODevice::WriteOnly)) { if(curl){ emit downloadStart(update_type); curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData()); // 设置用户名和密码 curl_easy_setopt(curl, CURLOPT_USERPWD, (username + ":" + password).toUtf8().constData()); // 设置回调函数,将下载的数据写入本地文件 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &localFile); // 设置进度回调函数 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); //curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progressCallback); curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progressCallback); curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this); res = curl_easy_perform(curl); if (res == CURLE_OK) { LOG("File downloaded and saved."); emit downloadFinished(true,update_type); } else { emit downloadFinished(false,update_type); qDebug() << "Download failed:" << curl_easy_strerror(res); } curl_easy_cleanup(curl); } else{ qDebug() << "Failed to open local file for writing."; } localFile.close(); curl_global_cleanup(); }else{ qDebug() << "Failed to open local file for writing."; } } public slots: void uploadFile(const QString &url){ QString username = param::ftp_main_uid; QString password = param::ftp_main_pwd; } signals: void downloadStart(QString mtype); void downloadFinished(bool result,QString mtype); void uploadFinished(bool result); void sglProgress(double val,double timeLeft,double speed); //进度信号 private: CURLcode res; QString update_type; QString url; QString outputPath; QString username; QString password; QFile localFile; static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { size_t totalSize = size * nmemb; QFile* file = static_cast(userp); if (file) { file->write(static_cast(contents), totalSize); } return totalSize; } // 下载进度回调函数 static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) { Q_UNUSED(ultotal); Q_UNUSED(ulnow); // 获取对象实例 CurlHandler* curl_handler = static_cast(clientp); // 计算下载百分比 double percent = 0.0; double speed = 0.0; double timeLeft = 0.0; if (dltotal > 0) { percent = (double)dlnow / (double)dltotal*100.0; speed = speed = (double)dlnow / 1024.0; // 下载速度(KB/s) timeLeft = ((double)dltotal - (double)dlnow) / (speed * 1024.0)*100; // 剩余时间(秒) } //qDebug() <<"Downloaded:" << dlnow <<"-" < 0) { emit curl_handler->sglProgress(percent,timeLeft,speed); QCoreApplication::processEvents(); } // 计算剩余时间 double remainingTime = 0.0; // 更新进度条或进行其他处理 // if (dlnow > 0) { // remainingTime = ((dltotal - dlnow) / (dlnow / 1000.0)); // 假设下载速度为每秒字节数 // } //Logger::instance()->log("Downloaded:"+QString::number(percent)+"%"+"Remaining Time:"+QString::number(remainingTime)+"seconds"); return 0; } }; #endif // MQTTHANDLER_H