@@ -1,192 +1,201 | |||||
1 | #include "CosinusProvider.h" |
|
1 | #include "CosinusProvider.h" | |
2 | #include "MockDefs.h" |
|
2 | #include "MockDefs.h" | |
3 |
|
3 | |||
4 | #include <Data/DataProviderParameters.h> |
|
4 | #include <Data/DataProviderParameters.h> | |
5 | #include <Data/ScalarSeries.h> |
|
5 | #include <Data/ScalarSeries.h> | |
6 | #include <Data/VectorSeries.h> |
|
6 | #include <Data/VectorSeries.h> | |
7 |
|
7 | |||
8 | #include <cmath> |
|
8 | #include <cmath> | |
9 |
|
9 | |||
10 | #include <QFuture> |
|
10 | #include <QFuture> | |
11 | #include <QThread> |
|
11 | #include <QThread> | |
12 | #include <QtConcurrent/QtConcurrent> |
|
12 | #include <QtConcurrent/QtConcurrent> | |
13 |
|
13 | |||
14 | Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") |
|
14 | Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") | |
15 |
|
15 | |||
16 | namespace { |
|
16 | namespace { | |
17 |
|
17 | |||
18 | /// Abstract cosinus type |
|
18 | /// Abstract cosinus type | |
19 | struct ICosinusType { |
|
19 | struct ICosinusType { | |
20 | virtual ~ICosinusType() = default; |
|
20 | virtual ~ICosinusType() = default; | |
21 | /// @return the number of components generated for the type |
|
21 | /// @return the number of components generated for the type | |
22 | virtual int componentCount() const = 0; |
|
22 | virtual int componentCount() const = 0; | |
23 | /// @return the data series created for the type |
|
23 | /// @return the data series created for the type | |
24 | virtual std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, |
|
24 | virtual std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, | |
25 | std::vector<double> valuesData, |
|
25 | std::vector<double> valuesData, | |
26 | Unit xAxisUnit, |
|
26 | Unit xAxisUnit, | |
27 | Unit valuesUnit) const = 0; |
|
27 | Unit valuesUnit) const = 0; | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | struct ScalarCosinus : public ICosinusType { |
|
30 | struct ScalarCosinus : public ICosinusType { | |
31 | int componentCount() const override { return 1; } |
|
31 | int componentCount() const override { return 1; } | |
32 |
|
32 | |||
33 | std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, |
|
33 | std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, | |
34 | std::vector<double> valuesData, Unit xAxisUnit, |
|
34 | std::vector<double> valuesData, Unit xAxisUnit, | |
35 | Unit valuesUnit) const override |
|
35 | Unit valuesUnit) const override | |
36 | { |
|
36 | { | |
37 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), |
|
37 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), | |
38 | xAxisUnit, valuesUnit); |
|
38 | xAxisUnit, valuesUnit); | |
39 | } |
|
39 | } | |
40 | }; |
|
40 | }; | |
41 | struct VectorCosinus : public ICosinusType { |
|
41 | struct VectorCosinus : public ICosinusType { | |
42 | int componentCount() const override { return 3; } |
|
42 | int componentCount() const override { return 3; } | |
43 |
|
43 | |||
44 | std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, |
|
44 | std::shared_ptr<IDataSeries> createDataSeries(std::vector<double> xAxisData, | |
45 | std::vector<double> valuesData, Unit xAxisUnit, |
|
45 | std::vector<double> valuesData, Unit xAxisUnit, | |
46 | Unit valuesUnit) const override |
|
46 | Unit valuesUnit) const override | |
47 | { |
|
47 | { | |
48 | return std::make_shared<VectorSeries>(std::move(xAxisData), std::move(valuesData), |
|
48 | return std::make_shared<VectorSeries>(std::move(xAxisData), std::move(valuesData), | |
49 | xAxisUnit, valuesUnit); |
|
49 | xAxisUnit, valuesUnit); | |
50 | } |
|
50 | } | |
51 | }; |
|
51 | }; | |
52 |
|
52 | |||
53 | /// Converts string to cosinus type |
|
53 | /// Converts string to cosinus type | |
54 | /// @return the cosinus type if the string could be converted, nullptr otherwise |
|
54 | /// @return the cosinus type if the string could be converted, nullptr otherwise | |
55 | std::unique_ptr<ICosinusType> cosinusType(const QString &type) noexcept |
|
55 | std::unique_ptr<ICosinusType> cosinusType(const QString &type) noexcept | |
56 | { |
|
56 | { | |
57 | if (type.compare(QStringLiteral("scalar"), Qt::CaseInsensitive) == 0) { |
|
57 | if (type.compare(QStringLiteral("scalar"), Qt::CaseInsensitive) == 0) { | |
58 | return std::make_unique<ScalarCosinus>(); |
|
58 | return std::make_unique<ScalarCosinus>(); | |
59 | } |
|
59 | } | |
60 | else if (type.compare(QStringLiteral("vector"), Qt::CaseInsensitive) == 0) { |
|
60 | else if (type.compare(QStringLiteral("vector"), Qt::CaseInsensitive) == 0) { | |
61 | return std::make_unique<VectorCosinus>(); |
|
61 | return std::make_unique<VectorCosinus>(); | |
62 | } |
|
62 | } | |
63 | else { |
|
63 | else { | |
64 | return nullptr; |
|
64 | return nullptr; | |
65 | } |
|
65 | } | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | } // namespace |
|
68 | } // namespace | |
69 |
|
69 | |||
70 | std::shared_ptr<IDataProvider> CosinusProvider::clone() const |
|
70 | std::shared_ptr<IDataProvider> CosinusProvider::clone() const | |
71 | { |
|
71 | { | |
72 | // No copy is made in clone |
|
72 | // No copy is made in clone | |
73 | return std::make_shared<CosinusProvider>(); |
|
73 | return std::make_shared<CosinusProvider>(); | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid acqIdentifier, |
|
76 | std::shared_ptr<IDataSeries> CosinusProvider::retrieveData(QUuid acqIdentifier, | |
77 | const SqpRange &dataRangeRequested, |
|
77 | const SqpRange &dataRangeRequested, | |
78 | const QVariantHash &data) |
|
78 | const QVariantHash &data) | |
79 | { |
|
79 | { | |
80 | // TODO: Add Mutex |
|
80 | // TODO: Add Mutex | |
81 | auto dataIndex = 0; |
|
81 | auto dataIndex = 0; | |
82 |
|
82 | |||
83 | // Retrieves cosinus type |
|
83 | // Retrieves cosinus type | |
84 | auto typeVariant = data.value(COSINUS_TYPE_KEY, COSINUS_TYPE_DEFAULT_VALUE); |
|
84 | auto typeVariant = data.value(COSINUS_TYPE_KEY, COSINUS_TYPE_DEFAULT_VALUE); | |
85 | if (!typeVariant.canConvert<QString>()) { |
|
85 | if (!typeVariant.canConvert<QString>()) { | |
86 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid type"); |
|
86 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid type"); | |
87 | return nullptr; |
|
87 | return nullptr; | |
88 | } |
|
88 | } | |
89 |
|
89 | |||
90 | auto type = cosinusType(typeVariant.toString()); |
|
90 | auto type = cosinusType(typeVariant.toString()); | |
91 | if (!type) { |
|
91 | if (!type) { | |
92 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: unknown type"); |
|
92 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: unknown type"); | |
93 | return nullptr; |
|
93 | return nullptr; | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | // Retrieves frequency |
|
96 | // Retrieves frequency | |
97 | auto freqVariant = data.value(COSINUS_FREQUENCY_KEY, COSINUS_FREQUENCY_DEFAULT_VALUE); |
|
97 | auto freqVariant = data.value(COSINUS_FREQUENCY_KEY, COSINUS_FREQUENCY_DEFAULT_VALUE); | |
98 | if (!freqVariant.canConvert<double>()) { |
|
98 | if (!freqVariant.canConvert<double>()) { | |
99 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid frequency"); |
|
99 | qCCritical(LOG_CosinusProvider()) << tr("Can't retrieve data: invalid frequency"); | |
100 | return nullptr; |
|
100 | return nullptr; | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | // Gets the timerange from the parameters |
|
103 | // Gets the timerange from the parameters | |
104 | double freq = freqVariant.toDouble(); |
|
104 | double freq = freqVariant.toDouble(); | |
105 | double start = std::ceil(dataRangeRequested.m_TStart * freq); |
|
105 | double start = std::ceil(dataRangeRequested.m_TStart * freq); | |
106 | double end = std::floor(dataRangeRequested.m_TEnd * freq); |
|
106 | double end = std::floor(dataRangeRequested.m_TEnd * freq); | |
107 |
|
107 | |||
108 | // We assure that timerange is valid |
|
108 | // We assure that timerange is valid | |
109 | if (end < start) { |
|
109 | if (end < start) { | |
110 | std::swap(start, end); |
|
110 | std::swap(start, end); | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | // Generates scalar series containing cosinus values (one value per second, end value is |
|
113 | // Generates scalar series containing cosinus values (one value per second, end value is | |
114 | // included) |
|
114 | // included) | |
115 | auto dataCount = end - start + 1; |
|
115 | auto dataCount = end - start + 1; | |
116 |
|
116 | |||
|
117 | // Number of components (depending on the cosinus type) | |||
|
118 | auto componentCount = type->componentCount(); | |||
|
119 | ||||
117 | auto xAxisData = std::vector<double>{}; |
|
120 | auto xAxisData = std::vector<double>{}; | |
118 | xAxisData.resize(dataCount); |
|
121 | xAxisData.resize(dataCount); | |
119 |
|
122 | |||
120 | auto valuesData = std::vector<double>{}; |
|
123 | auto valuesData = std::vector<double>{}; | |
121 | valuesData.resize(dataCount); |
|
124 | valuesData.resize(dataCount * componentCount); | |
122 |
|
125 | |||
123 | int progress = 0; |
|
126 | int progress = 0; | |
124 | auto progressEnd = dataCount; |
|
127 | auto progressEnd = dataCount; | |
125 | for (auto time = start; time <= end; ++time, ++dataIndex) { |
|
128 | for (auto time = start; time <= end; ++time, ++dataIndex) { | |
126 | auto it = m_VariableToEnableProvider.find(acqIdentifier); |
|
129 | auto it = m_VariableToEnableProvider.find(acqIdentifier); | |
127 | if (it != m_VariableToEnableProvider.end() && it.value()) { |
|
130 | if (it != m_VariableToEnableProvider.end() && it.value()) { | |
128 | const auto timeOnFreq = time / freq; |
|
131 | const auto timeOnFreq = time / freq; | |
129 |
|
132 | |||
130 | xAxisData[dataIndex] = timeOnFreq; |
|
133 | xAxisData[dataIndex] = timeOnFreq; | |
131 | valuesData[dataIndex] = std::cos(timeOnFreq); |
|
134 | ||
|
135 | // Generates all components' values | |||
|
136 | // Example: for a vector, values will be : cos(x), cos(x)/2, cos(x)/3 | |||
|
137 | auto value = std::cos(timeOnFreq); | |||
|
138 | for (auto i = 0; i < componentCount; ++i) { | |||
|
139 | valuesData[componentCount * dataIndex + i] = value / (i + 1); | |||
|
140 | } | |||
132 |
|
141 | |||
133 | // progression |
|
142 | // progression | |
134 | int currentProgress = (time - start) * 100.0 / progressEnd; |
|
143 | int currentProgress = (time - start) * 100.0 / progressEnd; | |
135 | if (currentProgress != progress) { |
|
144 | if (currentProgress != progress) { | |
136 | progress = currentProgress; |
|
145 | progress = currentProgress; | |
137 |
|
146 | |||
138 | emit dataProvidedProgress(acqIdentifier, progress); |
|
147 | emit dataProvidedProgress(acqIdentifier, progress); | |
139 | qCInfo(LOG_CosinusProvider()) << "TORM: CosinusProvider::retrieveData" |
|
148 | qCInfo(LOG_CosinusProvider()) << "TORM: CosinusProvider::retrieveData" | |
140 | << QThread::currentThread()->objectName() << progress; |
|
149 | << QThread::currentThread()->objectName() << progress; | |
141 | // NOTE: Try to use multithread if possible |
|
150 | // NOTE: Try to use multithread if possible | |
142 | } |
|
151 | } | |
143 | } |
|
152 | } | |
144 | else { |
|
153 | else { | |
145 | if (!it.value()) { |
|
154 | if (!it.value()) { | |
146 | qCDebug(LOG_CosinusProvider()) |
|
155 | qCDebug(LOG_CosinusProvider()) | |
147 | << "CosinusProvider::retrieveData: ARRET De l'acquisition detectΓ©" |
|
156 | << "CosinusProvider::retrieveData: ARRET De l'acquisition detectΓ©" | |
148 | << end - time; |
|
157 | << end - time; | |
149 | } |
|
158 | } | |
150 | } |
|
159 | } | |
151 | } |
|
160 | } | |
152 | if (progress != 100) { |
|
161 | if (progress != 100) { | |
153 | // We can close progression beacause all data has been retrieved |
|
162 | // We can close progression beacause all data has been retrieved | |
154 | emit dataProvidedProgress(acqIdentifier, 100); |
|
163 | emit dataProvidedProgress(acqIdentifier, 100); | |
155 | } |
|
164 | } | |
156 |
return |
|
165 | return type->createDataSeries(std::move(xAxisData), std::move(valuesData), | |
157 |
|
|
166 | Unit{QStringLiteral("t"), true}, Unit{}); | |
158 | } |
|
167 | } | |
159 |
|
168 | |||
160 | void CosinusProvider::requestDataLoading(QUuid acqIdentifier, |
|
169 | void CosinusProvider::requestDataLoading(QUuid acqIdentifier, | |
161 | const DataProviderParameters ¶meters) |
|
170 | const DataProviderParameters ¶meters) | |
162 | { |
|
171 | { | |
163 | // TODO: Add Mutex |
|
172 | // TODO: Add Mutex | |
164 | m_VariableToEnableProvider[acqIdentifier] = true; |
|
173 | m_VariableToEnableProvider[acqIdentifier] = true; | |
165 | qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::requestDataLoading" |
|
174 | qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::requestDataLoading" | |
166 | << QThread::currentThread()->objectName(); |
|
175 | << QThread::currentThread()->objectName(); | |
167 | // NOTE: Try to use multithread if possible |
|
176 | // NOTE: Try to use multithread if possible | |
168 | const auto times = parameters.m_Times; |
|
177 | const auto times = parameters.m_Times; | |
169 |
|
178 | |||
170 | for (const auto &dateTime : qAsConst(times)) { |
|
179 | for (const auto &dateTime : qAsConst(times)) { | |
171 | if (m_VariableToEnableProvider[acqIdentifier]) { |
|
180 | if (m_VariableToEnableProvider[acqIdentifier]) { | |
172 | auto scalarSeries = this->retrieveData(acqIdentifier, dateTime, parameters.m_Data); |
|
181 | auto scalarSeries = this->retrieveData(acqIdentifier, dateTime, parameters.m_Data); | |
173 | qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::dataProvided"; |
|
182 | qCDebug(LOG_CosinusProvider()) << "TORM: CosinusProvider::dataProvided"; | |
174 | emit dataProvided(acqIdentifier, scalarSeries, dateTime); |
|
183 | emit dataProvided(acqIdentifier, scalarSeries, dateTime); | |
175 | } |
|
184 | } | |
176 | } |
|
185 | } | |
177 | } |
|
186 | } | |
178 |
|
187 | |||
179 | void CosinusProvider::requestDataAborting(QUuid acqIdentifier) |
|
188 | void CosinusProvider::requestDataAborting(QUuid acqIdentifier) | |
180 | { |
|
189 | { | |
181 | // TODO: Add Mutex |
|
190 | // TODO: Add Mutex | |
182 | qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataAborting" << acqIdentifier |
|
191 | qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataAborting" << acqIdentifier | |
183 | << QThread::currentThread()->objectName(); |
|
192 | << QThread::currentThread()->objectName(); | |
184 | auto it = m_VariableToEnableProvider.find(acqIdentifier); |
|
193 | auto it = m_VariableToEnableProvider.find(acqIdentifier); | |
185 | if (it != m_VariableToEnableProvider.end()) { |
|
194 | if (it != m_VariableToEnableProvider.end()) { | |
186 | it.value() = false; |
|
195 | it.value() = false; | |
187 | } |
|
196 | } | |
188 | else { |
|
197 | else { | |
189 | qCWarning(LOG_CosinusProvider()) |
|
198 | qCWarning(LOG_CosinusProvider()) | |
190 | << tr("Aborting progression of inexistant identifier detected !!!"); |
|
199 | << tr("Aborting progression of inexistant identifier detected !!!"); | |
191 | } |
|
200 | } | |
192 | } |
|
201 | } |
General Comments 0
You need to be logged in to leave comments.
Login now