##// END OF EJS Templates
Updates IDataProvider::requestDataLoading() method's signature...
Updates IDataProvider::requestDataLoading() method's signature The parameters needed for data retrieval are passed to a DataProviderParameters object. For now, it concerns only the list of datetimes to process, but the object will be completed with extra data which may be necessary for certain providers

File last commit:

r408:49f712bf7e59
r408:49f712bf7e59
Show More
AmdaProvider.cpp
129 lines | 3.9 KiB | text/x-c | CppLexer
Alexandre Leroux
Inits Amda provider
r377 #include "AmdaProvider.h"
Alexandre Leroux
Amda provider (3)...
r380 #include "AmdaResultParser.h"
Alexandre Leroux
Amda provider (1)...
r378 #include <Data/DataProviderParameters.h>
Modify the AmdaProvider to remove from it all network controller...
r388 #include <Network/NetworkController.h>
#include <SqpApplication.h>
#include <Variable/Variable.h>
Alexandre Leroux
Amda provider (1)...
r378
#include <QNetworkAccessManager>
#include <QNetworkReply>
Alexandre Leroux
Amda provider (2)...
r379 #include <QTemporaryFile>
Modify the AmdaProvider to remove from it all network controller...
r388 #include <QThread>
Alexandre Leroux
Inits Amda provider
r377
Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
Alexandre Leroux
Amda provider (1)...
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/"
"getParameter.php?startTime=%1&stopTime=%2&parameterID=%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);
}
Modify the AmdaProvider to remove from it all network controller...
r388
Alexandre Leroux
Amda provider (1)...
r378 } // namespace
Alexandre Leroux
Inits Amda provider
r377 struct AmdaProvider::AmdaProviderPrivate {
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 SqpDateTime m_DateTime{};
Alexandre Leroux
Amda provider (1)...
r378 std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr};
QNetworkReply *m_Reply{nullptr};
Modify the AmdaProvider to remove from it all network controller...
r388 // std::unique_ptr<QTemporaryFile> m_File{nullptr};
Alexandre Leroux
Amda provider (1)...
r378 QUuid m_Token;
Alexandre Leroux
Inits Amda provider
r377 };
AmdaProvider::AmdaProvider() : impl{spimpl::make_unique_impl<AmdaProviderPrivate>()}
{
Modify the AmdaProvider to remove from it all network controller...
r388 qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider")
<< QThread::currentThread();
if (auto app = sqpApp) {
auto &networkController = app->networkController();
connect(this, &AmdaProvider::requestConstructed, &networkController,
&NetworkController::onProcessRequested);
}
Alexandre Leroux
Inits Amda provider
r377 }
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
Alexandre Leroux
Inits Amda provider
r377 {
// NOTE: Try to use multithread if possible
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 const auto times = parameters.m_Times;
for (const auto &dateTime : qAsConst(times)) {
retrieveData(token, dateTime);
Alexandre Leroux
Inits Amda provider
r377 }
}
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime)
Alexandre Leroux
Amda provider (1)...
r378 {
// /////////// //
// Creates URL //
// /////////// //
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 auto startDate = dateFormat(dateTime.m_TStart);
auto endDate = dateFormat(dateTime.m_TEnd);
Alexandre Leroux
Amda provider (1)...
r378 auto productId = QStringLiteral("imf(0)");
auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
Modify the AmdaProvider to remove from it all network controller...
r388 auto tempFile = std::make_shared<QTemporaryFile>();
Alexandre Leroux
Amda provider (1)...
r378
Modify the AmdaProvider to remove from it all network controller...
r388 // LAMBDA
auto httpDownloadFinished = [this, tempFile](QNetworkReply *reply, QUuid dataId) noexcept {
Alexandre Leroux
Amda provider (2)...
r379
Modify the AmdaProvider to remove from it all network controller...
r388 if (tempFile) {
auto replyReadAll = reply->readAll();
if (!replyReadAll.isEmpty()) {
tempFile->write(replyReadAll);
}
tempFile->close();
Alexandre Leroux
Amda provider (3)...
r380
Modify the AmdaProvider to remove from it all network controller...
r388 // Parse results file
if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 emit dataProvided(impl->m_Token, dataSeries, impl->m_DateTime);
Modify the AmdaProvider to remove from it all network controller...
r388 }
else {
/// @todo ALX : debug
}
Alexandre Leroux
Amda provider (3)...
r380 }
Modify the AmdaProvider to remove from it all network controller...
r388 // Deletes reply
reply->deleteLater();
reply = nullptr;
};
auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
QUuid dataId) noexcept {
Alexandre Leroux
Amda provider (2)...
r379
Modify the AmdaProvider to remove from it all network controller...
r388 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
// Deletes old reply
reply->deleteLater();
Alexandre Leroux
Amda provider (2)...
r379
Modify the AmdaProvider to remove from it all network controller...
r388 // 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;
Alexandre Leroux
Updates IDataProvider::requestDataLoading() method's signature...
r408 impl->m_DateTime = dateTime;
Modify the AmdaProvider to remove from it all network controller...
r388 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
Alexandre Leroux
Inits Amda provider
r377 }