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