AmdaProvider.cpp
128 lines
| 4.0 KiB
| text/x-c
|
CppLexer
|
r348 | #include "AmdaProvider.h" | ||
|
r351 | #include "AmdaResultParser.h" | ||
|
r349 | #include <Data/DataProviderParameters.h> | ||
r358 | #include <Network/NetworkController.h> | |||
#include <SqpApplication.h> | ||||
#include <Variable/Variable.h> | ||||
|
r349 | |||
#include <QNetworkAccessManager> | ||||
#include <QNetworkReply> | ||||
|
r350 | #include <QTemporaryFile> | ||
r358 | #include <QThread> | |||
|
r348 | |||
Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") | ||||
|
r349 | namespace { | ||
/// URL format for a request on AMDA server. The parameters are as follows: | ||||
/// - %1: start date | ||||
/// - %2: end date | ||||
/// - %3: parameter id | ||||
const auto AMDA_URL_FORMAT = QStringLiteral( | ||||
"http://amda.irap.omp.eu/php/rest/" | ||||
"getParameter.php?startTime=%1&stopTime=%2¶meterID=%3&sampling=60&outputFormat=ASCII&" | ||||
"timeFormat=ISO8601&gzip=0"); | ||||
/// Dates format passed in the URL (e.g 2013-09-23T09:00) | ||||
const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss"); | ||||
/// Formats a time to a date that can be passed in URL | ||||
QString dateFormat(double sqpDateTime) noexcept | ||||
{ | ||||
auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.); | ||||
return dateTime.toString(AMDA_TIME_FORMAT); | ||||
} | ||||
r358 | ||||
|
r349 | } // namespace | ||
|
r348 | struct AmdaProvider::AmdaProviderPrivate { | ||
|
r349 | DataProviderParameters m_Params{}; | ||
std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | ||||
QNetworkReply *m_Reply{nullptr}; | ||||
r358 | // std::unique_ptr<QTemporaryFile> m_File{nullptr}; | |||
|
r349 | QUuid m_Token; | ||
|
r348 | }; | ||
AmdaProvider::AmdaProvider() : impl{spimpl::make_unique_impl<AmdaProviderPrivate>()} | ||||
{ | ||||
r358 | qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") | |||
<< QThread::currentThread(); | ||||
if (auto app = sqpApp) { | ||||
auto &networkController = app->networkController(); | ||||
connect(this, &AmdaProvider::requestConstructed, &networkController, | ||||
&NetworkController::onProcessRequested); | ||||
} | ||||
|
r348 | } | ||
void AmdaProvider::requestDataLoading(QUuid token, const QVector<SqpDateTime> &dateTimeList) | ||||
{ | ||||
// NOTE: Try to use multithread if possible | ||||
for (const auto &dateTime : dateTimeList) { | ||||
retrieveData(token, DataProviderParameters{dateTime}); | ||||
} | ||||
} | ||||
r358 | void AmdaProvider::retrieveData(QUuid token, const DataProviderParameters ¶meters) | |||
|
r349 | { | ||
// /////////// // | ||||
// Creates URL // | ||||
// /////////// // | ||||
auto startDate = dateFormat(parameters.m_Time.m_TStart); | ||||
auto endDate = dateFormat(parameters.m_Time.m_TEnd); | ||||
auto productId = QStringLiteral("imf(0)"); | ||||
auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; | ||||
r358 | auto tempFile = std::make_shared<QTemporaryFile>(); | |||
|
r349 | |||
r358 | // LAMBDA | |||
auto httpDownloadFinished = [this, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { | ||||
|
r350 | |||
r358 | if (tempFile) { | |||
auto replyReadAll = reply->readAll(); | ||||
if (!replyReadAll.isEmpty()) { | ||||
tempFile->write(replyReadAll); | ||||
} | ||||
tempFile->close(); | ||||
|
r351 | |||
r358 | // Parse results file | |||
if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { | ||||
emit dataProvided(impl->m_Token, dataSeries, impl->m_Params.m_Time); | ||||
} | ||||
else { | ||||
/// @todo ALX : debug | ||||
} | ||||
|
r351 | } | ||
r358 | // Deletes reply | |||
reply->deleteLater(); | ||||
reply = nullptr; | ||||
}; | ||||
auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, | ||||
QUuid dataId) noexcept { | ||||
|
r350 | |||
r358 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; | |||
// Deletes old reply | ||||
reply->deleteLater(); | ||||
|
r350 | |||
r358 | // Executes request for downloading file // | |||
// Creates destination file | ||||
if (tempFile->open()) { | ||||
// Executes request | ||||
emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); | ||||
} | ||||
}; | ||||
// //////////////// // | ||||
// Executes request // | ||||
// //////////////// // | ||||
impl->m_Token = token; | ||||
impl->m_Params = parameters; | ||||
emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); | ||||
|
r348 | } | ||