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