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