CuteHMI - Services (CuteHMI.Services.2)
Service.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_SERVICES_2_INCLUDE_CUTEHMI_SERVICES_SERVICE_HPP
2 #define H_EXTENSIONS_CUTEHMI_SERVICES_2_INCLUDE_CUTEHMI_SERVICES_SERVICE_HPP
3 
4 #include "internal/common.hpp"
5 #include "internal/StateInterface.hpp"
6 #include "Serviceable.hpp"
7 
8 #include <QString>
9 #include <QVariant>
10 #include <QObject>
11 #include <QStateMachine>
12 #include <QTimer>
13 
14 namespace cutehmi {
15 namespace services {
16 
22 class CUTEHMI_SERVICES_API Service:
23  public QObject
24 {
25  Q_OBJECT
26 
27  friend class ServiceManager;
28 
29  public:
30  static constexpr int INITIAL_STOP_TIMEOUT = 30000;
31  static constexpr int INITIAL_START_TIMEOUT = 30000;
32  static constexpr int INITIAL_REPAIR_TIMEOUT = 30000;
33  static constexpr const char * INITIAL_NAME = "Unnamed Service";
34 
35  Q_PROPERTY(int stopTimeout READ stopTimeout WRITE setStopTimeout NOTIFY stopTimeoutChanged)
36  Q_PROPERTY(int startTimeout READ startTimeout WRITE setStartTimeout NOTIFY startTimeoutChanged)
37  Q_PROPERTY(int repairTimeout READ repairTimeout WRITE setRepairTimeout NOTIFY repairTimeoutChanged)
38  Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
39  Q_PROPERTY(QString status READ status NOTIFY statusChanged)
40  Q_PROPERTY(QVariant serviceable READ serviceable WRITE setServiceable NOTIFY serviceableChanged)
41 
42  Q_CLASSINFO("DefaultProperty", "serviceable")
43 
44  Service(QObject * parent = nullptr);
45 
46  ~Service() override;
47 
48  int stopTimeout() const;
49 
55  void setStopTimeout(int stopTimeout);
56 
57  int startTimeout() const;
58 
64  void setStartTimeout(int startTimeout);
65 
66  int repairTimeout() const;
67 
73  void setRepairTimeout(int repairTimeout);
74 
75  QString name() const;
76 
77  void setName(const QString & name);
78 
79  QString status() const;
80 
87  void setServiceable(QVariant serviceable);
88 
93  QVariant serviceable() const;
94 
113  Q_INVOKABLE QAbstractState * findState(const QString & name) const;
114 
115  public slots:
116  void start();
117 
118  void stop();
119 
120  signals:
121  void stopTimeoutChanged();
122 
123  void startTimeoutChanged();
124 
125  void repairTimeoutChanged();
126 
127  void nameChanged();
128 
129  void statusChanged();
130 
131  void serviceableChanged();
132 
133  void started();
134 
135  void stopped();
136 
137  void activated();
138 
139  protected:
140  internal::StateInterface * stateInterface();
141 
142  const internal::StateInterface * stateInterface() const;
143 
144  protected slots:
145  void activate();
146 
147  void setStatus(const QString & status);
148 
149  private:
150  static QString & DefaultStatus();
151 
152  void destroyStateMachine();
153 
154  void initializeStateMachine(Serviceable & serviceable);
155 
156  void addTransition(QState * source, QState * target, std::unique_ptr<QAbstractTransition>);
157 
158  void addStatuses(std::unique_ptr<Serviceable::ServiceStatuses> statuses);
159 
160  struct Members {
161  int stopTimeout = INITIAL_STOP_TIMEOUT;
162  int startTimeout = INITIAL_START_TIMEOUT;
163  int repairTimeout = INITIAL_REPAIR_TIMEOUT;
164  QString name = INITIAL_NAME;
165  QString status;
166  Serviceable * serviceable = nullptr;
167  QStateMachine * stateMachine = nullptr;
168  internal::StateInterface * stateInterface = nullptr;
169  QTimer timeoutTimer;
170  QState * lastNotifiableState = nullptr;
171  };
172 
173  MPtr<Members> m;
174 };
175 
176 }
177 }
178 
179 #endif
180 
181 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>. All rights reserved.
182 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
183 //(c)C: This file is a part of CuteHMI.
184 //(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.
185 //(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.
186 //(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/>.
187 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
188 //(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:
189 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
190 //(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
QAbstractState
QState
QStateMachine
cutehmi::services::Service
Service.
Definition: Service.hpp:22
cutehmi::services::internal::StateInterface
Definition: StateInterface.hpp:16
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::services::Serviceable
Serviceable interface.
Definition: Serviceable.hpp:29
QAbstractTransition
std
cutehmi::services::ServiceManager
Service manager.
Definition: ServiceManager.hpp:22
QVariant