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