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