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