CuteHMI - Data Acquisition (CuteHMI.DataAcquisition.0)
HistoryWriter.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_HISTORYWRITER_HPP
2 #define H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_HISTORYWRITER_HPP
3 
4 #include "internal/common.hpp"
5 #include "internal/HistoryCollective.hpp"
6 #include "AbstractWriter.hpp"
7 
8 #include <cutehmi/services/Serviceable.hpp>
9 
10 #include <QTimer>
11 
12 namespace cutehmi {
13 namespace dataacquisition {
14 
15 class CUTEHMI_DATAACQUISITION_API HistoryWriter:
16  public AbstractWriter,
17  private internal::DbServiceableMixin<HistoryWriter>
18 
19 {
20  Q_OBJECT
21 
23 
24  public:
25  static constexpr int INITIAL_INTERVAL = 100;
26  static constexpr int INITIAL_SAMPLES = 100;
27 
34  Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
35 
36 
42  Q_PROPERTY(int samples READ samples WRITE setSamples NOTIFY samplesChanged)
43 
44  HistoryWriter(QObject * parent = nullptr);
45 
46  int interval() const;
47 
48  void setInterval(int interval);
49 
50  int samples() const;
51 
52  void setSamples(int samples);
53 
54  std::unique_ptr<ServiceStatuses> configureStarting(QState * starting) override;
55 
56  std::unique_ptr<ServiceStatuses> configureStarted(QState * active, const QState * idling, const QState * yielding) override;
57 
58  std::unique_ptr<ServiceStatuses> configureStopping(QState * stopping) override;
59 
60  std::unique_ptr<ServiceStatuses> configureBroken(QState * broken) override;
61 
62  std::unique_ptr<ServiceStatuses> configureRepairing(QState * repairing) override;
63 
64  std::unique_ptr<ServiceStatuses> configureEvacuating(QState * evacuating) override;
65 
66  std::unique_ptr<QAbstractTransition> transitionToStarted() const override;
67 
68  std::unique_ptr<QAbstractTransition> transitionToStopped() const override;
69 
70  std::unique_ptr<QAbstractTransition> transitionToBroken() const override;
71 
72  std::unique_ptr<QAbstractTransition> transitionToYielding() const override;
73 
74  std::unique_ptr<QAbstractTransition> transitionToIdling() const override;
75 
76  signals:
77  void intervalChanged();
78 
79  void samplesChanged();
80 
81  protected slots:
82  void sampleValues();
83 
84  void insertValues();
85 
86  CUTEHMI_PROTECTED_SIGNALS:
87  void initialized();
88 
89  void samplingTimerStarted();
90 
91  void samplingTimerStopped();
92 
93  void schemaValidatedTrue();
94 
95  void schemaValidatedFalse();
96 
97  void insertValuesBegan();
98 
99  void collectiveFinished();
100 
101  private slots:
102  void onSchemaChanged();
103 
104  void initialize();
105 
106  void adjustSamplingTimer();
107 
108  void startSamplingTimer();
109 
110  void stopSamplingTimer();
111 
112  void confirmCollectiveFinished();
113 
114  private:
115  std::unique_ptr<ServiceStatuses> configureStartingOrRepairing(QState * parent);
116 
117  void clearData();
118 
119  template <typename T>
120  void addSample(T value, internal::HistoryCollective::Tuple & tuple);
121 
122  struct Members
123  {
125  internal::HistoryCollective dbCollective;
126  QTimer samplingTimer;
127  int interval;
128  int samples;
129  int sampleCounter;
130 
131  Members():
132  interval(INITIAL_INTERVAL),
133  samples(INITIAL_SAMPLES),
134  sampleCounter(0)
135  {
136  }
137  };
138 
139  MPtr<Members> m;
140 };
141 
142 }
143 }
144 
145 #endif
146 
147 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
148 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
149 //(c)C: This file is a part of CuteHMI.
150 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
151 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
152 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
153 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
154 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
155 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
156 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
QTimer
QState
cutehmi::dataacquisition::internal::HistoryCollective
Definition: HistoryCollective.hpp:14
cutehmi::MPtr< Members >
QObject
cutehmi
QAbstractTransition
cutehmi::dataacquisition::AbstractWriter
Abstract database writer.
Definition: AbstractWriter.hpp:20
std
QHash
cutehmi::dataacquisition::internal::DbServiceableMixin
Definition: DbServiceableMixin.hpp:16
cutehmi::dataacquisition::HistoryWriter
Definition: HistoryWriter.hpp:15