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