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