CuteHMI - Services (CuteHMI.Services.3)
ServiceStateInterface.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_SERVICES_3_SRC_CUTEHMI_SERVICES_INTERNAL_SERVICESTATEINTERFACE_HPP
2#define H_EXTENSIONS_CUTEHMI_SERVICES_3_SRC_CUTEHMI_SERVICES_INTERNAL_SERVICESTATEINTERFACE_HPP
3
4#include "internal/common.hpp"
5#include "StateInterface.hpp"
6#include "ServiceStartedStateInterface.hpp"
7#include "ServiceStateMachine.hpp"
8
9#include <QState>
10#include <QTimer>
11#include <QStateMachine>
12
13#include <memory>
14
15namespace cutehmi {
16namespace services {
17
18class Serviceable;
19
20namespace internal {
21
22class CUTEHMI_SERVICES_PRIVATE ServiceStateInterface:
23 public StateInterface
24{
25 Q_OBJECT
26
27 public:
28 Q_PROPERTY(QString status READ status WRITE setStatus NOTIFY statusChanged)
29
31
32 ~ServiceStateInterface() override;
33
35
36 QString status() const;
37
38 void setStatus(const QString & status);
39
40 bool isShutdown() const;
41
42 ServiceStateMachine * stateMachine() const;
43
44 void configureServiceable(Serviceable * serviceable);
45
46 QAbstractState * stopped() const override;
47
48 QAbstractState * starting() const override;
49
50 QAbstractState * started() const override;
51
52 QAbstractState * stopping() const override;
53
54 QAbstractState * broken() const override;
55
56 QAbstractState * repairing() const override;
57
58 QAbstractState * evacuating() const override;
59
60 QAbstractState * interrupted() const override;
61
62 StartedStateInterface * startedStates() const override;
63
64 void reconfigureStopped(const Serviceable & serviceable);
65
66 void reconfigureStarting(Serviceable & serviceable);
67
68 void reconfigureStarted(Serviceable & serviceable);
69
70 void reconfigureStopping(Serviceable & serviceable);
71
72 void reconfigureBroken(Serviceable & serviceable);
73
74 void reconfigureRepairing(Serviceable & serviceable);
75
76 void reconfigureEvacuating(Serviceable & serviceable);
77
78 void reconfigureInterrupted(const Serviceable & serviceable);
79
80 void replaceTransitionToStarted(const Serviceable & serviceable);
81
82 void replaceTransitionToStopped(const Serviceable & serviceable);
83
84 void replaceTransitionToBroken(const Serviceable & serviceable);
85
86 void replaceTransitionToYielding(const Serviceable & serviceable);
87
88 void replaceTransitionToIdling(const Serviceable & serviceable);
89
99 void shutdown();
100
101 signals:
102 void statusChanged(const QString & status);
103
104 private:
105 QState * startingPersistent() const;
106
107 QState * startedPersistent() const;
108
110
111 QState * stoppingPersistent() const;
112
113 QState * stoppedPersistent() const;
114
115 QState * brokenPersistent() const;
116
117 QState * repairingPersistent() const;
118
119 QState * evacuatingPersistent() const;
120
121 QState * interruptedPersistent() const;
122
123 QState * startingEphemeric() const;
124
125 QState * startedEphemeric() const;
126
127 QState * stoppingEphemeric() const;
128
129 QState * stoppedEphemeric() const;
130
131 QState * brokenEphemeric() const;
132
133 QState * repairingEphemeric() const;
134
135 QState * evacuatingEphemeric() const;
136
137 QState * interruptedEphemeric() const;
138
139 QAbstractTransition *& startingTransition(int index);
140
141 QAbstractTransition * const & startingTransition(int index) const;
142
143 QAbstractTransition *& startedTransition(int index);
144
145 QAbstractTransition * const & startedTransition(int index) const;
146
147 QAbstractTransition *& stoppingTransition(int index);
148
149 QAbstractTransition * const & stoppingTransition(int index) const;
150
151 QAbstractTransition *& stoppedTransition(int index);
152
153 QAbstractTransition * const & stoppedTransition(int index) const;
154
155 QAbstractTransition *& brokenTransition(int index);
156
157 QAbstractTransition * const & brokenTransition(int index) const;
158
159 QAbstractTransition *& repairingTransition(int index);
160
161 QAbstractTransition * const & repairingTransition(int index) const;
162
163 QAbstractTransition *& evacuatingTransition(int index);
164
165 QAbstractTransition * const & evacuatingTransition(int index) const;
166
167 void initializePersistentStates();
168
169 void resetEphemericStates();
170
171 void resetStartingEphemeric();
172
173 void resetStartedEphemeric();
174
175 void resetStoppingEphemeric();
176
177 void resetStoppedEphemeric();
178
179 void resetBrokenEphemeric();
180
181 void resetRepairingEphemeric();
182
183 void resetEvacuatingEphemeric();
184
185 void resetInterruptedEphemeric();
186
187 void setUpStopped(bool reconfigure, const Serviceable & serviceable);
188
189 void setUpStarting(bool reconfigure, Serviceable & serviceable);
190
191 void setUpStarted(bool reconfigure, Serviceable & serviceable);
192
193 void setUpStopping(bool reconfigure, Serviceable & serviceable);
194
195 void setUpBroken(bool reconfigure, Serviceable & serviceable);
196
197 void setUpRepairing(bool reconfigure, Serviceable & serviceable);
198
199 void setUpEvacuating(bool reconfigure, Serviceable & serviceable);
200
201 void setUpInterrupted(bool reconfigure, const Serviceable & serviceable);
202
207 void addStoppedTransition(int index);
208
214 void addStartingTransition(int index, const Serviceable & serviceable);
215
221 void addStartedTransition(int index, const Serviceable & serviceable);
222
228 void addStoppingTransition(int index, const Serviceable & serviceable);
229
234 void addBrokenTransition(int index);
235
241 void addRepairingTransition(int index, const Serviceable & serviceable);
242
248 void addEvacuatingTransition(int index, const Serviceable & serviceable);
249
254 void addInterrputedTransition(int index);
255
256 private:
257 struct Members {
258 QTimer timeoutTimer;
259
260 // Variable m->lastNotifiableState is used to prevent notification spam, i.e. when service fails to leave notifiable state
261 // through intermediate, non-notifiable state (e.g. 'broken' is a notifiable state, 'repairing' is an intermediate state;
262 // without the condition "Service 'XYZ' broke" message would be posted after each failed repair attempt).
263 QState * lastNotifiableState = nullptr;
264
265 ServiceStateMachine * stateMachine;
266 struct {
267 QState * persistent;
268 QState * ephemeric;
270 } starting;
271 struct {
272 QState * persistent;
273 QState * ephemeric;
276 } started;
277 struct {
278 QState * persistent;
279 QState * ephemeric;
281 } stopping;
282 struct {
283 QState * persistent;
284 QState * ephemeric;
286 } stopped;
287 struct {
288 QState * persistent;
289 QState * ephemeric;
291 } broken;
292 struct {
293 QState * persistent;
294 QState * ephemeric;
296 } repairing;
297 struct {
298 QState * persistent;
299 QState * ephemeric;
301 } evacuating;
302 struct {
303 QState * persistent;
304 QState * ephemeric;
306 } interrupted;
307 QString status;
308 bool isShutdown;
309
310 Members(ServiceStateInterface * p_parent):
311 stateMachine(new ServiceStateMachine(p_parent)),
312 starting{new QState(stateMachine), nullptr, {}},
313 started{new QState(stateMachine), nullptr, {}, nullptr},
314 stopping{new QState(stateMachine), nullptr, {}},
315 stopped{new QState(stateMachine), nullptr, {}},
316 broken{new QState(stateMachine), nullptr, {}},
317 repairing{new QState(stateMachine), nullptr, {}},
318 evacuating{new QState(stateMachine), nullptr, {}},
319 interrupted{new QState(stateMachine), nullptr, {}},
320 isShutdown(false)
321 {
322 started.interface = new ServiceStartedStateInterface(p_parent, started.persistent);
323 }
324 };
325
326 MPtr<Members> m;
327};
328
329}
330}
331}
332
333#endif
334
335//(c)C: Copyright © 2022-2023, Michał Policht <michal@policht.pl>. All rights reserved.
336//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
337//(c)C: This file is a part of CuteHMI.
338//(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.
339//(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.
340//(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/>.
341//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
342//(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:
343//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
344//(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.
Serviceable interface.
Definition: Serviceable.hpp:33
Substates of started state of the state interface.
Definition: StartedStateInterface.hpp:24
State interface.
Definition: StateInterface.hpp:30
AbstractService * service() const
Definition: StateInterface.cpp:27
Definition: ServiceStartedStateInterface.hpp:16
Definition: ServiceStateInterface.hpp:24
Service state machine.
Definition: ServiceStateMachine.hpp:24
T internal(T... args)