@@ -1,64 +1,70 | |||
|
1 | 1 | #ifndef SCIQLOP_IDATAPROVIDER_H |
|
2 | 2 | #define SCIQLOP_IDATAPROVIDER_H |
|
3 | 3 | |
|
4 | 4 | #include <memory> |
|
5 | 5 | |
|
6 | 6 | #include <QObject> |
|
7 | 7 | #include <QUuid> |
|
8 | 8 | |
|
9 | 9 | #include <Common/MetaTypes.h> |
|
10 | 10 | |
|
11 | 11 | #include <Data/SqpDateTime.h> |
|
12 | 12 | |
|
13 | 13 | class DataProviderParameters; |
|
14 | 14 | class IDataSeries; |
|
15 | 15 | class QNetworkReply; |
|
16 | 16 | class QNetworkRequest; |
|
17 | 17 | |
|
18 | 18 | /** |
|
19 | 19 | * @brief The IDataProvider interface aims to declare a data provider. |
|
20 | 20 | * |
|
21 | 21 | * A data provider is an entity that generates data and returns it according to various parameters |
|
22 | 22 | * (time interval, product to retrieve the data, etc.) |
|
23 | 23 | * |
|
24 | 24 | * @sa IDataSeries |
|
25 | 25 | */ |
|
26 | 26 | class IDataProvider : public QObject { |
|
27 | 27 | |
|
28 | 28 | Q_OBJECT |
|
29 | 29 | public: |
|
30 | 30 | virtual ~IDataProvider() noexcept = default; |
|
31 | 31 | |
|
32 | 32 | /** |
|
33 | 33 | * @brief requestDataLoading provide datas for the data identified by identifier and parameters |
|
34 | 34 | */ |
|
35 | 35 | virtual void requestDataLoading(QUuid identifier, const DataProviderParameters ¶meters) = 0; |
|
36 | 36 | |
|
37 | 37 | /** |
|
38 | 38 | * @brief requestDataAborting stop data loading of the data identified by identifier |
|
39 | 39 | */ |
|
40 | 40 | virtual void requestDataAborting(QUuid identifier) = 0; |
|
41 | 41 | |
|
42 | 42 | signals: |
|
43 | 43 | /** |
|
44 | 44 | * @brief dataProvided send dataSeries under dateTime and that corresponds of the data |
|
45 | 45 | * identified by identifier |
|
46 | 46 | */ |
|
47 | 47 | void dataProvided(QUuid identifier, std::shared_ptr<IDataSeries> dateSerie, |
|
48 | 48 | const SqpDateTime &dateTime); |
|
49 | 49 | |
|
50 | /** | |
|
51 | * @brief dataProvided send dataSeries under dateTime and that corresponds of the data | |
|
52 | * identified by identifier | |
|
53 | */ | |
|
54 | void dataProvidedProgress(QUuid identifier, double progress); | |
|
55 | ||
|
50 | 56 | |
|
51 | 57 | /** |
|
52 | 58 | * @brief requestConstructed send a request for the data identified by identifier |
|
53 | 59 | * @callback is the methode call by the reply of the request when it is finished. |
|
54 | 60 | */ |
|
55 | 61 | void requestConstructed(const QNetworkRequest &request, QUuid identifier, |
|
56 | 62 | std::function<void(QNetworkReply *, QUuid)> callback); |
|
57 | 63 | }; |
|
58 | 64 | |
|
59 | 65 | // Required for using shared_ptr in signals/slots |
|
60 | 66 | SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_PTR_REGISTRY, std::shared_ptr<IDataProvider>) |
|
61 | 67 | SCIQLOP_REGISTER_META_TYPE(IDATAPROVIDER_FUNCTION_REGISTRY, |
|
62 | 68 | std::function<void(QNetworkReply *, QUuid)>) |
|
63 | 69 | |
|
64 | 70 | #endif // SCIQLOP_IDATAPROVIDER_H |
@@ -1,238 +1,240 | |||
|
1 | 1 | #include <Variable/Variable.h> |
|
2 | 2 | #include <Variable/VariableCacheController.h> |
|
3 | 3 | #include <Variable/VariableController.h> |
|
4 | 4 | #include <Variable/VariableModel.h> |
|
5 | 5 | |
|
6 | 6 | #include <Data/DataProviderParameters.h> |
|
7 | 7 | #include <Data/IDataProvider.h> |
|
8 | 8 | #include <Data/IDataSeries.h> |
|
9 | 9 | #include <Time/TimeController.h> |
|
10 | 10 | |
|
11 | 11 | #include <QDateTime> |
|
12 | 12 | #include <QMutex> |
|
13 | 13 | #include <QThread> |
|
14 | 14 | #include <QUuid> |
|
15 | 15 | #include <QtCore/QItemSelectionModel> |
|
16 | 16 | |
|
17 | 17 | #include <unordered_map> |
|
18 | 18 | |
|
19 | 19 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") |
|
20 | 20 | |
|
21 | 21 | struct VariableController::VariableControllerPrivate { |
|
22 | 22 | explicit VariableControllerPrivate(VariableController *parent) |
|
23 | 23 | : m_WorkingMutex{}, |
|
24 | 24 | m_VariableModel{new VariableModel{parent}}, |
|
25 | 25 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, |
|
26 | 26 | m_VariableCacheController{std::make_unique<VariableCacheController>()} |
|
27 | 27 | { |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | QMutex m_WorkingMutex; |
|
31 | 31 | /// Variable model. The VariableController has the ownership |
|
32 | 32 | VariableModel *m_VariableModel; |
|
33 | 33 | QItemSelectionModel *m_VariableSelectionModel; |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | TimeController *m_TimeController{nullptr}; |
|
37 | 37 | std::unique_ptr<VariableCacheController> m_VariableCacheController; |
|
38 | 38 | |
|
39 | 39 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
40 | 40 | m_VariableToProviderMap; |
|
41 | 41 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; |
|
42 | 42 | }; |
|
43 | 43 | |
|
44 | 44 | VariableController::VariableController(QObject *parent) |
|
45 | 45 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
46 | 46 | { |
|
47 | 47 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
48 | 48 | << QThread::currentThread(); |
|
49 | 49 | |
|
50 | 50 | connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, |
|
51 | 51 | &VariableController::onAbortProgressRequested); |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | VariableController::~VariableController() |
|
55 | 55 | { |
|
56 | 56 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
57 | 57 | << QThread::currentThread(); |
|
58 | 58 | this->waitForFinish(); |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | VariableModel *VariableController::variableModel() noexcept |
|
62 | 62 | { |
|
63 | 63 | return impl->m_VariableModel; |
|
64 | 64 | } |
|
65 | 65 | |
|
66 | 66 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept |
|
67 | 67 | { |
|
68 | 68 | return impl->m_VariableSelectionModel; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
72 | 72 | { |
|
73 | 73 | impl->m_TimeController = timeController; |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
77 | 77 | { |
|
78 | 78 | if (!variable) { |
|
79 | 79 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; |
|
80 | 80 | return; |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can |
|
84 | 84 | // make some treatments before the deletion |
|
85 | 85 | emit variableAboutToBeDeleted(variable); |
|
86 | 86 | |
|
87 | 87 | // Deletes identifier |
|
88 | 88 | impl->m_VariableToIdentifierMap.erase(variable); |
|
89 | 89 | |
|
90 | 90 | // Deletes provider |
|
91 | 91 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); |
|
92 | 92 | qCDebug(LOG_VariableController()) |
|
93 | 93 | << tr("Number of providers deleted for variable %1: %2") |
|
94 | 94 | .arg(variable->name(), QString::number(nbProvidersDeleted)); |
|
95 | 95 | |
|
96 | 96 | // Clears cache |
|
97 | 97 | impl->m_VariableCacheController->clear(variable); |
|
98 | 98 | |
|
99 | 99 | // Deletes from model |
|
100 | 100 | impl->m_VariableModel->deleteVariable(variable); |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | void VariableController::deleteVariables( |
|
104 | 104 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
105 | 105 | { |
|
106 | 106 | for (auto variable : qAsConst(variables)) { |
|
107 | 107 | deleteVariable(variable); |
|
108 | 108 | } |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | void VariableController::abortProgress(std::shared_ptr<Variable> variable) |
|
112 | 112 | { |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | void VariableController::createVariable(const QString &name, const QVariantHash &metadata, |
|
116 | 116 | std::shared_ptr<IDataProvider> provider) noexcept |
|
117 | 117 | { |
|
118 | 118 | |
|
119 | 119 | if (!impl->m_TimeController) { |
|
120 | 120 | qCCritical(LOG_VariableController()) |
|
121 | 121 | << tr("Impossible to create variable: The time controller is null"); |
|
122 | 122 | return; |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | auto dateTime = impl->m_TimeController->dateTime(); |
|
126 | 126 | |
|
127 | 127 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { |
|
128 | 128 | auto identifier = QUuid::createUuid(); |
|
129 | 129 | |
|
130 | 130 | // store the provider |
|
131 | 131 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
132 | 132 | impl->m_VariableToIdentifierMap[newVariable] = identifier; |
|
133 | 133 | |
|
134 | 134 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( |
|
135 | 135 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) |
|
136 | 136 | { |
|
137 | 137 | if (auto variable = varW.lock()) { |
|
138 | 138 | auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable); |
|
139 | 139 | if (varIdentifier == identifier) { |
|
140 | 140 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); |
|
141 | 141 | variable->setDataSeries(dataSeriesAcquired); |
|
142 | 142 | } |
|
143 | 143 | } |
|
144 | 144 | }; |
|
145 | 145 | |
|
146 | 146 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
147 | connect(provider.get(), &IDataProvider::dataProvidedProgress, this, | |
|
148 | &VariableController::onVariableRetrieveDataInProgress); | |
|
147 | 149 | this->onRequestDataLoading(newVariable, dateTime); |
|
148 | 150 | } |
|
149 | 151 | } |
|
150 | 152 | |
|
151 | 153 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
152 | 154 | { |
|
153 | 155 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" |
|
154 | 156 | << QThread::currentThread()->objectName(); |
|
155 | 157 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
156 | 158 | |
|
157 | 159 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
158 | 160 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
159 | 161 | selectedVariable->setDateTime(dateTime); |
|
160 | 162 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
161 | 163 | } |
|
162 | 164 | } |
|
163 | 165 | } |
|
164 | 166 | |
|
165 | 167 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) |
|
166 | 168 | { |
|
167 | 169 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; |
|
168 | 170 | |
|
169 | 171 | auto end = impl->m_VariableToIdentifierMap.cend(); |
|
170 | 172 | auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply); |
|
171 | 173 | if (it != end) { |
|
172 | 174 | impl->m_VariableModel->setDataProgress(it->first, progress); |
|
173 | 175 | } |
|
174 | 176 | } |
|
175 | 177 | |
|
176 | 178 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) |
|
177 | 179 | { |
|
178 | 180 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested" |
|
179 | 181 | << QThread::currentThread()->objectName(); |
|
180 | 182 | |
|
181 | 183 | auto it = impl->m_VariableToIdentifierMap.find(variable); |
|
182 | 184 | if (it != impl->m_VariableToIdentifierMap.cend()) { |
|
183 | 185 | impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second); |
|
184 | 186 | } |
|
185 | 187 | else { |
|
186 | 188 | qCWarning(LOG_VariableController()) |
|
187 | 189 | << tr("Aborting progression of inexistant variable detected !!!") |
|
188 | 190 | << QThread::currentThread()->objectName(); |
|
189 | 191 | } |
|
190 | 192 | } |
|
191 | 193 | |
|
192 | 194 | |
|
193 | 195 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
194 | 196 | const SqpDateTime &dateTime) |
|
195 | 197 | { |
|
196 | 198 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" |
|
197 | 199 | << QThread::currentThread()->objectName(); |
|
198 | 200 | // we want to load data of the variable for the dateTime. |
|
199 | 201 | // First we check if the cache contains some of them. |
|
200 | 202 | // For the other, we ask the provider to give them. |
|
201 | 203 | if (variable) { |
|
202 | 204 | |
|
203 | 205 | auto dateTimeListNotInCache |
|
204 | 206 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
205 | 207 | |
|
206 | 208 | if (!dateTimeListNotInCache.empty()) { |
|
207 | 209 | // Ask the provider for each data on the dateTimeListNotInCache |
|
208 | 210 | auto identifier = impl->m_VariableToIdentifierMap.at(variable); |
|
209 | 211 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
210 | 212 | identifier, |
|
211 | 213 | DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); |
|
212 | 214 | } |
|
213 | 215 | else { |
|
214 | 216 | emit variable->updated(); |
|
215 | 217 | } |
|
216 | 218 | } |
|
217 | 219 | else { |
|
218 | 220 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
219 | 221 | } |
|
220 | 222 | } |
|
221 | 223 | |
|
222 | 224 | |
|
223 | 225 | void VariableController::initialize() |
|
224 | 226 | { |
|
225 | 227 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
226 | 228 | impl->m_WorkingMutex.lock(); |
|
227 | 229 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
228 | 230 | } |
|
229 | 231 | |
|
230 | 232 | void VariableController::finalize() |
|
231 | 233 | { |
|
232 | 234 | impl->m_WorkingMutex.unlock(); |
|
233 | 235 | } |
|
234 | 236 | |
|
235 | 237 | void VariableController::waitForFinish() |
|
236 | 238 | { |
|
237 | 239 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
238 | 240 | } |
@@ -1,147 +1,142 | |||
|
1 | 1 | #include "SqpApplication.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/IDataProvider.h> |
|
4 | 4 | #include <DataSource/DataSourceController.h> |
|
5 | 5 | #include <Network/NetworkController.h> |
|
6 | 6 | #include <QThread> |
|
7 | 7 | #include <Time/TimeController.h> |
|
8 | 8 | #include <Variable/Variable.h> |
|
9 | 9 | #include <Variable/VariableController.h> |
|
10 | 10 | #include <Visualization/VisualizationController.h> |
|
11 | 11 | |
|
12 | 12 | Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication") |
|
13 | 13 | |
|
14 | 14 | class SqpApplication::SqpApplicationPrivate { |
|
15 | 15 | public: |
|
16 | 16 | SqpApplicationPrivate() |
|
17 | 17 | : m_DataSourceController{std::make_unique<DataSourceController>()}, |
|
18 | 18 | m_NetworkController{std::make_unique<NetworkController>()}, |
|
19 | 19 | m_TimeController{std::make_unique<TimeController>()}, |
|
20 | 20 | m_VariableController{std::make_unique<VariableController>()}, |
|
21 | 21 | m_VisualizationController{std::make_unique<VisualizationController>()} |
|
22 | 22 | { |
|
23 | 23 | // /////////////////////////////// // |
|
24 | 24 | // Connections between controllers // |
|
25 | 25 | // /////////////////////////////// // |
|
26 | 26 | |
|
27 | 27 | // VariableController <-> DataSourceController |
|
28 | 28 | connect(m_DataSourceController.get(), |
|
29 | 29 | SIGNAL(variableCreationRequested(const QString &, const QVariantHash &, |
|
30 | 30 | std::shared_ptr<IDataProvider>)), |
|
31 | 31 | m_VariableController.get(), |
|
32 | 32 | SLOT(createVariable(const QString &, const QVariantHash &, |
|
33 | 33 | std::shared_ptr<IDataProvider>))); |
|
34 | 34 | |
|
35 | 35 | // VariableController <-> VisualizationController |
|
36 | 36 | connect(m_VariableController.get(), |
|
37 | 37 | SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), |
|
38 | 38 | m_VisualizationController.get(), |
|
39 | 39 | SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection); |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | m_DataSourceController->moveToThread(&m_DataSourceControllerThread); |
|
43 | 43 | m_NetworkController->moveToThread(&m_NetworkControllerThread); |
|
44 | 44 | m_VariableController->moveToThread(&m_VariableControllerThread); |
|
45 | 45 | m_VisualizationController->moveToThread(&m_VisualizationControllerThread); |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | // Additionnal init |
|
49 | 49 | m_VariableController->setTimeController(m_TimeController.get()); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | virtual ~SqpApplicationPrivate() |
|
53 | 53 | { |
|
54 | 54 | qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction"); |
|
55 | 55 | m_DataSourceControllerThread.quit(); |
|
56 | 56 | m_DataSourceControllerThread.wait(); |
|
57 | 57 | |
|
58 | 58 | m_NetworkControllerThread.quit(); |
|
59 | 59 | m_NetworkControllerThread.wait(); |
|
60 | 60 | |
|
61 | 61 | m_VariableControllerThread.quit(); |
|
62 | 62 | m_VariableControllerThread.wait(); |
|
63 | 63 | |
|
64 | 64 | m_VisualizationControllerThread.quit(); |
|
65 | 65 | m_VisualizationControllerThread.wait(); |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | std::unique_ptr<DataSourceController> m_DataSourceController; |
|
69 | 69 | std::unique_ptr<VariableController> m_VariableController; |
|
70 | 70 | std::unique_ptr<TimeController> m_TimeController; |
|
71 | 71 | std::unique_ptr<NetworkController> m_NetworkController; |
|
72 | 72 | std::unique_ptr<VisualizationController> m_VisualizationController; |
|
73 | 73 | QThread m_DataSourceControllerThread; |
|
74 | 74 | QThread m_NetworkControllerThread; |
|
75 | 75 | QThread m_VariableControllerThread; |
|
76 | 76 | QThread m_VisualizationControllerThread; |
|
77 | 77 | }; |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | SqpApplication::SqpApplication(int &argc, char **argv) |
|
81 | 81 | : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()} |
|
82 | 82 | { |
|
83 | 83 | qCDebug(LOG_SqpApplication()) << tr("SqpApplication construction") << QThread::currentThread(); |
|
84 | 84 | |
|
85 | 85 | connect(&impl->m_DataSourceControllerThread, &QThread::started, |
|
86 | 86 | impl->m_DataSourceController.get(), &DataSourceController::initialize); |
|
87 | 87 | connect(&impl->m_DataSourceControllerThread, &QThread::finished, |
|
88 | 88 | impl->m_DataSourceController.get(), &DataSourceController::finalize); |
|
89 | 89 | |
|
90 | 90 | connect(&impl->m_NetworkControllerThread, &QThread::started, impl->m_NetworkController.get(), |
|
91 | 91 | &NetworkController::initialize); |
|
92 | 92 | connect(&impl->m_NetworkControllerThread, &QThread::finished, impl->m_NetworkController.get(), |
|
93 | 93 | &NetworkController::finalize); |
|
94 | 94 | |
|
95 | 95 | connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(), |
|
96 | 96 | &VariableController::initialize); |
|
97 | 97 | connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(), |
|
98 | 98 | &VariableController::finalize); |
|
99 | 99 | |
|
100 | 100 | connect(&impl->m_VisualizationControllerThread, &QThread::started, |
|
101 | 101 | impl->m_VisualizationController.get(), &VisualizationController::initialize); |
|
102 | 102 | connect(&impl->m_VisualizationControllerThread, &QThread::finished, |
|
103 | 103 | impl->m_VisualizationController.get(), &VisualizationController::finalize); |
|
104 | 104 | |
|
105 | 105 | impl->m_DataSourceControllerThread.start(); |
|
106 | 106 | impl->m_NetworkControllerThread.start(); |
|
107 | 107 | impl->m_VariableControllerThread.start(); |
|
108 | 108 | impl->m_VisualizationControllerThread.start(); |
|
109 | ||
|
110 | // Core connections: | |
|
111 | // NetworkController <-> VariableController | |
|
112 | connect(&sqpApp->networkController(), &NetworkController::replyDownloadProgress, | |
|
113 | &sqpApp->variableController(), &VariableController::onVariableRetrieveDataInProgress); | |
|
114 | 109 | } |
|
115 | 110 | |
|
116 | 111 | SqpApplication::~SqpApplication() |
|
117 | 112 | { |
|
118 | 113 | } |
|
119 | 114 | |
|
120 | 115 | void SqpApplication::initialize() |
|
121 | 116 | { |
|
122 | 117 | } |
|
123 | 118 | |
|
124 | 119 | DataSourceController &SqpApplication::dataSourceController() noexcept |
|
125 | 120 | { |
|
126 | 121 | return *impl->m_DataSourceController; |
|
127 | 122 | } |
|
128 | 123 | |
|
129 | 124 | NetworkController &SqpApplication::networkController() noexcept |
|
130 | 125 | { |
|
131 | 126 | return *impl->m_NetworkController; |
|
132 | 127 | } |
|
133 | 128 | |
|
134 | 129 | TimeController &SqpApplication::timeController() noexcept |
|
135 | 130 | { |
|
136 | 131 | return *impl->m_TimeController; |
|
137 | 132 | } |
|
138 | 133 | |
|
139 | 134 | VariableController &SqpApplication::variableController() noexcept |
|
140 | 135 | { |
|
141 | 136 | return *impl->m_VariableController; |
|
142 | 137 | } |
|
143 | 138 | |
|
144 | 139 | VisualizationController &SqpApplication::visualizationController() noexcept |
|
145 | 140 | { |
|
146 | 141 | return *impl->m_VisualizationController; |
|
147 | 142 | } |
@@ -1,134 +1,138 | |||
|
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¶meterID=%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 | 46 | connect(this, SIGNAL(requestConstructed(QNetworkRequest, QUuid, |
|
47 | 47 | std::function<void(QNetworkReply *, QUuid)>)), |
|
48 | 48 | &networkController, |
|
49 | 49 | SLOT(onProcessRequested(QNetworkRequest, QUuid, |
|
50 | 50 | std::function<void(QNetworkReply *, QUuid)>))); |
|
51 | ||
|
52 | ||
|
53 | connect(&sqpApp->networkController(), SIGNAL(replyDownloadProgress(QUuid, double)), this, | |
|
54 | SIGNAL(dataProvidedProgress(QUuid, double))); | |
|
51 | 55 | } |
|
52 | 56 | } |
|
53 | 57 | |
|
54 | 58 | void AmdaProvider::requestDataLoading(QUuid token, const DataProviderParameters ¶meters) |
|
55 | 59 | { |
|
56 | 60 | // NOTE: Try to use multithread if possible |
|
57 | 61 | const auto times = parameters.m_Times; |
|
58 | 62 | const auto data = parameters.m_Data; |
|
59 | 63 | for (const auto &dateTime : qAsConst(times)) { |
|
60 | 64 | retrieveData(token, dateTime, data); |
|
61 | 65 | } |
|
62 | 66 | } |
|
63 | 67 | |
|
64 | 68 | void AmdaProvider::requestDataAborting(QUuid identifier) |
|
65 | 69 | { |
|
66 | 70 | if (auto app = sqpApp) { |
|
67 | 71 | auto &networkController = app->networkController(); |
|
68 | 72 | networkController.onReplyCanceled(identifier); |
|
69 | 73 | } |
|
70 | 74 | } |
|
71 | 75 | |
|
72 | 76 | void AmdaProvider::retrieveData(QUuid token, const SqpDateTime &dateTime, const QVariantHash &data) |
|
73 | 77 | { |
|
74 | 78 | // Retrieves product ID from data: if the value is invalid, no request is made |
|
75 | 79 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); |
|
76 | 80 | if (productId.isNull()) { |
|
77 | 81 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); |
|
78 | 82 | return; |
|
79 | 83 | } |
|
80 | 84 | |
|
81 | 85 | // /////////// // |
|
82 | 86 | // Creates URL // |
|
83 | 87 | // /////////// // |
|
84 | 88 | |
|
85 | 89 | auto startDate = dateFormat(dateTime.m_TStart); |
|
86 | 90 | auto endDate = dateFormat(dateTime.m_TEnd); |
|
87 | 91 | |
|
88 | 92 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(startDate, endDate, productId)}; |
|
89 | 93 | |
|
90 | 94 | auto tempFile = std::make_shared<QTemporaryFile>(); |
|
91 | 95 | |
|
92 | 96 | // LAMBDA |
|
93 | 97 | auto httpDownloadFinished |
|
94 | 98 | = [this, dateTime, tempFile, token](QNetworkReply *reply, QUuid dataId) noexcept { |
|
95 | 99 | Q_UNUSED(dataId); |
|
96 | 100 | |
|
97 | 101 | if (tempFile) { |
|
98 | 102 | auto replyReadAll = reply->readAll(); |
|
99 | 103 | if (!replyReadAll.isEmpty()) { |
|
100 | 104 | tempFile->write(replyReadAll); |
|
101 | 105 | } |
|
102 | 106 | tempFile->close(); |
|
103 | 107 | |
|
104 | 108 | // Parse results file |
|
105 | 109 | if (auto dataSeries = AmdaResultParser::readTxt(tempFile->fileName())) { |
|
106 | 110 | emit dataProvided(token, dataSeries, dateTime); |
|
107 | 111 | } |
|
108 | 112 | else { |
|
109 | 113 | /// @todo ALX : debug |
|
110 | 114 | } |
|
111 | 115 | } |
|
112 | 116 | |
|
113 | 117 | |
|
114 | 118 | }; |
|
115 | 119 | auto httpFinishedLambda = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, |
|
116 | 120 | QUuid dataId) noexcept { |
|
117 | 121 | |
|
118 | 122 | auto downloadFileUrl = QUrl{QString{reply->readAll()}}; |
|
119 | 123 | |
|
120 | 124 | |
|
121 | 125 | // Executes request for downloading file // |
|
122 | 126 | |
|
123 | 127 | // Creates destination file |
|
124 | 128 | if (tempFile->open()) { |
|
125 | 129 | // Executes request |
|
126 | 130 | emit requestConstructed(QNetworkRequest{downloadFileUrl}, dataId, httpDownloadFinished); |
|
127 | 131 | } |
|
128 | 132 | }; |
|
129 | 133 | |
|
130 | 134 | // //////////////// // |
|
131 | 135 | // Executes request // |
|
132 | 136 | // //////////////// // |
|
133 | 137 | emit requestConstructed(QNetworkRequest{url}, token, httpFinishedLambda); |
|
134 | 138 | } |
General Comments 0
You need to be logged in to leave comments.
Login now