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