#include "DataCollection.h" #include "BoostLog.h" #include #include #include #include #include #include #include DataCollection::DataCollection(QObject *parent) : QObject{parent} { m_manager = new QNetworkAccessManager(this); } void DataCollection::start(const QString &address) { m_address = address; m_enabled = true; emit enabledChanged(); start(); } void DataCollection::stop() { m_enabled = false; emit enabledChanged(); } QString DataCollection::path() const { return m_path; } void DataCollection::setPath(const QString &path) { // file:///E:/Downloads/logs auto p = path; if (p.startsWith("file:///")) { p.remove(0, 8); } if (m_path != p) { m_path = p; emit pathChanged(); LOG(info) << "set data path: " << m_path.toStdString(); } } bool DataCollection::enabled() const { return m_enabled; } void DataCollection::start() { auto url = QString("http://%1:8080/capture.do").arg(m_address); QNetworkRequest request; request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); request.setUrl(url); boost::json::object data; data["devid"] = 1; auto content = boost::json::serialize(data); QNetworkReply *reply = m_manager->post(request, QString::fromStdString(content).toLocal8Bit()); connect(reply, &QNetworkReply::finished, this, &DataCollection::onCaptureFinished); } void DataCollection::onCaptureFinished() { auto reply = dynamic_cast(sender()); reply->deleteLater(); auto text = reply->readAll().toStdString(); auto replyVale = boost::json::parse(text); auto root = replyVale.as_object(); m_filename = QString::fromStdString(std::string(root.at("path").as_string())); auto url = QString("http://%1:8080/%2").arg(m_address, m_filename); QNetworkRequest request; request.setUrl(url); QNetworkReply *dataReply = m_manager->get(request); connect(dataReply, &QNetworkReply::finished, this, &DataCollection::onDataGetFinished); } void DataCollection::onDataGetFinished() { auto reply = dynamic_cast(sender()); auto data = reply->readAll(); LOG(info) << "error: " << reply->error() << ", capture data size: " << data.size(); QFile file(m_path + "/" + m_filename); file.open(QIODevice::WriteOnly); file.write(data); file.close(); if (m_enabled) { QTimer::singleShot(0, this, [this]() { start(); }); } }