AmdaProvider.cpp
88 lines
| 2.8 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
|
r1115 | #include "AmdaServer.h" | ||
Alexandre Leroux
|
r380 | |||
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> | |||
r1351 | #include <QJsonDocument> | |||
#include <Network/Downloader.h> | ||||
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: | ||||
Alexandre Leroux
|
r943 | /// - %1: server URL | ||
/// - %2: start date | ||||
/// - %3: end date | ||||
/// - %4: parameter id | ||||
Alexandre Leroux
|
r790 | /// AMDA V2: http://amdatest.irap.omp.eu/php/rest/ | ||
Alexandre Leroux
|
r378 | const auto AMDA_URL_FORMAT = QStringLiteral( | ||
Alexandre Leroux
|
r943 | "http://%1/php/rest/" | ||
"getParameter.php?startTime=%2&stopTime=%3¶meterID=%4&outputFormat=ASCII&" | ||||
Alexandre Leroux
|
r378 | "timeFormat=ISO8601&gzip=0"); | ||
r1351 | const auto AMDA_URL_FORMAT_WITH_TOKEN = QStringLiteral( | |||
"http://%1/php/rest/" | ||||
"getParameter.php?startTime=%2&stopTime=%3¶meterID=%4&outputFormat=ASCII&" | ||||
"timeFormat=ISO8601&gzip=0&" | ||||
"token=%5"); | ||||
const auto AMDA_TOKEN_URL_FORMAT = QStringLiteral( | ||||
"http://%1/php/rest/" | ||||
"auth.php"); | ||||
Alexandre Leroux
|
r378 | /// 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); | ||
} | ||||
Alexandre Leroux
|
r566 | |||
Alexandre Leroux
|
r378 | } // namespace | ||
Alexandre Leroux
|
r409 | AmdaProvider::AmdaProvider() | ||
Alexandre Leroux
|
r377 | { | ||
r425 | ||||
Alexandre Leroux
|
r377 | } | ||
Alexandre Leroux
|
r712 | std::shared_ptr<IDataProvider> AmdaProvider::clone() const | ||
{ | ||||
// No copy is made in the clone | ||||
return std::make_shared<AmdaProvider>(); | ||||
} | ||||
r1351 | IDataSeries* AmdaProvider::getData(const DataProviderParameters ¶meters) | |||
Alexandre Leroux
|
r378 | { | ||
r1351 | auto range = parameters.m_Times.front(); | |||
auto metaData = parameters.m_Data; | ||||
auto productId = metaData.value(AMDA_XML_ID_KEY).toString(); | ||||
Alexandre Leroux
|
r1278 | auto productValueType | ||
r1351 | = DataSeriesTypeUtils::fromString(metaData.value(AMDA_DATA_TYPE_KEY).toString()); | |||
auto startDate = dateFormat(range.m_TStart); | ||||
auto endDate = dateFormat(range.m_TEnd); | ||||
QVariantHash urlProperties{{AMDA_SERVER_KEY, metaData.value(AMDA_SERVER_KEY)}}; | ||||
auto token_url = QString{AMDA_TOKEN_URL_FORMAT}.arg(AmdaServer::instance().url(urlProperties)); | ||||
auto response = Downloader::get(token_url); | ||||
auto url = QString{AMDA_URL_FORMAT_WITH_TOKEN}.arg(AmdaServer::instance().url(urlProperties), | ||||
startDate, endDate, productId, QString(response.data())); | ||||
response = Downloader::get(url); | ||||
auto test = QJsonDocument::fromJson(response.data()); | ||||
url = test["dataFileURLs"].toString(); | ||||
response = Downloader::get(url); | ||||
return AmdaResultParser::readTxt(QTextStream{response.data()},productValueType); | ||||
r750 | } | |||