AmdaProvider.cpp
148 lines
| 5.1 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r377 | #include "AmdaProvider.h" | ||
Alexandre Leroux
|
r413 | #include "AmdaDefs.h" | ||
Alexandre Leroux
|
r380 | #include "AmdaResultParser.h" | ||
Alexandre Leroux
|
r488 | #include <Common/DateUtils.h> | ||
Alexandre Leroux
|
r378 | #include <Data/DataProviderParameters.h> | ||
r388 | #include <Network/NetworkController.h> | |||
#include <SqpApplication.h> | ||||
#include <Variable/Variable.h> | ||||
Alexandre Leroux
|
r378 | |||
#include <QNetworkAccessManager> | ||||
#include <QNetworkReply> | ||||
Alexandre Leroux
|
r379 | #include <QTemporaryFile> | ||
r388 | #include <QThread> | |||
Alexandre Leroux
|
r377 | |||
Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") | ||||
Alexandre Leroux
|
r378 | 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/" | ||||
Alexandre Leroux
|
r473 | "getParameter.php?startTime=%1&stopTime=%2¶meterID=%3&outputFormat=ASCII&" | ||
Alexandre Leroux
|
r378 | "timeFormat=ISO8601&gzip=0"); | ||
/// Dates format passed in the URL (e.g 2013-09-23T09:00) | ||||
r440 | const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss"); | |||
Alexandre Leroux
|
r378 | |||
/// Formats a time to a date that can be passed in URL | ||||
r512 | QString dateFormat(double sqpRange) noexcept | |||
Alexandre Leroux
|
r378 | { | ||
r512 | auto dateTime = DateUtils::dateTime(sqpRange); | |||
Alexandre Leroux
|
r378 | return dateTime.toString(AMDA_TIME_FORMAT); | ||
} | ||||
} // namespace | ||||
Alexandre Leroux
|
r409 | AmdaProvider::AmdaProvider() | ||
Alexandre Leroux
|
r377 | { | ||
Alexandre Leroux
|
r460 | qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::AmdaProvider") << QThread::currentThread(); | ||
r388 | if (auto app = sqpApp) { | |||
auto &networkController = app->networkController(); | ||||
Alexandre Leroux
|
r416 | connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, | ||
std::function<void(QNetworkReply *, QUuid)>)), | ||||
&networkController, | ||||
SLOT(onProcessRequested(QNetworkRequest, QUuid, | ||||
std::function<void(QNetworkReply *, QUuid)>))); | ||||
r425 | ||||
connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this, | ||||
SIGNAL(dataProvidedProgress(QUuid, double))); | ||||
r388 | } | |||
Alexandre Leroux
|
r377 | } | ||
Alexandre Leroux
|
r408 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) | ||
Alexandre Leroux
|
r377 | { | ||
// NOTE: Try to use multithread if possible | ||||
Alexandre Leroux
|
r408 | const auto times = parameters.m_Times; | ||
Alexandre Leroux
|
r413 | const auto data = parameters.m_Data; | ||
Alexandre Leroux
|
r408 | for (const auto &dateTime : qAsConst(times)) { | ||
Alexandre Leroux
|
r413 | retrieveData(token, dateTime, data); | ||
Alexandre Leroux
|
r377 | } | ||
} | ||||
r422 | void AmdaProvider::requestDataAborting(QUuid identifier) | |||
{ | ||||
if (auto app = sqpApp) { | ||||
auto &networkController = app->networkController(); | ||||
networkController.onReplyCanceled(identifier); | ||||
} | ||||
} | ||||
r512 | void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVariantHash &data) | |||
Alexandre Leroux
|
r378 | { | ||
Alexandre Leroux
|
r413 | // Retrieves product ID from data: if the value is invalid, no request is made | ||
auto productId = data.value(AMDA_XML_ID_KEY).toString(); | ||||
if (productId.isNull()) { | ||||
qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); | ||||
return; | ||||
} | ||||
r440 | qCInfo(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData") << dateTime; | |||
Alexandre Leroux
|
r413 | |||
Alexandre Leroux
|
r378 | // /////////// // | ||
// Creates URL // | ||||
// /////////// // | ||||
Alexandre Leroux
|
r408 | auto startDate = dateFormat(dateTime.m_TStart); | ||
auto endDate = dateFormat(dateTime.m_TEnd); | ||||
Alexandre Leroux
|
r378 | |||
auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; | ||||
r440 | qCInfo(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData url:") << url; | |||
r388 | auto tempFile = std::make_shared<QTemporaryFile>(); | |||
Alexandre Leroux
|
r378 | |||
r388 | // LAMBDA | |||
Alexandre Leroux
|
r409 | auto httpDownloadFinished | ||
= [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { | ||||
Q_UNUSED(dataId); | ||||
r431 | // Don't do anything if the reply was abort | |||
if (reply->error() != QNetworkReply::OperationCanceledError) { | ||||
if (tempFile) { | ||||
auto replyReadAll = reply->readAll(); | ||||
if (!replyReadAll.isEmpty()) { | ||||
tempFile->write(replyReadAll); | ||||
} | ||||
tempFile->close(); | ||||
// Parse results file | ||||
if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { | ||||
emit dataProvided(token, dataSeries, dateTime); | ||||
} | ||||
else { | ||||
/// @todo ALX : debug | ||||
} | ||||
Alexandre Leroux
|
r409 | } | ||
} | ||||
}; | ||||
r431 | auto httpFinishedLambda | |||
= [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { | ||||
Alexandre Leroux
|
r379 | |||
r431 | // Don't do anything if the reply was abort | |||
if (reply->error() != QNetworkReply::OperationCanceledError) { | ||||
auto downloadFileUrl = QUrl{QString{reply->readAll()}}; | ||||
r422 | ||||
Alexandre Leroux
|
r379 | |||
r440 | qCInfo(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData downloadFileUrl:") | |||
<< downloadFileUrl; | ||||
r431 | // Executes request for downloading file // | |||
r388 | ||||
r431 | // Creates destination file | |||
if (tempFile->open()) { | ||||
// Executes request | ||||
emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, | ||||
httpDownloadFinished); | ||||
} | ||||
} | ||||
}; | ||||
r388 | ||||
// //////////////// // | ||||
// Executes request // | ||||
// //////////////// // | ||||
emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); | ||||
Alexandre Leroux
|
r377 | } | ||