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