##// END OF EJS Templates
Fixes the refresh of data that was not working all the time
Alexandre Leroux -
r1270:c436df4b66de
parent child
Show More
@@ -1,1069 +1,1070
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableAcquisitionWorker.h>
2 #include <Variable/VariableAcquisitionWorker.h>
3 #include <Variable/VariableCacheStrategy.h>
3 #include <Variable/VariableCacheStrategy.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
4 #include <Variable/VariableCacheStrategyFactory.h>
5 #include <Variable/VariableController.h>
5 #include <Variable/VariableController.h>
6 #include <Variable/VariableModel.h>
6 #include <Variable/VariableModel.h>
7 #include <Variable/VariableSynchronizationGroup.h>
7 #include <Variable/VariableSynchronizationGroup.h>
8
8
9 #include <Data/DataProviderParameters.h>
9 #include <Data/DataProviderParameters.h>
10 #include <Data/IDataProvider.h>
10 #include <Data/IDataProvider.h>
11 #include <Data/IDataSeries.h>
11 #include <Data/IDataSeries.h>
12 #include <Data/VariableRequest.h>
12 #include <Data/VariableRequest.h>
13 #include <Time/TimeController.h>
13 #include <Time/TimeController.h>
14
14
15 #include <QDataStream>
15 #include <QDataStream>
16 #include <QMutex>
16 #include <QMutex>
17 #include <QThread>
17 #include <QThread>
18 #include <QUuid>
18 #include <QUuid>
19 #include <QtCore/QItemSelectionModel>
19 #include <QtCore/QItemSelectionModel>
20
20
21 #include <deque>
21 #include <deque>
22 #include <set>
22 #include <set>
23 #include <unordered_map>
23 #include <unordered_map>
24
24
25 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
25 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
26
26
27 namespace {
27 namespace {
28
28
29 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
29 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
30 const SqpRange &oldGraphRange)
30 const SqpRange &oldGraphRange)
31 {
31 {
32 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
32 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
33
33
34 auto varRangeRequested = varRange;
34 auto varRangeRequested = varRange;
35 switch (zoomType) {
35 switch (zoomType) {
36 case AcquisitionZoomType::ZoomIn: {
36 case AcquisitionZoomType::ZoomIn: {
37 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
37 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
38 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
38 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
39 varRangeRequested.m_TStart += deltaLeft;
39 varRangeRequested.m_TStart += deltaLeft;
40 varRangeRequested.m_TEnd -= deltaRight;
40 varRangeRequested.m_TEnd -= deltaRight;
41 break;
41 break;
42 }
42 }
43
43
44 case AcquisitionZoomType::ZoomOut: {
44 case AcquisitionZoomType::ZoomOut: {
45 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
45 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
46 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
46 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
47 varRangeRequested.m_TStart -= deltaLeft;
47 varRangeRequested.m_TStart -= deltaLeft;
48 varRangeRequested.m_TEnd += deltaRight;
48 varRangeRequested.m_TEnd += deltaRight;
49 break;
49 break;
50 }
50 }
51 case AcquisitionZoomType::PanRight: {
51 case AcquisitionZoomType::PanRight: {
52 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
52 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
53 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
53 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
54 varRangeRequested.m_TStart += deltaLeft;
54 varRangeRequested.m_TStart += deltaLeft;
55 varRangeRequested.m_TEnd += deltaRight;
55 varRangeRequested.m_TEnd += deltaRight;
56 break;
56 break;
57 }
57 }
58 case AcquisitionZoomType::PanLeft: {
58 case AcquisitionZoomType::PanLeft: {
59 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
59 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
60 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
60 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
61 varRangeRequested.m_TStart -= deltaLeft;
61 varRangeRequested.m_TStart -= deltaLeft;
62 varRangeRequested.m_TEnd -= deltaRight;
62 varRangeRequested.m_TEnd -= deltaRight;
63 break;
63 break;
64 }
64 }
65 case AcquisitionZoomType::Unknown: {
65 case AcquisitionZoomType::Unknown: {
66 qCCritical(LOG_VariableController())
66 qCCritical(LOG_VariableController())
67 << VariableController::tr("Impossible to synchronize: zoom type unknown");
67 << VariableController::tr("Impossible to synchronize: zoom type unknown");
68 break;
68 break;
69 }
69 }
70 default:
70 default:
71 qCCritical(LOG_VariableController()) << VariableController::tr(
71 qCCritical(LOG_VariableController()) << VariableController::tr(
72 "Impossible to synchronize: zoom type not take into account");
72 "Impossible to synchronize: zoom type not take into account");
73 // No action
73 // No action
74 break;
74 break;
75 }
75 }
76
76
77 return varRangeRequested;
77 return varRangeRequested;
78 }
78 }
79 }
79 }
80
80
81 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
81 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
82
82
83 struct VariableRequestHandler {
83 struct VariableRequestHandler {
84
84
85 VariableRequestHandler()
85 VariableRequestHandler()
86 {
86 {
87 m_CanUpdate = false;
87 m_CanUpdate = false;
88 m_State = VariableRequestHandlerState::OFF;
88 m_State = VariableRequestHandlerState::OFF;
89 }
89 }
90
90
91 QUuid m_VarId;
91 QUuid m_VarId;
92 VariableRequest m_RunningVarRequest;
92 VariableRequest m_RunningVarRequest;
93 VariableRequest m_PendingVarRequest;
93 VariableRequest m_PendingVarRequest;
94 VariableRequestHandlerState m_State;
94 VariableRequestHandlerState m_State;
95 bool m_CanUpdate;
95 bool m_CanUpdate;
96 };
96 };
97
97
98 struct VariableController::VariableControllerPrivate {
98 struct VariableController::VariableControllerPrivate {
99 explicit VariableControllerPrivate(VariableController *parent)
99 explicit VariableControllerPrivate(VariableController *parent)
100 : m_WorkingMutex{},
100 : m_WorkingMutex{},
101 m_VariableModel{new VariableModel{parent}},
101 m_VariableModel{new VariableModel{parent}},
102 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
102 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
103 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
103 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
104 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
104 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
105 CacheStrategy::SingleThreshold)},
105 CacheStrategy::SingleThreshold)},
106 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
106 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
107 q{parent}
107 q{parent}
108 {
108 {
109
109
110 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
110 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
111 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
111 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
112 }
112 }
113
113
114
114
115 virtual ~VariableControllerPrivate()
115 virtual ~VariableControllerPrivate()
116 {
116 {
117 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
117 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
118 m_VariableAcquisitionWorkerThread.quit();
118 m_VariableAcquisitionWorkerThread.quit();
119 m_VariableAcquisitionWorkerThread.wait();
119 m_VariableAcquisitionWorkerThread.wait();
120 }
120 }
121
121
122
122
123 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
123 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
124 QUuid varRequestId);
124 QUuid varRequestId);
125
125
126 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
126 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
127 std::shared_ptr<IDataSeries>
127 std::shared_ptr<IDataSeries>
128 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
128 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
129
129
130 void registerProvider(std::shared_ptr<IDataProvider> provider);
130 void registerProvider(std::shared_ptr<IDataProvider> provider);
131
131
132 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
132 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
133 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
133 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
134 void updateVariables(QUuid varRequestId);
134 void updateVariables(QUuid varRequestId);
135 void updateVariableRequest(QUuid varRequestId);
135 void updateVariableRequest(QUuid varRequestId);
136 void cancelVariableRequest(QUuid varRequestId);
136 void cancelVariableRequest(QUuid varRequestId);
137 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
137 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
138
138
139 QMutex m_WorkingMutex;
139 QMutex m_WorkingMutex;
140 /// Variable model. The VariableController has the ownership
140 /// Variable model. The VariableController has the ownership
141 VariableModel *m_VariableModel;
141 VariableModel *m_VariableModel;
142 QItemSelectionModel *m_VariableSelectionModel;
142 QItemSelectionModel *m_VariableSelectionModel;
143
143
144
144
145 TimeController *m_TimeController{nullptr};
145 TimeController *m_TimeController{nullptr};
146 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
146 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
147 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
147 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
148 QThread m_VariableAcquisitionWorkerThread;
148 QThread m_VariableAcquisitionWorkerThread;
149
149
150 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
150 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
151 m_VariableToProviderMap;
151 m_VariableToProviderMap;
152 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
152 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
153 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
153 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
154 m_GroupIdToVariableSynchronizationGroupMap;
154 m_GroupIdToVariableSynchronizationGroupMap;
155 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
155 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
156 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
156 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
157
157
158 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
158 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
159 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
159 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
160
160
161 VariableController *q;
161 VariableController *q;
162 };
162 };
163
163
164
164
165 VariableController::VariableController(QObject *parent)
165 VariableController::VariableController(QObject *parent)
166 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
166 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
167 {
167 {
168 qCDebug(LOG_VariableController()) << tr("VariableController construction")
168 qCDebug(LOG_VariableController()) << tr("VariableController construction")
169 << QThread::currentThread();
169 << QThread::currentThread();
170
170
171 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
171 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
172 &VariableController::onAbortProgressRequested);
172 &VariableController::onAbortProgressRequested);
173
173
174 connect(impl->m_VariableAcquisitionWorker.get(),
174 connect(impl->m_VariableAcquisitionWorker.get(),
175 &VariableAcquisitionWorker::variableCanceledRequested, this,
175 &VariableAcquisitionWorker::variableCanceledRequested, this,
176 &VariableController::onAbortAcquisitionRequested);
176 &VariableController::onAbortAcquisitionRequested);
177
177
178 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
178 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
179 &VariableController::onDataProvided);
179 &VariableController::onDataProvided);
180 connect(impl->m_VariableAcquisitionWorker.get(),
180 connect(impl->m_VariableAcquisitionWorker.get(),
181 &VariableAcquisitionWorker::variableRequestInProgress, this,
181 &VariableAcquisitionWorker::variableRequestInProgress, this,
182 &VariableController::onVariableRetrieveDataInProgress);
182 &VariableController::onVariableRetrieveDataInProgress);
183
183
184
184
185 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
185 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
186 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
186 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
187 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
187 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
188 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
188 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
189
189
190 connect(impl->m_VariableModel, &VariableModel::requestVariableRangeUpdate, this,
190 connect(impl->m_VariableModel, &VariableModel::requestVariableRangeUpdate, this,
191 &VariableController::onUpdateDateTime);
191 &VariableController::onUpdateDateTime);
192
192
193 impl->m_VariableAcquisitionWorkerThread.start();
193 impl->m_VariableAcquisitionWorkerThread.start();
194 }
194 }
195
195
196 VariableController::~VariableController()
196 VariableController::~VariableController()
197 {
197 {
198 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
198 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
199 << QThread::currentThread();
199 << QThread::currentThread();
200 this->waitForFinish();
200 this->waitForFinish();
201 }
201 }
202
202
203 VariableModel *VariableController::variableModel() noexcept
203 VariableModel *VariableController::variableModel() noexcept
204 {
204 {
205 return impl->m_VariableModel;
205 return impl->m_VariableModel;
206 }
206 }
207
207
208 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
208 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
209 {
209 {
210 return impl->m_VariableSelectionModel;
210 return impl->m_VariableSelectionModel;
211 }
211 }
212
212
213 void VariableController::setTimeController(TimeController *timeController) noexcept
213 void VariableController::setTimeController(TimeController *timeController) noexcept
214 {
214 {
215 impl->m_TimeController = timeController;
215 impl->m_TimeController = timeController;
216 }
216 }
217
217
218 std::shared_ptr<Variable>
218 std::shared_ptr<Variable>
219 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
219 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
220 {
220 {
221 if (impl->m_VariableModel->containsVariable(variable)) {
221 if (impl->m_VariableModel->containsVariable(variable)) {
222 // Clones variable
222 // Clones variable
223 auto duplicate = variable->clone();
223 auto duplicate = variable->clone();
224
224
225 // Adds clone to model
225 // Adds clone to model
226 impl->m_VariableModel->addVariable(duplicate);
226 impl->m_VariableModel->addVariable(duplicate);
227
227
228 // Generates clone identifier
228 // Generates clone identifier
229 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
229 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
230
230
231 // Registers provider
231 // Registers provider
232 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
232 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
233 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
233 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
234
234
235 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
235 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
236 if (duplicateProvider) {
236 if (duplicateProvider) {
237 impl->registerProvider(duplicateProvider);
237 impl->registerProvider(duplicateProvider);
238 }
238 }
239
239
240 return duplicate;
240 return duplicate;
241 }
241 }
242 else {
242 else {
243 qCCritical(LOG_VariableController())
243 qCCritical(LOG_VariableController())
244 << tr("Can't create duplicate of variable %1: variable not registered in the model")
244 << tr("Can't create duplicate of variable %1: variable not registered in the model")
245 .arg(variable->name());
245 .arg(variable->name());
246 return nullptr;
246 return nullptr;
247 }
247 }
248 }
248 }
249
249
250 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
250 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
251 {
251 {
252 if (!variable) {
252 if (!variable) {
253 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
253 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
254 return;
254 return;
255 }
255 }
256
256
257 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
257 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
258 // make some treatments before the deletion
258 // make some treatments before the deletion
259 emit variableAboutToBeDeleted(variable);
259 emit variableAboutToBeDeleted(variable);
260
260
261 // Deletes identifier
261 // Deletes identifier
262 impl->m_VariableToIdentifierMap.erase(variable);
262 impl->m_VariableToIdentifierMap.erase(variable);
263
263
264 // Deletes provider
264 // Deletes provider
265 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
265 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
266 qCDebug(LOG_VariableController())
266 qCDebug(LOG_VariableController())
267 << tr("Number of providers deleted for variable %1: %2")
267 << tr("Number of providers deleted for variable %1: %2")
268 .arg(variable->name(), QString::number(nbProvidersDeleted));
268 .arg(variable->name(), QString::number(nbProvidersDeleted));
269
269
270
270
271 // Deletes from model
271 // Deletes from model
272 impl->m_VariableModel->deleteVariable(variable);
272 impl->m_VariableModel->deleteVariable(variable);
273 }
273 }
274
274
275 void VariableController::deleteVariables(
275 void VariableController::deleteVariables(
276 const QVector<std::shared_ptr<Variable> > &variables) noexcept
276 const QVector<std::shared_ptr<Variable> > &variables) noexcept
277 {
277 {
278 for (auto variable : qAsConst(variables)) {
278 for (auto variable : qAsConst(variables)) {
279 deleteVariable(variable);
279 deleteVariable(variable);
280 }
280 }
281 }
281 }
282
282
283 QByteArray
283 QByteArray
284 VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const
284 VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const
285 {
285 {
286 auto encodedData = QByteArray{};
286 auto encodedData = QByteArray{};
287
287
288 QVariantList ids;
288 QVariantList ids;
289 for (auto &var : variables) {
289 for (auto &var : variables) {
290 auto itVar = impl->m_VariableToIdentifierMap.find(var);
290 auto itVar = impl->m_VariableToIdentifierMap.find(var);
291 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
291 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
292 qCCritical(LOG_VariableController())
292 qCCritical(LOG_VariableController())
293 << tr("Impossible to find the data for an unknown variable.");
293 << tr("Impossible to find the data for an unknown variable.");
294 }
294 }
295
295
296 ids << itVar->second.toByteArray();
296 ids << itVar->second.toByteArray();
297 }
297 }
298
298
299 QDataStream stream{&encodedData, QIODevice::WriteOnly};
299 QDataStream stream{&encodedData, QIODevice::WriteOnly};
300 stream << ids;
300 stream << ids;
301
301
302 return encodedData;
302 return encodedData;
303 }
303 }
304
304
305 QList<std::shared_ptr<Variable> >
305 QList<std::shared_ptr<Variable> >
306 VariableController::variablesForMimeData(const QByteArray &mimeData) const
306 VariableController::variablesForMimeData(const QByteArray &mimeData) const
307 {
307 {
308 auto variables = QList<std::shared_ptr<Variable> >{};
308 auto variables = QList<std::shared_ptr<Variable> >{};
309 QDataStream stream{mimeData};
309 QDataStream stream{mimeData};
310
310
311 QVariantList ids;
311 QVariantList ids;
312 stream >> ids;
312 stream >> ids;
313
313
314 for (auto id : ids) {
314 for (auto id : ids) {
315 auto uuid = QUuid{id.toByteArray()};
315 auto uuid = QUuid{id.toByteArray()};
316 auto var = impl->findVariable(uuid);
316 auto var = impl->findVariable(uuid);
317 variables << var;
317 variables << var;
318 }
318 }
319
319
320 return variables;
320 return variables;
321 }
321 }
322
322
323 std::shared_ptr<Variable>
323 std::shared_ptr<Variable>
324 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
324 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
325 std::shared_ptr<IDataProvider> provider) noexcept
325 std::shared_ptr<IDataProvider> provider) noexcept
326 {
326 {
327 if (!impl->m_TimeController) {
327 if (!impl->m_TimeController) {
328 qCCritical(LOG_VariableController())
328 qCCritical(LOG_VariableController())
329 << tr("Impossible to create variable: The time controller is null");
329 << tr("Impossible to create variable: The time controller is null");
330 return nullptr;
330 return nullptr;
331 }
331 }
332
332
333 auto range = impl->m_TimeController->dateTime();
333 auto range = impl->m_TimeController->dateTime();
334
334
335 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
335 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
336 auto varId = QUuid::createUuid();
336 auto varId = QUuid::createUuid();
337
337
338 // Create the handler
338 // Create the handler
339 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
339 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
340 varRequestHandler->m_VarId = varId;
340 varRequestHandler->m_VarId = varId;
341
341
342 impl->m_VarIdToVarRequestHandler.insert(
342 impl->m_VarIdToVarRequestHandler.insert(
343 std::make_pair(varId, std::move(varRequestHandler)));
343 std::make_pair(varId, std::move(varRequestHandler)));
344
344
345 // store the provider
345 // store the provider
346 impl->registerProvider(provider);
346 impl->registerProvider(provider);
347
347
348 // Associate the provider
348 // Associate the provider
349 impl->m_VariableToProviderMap[newVariable] = provider;
349 impl->m_VariableToProviderMap[newVariable] = provider;
350 impl->m_VariableToIdentifierMap[newVariable] = varId;
350 impl->m_VariableToIdentifierMap[newVariable] = varId;
351
351
352 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
352 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
353
353
354 // auto varRequestId = QUuid::createUuid();
354 // auto varRequestId = QUuid::createUuid();
355 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
355 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
356 // impl->processRequest(newVariable, range, varRequestId);
356 // impl->processRequest(newVariable, range, varRequestId);
357 // impl->updateVariableRequest(varRequestId);
357 // impl->updateVariableRequest(varRequestId);
358
358
359 return newVariable;
359 return newVariable;
360 }
360 }
361
361
362 qCCritical(LOG_VariableController()) << tr("Impossible to create variable");
362 qCCritical(LOG_VariableController()) << tr("Impossible to create variable");
363 return nullptr;
363 return nullptr;
364 }
364 }
365
365
366 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
366 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
367 {
367 {
368 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
368 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
369 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
369 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
370 << QThread::currentThread()->objectName();
370 << QThread::currentThread()->objectName();
371 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
371 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
372
372
373 // NOTE we only permit the time modification for one variable
373 // NOTE we only permit the time modification for one variable
374 // DEPRECATED
374 // DEPRECATED
375 // auto variables = QVector<std::shared_ptr<Variable> >{};
375 // auto variables = QVector<std::shared_ptr<Variable> >{};
376 // for (const auto &selectedRow : qAsConst(selectedRows)) {
376 // for (const auto &selectedRow : qAsConst(selectedRows)) {
377 // if (auto selectedVariable =
377 // if (auto selectedVariable =
378 // impl->m_VariableModel->variable(selectedRow.row())) {
378 // impl->m_VariableModel->variable(selectedRow.row())) {
379 // variables << selectedVariable;
379 // variables << selectedVariable;
380
380
381 // // notify that rescale operation has to be done
381 // // notify that rescale operation has to be done
382 // emit rangeChanged(selectedVariable, dateTime);
382 // emit rangeChanged(selectedVariable, dateTime);
383 // }
383 // }
384 // }
384 // }
385 // if (!variables.isEmpty()) {
385 // if (!variables.isEmpty()) {
386 // this->onRequestDataLoading(variables, dateTime, synchro);
386 // this->onRequestDataLoading(variables, dateTime, synchro);
387 // }
387 // }
388 if (selectedRows.size() == 1) {
388 if (selectedRows.size() == 1) {
389
389
390 if (auto selectedVariable
390 if (auto selectedVariable
391 = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) {
391 = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) {
392
392
393 onUpdateDateTime(selectedVariable, dateTime);
393 onUpdateDateTime(selectedVariable, dateTime);
394 }
394 }
395 }
395 }
396 else if (selectedRows.size() > 1) {
396 else if (selectedRows.size() > 1) {
397 qCCritical(LOG_VariableController())
397 qCCritical(LOG_VariableController())
398 << tr("Impossible to set time for more than 1 variable in the same time");
398 << tr("Impossible to set time for more than 1 variable in the same time");
399 }
399 }
400 else {
400 else {
401 qCWarning(LOG_VariableController())
401 qCWarning(LOG_VariableController())
402 << tr("There is no variable selected to set the time one");
402 << tr("There is no variable selected to set the time one");
403 }
403 }
404 }
404 }
405
405
406 void VariableController::onUpdateDateTime(std::shared_ptr<Variable> variable,
406 void VariableController::onUpdateDateTime(std::shared_ptr<Variable> variable,
407 const SqpRange &dateTime)
407 const SqpRange &dateTime)
408 {
408 {
409 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
409 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
410 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
410 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
411 qCCritical(LOG_VariableController())
411 qCCritical(LOG_VariableController())
412 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
412 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
413 return;
413 return;
414 }
414 }
415
415
416 // notify that rescale operation has to be done
416 // notify that rescale operation has to be done
417 emit rangeChanged(variable, dateTime);
417 emit rangeChanged(variable, dateTime);
418
418
419 auto synchro
419 auto synchro
420 = impl->m_VariableIdGroupIdMap.find(itVar->second) != impl->m_VariableIdGroupIdMap.cend();
420 = impl->m_VariableIdGroupIdMap.find(itVar->second) != impl->m_VariableIdGroupIdMap.cend();
421
421
422 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{variable}, dateTime, synchro);
422 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{variable}, dateTime, synchro);
423 }
423 }
424
424
425 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
425 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
426 const SqpRange &cacheRangeRequested,
426 const SqpRange &cacheRangeRequested,
427 QVector<AcquisitionDataPacket> dataAcquired)
427 QVector<AcquisitionDataPacket> dataAcquired)
428 {
428 {
429 qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
429 qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
430 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
430 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
431 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
431 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
432 if (!varRequestId.isNull()) {
432 if (!varRequestId.isNull()) {
433 impl->updateVariables(varRequestId);
433 impl->updateVariables(varRequestId);
434 }
434 }
435 }
435 }
436
436
437 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
437 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
438 {
438 {
439 qCDebug(LOG_VariableController())
439 qCDebug(LOG_VariableController())
440 << "TORM: variableController::onVariableRetrieveDataInProgress"
440 << "TORM: variableController::onVariableRetrieveDataInProgress"
441 << QThread::currentThread()->objectName() << progress;
441 << QThread::currentThread()->objectName() << progress;
442 if (auto var = impl->findVariable(identifier)) {
442 if (auto var = impl->findVariable(identifier)) {
443 impl->m_VariableModel->setDataProgress(var, progress);
443 impl->m_VariableModel->setDataProgress(var, progress);
444 }
444 }
445 else {
445 else {
446 qCCritical(LOG_VariableController())
446 qCCritical(LOG_VariableController())
447 << tr("Impossible to notify progression of a null variable");
447 << tr("Impossible to notify progression of a null variable");
448 }
448 }
449 }
449 }
450
450
451 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
451 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
452 {
452 {
453 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested"
453 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested"
454 << QThread::currentThread()->objectName() << variable->name();
454 << QThread::currentThread()->objectName() << variable->name();
455
455
456 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
456 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
457 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
457 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
458 qCCritical(LOG_VariableController())
458 qCCritical(LOG_VariableController())
459 << tr("Impossible to onAbortProgressRequested request for unknown variable");
459 << tr("Impossible to onAbortProgressRequested request for unknown variable");
460 return;
460 return;
461 }
461 }
462
462
463 auto varId = itVar->second;
463 auto varId = itVar->second;
464
464
465 auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId);
465 auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId);
466 if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) {
466 if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) {
467 qCCritical(LOG_VariableController())
467 qCCritical(LOG_VariableController())
468 << tr("Impossible to onAbortProgressRequested for variable with unknown handler");
468 << tr("Impossible to onAbortProgressRequested for variable with unknown handler");
469 return;
469 return;
470 }
470 }
471
471
472 auto varHandler = itVarHandler->second.get();
472 auto varHandler = itVarHandler->second.get();
473
473
474 // case where a variable has a running request
474 // case where a variable has a running request
475 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
475 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
476 impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId);
476 impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId);
477 }
477 }
478 }
478 }
479
479
480 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
480 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
481 {
481 {
482 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
482 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
483 << QThread::currentThread()->objectName() << vIdentifier;
483 << QThread::currentThread()->objectName() << vIdentifier;
484
484
485 if (auto var = impl->findVariable(vIdentifier)) {
485 if (auto var = impl->findVariable(vIdentifier)) {
486 this->onAbortProgressRequested(var);
486 this->onAbortProgressRequested(var);
487 }
487 }
488 else {
488 else {
489 qCCritical(LOG_VariableController())
489 qCCritical(LOG_VariableController())
490 << tr("Impossible to abort Acquisition Requestof a null variable");
490 << tr("Impossible to abort Acquisition Requestof a null variable");
491 }
491 }
492 }
492 }
493
493
494 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
494 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
495 {
495 {
496 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
496 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
497 << QThread::currentThread()->objectName()
497 << QThread::currentThread()->objectName()
498 << synchronizationGroupId;
498 << synchronizationGroupId;
499 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
499 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
500 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
500 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
501 std::make_pair(synchronizationGroupId, vSynchroGroup));
501 std::make_pair(synchronizationGroupId, vSynchroGroup));
502 }
502 }
503
503
504 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
504 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
505 {
505 {
506 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
506 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
507 }
507 }
508
508
509 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
509 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
510 QUuid synchronizationGroupId)
510 QUuid synchronizationGroupId)
511
511
512 {
512 {
513 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
513 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
514 << synchronizationGroupId;
514 << synchronizationGroupId;
515 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
515 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
516 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
516 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
517 auto groupIdToVSGIt
517 auto groupIdToVSGIt
518 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
518 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
519 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
519 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
520 impl->m_VariableIdGroupIdMap.insert(
520 impl->m_VariableIdGroupIdMap.insert(
521 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
521 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
522 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
522 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
523 }
523 }
524 else {
524 else {
525 qCCritical(LOG_VariableController())
525 qCCritical(LOG_VariableController())
526 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
526 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
527 << variable->name();
527 << variable->name();
528 }
528 }
529 }
529 }
530 else {
530 else {
531 qCCritical(LOG_VariableController())
531 qCCritical(LOG_VariableController())
532 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
532 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
533 }
533 }
534 }
534 }
535
535
536 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
536 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
537 QUuid synchronizationGroupId)
537 QUuid synchronizationGroupId)
538 {
538 {
539 // Gets variable id
539 // Gets variable id
540 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
540 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
541 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
541 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
542 qCCritical(LOG_VariableController())
542 qCCritical(LOG_VariableController())
543 << tr("Can't desynchronize variable %1: variable identifier not found")
543 << tr("Can't desynchronize variable %1: variable identifier not found")
544 .arg(variable->name());
544 .arg(variable->name());
545 return;
545 return;
546 }
546 }
547
547
548 // Gets synchronization group
548 // Gets synchronization group
549 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
549 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
550 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
550 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
551 qCCritical(LOG_VariableController())
551 qCCritical(LOG_VariableController())
552 << tr("Can't desynchronize variable %1: unknown synchronization group")
552 << tr("Can't desynchronize variable %1: unknown synchronization group")
553 .arg(variable->name());
553 .arg(variable->name());
554 return;
554 return;
555 }
555 }
556
556
557 auto variableId = variableIt->second;
557 auto variableId = variableIt->second;
558
558
559 // Removes variable from synchronization group
559 // Removes variable from synchronization group
560 auto synchronizationGroup = groupIt->second;
560 auto synchronizationGroup = groupIt->second;
561 synchronizationGroup->removeVariableId(variableId);
561 synchronizationGroup->removeVariableId(variableId);
562
562
563 // Removes link between variable and synchronization group
563 // Removes link between variable and synchronization group
564 impl->m_VariableIdGroupIdMap.erase(variableId);
564 impl->m_VariableIdGroupIdMap.erase(variableId);
565 }
565 }
566
566
567 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
567 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
568 const SqpRange &range, bool synchronise)
568 const SqpRange &range, bool synchronise)
569 {
569 {
570 // variables is assumed synchronized
570 // variables is assumed synchronized
571 // TODO: Asser variables synchronization
571 // TODO: Asser variables synchronization
572 // we want to load data of the variable for the dateTime.
572 // we want to load data of the variable for the dateTime.
573 if (variables.isEmpty()) {
573 if (variables.isEmpty()) {
574 return;
574 return;
575 }
575 }
576
576
577 auto varRequestId = QUuid::createUuid();
577 auto varRequestId = QUuid::createUuid();
578 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
578 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
579 << QThread::currentThread()->objectName() << varRequestId
579 << QThread::currentThread()->objectName() << varRequestId
580 << range << synchronise;
580 << range << synchronise;
581
581
582 if (!synchronise) {
582 if (!synchronise) {
583 auto varIds = std::list<QUuid>{};
583 auto varIds = std::list<QUuid>{};
584 for (const auto &var : variables) {
584 for (const auto &var : variables) {
585 auto vId = impl->m_VariableToIdentifierMap.at(var);
585 auto vId = impl->m_VariableToIdentifierMap.at(var);
586 varIds.push_back(vId);
586 varIds.push_back(vId);
587 }
587 }
588 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
588 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
589 for (const auto &var : variables) {
589 for (const auto &var : variables) {
590 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
590 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
591 << varIds.size();
591 << varIds.size();
592 impl->processRequest(var, range, varRequestId);
592 impl->processRequest(var, range, varRequestId);
593 }
593 }
594 }
594 }
595 else {
595 else {
596 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
596 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
597 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
597 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
598 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
598 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
599 auto groupId = varIdToGroupIdIt->second;
599 auto groupId = varIdToGroupIdIt->second;
600
600
601 auto vSynchronizationGroup
601 auto vSynchronizationGroup
602 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
602 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
603 auto vSyncIds = vSynchronizationGroup->getIds();
603 auto vSyncIds = vSynchronizationGroup->getIds();
604
604
605 auto varIds = std::list<QUuid>{};
605 auto varIds = std::list<QUuid>{};
606 for (auto vId : vSyncIds) {
606 for (auto vId : vSyncIds) {
607 varIds.push_back(vId);
607 varIds.push_back(vId);
608 }
608 }
609 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
609 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
610
610
611 for (auto vId : vSyncIds) {
611 for (auto vId : vSyncIds) {
612 auto var = impl->findVariable(vId);
612 auto var = impl->findVariable(vId);
613
613
614 // Don't process already processed var
614 // Don't process already processed var
615 if (var != nullptr) {
615 if (var != nullptr) {
616 qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name()
616 qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name()
617 << varRequestId;
617 << varRequestId;
618 auto vSyncRangeRequested
618 auto vSyncRangeRequested
619 = variables.contains(var)
619 = variables.contains(var)
620 ? range
620 ? range
621 : computeSynchroRangeRequested(var->range(), range,
621 : computeSynchroRangeRequested(var->range(), range,
622 variables.first()->range());
622 variables.first()->range());
623 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
623 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
624 impl->processRequest(var, vSyncRangeRequested, varRequestId);
624 impl->processRequest(var, vSyncRangeRequested, varRequestId);
625 }
625 }
626 else {
626 else {
627 qCCritical(LOG_VariableController())
627 qCCritical(LOG_VariableController())
628
628
629 << tr("Impossible to synchronize a null variable");
629 << tr("Impossible to synchronize a null variable");
630 }
630 }
631 }
631 }
632 }
632 }
633 }
633 }
634
634
635 impl->updateVariables(varRequestId);
635 impl->updateVariables(varRequestId);
636 }
636 }
637
637
638
638
639 void VariableController::initialize()
639 void VariableController::initialize()
640 {
640 {
641 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
641 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
642 impl->m_WorkingMutex.lock();
642 impl->m_WorkingMutex.lock();
643 qCDebug(LOG_VariableController()) << tr("VariableController init END");
643 qCDebug(LOG_VariableController()) << tr("VariableController init END");
644 }
644 }
645
645
646 void VariableController::finalize()
646 void VariableController::finalize()
647 {
647 {
648 impl->m_WorkingMutex.unlock();
648 impl->m_WorkingMutex.unlock();
649 }
649 }
650
650
651 void VariableController::waitForFinish()
651 void VariableController::waitForFinish()
652 {
652 {
653 QMutexLocker locker{&impl->m_WorkingMutex};
653 QMutexLocker locker{&impl->m_WorkingMutex};
654 }
654 }
655
655
656 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
656 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
657 {
657 {
658 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
658 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
659 auto zoomType = AcquisitionZoomType::Unknown;
659 auto zoomType = AcquisitionZoomType::Unknown;
660 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
660 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
661 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
661 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
662 zoomType = AcquisitionZoomType::ZoomOut;
662 zoomType = AcquisitionZoomType::ZoomOut;
663 }
663 }
664 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
664 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
665 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
665 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
666 zoomType = AcquisitionZoomType::PanRight;
666 zoomType = AcquisitionZoomType::PanRight;
667 }
667 }
668 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
668 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
669 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
669 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
670 zoomType = AcquisitionZoomType::PanLeft;
670 zoomType = AcquisitionZoomType::PanLeft;
671 }
671 }
672 else if (range.m_TStart >= oldRange.m_TStart && oldRange.m_TEnd >= range.m_TEnd) {
672 else if (range.m_TStart >= oldRange.m_TStart && oldRange.m_TEnd >= range.m_TEnd) {
673 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
673 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
674 zoomType = AcquisitionZoomType::ZoomIn;
674 zoomType = AcquisitionZoomType::ZoomIn;
675 }
675 }
676 else {
676 else {
677 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
677 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
678 }
678 }
679 return zoomType;
679 return zoomType;
680 }
680 }
681
681
682 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
682 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
683 const SqpRange &rangeRequested,
683 const SqpRange &rangeRequested,
684 QUuid varRequestId)
684 QUuid varRequestId)
685 {
685 {
686 auto itVar = m_VariableToIdentifierMap.find(var);
686 auto itVar = m_VariableToIdentifierMap.find(var);
687 if (itVar == m_VariableToIdentifierMap.cend()) {
687 if (itVar == m_VariableToIdentifierMap.cend()) {
688 qCCritical(LOG_VariableController())
688 qCCritical(LOG_VariableController())
689 << tr("Impossible to process request for unknown variable");
689 << tr("Impossible to process request for unknown variable");
690 return;
690 return;
691 }
691 }
692
692
693 auto varId = itVar->second;
693 auto varId = itVar->second;
694
694
695 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
695 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
696 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
696 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
697 qCCritical(LOG_VariableController())
697 qCCritical(LOG_VariableController())
698 << tr("Impossible to process request for variable with unknown handler");
698 << tr("Impossible to process request for variable with unknown handler");
699 return;
699 return;
700 }
700 }
701
701
702 auto oldRange = var->range();
702 auto oldRange = var->range();
703
703
704 auto varHandler = itVarHandler->second.get();
704 auto varHandler = itVarHandler->second.get();
705
705
706 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
706 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
707 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
707 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
708 }
708 }
709
709
710 auto varRequest = VariableRequest{};
710 auto varRequest = VariableRequest{};
711 varRequest.m_VariableGroupId = varRequestId;
711 varRequest.m_VariableGroupId = varRequestId;
712 auto varStrategyRangesRequested
712 auto varStrategyRangesRequested
713 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
713 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
714 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
714 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
715 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
715 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
716
716
717 switch (varHandler->m_State) {
717 switch (varHandler->m_State) {
718 case VariableRequestHandlerState::OFF: {
718 case VariableRequestHandlerState::OFF: {
719 qCDebug(LOG_VariableController()) << tr("Process Request OFF")
719 qCDebug(LOG_VariableController()) << tr("Process Request OFF")
720 << varRequest.m_RangeRequested
720 << varRequest.m_RangeRequested
721 << varRequest.m_CacheRangeRequested;
721 << varRequest.m_CacheRangeRequested;
722 varHandler->m_RunningVarRequest = varRequest;
722 varHandler->m_RunningVarRequest = varRequest;
723 varHandler->m_State = VariableRequestHandlerState::RUNNING;
723 varHandler->m_State = VariableRequestHandlerState::RUNNING;
724 executeVarRequest(var, varRequest);
724 executeVarRequest(var, varRequest);
725 break;
725 break;
726 }
726 }
727 case VariableRequestHandlerState::RUNNING: {
727 case VariableRequestHandlerState::RUNNING: {
728 qCDebug(LOG_VariableController()) << tr("Process Request RUNNING")
728 qCDebug(LOG_VariableController()) << tr("Process Request RUNNING")
729 << varRequest.m_RangeRequested
729 << varRequest.m_RangeRequested
730 << varRequest.m_CacheRangeRequested;
730 << varRequest.m_CacheRangeRequested;
731 varHandler->m_State = VariableRequestHandlerState::PENDING;
731 varHandler->m_State = VariableRequestHandlerState::PENDING;
732 varHandler->m_PendingVarRequest = varRequest;
732 varHandler->m_PendingVarRequest = varRequest;
733 break;
733 break;
734 }
734 }
735 case VariableRequestHandlerState::PENDING: {
735 case VariableRequestHandlerState::PENDING: {
736 qCDebug(LOG_VariableController()) << tr("Process Request PENDING")
736 qCDebug(LOG_VariableController()) << tr("Process Request PENDING")
737 << varRequest.m_RangeRequested
737 << varRequest.m_RangeRequested
738 << varRequest.m_CacheRangeRequested;
738 << varRequest.m_CacheRangeRequested;
739 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
739 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
740 cancelVariableRequest(variableGroupIdToCancel);
740 cancelVariableRequest(variableGroupIdToCancel);
741 // Cancel variable can make state downgrade
741 // Cancel variable can make state downgrade
742 varHandler->m_State = VariableRequestHandlerState::PENDING;
742 varHandler->m_State = VariableRequestHandlerState::PENDING;
743 varHandler->m_PendingVarRequest = varRequest;
743 varHandler->m_PendingVarRequest = varRequest;
744
744
745 break;
745 break;
746 }
746 }
747 default:
747 default:
748 qCCritical(LOG_VariableController())
748 qCCritical(LOG_VariableController())
749 << QObject::tr("Unknown VariableRequestHandlerState");
749 << QObject::tr("Unknown VariableRequestHandlerState");
750 }
750 }
751 }
751 }
752
752
753 std::shared_ptr<Variable>
753 std::shared_ptr<Variable>
754 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
754 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
755 {
755 {
756 std::shared_ptr<Variable> var;
756 std::shared_ptr<Variable> var;
757 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
757 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
758
758
759 auto end = m_VariableToIdentifierMap.cend();
759 auto end = m_VariableToIdentifierMap.cend();
760 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
760 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
761 if (it != end) {
761 if (it != end) {
762 var = it->first;
762 var = it->first;
763 }
763 }
764 else {
764 else {
765 qCCritical(LOG_VariableController())
765 qCCritical(LOG_VariableController())
766 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
766 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
767 }
767 }
768
768
769 return var;
769 return var;
770 }
770 }
771
771
772 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
772 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
773 const QVector<AcquisitionDataPacket> acqDataPacketVector)
773 const QVector<AcquisitionDataPacket> acqDataPacketVector)
774 {
774 {
775 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
775 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
776 << acqDataPacketVector.size();
776 << acqDataPacketVector.size();
777 std::shared_ptr<IDataSeries> dataSeries;
777 std::shared_ptr<IDataSeries> dataSeries;
778 if (!acqDataPacketVector.isEmpty()) {
778 if (!acqDataPacketVector.isEmpty()) {
779 dataSeries = acqDataPacketVector[0].m_DateSeries;
779 dataSeries = acqDataPacketVector[0].m_DateSeries;
780 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
780 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
781 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
781 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
782 }
782 }
783 }
783 }
784 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
784 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
785 << acqDataPacketVector.size();
785 << acqDataPacketVector.size();
786 return dataSeries;
786 return dataSeries;
787 }
787 }
788
788
789 void VariableController::VariableControllerPrivate::registerProvider(
789 void VariableController::VariableControllerPrivate::registerProvider(
790 std::shared_ptr<IDataProvider> provider)
790 std::shared_ptr<IDataProvider> provider)
791 {
791 {
792 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
792 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
793 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
793 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
794 << provider->objectName();
794 << provider->objectName();
795 m_ProviderSet.insert(provider);
795 m_ProviderSet.insert(provider);
796 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
796 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
797 &VariableAcquisitionWorker::onVariableDataAcquired);
797 &VariableAcquisitionWorker::onVariableDataAcquired);
798 connect(provider.get(), &IDataProvider::dataProvidedProgress,
798 connect(provider.get(), &IDataProvider::dataProvidedProgress,
799 m_VariableAcquisitionWorker.get(),
799 m_VariableAcquisitionWorker.get(),
800 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
800 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
801 connect(provider.get(), &IDataProvider::dataProvidedFailed,
801 connect(provider.get(), &IDataProvider::dataProvidedFailed,
802 m_VariableAcquisitionWorker.get(),
802 m_VariableAcquisitionWorker.get(),
803 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
803 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
804 }
804 }
805 else {
805 else {
806 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
806 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
807 }
807 }
808 }
808 }
809
809
810 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
810 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
811 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
811 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
812 {
812 {
813 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
813 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
814 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
814 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
815 return QUuid();
815 return QUuid();
816 }
816 }
817
817
818 auto varHandler = itVarHandler->second.get();
818 auto varHandler = itVarHandler->second.get();
819 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
819 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
820 qCCritical(LOG_VariableController())
820 qCCritical(LOG_VariableController())
821 << tr("acceptVariableRequest impossible on a variable with OFF state");
821 << tr("acceptVariableRequest impossible on a variable with OFF state");
822 }
822 }
823
823
824 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
824 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
825 varHandler->m_CanUpdate = true;
825 varHandler->m_CanUpdate = true;
826
826
827 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
827 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
828 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
828 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
829 qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
829 qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
830 << m_VarGroupIdToVarIds.size();
830 << m_VarGroupIdToVarIds.size();
831
831
832 return varHandler->m_RunningVarRequest.m_VariableGroupId;
832 return varHandler->m_RunningVarRequest.m_VariableGroupId;
833 }
833 }
834
834
835 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
835 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
836 {
836 {
837 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
837 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
838 << QThread::currentThread()->objectName() << varRequestId;
838 << QThread::currentThread()->objectName() << varRequestId;
839
839
840 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
840 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
841 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
841 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
842 qCWarning(LOG_VariableController())
842 qCWarning(LOG_VariableController())
843 << tr("Impossible to updateVariables of unknown variables") << varRequestId;
843 << tr("Impossible to updateVariables of unknown variables") << varRequestId;
844 return;
844 return;
845 }
845 }
846
846
847 auto &varIds = varGroupIdToVarIdsIt->second;
847 auto &varIds = varGroupIdToVarIdsIt->second;
848 auto varIdsEnd = varIds.end();
848 auto varIdsEnd = varIds.end();
849 bool processVariableUpdate = true;
849 bool processVariableUpdate = true;
850 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
850 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
851 << varRequestId << varIds.size();
851 << varRequestId << varIds.size();
852 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
852 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
853 ++varIdsIt) {
853 ++varIdsIt) {
854 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
854 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
855 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
855 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
856 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
856 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
857 }
857 }
858 }
858 }
859
859
860 if (processVariableUpdate) {
860 if (processVariableUpdate) {
861 qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
861 qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
862 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
862 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
863 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
863 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
864 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
864 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
865 if (auto var = findVariable(*varIdsIt)) {
865 if (auto var = findVariable(*varIdsIt)) {
866 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
866 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
867 var->setRange(varRequest.m_RangeRequested);
867 var->setRange(varRequest.m_RangeRequested);
868 var->setCacheRange(varRequest.m_CacheRangeRequested);
868 var->setCacheRange(varRequest.m_CacheRangeRequested);
869 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
869 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
870 << varRequest.m_RangeRequested
870 << varRequest.m_RangeRequested
871 << varRequest.m_CacheRangeRequested;
871 << varRequest.m_CacheRangeRequested;
872 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
872 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
873 << var->nbPoints()
873 << var->nbPoints()
874 << varRequest.m_DataSeries->nbPoints();
874 << varRequest.m_DataSeries->nbPoints();
875 var->mergeDataSeries(varRequest.m_DataSeries);
875 var->mergeDataSeries(varRequest.m_DataSeries);
876 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
876 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
877 << var->nbPoints();
877 << var->nbPoints();
878
878
879 emit var->updated();
879 emit var->updated();
880 qCDebug(LOG_VariableController()) << tr("Update OK");
880 qCDebug(LOG_VariableController()) << tr("Update OK");
881 }
881 }
882 else {
882 else {
883 qCCritical(LOG_VariableController())
883 qCCritical(LOG_VariableController())
884 << tr("Impossible to update data to a null variable");
884 << tr("Impossible to update data to a null variable");
885 }
885 }
886 }
886 }
887 }
887 }
888 updateVariableRequest(varRequestId);
888 updateVariableRequest(varRequestId);
889
889
890 // cleaning varRequestId
890 // cleaning varRequestId
891 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
891 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
892 m_VarGroupIdToVarIds.erase(varRequestId);
892 m_VarGroupIdToVarIds.erase(varRequestId);
893 if (m_VarGroupIdToVarIds.empty()) {
893 if (m_VarGroupIdToVarIds.empty()) {
894 emit q->acquisitionFinished();
894 emit q->acquisitionFinished();
895 }
895 }
896 }
896 }
897 }
897 }
898
898
899
899
900 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
900 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
901 {
901 {
902 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
902 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
903 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
903 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
904 qCCritical(LOG_VariableController()) << QObject::tr(
904 qCCritical(LOG_VariableController()) << QObject::tr(
905 "Impossible to updateVariableRequest since varGroupdId isn't here anymore");
905 "Impossible to updateVariableRequest since varGroupdId isn't here anymore");
906
906
907 return;
907 return;
908 }
908 }
909
909
910 auto &varIds = varGroupIdToVarIdsIt->second;
910 auto &varIds = varGroupIdToVarIdsIt->second;
911 auto varIdsEnd = varIds.end();
911 auto varIdsEnd = varIds.end();
912 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
912 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
913 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
913 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
914 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
914 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
915
915
916 auto varHandler = itVarHandler->second.get();
916 auto varHandler = itVarHandler->second.get();
917 varHandler->m_CanUpdate = false;
917 varHandler->m_CanUpdate = false;
918
918
919
919
920 switch (varHandler->m_State) {
920 switch (varHandler->m_State) {
921 case VariableRequestHandlerState::OFF: {
921 case VariableRequestHandlerState::OFF: {
922 qCCritical(LOG_VariableController())
922 qCCritical(LOG_VariableController())
923 << QObject::tr("Impossible to update a variable with handler in OFF state");
923 << QObject::tr("Impossible to update a variable with handler in OFF state");
924 } break;
924 } break;
925 case VariableRequestHandlerState::RUNNING: {
925 case VariableRequestHandlerState::RUNNING: {
926 varHandler->m_State = VariableRequestHandlerState::OFF;
926 varHandler->m_State = VariableRequestHandlerState::OFF;
927 varHandler->m_RunningVarRequest = VariableRequest{};
927 varHandler->m_RunningVarRequest = VariableRequest{};
928 break;
928 break;
929 }
929 }
930 case VariableRequestHandlerState::PENDING: {
930 case VariableRequestHandlerState::PENDING: {
931 varHandler->m_State = VariableRequestHandlerState::RUNNING;
931 varHandler->m_State = VariableRequestHandlerState::RUNNING;
932 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
932 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
933 varHandler->m_PendingVarRequest = VariableRequest{};
933 varHandler->m_PendingVarRequest = VariableRequest{};
934 auto var = findVariable(itVarHandler->first);
934 auto var = findVariable(itVarHandler->first);
935 executeVarRequest(var, varHandler->m_RunningVarRequest);
935 executeVarRequest(var, varHandler->m_RunningVarRequest);
936 updateVariables(varHandler->m_RunningVarRequest.m_VariableGroupId);
936 break;
937 break;
937 }
938 }
938 default:
939 default:
939 qCCritical(LOG_VariableController())
940 qCCritical(LOG_VariableController())
940 << QObject::tr("Unknown VariableRequestHandlerState");
941 << QObject::tr("Unknown VariableRequestHandlerState");
941 }
942 }
942 }
943 }
943 }
944 }
944 }
945 }
945
946
946
947
947 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
948 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
948 {
949 {
949 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId;
950 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId;
950
951
951 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
952 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
952 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
953 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
953 qCCritical(LOG_VariableController())
954 qCCritical(LOG_VariableController())
954 << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId;
955 << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId;
955 return;
956 return;
956 }
957 }
957
958
958 auto &varIds = varGroupIdToVarIdsIt->second;
959 auto &varIds = varGroupIdToVarIdsIt->second;
959 auto varIdsEnd = varIds.end();
960 auto varIdsEnd = varIds.end();
960 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
961 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
961 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
962 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
962 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
963 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
963
964
964 auto varHandler = itVarHandler->second.get();
965 auto varHandler = itVarHandler->second.get();
965 varHandler->m_VarId = QUuid{};
966 varHandler->m_VarId = QUuid{};
966 switch (varHandler->m_State) {
967 switch (varHandler->m_State) {
967 case VariableRequestHandlerState::OFF: {
968 case VariableRequestHandlerState::OFF: {
968 qCWarning(LOG_VariableController())
969 qCWarning(LOG_VariableController())
969 << QObject::tr("Impossible to cancel a variable with no running request");
970 << QObject::tr("Impossible to cancel a variable with no running request");
970 break;
971 break;
971 }
972 }
972 case VariableRequestHandlerState::RUNNING: {
973 case VariableRequestHandlerState::RUNNING: {
973
974
974 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
975 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
975 auto var = findVariable(itVarHandler->first);
976 auto var = findVariable(itVarHandler->first);
976 auto varProvider = m_VariableToProviderMap.at(var);
977 auto varProvider = m_VariableToProviderMap.at(var);
977 if (varProvider != nullptr) {
978 if (varProvider != nullptr) {
978 m_VariableAcquisitionWorker->abortProgressRequested(
979 m_VariableAcquisitionWorker->abortProgressRequested(
979 itVarHandler->first);
980 itVarHandler->first);
980 }
981 }
981 m_VariableModel->setDataProgress(var, 0.0);
982 m_VariableModel->setDataProgress(var, 0.0);
982 varHandler->m_CanUpdate = false;
983 varHandler->m_CanUpdate = false;
983 varHandler->m_State = VariableRequestHandlerState::OFF;
984 varHandler->m_State = VariableRequestHandlerState::OFF;
984 varHandler->m_RunningVarRequest = VariableRequest{};
985 varHandler->m_RunningVarRequest = VariableRequest{};
985 }
986 }
986 else {
987 else {
987 // TODO: log Impossible to cancel the running variable request beacause its
988 // TODO: log Impossible to cancel the running variable request beacause its
988 // varRequestId isn't not the canceled one
989 // varRequestId isn't not the canceled one
989 }
990 }
990 break;
991 break;
991 }
992 }
992 case VariableRequestHandlerState::PENDING: {
993 case VariableRequestHandlerState::PENDING: {
993 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
994 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
994 auto var = findVariable(itVarHandler->first);
995 auto var = findVariable(itVarHandler->first);
995 auto varProvider = m_VariableToProviderMap.at(var);
996 auto varProvider = m_VariableToProviderMap.at(var);
996 if (varProvider != nullptr) {
997 if (varProvider != nullptr) {
997 m_VariableAcquisitionWorker->abortProgressRequested(
998 m_VariableAcquisitionWorker->abortProgressRequested(
998 itVarHandler->first);
999 itVarHandler->first);
999 }
1000 }
1000 m_VariableModel->setDataProgress(var, 0.0);
1001 m_VariableModel->setDataProgress(var, 0.0);
1001 varHandler->m_CanUpdate = false;
1002 varHandler->m_CanUpdate = false;
1002 varHandler->m_State = VariableRequestHandlerState::RUNNING;
1003 varHandler->m_State = VariableRequestHandlerState::RUNNING;
1003 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
1004 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
1004 varHandler->m_PendingVarRequest = VariableRequest{};
1005 varHandler->m_PendingVarRequest = VariableRequest{};
1005 executeVarRequest(var, varHandler->m_RunningVarRequest);
1006 executeVarRequest(var, varHandler->m_RunningVarRequest);
1006 }
1007 }
1007 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
1008 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
1008 varHandler->m_State = VariableRequestHandlerState::RUNNING;
1009 varHandler->m_State = VariableRequestHandlerState::RUNNING;
1009 varHandler->m_PendingVarRequest = VariableRequest{};
1010 varHandler->m_PendingVarRequest = VariableRequest{};
1010 }
1011 }
1011 else {
1012 else {
1012 // TODO: log Impossible to cancel the variable request beacause its
1013 // TODO: log Impossible to cancel the variable request beacause its
1013 // varRequestId isn't not the canceled one
1014 // varRequestId isn't not the canceled one
1014 }
1015 }
1015 break;
1016 break;
1016 }
1017 }
1017 default:
1018 default:
1018 qCCritical(LOG_VariableController())
1019 qCCritical(LOG_VariableController())
1019 << QObject::tr("Unknown VariableRequestHandlerState");
1020 << QObject::tr("Unknown VariableRequestHandlerState");
1020 }
1021 }
1021 }
1022 }
1022 }
1023 }
1023 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
1024 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
1024 m_VarGroupIdToVarIds.erase(varRequestId);
1025 m_VarGroupIdToVarIds.erase(varRequestId);
1025 if (m_VarGroupIdToVarIds.empty()) {
1026 if (m_VarGroupIdToVarIds.empty()) {
1026 emit q->acquisitionFinished();
1027 emit q->acquisitionFinished();
1027 }
1028 }
1028 }
1029 }
1029
1030
1030 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
1031 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
1031 VariableRequest &varRequest)
1032 VariableRequest &varRequest)
1032 {
1033 {
1033 qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest");
1034 qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest");
1034
1035
1035 auto varId = m_VariableToIdentifierMap.at(var);
1036 auto varId = m_VariableToIdentifierMap.at(var);
1036
1037
1037 auto varCacheRange = var->cacheRange();
1038 auto varCacheRange = var->cacheRange();
1038 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
1039 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
1039 auto notInCacheRangeList
1040 auto notInCacheRangeList
1040 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
1041 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
1041 auto inCacheRangeList
1042 auto inCacheRangeList
1042 = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
1043 = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
1043
1044
1044 if (!notInCacheRangeList.empty()) {
1045 if (!notInCacheRangeList.empty()) {
1045
1046
1046 auto varProvider = m_VariableToProviderMap.at(var);
1047 auto varProvider = m_VariableToProviderMap.at(var);
1047 if (varProvider != nullptr) {
1048 if (varProvider != nullptr) {
1048 qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested
1049 qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested
1049 << varRequest.m_CacheRangeRequested;
1050 << varRequest.m_CacheRangeRequested;
1050 m_VariableAcquisitionWorker->pushVariableRequest(
1051 m_VariableAcquisitionWorker->pushVariableRequest(
1051 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
1052 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
1052 varRequest.m_CacheRangeRequested,
1053 varRequest.m_CacheRangeRequested,
1053 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
1054 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
1054 varProvider);
1055 varProvider);
1055 }
1056 }
1056 else {
1057 else {
1057 qCCritical(LOG_VariableController())
1058 qCCritical(LOG_VariableController())
1058 << "Impossible to provide data with a null provider";
1059 << "Impossible to provide data with a null provider";
1059 }
1060 }
1060
1061
1061 if (!inCacheRangeList.empty()) {
1062 if (!inCacheRangeList.empty()) {
1062 emit q->updateVarDisplaying(var, inCacheRangeList.first());
1063 emit q->updateVarDisplaying(var, inCacheRangeList.first());
1063 }
1064 }
1064 }
1065 }
1065 else {
1066 else {
1066 acceptVariableRequest(varId,
1067 acceptVariableRequest(varId,
1067 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
1068 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
1068 }
1069 }
1069 }
1070 }
General Comments 0
You need to be logged in to leave comments. Login now