@@ -1,126 +1,129 | |||||
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, |
|
46 | connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, | |
47 | &NetworkController::onProcessRequested); |
|
47 | std::function<void(QNetworkReply *, QUuid)>)), | |
|
48 | &networkController, | |||
|
49 | SLOT(onProcessRequested(QNetworkRequest, QUuid, | |||
|
50 | std::function<void(QNetworkReply *, QUuid)>))); | |||
48 | } |
|
51 | } | |
49 | } |
|
52 | } | |
50 |
|
53 | |||
51 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) |
|
54 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) | |
52 | { |
|
55 | { | |
53 | // NOTE: Try to use multithread if possible |
|
56 | // NOTE: Try to use multithread if possible | |
54 | const auto times = parameters.m_Times; |
|
57 | const auto times = parameters.m_Times; | |
55 | const auto data = parameters.m_Data; |
|
58 | const auto data = parameters.m_Data; | |
56 | for (const auto &dateTime : qAsConst(times)) { |
|
59 | for (const auto &dateTime : qAsConst(times)) { | |
57 | retrieveData(token, dateTime, data); |
|
60 | retrieveData(token, dateTime, data); | |
58 | } |
|
61 | } | |
59 | } |
|
62 | } | |
60 |
|
63 | |||
61 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) |
|
64 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) | |
62 | { |
|
65 | { | |
63 | // Retrieves product ID from data: if the value is invalid, no request is made |
|
66 | // Retrieves product ID from data: if the value is invalid, no request is made | |
64 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); |
|
67 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); | |
65 | if (productId.isNull()) { |
|
68 | if (productId.isNull()) { | |
66 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); |
|
69 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); | |
67 | return; |
|
70 | return; | |
68 | } |
|
71 | } | |
69 |
|
72 | |||
70 | // /////////// // |
|
73 | // /////////// // | |
71 | // Creates URL // |
|
74 | // Creates URL // | |
72 | // /////////// // |
|
75 | // /////////// // | |
73 |
|
76 | |||
74 | auto startDate = dateFormat(dateTime.m_TStart); |
|
77 | auto startDate = dateFormat(dateTime.m_TStart); | |
75 | auto endDate = dateFormat(dateTime.m_TEnd); |
|
78 | auto endDate = dateFormat(dateTime.m_TEnd); | |
76 |
|
79 | |||
77 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; |
|
80 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; | |
78 |
|
81 | |||
79 | auto tempFile = std::make_shared<QTemporaryFile>(); |
|
82 | auto tempFile = std::make_shared<QTemporaryFile>(); | |
80 |
|
83 | |||
81 | // LAMBDA |
|
84 | // LAMBDA | |
82 | auto httpDownloadFinished |
|
85 | auto httpDownloadFinished | |
83 | = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { |
|
86 | = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { | |
84 | Q_UNUSED(dataId); |
|
87 | Q_UNUSED(dataId); | |
85 |
|
88 | |||
86 | if (tempFile) { |
|
89 | if (tempFile) { | |
87 | auto replyReadAll = reply->readAll(); |
|
90 | auto replyReadAll = reply->readAll(); | |
88 | if (!replyReadAll.isEmpty()) { |
|
91 | if (!replyReadAll.isEmpty()) { | |
89 | tempFile->write(replyReadAll); |
|
92 | tempFile->write(replyReadAll); | |
90 | } |
|
93 | } | |
91 | tempFile->close(); |
|
94 | tempFile->close(); | |
92 |
|
95 | |||
93 | // Parse results file |
|
96 | // Parse results file | |
94 | if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { |
|
97 | if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { | |
95 | emit dataProvided(token, dataSeries, dateTime); |
|
98 | emit dataProvided(token, dataSeries, dateTime); | |
96 | } |
|
99 | } | |
97 | else { |
|
100 | else { | |
98 | /// @todo ALX : debug |
|
101 | /// @todo ALX : debug | |
99 | } |
|
102 | } | |
100 | } |
|
103 | } | |
101 |
|
104 | |||
102 | // Deletes reply |
|
105 | // Deletes reply | |
103 | reply->deleteLater(); |
|
106 | reply->deleteLater(); | |
104 | reply = nullptr; |
|
107 | reply = nullptr; | |
105 | }; |
|
108 | }; | |
106 | auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, |
|
109 | auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, | |
107 | QUuid dataId) noexcept { |
|
110 | QUuid dataId) noexcept { | |
108 |
|
111 | |||
109 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; |
|
112 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; | |
110 | // Deletes old reply |
|
113 | // Deletes old reply | |
111 | reply->deleteLater(); |
|
114 | reply->deleteLater(); | |
112 |
|
115 | |||
113 | // Executes request for downloading file // |
|
116 | // Executes request for downloading file // | |
114 |
|
117 | |||
115 | // Creates destination file |
|
118 | // Creates destination file | |
116 | if (tempFile->open()) { |
|
119 | if (tempFile->open()) { | |
117 | // Executes request |
|
120 | // Executes request | |
118 | emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); |
|
121 | emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); | |
119 | } |
|
122 | } | |
120 | }; |
|
123 | }; | |
121 |
|
124 | |||
122 | // //////////////// // |
|
125 | // //////////////// // | |
123 | // Executes request // |
|
126 | // Executes request // | |
124 | // //////////////// // |
|
127 | // //////////////// // | |
125 | emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); |
|
128 | emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); | |
126 | } |
|
129 | } |
General Comments 0
You need to be logged in to leave comments.
Login now