AmdaProvider.cpp
118 lines
| 3.7 KiB
| text/x-c
|
CppLexer
|
r377 | #include "AmdaProvider.h" | ||
|
r380 | #include "AmdaResultParser.h" | ||
|
r378 | #include <Data/DataProviderParameters.h> | ||
r388 | #include <Network/NetworkController.h> | |||
#include <SqpApplication.h> | ||||
#include <Variable/Variable.h> | ||||
|
r378 | |||
#include <QNetworkAccessManager> | ||||
#include <QNetworkReply> | ||||
|
r379 | #include <QTemporaryFile> | ||
r388 | #include <QThread> | |||
|
r377 | |||
Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") | ||||
|
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¶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); | ||||
} | ||||
} // namespace | ||||
|
r409 | AmdaProvider::AmdaProvider() | ||
|
r377 | { | ||
r388 | qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") | |||
<< QThread::currentThread(); | ||||
if (auto app = sqpApp) { | ||||
auto &networkController = app->networkController(); | ||||
connect(this, &AmdaProvider::requestConstructed, &networkController, | ||||
&NetworkController::onProcessRequested); | ||||
} | ||||
|
r377 | } | ||
|
r408 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) | ||
|
r377 | { | ||
// NOTE: Try to use multithread if possible | ||||
|
r408 | const auto times = parameters.m_Times; | ||
for (const auto &dateTime : qAsConst(times)) { | ||||
retrieveData(token, dateTime); | ||||
|
r377 | } | ||
} | ||||
|
r408 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime) | ||
|
r378 | { | ||
// /////////// // | ||||
// Creates URL // | ||||
// /////////// // | ||||
|
r408 | auto startDate = dateFormat(dateTime.m_TStart); | ||
auto endDate = dateFormat(dateTime.m_TEnd); | ||||
|
r378 | auto productId = QStringLiteral("imf(0)"); | ||
auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; | ||||
r388 | auto tempFile = std::make_shared<QTemporaryFile>(); | |||
|
r378 | |||
r388 | // LAMBDA | |||
|
r409 | auto httpDownloadFinished | ||
= [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { | ||||
Q_UNUSED(dataId); | ||||
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 | ||||
} | ||||
} | ||||
// Deletes reply | ||||
reply->deleteLater(); | ||||
reply = nullptr; | ||||
}; | ||||
r388 | auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, | |||
QUuid dataId) noexcept { | ||||
|
r379 | |||
r388 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; | |||
// Deletes old reply | ||||
reply->deleteLater(); | ||||
|
r379 | |||
r388 | // Executes request for downloading file // | |||
// Creates destination file | ||||
if (tempFile->open()) { | ||||
// Executes request | ||||
emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); | ||||
} | ||||
}; | ||||
// //////////////// // | ||||
// Executes request // | ||||
// //////////////// // | ||||
emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); | ||||
|
r377 | } | ||