##// END OF EJS Templates
Amda provider update (2)...
Alexandre Leroux -
r380:0a12bde5fff2
parent child
Show More
@@ -1,28 +1,28
1 #ifndef SCIQLOP_AMDAPROVIDER_H
1 #ifndef SCIQLOP_AMDAPROVIDER_H
2 #define SCIQLOP_AMDAPROVIDER_H
2 #define SCIQLOP_AMDAPROVIDER_H
3
3
4 #include "AmdaGlobal.h"
4 #include "AmdaGlobal.h"
5
5
6 #include <Data/IDataProvider.h>
6 #include <Data/IDataProvider.h>
7
7
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9
9
10
10
11 Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaProvider)
11 Q_DECLARE_LOGGING_CATEGORY(LOG_AmdaProvider)
12
12
13 class QNetworkReply;
13 class QNetworkReply;
14
14
15 /**
15 /**
16 * @brief The AmdaProvider class is an example of how a data provider can generate data
16 * @brief The AmdaProvider class is an example of how a data provider can generate data
17 */
17 */
18 class SCIQLOP_AMDA_EXPORT AmdaProvider : public IDataProvider {
18 class SCIQLOP_AMDA_EXPORT AmdaProvider : public IDataProvider {
19 public:
19 public:
20 explicit AmdaProvider();
20 explicit AmdaProvider();
21
21
22 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
22 void requestDataLoading(QUuid token, const DataProviderParameters &parameters) override;
23
23
24 private:
24 private:
25 void retrieveData(QUuid token, const SqpDateTime &dateTime);
25 void retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data);
26 };
26 };
27
27
28 #endif // SCIQLOP_AMDAPROVIDER_H
28 #endif // SCIQLOP_AMDAPROVIDER_H
@@ -1,118 +1,126
1 #include "AmdaProvider.h"
1 #include "AmdaProvider.h"
2 #include "AmdaDefs.h"
2 #include "AmdaResultParser.h"
3 #include "AmdaResultParser.h"
3
4
4 #include <Data/DataProviderParameters.h>
5 #include <Data/DataProviderParameters.h>
5 #include <Network/NetworkController.h>
6 #include <Network/NetworkController.h>
6 #include <SqpApplication.h>
7 #include <SqpApplication.h>
7 #include <Variable/Variable.h>
8 #include <Variable/Variable.h>
8
9
9 #include <QNetworkAccessManager>
10 #include <QNetworkAccessManager>
10 #include <QNetworkReply>
11 #include <QNetworkReply>
11 #include <QTemporaryFile>
12 #include <QTemporaryFile>
12 #include <QThread>
13 #include <QThread>
13
14
14 Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
15 Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider")
15
16
16 namespace {
17 namespace {
17
18
18 /// URL format for a request on AMDA server. The parameters are as follows:
19 /// URL format for a request on AMDA server. The parameters are as follows:
19 /// - %1: start date
20 /// - %1: start date
20 /// - %2: end date
21 /// - %2: end date
21 /// - %3: parameter id
22 /// - %3: parameter id
22 const auto AMDA_URL_FORMAT = QStringLiteral(
23 const auto AMDA_URL_FORMAT = QStringLiteral(
23 "http://amda.irap.omp.eu/php/rest/"
24 "http://amda.irap.omp.eu/php/rest/"
24 "getParameter.php?startTime=%1&stopTime=%2&parameterID=%3&sampling=60&outputFormat=ASCII&"
25 "getParameter.php?startTime=%1&stopTime=%2&parameterID=%3&sampling=60&outputFormat=ASCII&"
25 "timeFormat=ISO8601&gzip=0");
26 "timeFormat=ISO8601&gzip=0");
26
27
27 /// Dates format passed in the URL (e.g 2013-09-23T09:00)
28 /// Dates format passed in the URL (e.g 2013-09-23T09:00)
28 const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss");
29 const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss");
29
30
30 /// Formats a time to a date that can be passed in URL
31 /// Formats a time to a date that can be passed in URL
31 QString dateFormat(double sqpDateTime) noexcept
32 QString dateFormat(double sqpDateTime) noexcept
32 {
33 {
33 auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.);
34 auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.);
34 return dateTime.toString(AMDA_TIME_FORMAT);
35 return dateTime.toString(AMDA_TIME_FORMAT);
35 }
36 }
36
37
37 } // namespace
38 } // namespace
38
39
39 AmdaProvider::AmdaProvider()
40 AmdaProvider::AmdaProvider()
40 {
41 {
41 qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider")
42 qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider")
42 << QThread::currentThread();
43 << QThread::currentThread();
43 if (auto app = sqpApp) {
44 if (auto app = sqpApp) {
44 auto &networkController = app->networkController();
45 auto &networkController = app->networkController();
45 connect(this, &AmdaProvider::requestConstructed, &networkController,
46 connect(this, &AmdaProvider::requestConstructed, &networkController,
46 &NetworkController::onProcessRequested);
47 &NetworkController::onProcessRequested);
47 }
48 }
48 }
49 }
49
50
50 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
51 void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters &parameters)
51 {
52 {
52 // NOTE: Try to use multithread if possible
53 // NOTE: Try to use multithread if possible
53 const auto times = parameters.m_Times;
54 const auto times = parameters.m_Times;
55 const auto data = parameters.m_Data;
54 for (const auto &dateTime : qAsConst(times)) {
56 for (const auto &dateTime : qAsConst(times)) {
55 retrieveData(token, dateTime);
57 retrieveData(token, dateTime, data);
56 }
58 }
57 }
59 }
58
60
59 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime)
61 void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data)
60 {
62 {
63 // Retrieves product ID from data: if the value is invalid, no request is made
64 auto productId = data.value(AMDA_XML_ID_KEY).toString();
65 if (productId.isNull()) {
66 qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id");
67 return;
68 }
69
61 // /////////// //
70 // /////////// //
62 // Creates URL //
71 // Creates URL //
63 // /////////// //
72 // /////////// //
64
73
65 auto startDate = dateFormat(dateTime.m_TStart);
74 auto startDate = dateFormat(dateTime.m_TStart);
66 auto endDate = dateFormat(dateTime.m_TEnd);
75 auto endDate = dateFormat(dateTime.m_TEnd);
67 auto productId = QStringLiteral("imf(0)");
68
76
69 auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
77 auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
70
78
71 auto tempFile = std::make_shared<QTemporaryFile>();
79 auto tempFile = std::make_shared<QTemporaryFile>();
72
80
73 // LAMBDA
81 // LAMBDA
74 auto httpDownloadFinished
82 auto httpDownloadFinished
75 = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept {
83 = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept {
76 Q_UNUSED(dataId);
84 Q_UNUSED(dataId);
77
85
78 if (tempFile) {
86 if (tempFile) {
79 auto replyReadAll = reply->readAll();
87 auto replyReadAll = reply->readAll();
80 if (!replyReadAll.isEmpty()) {
88 if (!replyReadAll.isEmpty()) {
81 tempFile->write(replyReadAll);
89 tempFile->write(replyReadAll);
82 }
90 }
83 tempFile->close();
91 tempFile->close();
84
92
85 // Parse results file
93 // Parse results file
86 if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
94 if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
87 emit dataProvided(token, dataSeries, dateTime);
95 emit dataProvided(token, dataSeries, dateTime);
88 }
96 }
89 else {
97 else {
90 /// @todo ALX : debug
98 /// @todo ALX : debug
91 }
99 }
92 }
100 }
93
101
94 // Deletes reply
102 // Deletes reply
95 reply->deleteLater();
103 reply->deleteLater();
96 reply = nullptr;
104 reply = nullptr;
97 };
105 };
98 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
106 auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply,
99 QUuid dataId) noexcept {
107 QUuid dataId) noexcept {
100
108
101 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
109 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
102 // Deletes old reply
110 // Deletes old reply
103 reply->deleteLater();
111 reply->deleteLater();
104
112
105 // Executes request for downloading file //
113 // Executes request for downloading file //
106
114
107 // Creates destination file
115 // Creates destination file
108 if (tempFile->open()) {
116 if (tempFile->open()) {
109 // Executes request
117 // Executes request
110 emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished);
118 emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished);
111 }
119 }
112 };
120 };
113
121
114 // //////////////// //
122 // //////////////// //
115 // Executes request //
123 // Executes request //
116 // //////////////// //
124 // //////////////// //
117 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
125 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
118 }
126 }
General Comments 0
You need to be logged in to leave comments. Login now