##// END OF EJS Templates
Reads variable metadata to get the data type...
Alexandre Leroux -
r566:a6f250cc335f
parent child
Show More
@@ -1,16 +1,17
1 1 #ifndef SCIQLOP_AMDADEFS_H
2 2 #define SCIQLOP_AMDADEFS_H
3 3
4 4 #include <QString>
5 5
6 6 // ////////////// //
7 7 // AMDA constants //
8 8 // ////////////// //
9 9
10 10 // Relevant keys in JSON file
11 11 extern const QString AMDA_COMPONENT_KEY;
12 extern const QString AMDA_DATA_TYPE_KEY;
12 13 extern const QString AMDA_PRODUCT_KEY;
13 14 extern const QString AMDA_ROOT_KEY;
14 15 extern const QString AMDA_XML_ID_KEY;
15 16
16 17 #endif // SCIQLOP_AMDADEFS_H
@@ -1,6 +1,7
1 1 #include "AmdaDefs.h"
2 2
3 3 const QString AMDA_COMPONENT_KEY = QStringLiteral("component");
4 const QString AMDA_DATA_TYPE_KEY = QStringLiteral("dataType");
4 5 const QString AMDA_PRODUCT_KEY = QStringLiteral("parameter");
5 6 const QString AMDA_ROOT_KEY = QStringLiteral("dataCenter");
6 7 const QString AMDA_XML_ID_KEY = QStringLiteral("xml:id");
@@ -1,150 +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&parameterID=%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 AmdaResultParser::ValueType valueType(const QString &valueType)
40 {
41 if (valueType == QStringLiteral("scalar")) {
42 return AmdaResultParser::ValueType::SCALAR;
43 }
44 else if (valueType == QStringLiteral("vector")) {
45 return AmdaResultParser::ValueType::VECTOR;
46 }
47 else {
48 return AmdaResultParser::ValueType::UNKNOWN;
49 }
50 }
51
39 52 } // namespace
40 53
41 54 AmdaProvider::AmdaProvider()
42 55 {
43 56 qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::AmdaProvider") << QThread::currentThread();
44 57 if (auto app = sqpApp) {
45 58 auto &networkController = app->networkController();
46 59 connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid,
47 60 std::function<void(QNetworkReply *, QUuid)>)),
48 61 &networkController,
49 62 SLOT(onProcessRequested(QNetworkRequest, QUuid,
50 63 std::function<void(QNetworkReply *, QUuid)>)));
51 64
52 65
53 66 connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this,
54 67 SIGNAL(dataProvidedProgress(QUuid, double)));
55 68 }
56 69 }
57 70
58 71 void AmdaProvider::requestDataLoading(QUuid acqIdentifier, const DataProviderParameters &parameters)
59 72 {
60 73 // NOTE: Try to use multithread if possible
61 74 const auto times = parameters.m_Times;
62 75 const auto data = parameters.m_Data;
63 76 for (const auto &dateTime : qAsConst(times)) {
64 77 this->retrieveData(acqIdentifier, dateTime, data);
65 78
66 79 // TORM
67 80 // QThread::msleep(200);
68 81 }
69 82 }
70 83
71 84 void AmdaProvider::requestDataAborting(QUuid acqIdentifier)
72 85 {
73 86 if (auto app = sqpApp) {
74 87 auto &networkController = app->networkController();
75 88 networkController.onReplyCanceled(acqIdentifier);
76 89 }
77 90 }
78 91
79 92 void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVariantHash &data)
80 93 {
81 94 // Retrieves product ID from data: if the value is invalid, no request is made
82 95 auto productId = data.value(AMDA_XML_ID_KEY).toString();
83 96 if (productId.isNull()) {
84 97 qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id");
85 98 return;
86 99 }
87 100 qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData") << dateTime;
88 101
102 // Retrieves the data type that determines whether the expected format for the result file is
103 // scalar, vector...
104 auto productValueType = valueType(data.value(AMDA_DATA_TYPE_KEY).toString());
105
89 106 // /////////// //
90 107 // Creates URL //
91 108 // /////////// //
92 109
93 110 auto startDate = dateFormat(dateTime.m_TStart);
94 111 auto endDate = dateFormat(dateTime.m_TEnd);
95 112
96 113 auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)};
97 114 qCInfo(LOG_AmdaProvider()) << tr("TORM AmdaProvider::retrieveData url:") << url;
98 115 auto tempFile = std::make_shared<QTemporaryFile>();
99 116
100 117 // LAMBDA
101 auto httpDownloadFinished
102 = [this, dateTime, tempFile](QNetworkReply *reply, QUuid dataId) noexcept {
103
104 // Don't do anything if the reply was abort
105 if (reply->error() != QNetworkReply::OperationCanceledError) {
106
107 if (tempFile) {
108 auto replyReadAll = reply->readAll();
109 if (!replyReadAll.isEmpty()) {
110 tempFile->write(replyReadAll);
111 }
112 tempFile->close();
113
114 // Parse results file
115 if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) {
116 emit dataProvided(dataId, dataSeries, dateTime);
117 }
118 else {
119 /// @todo ALX : debug
120 }
121 }
122 }
123
124 };
118 auto httpDownloadFinished = [this, dateTime, tempFile,
119 productValueType](QNetworkReply *reply, QUuid dataId) noexcept {
120
121 // Don't do anything if the reply was abort
122 if (reply->error() != QNetworkReply::OperationCanceledError) {
123
124 if (tempFile) {
125 auto replyReadAll = reply->readAll();
126 if (!replyReadAll.isEmpty()) {
127 tempFile->write(replyReadAll);
128 }
129 tempFile->close();
130
131 // Parse results file
132 if (auto dataSeries
133 = AmdaResultParser::readTxt(tempFile->fileName(), productValueType)) {
134 emit dataProvided(dataId, dataSeries, dateTime);
135 }
136 else {
137 /// @todo ALX : debug
138 }
139 }
140 }
141
142 };
125 143 auto httpFinishedLambda
126 144 = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept {
127 145
128 146 // Don't do anything if the reply was abort
129 147 if (reply->error() != QNetworkReply::OperationCanceledError) {
130 148 auto downloadFileUrl = QUrl{QString{reply->readAll()}};
131 149
132 150
133 151 qCInfo(LOG_AmdaProvider())
134 152 << tr("TORM AmdaProvider::retrieveData downloadFileUrl:") << downloadFileUrl;
135 153 // Executes request for downloading file //
136 154
137 155 // Creates destination file
138 156 if (tempFile->open()) {
139 157 // Executes request
140 158 emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId,
141 159 httpDownloadFinished);
142 160 }
143 161 }
144 162 };
145 163
146 164 // //////////////// //
147 165 // Executes request //
148 166 // //////////////// //
149 167 emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda);
150 168 }
General Comments 0
You need to be logged in to leave comments. Login now