@@ -1,138 +1,145 | |||||
1 | #include "AmdaProvider.h" |
|
1 | #include "AmdaProvider.h" | |
2 | #include "AmdaDefs.h" |
|
2 | #include "AmdaDefs.h" | |
3 | #include "AmdaResultParser.h" |
|
3 | #include "AmdaResultParser.h" | |
4 |
|
4 | |||
5 | #include <Data/DataProviderParameters.h> |
|
5 | #include <Data/DataProviderParameters.h> | |
6 | #include <Network/NetworkController.h> |
|
6 | #include <Network/NetworkController.h> | |
7 | #include <SqpApplication.h> |
|
7 | #include <SqpApplication.h> | |
8 | #include <Variable/Variable.h> |
|
8 | #include <Variable/Variable.h> | |
9 |
|
9 | |||
10 | #include <QNetworkAccessManager> |
|
10 | #include <QNetworkAccessManager> | |
11 | #include <QNetworkReply> |
|
11 | #include <QNetworkReply> | |
12 | #include <QTemporaryFile> |
|
12 | #include <QTemporaryFile> | |
13 | #include <QThread> |
|
13 | #include <QThread> | |
14 |
|
14 | |||
15 | Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") |
|
15 | Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") | |
16 |
|
16 | |||
17 | namespace { |
|
17 | namespace { | |
18 |
|
18 | |||
19 | /// 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: | |
20 | /// - %1: start date |
|
20 | /// - %1: start date | |
21 | /// - %2: end date |
|
21 | /// - %2: end date | |
22 | /// - %3: parameter id |
|
22 | /// - %3: parameter id | |
23 | const auto AMDA_URL_FORMAT = QStringLiteral( |
|
23 | const auto AMDA_URL_FORMAT = QStringLiteral( | |
24 | "http://amda.irap.omp.eu/php/rest/" |
|
24 | "http://amda.irap.omp.eu/php/rest/" | |
25 | "getParameter.php?startTime=%1&stopTime=%2¶meterID=%3&sampling=60&outputFormat=ASCII&" |
|
25 | "getParameter.php?startTime=%1&stopTime=%2¶meterID=%3&sampling=60&outputFormat=ASCII&" | |
26 | "timeFormat=ISO8601&gzip=0"); |
|
26 | "timeFormat=ISO8601&gzip=0"); | |
27 |
|
27 | |||
28 | /// 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) | |
29 | const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss"); |
|
29 | const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:ss"); | |
30 |
|
30 | |||
31 | /// 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 | |
32 | QString dateFormat(double sqpDateTime) noexcept |
|
32 | QString dateFormat(double sqpDateTime) noexcept | |
33 | { |
|
33 | { | |
34 | auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.); |
|
34 | auto dateTime = QDateTime::fromMSecsSinceEpoch(sqpDateTime * 1000.); | |
35 | return dateTime.toString(AMDA_TIME_FORMAT); |
|
35 | return dateTime.toString(AMDA_TIME_FORMAT); | |
36 | } |
|
36 | } | |
37 |
|
37 | |||
38 | } // namespace |
|
38 | } // namespace | |
39 |
|
39 | |||
40 | AmdaProvider::AmdaProvider() |
|
40 | AmdaProvider::AmdaProvider() | |
41 | { |
|
41 | { | |
42 | qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") |
|
42 | qCDebug(LOG_NetworkController()) << tr("AmdaProvider::AmdaProvider") | |
43 | << QThread::currentThread(); |
|
43 | << QThread::currentThread(); | |
44 | if (auto app = sqpApp) { |
|
44 | if (auto app = sqpApp) { | |
45 | auto &networkController = app->networkController(); |
|
45 | auto &networkController = app->networkController(); | |
46 | connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, |
|
46 | connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, | |
47 | std::function<void(QNetworkReply *, QUuid)>)), |
|
47 | std::function<void(QNetworkReply *, QUuid)>)), | |
48 | &networkController, |
|
48 | &networkController, | |
49 | SLOT(onProcessRequested(QNetworkRequest, QUuid, |
|
49 | SLOT(onProcessRequested(QNetworkRequest, QUuid, | |
50 | std::function<void(QNetworkReply *, QUuid)>))); |
|
50 | std::function<void(QNetworkReply *, QUuid)>))); | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this, |
|
53 | connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this, | |
54 | SIGNAL(dataProvidedProgress(QUuid, double))); |
|
54 | SIGNAL(dataProvidedProgress(QUuid, double))); | |
55 | } |
|
55 | } | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) |
|
58 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) | |
59 | { |
|
59 | { | |
60 | // NOTE: Try to use multithread if possible |
|
60 | // NOTE: Try to use multithread if possible | |
61 | const auto times = parameters.m_Times; |
|
61 | const auto times = parameters.m_Times; | |
62 | const auto data = parameters.m_Data; |
|
62 | const auto data = parameters.m_Data; | |
63 | for (const auto &dateTime : qAsConst(times)) { |
|
63 | for (const auto &dateTime : qAsConst(times)) { | |
64 | retrieveData(token, dateTime, data); |
|
64 | retrieveData(token, dateTime, data); | |
65 | } |
|
65 | } | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | void AmdaProvider::requestDataAborting(QUuid identifier) |
|
68 | void AmdaProvider::requestDataAborting(QUuid identifier) | |
69 | { |
|
69 | { | |
70 | if (auto app = sqpApp) { |
|
70 | if (auto app = sqpApp) { | |
71 | auto &networkController = app->networkController(); |
|
71 | auto &networkController = app->networkController(); | |
72 | networkController.onReplyCanceled(identifier); |
|
72 | networkController.onReplyCanceled(identifier); | |
73 | } |
|
73 | } | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) |
|
76 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) | |
77 | { |
|
77 | { | |
78 | // Retrieves product ID from data: if the value is invalid, no request is made |
|
78 | // Retrieves product ID from data: if the value is invalid, no request is made | |
79 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); |
|
79 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); | |
80 | if (productId.isNull()) { |
|
80 | if (productId.isNull()) { | |
81 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); |
|
81 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); | |
82 | return; |
|
82 | return; | |
83 | } |
|
83 | } | |
84 |
|
84 | |||
85 | // /////////// // |
|
85 | // /////////// // | |
86 | // Creates URL // |
|
86 | // Creates URL // | |
87 | // /////////// // |
|
87 | // /////////// // | |
88 |
|
88 | |||
89 | auto startDate = dateFormat(dateTime.m_TStart); |
|
89 | auto startDate = dateFormat(dateTime.m_TStart); | |
90 | auto endDate = dateFormat(dateTime.m_TEnd); |
|
90 | auto endDate = dateFormat(dateTime.m_TEnd); | |
91 |
|
91 | |||
92 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; |
|
92 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; | |
93 |
|
93 | |||
94 | auto tempFile = std::make_shared<QTemporaryFile>(); |
|
94 | auto tempFile = std::make_shared<QTemporaryFile>(); | |
95 |
|
95 | |||
96 | // LAMBDA |
|
96 | // LAMBDA | |
97 | auto httpDownloadFinished |
|
97 | auto httpDownloadFinished | |
98 | = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { |
|
98 | = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { | |
99 | Q_UNUSED(dataId); |
|
99 | Q_UNUSED(dataId); | |
100 |
|
100 | |||
|
101 | // Don't do anything if the reply was abort | |||
|
102 | if (reply->error() != QNetworkReply::OperationCanceledError) { | |||
|
103 | ||||
101 | if (tempFile) { |
|
104 | if (tempFile) { | |
102 | auto replyReadAll = reply->readAll(); |
|
105 | auto replyReadAll = reply->readAll(); | |
103 | if (!replyReadAll.isEmpty()) { |
|
106 | if (!replyReadAll.isEmpty()) { | |
104 | tempFile->write(replyReadAll); |
|
107 | tempFile->write(replyReadAll); | |
105 | } |
|
108 | } | |
106 | tempFile->close(); |
|
109 | tempFile->close(); | |
107 |
|
110 | |||
108 | // Parse results file |
|
111 | // Parse results file | |
109 | if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { |
|
112 | if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { | |
110 | emit dataProvided(token, dataSeries, dateTime); |
|
113 | emit dataProvided(token, dataSeries, dateTime); | |
111 | } |
|
114 | } | |
112 | else { |
|
115 | else { | |
113 | /// @todo ALX : debug |
|
116 | /// @todo ALX : debug | |
114 | } |
|
117 | } | |
115 | } |
|
118 | } | |
116 |
|
119 | } | ||
117 |
|
120 | |||
118 | }; |
|
121 | }; | |
119 | auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, |
|
122 | auto httpFinishedLambda | |
120 | QUuid dataId) noexcept { |
|
123 | = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { | |
121 |
|
124 | |||
|
125 | // Don't do anything if the reply was abort | |||
|
126 | if (reply->error() != QNetworkReply::OperationCanceledError) { | |||
122 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; |
|
127 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; | |
123 |
|
128 | |||
124 |
|
129 | |||
125 | // Executes request for downloading file // |
|
130 | // Executes request for downloading file // | |
126 |
|
131 | |||
127 | // Creates destination file |
|
132 | // Creates destination file | |
128 | if (tempFile->open()) { |
|
133 | if (tempFile->open()) { | |
129 | // Executes request |
|
134 | // Executes request | |
130 |
emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, |
|
135 | emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, | |
|
136 | httpDownloadFinished); | |||
|
137 | } | |||
131 | } |
|
138 | } | |
132 | }; |
|
139 | }; | |
133 |
|
140 | |||
134 | // //////////////// // |
|
141 | // //////////////// // | |
135 | // Executes request // |
|
142 | // Executes request // | |
136 | // //////////////// // |
|
143 | // //////////////// // | |
137 | emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); |
|
144 | emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); | |
138 | } |
|
145 | } |
General Comments 0
You need to be logged in to leave comments.
Login now