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