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