CuteHMI - Services (CuteHMI.Services.3)
ServiceAutoRepair.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_SERVICES_3_INCLUDE_CUTEHMI_SERVICES_SERVICEAUTOREPAIR_HPP
2#define H_EXTENSIONS_CUTEHMI_SERVICES_3_INCLUDE_CUTEHMI_SERVICES_SERVICEAUTOREPAIR_HPP
3
4#include "internal/common.hpp"
5
6#include "AbstractServiceController.hpp"
7
8#include <QHash>
9
10#include <memory>
11
12class QAbstractState;
13class QTimer;
14
15namespace cutehmi {
16namespace services {
17
18class CUTEHMI_SERVICES_API ServiceAutoRepair:
20 public QQmlParserStatus
21{
22 Q_OBJECT
23 QML_NAMED_ELEMENT(ServiceAutoRepair)
24 Q_INTERFACES(QQmlParserStatus)
25
26 public:
27 static constexpr int INITIAL_INITIAL_INTERVAL = 10000;
28
29 static constexpr const char * INITIAL_INTERVAL_FUNCTION = "(function (previousInterval) { return previousInterval; })";
30
31 Q_PROPERTY(int initialInterval READ initialInterval WRITE setInitialInterval NOTIFY initialIntervalChanged)
32
33 Q_PROPERTY(QJSValue intervalFunction READ intervalFunction WRITE setIntervalFunction NOTIFY intervalFunctionChanged)
34
35 explicit ServiceAutoRepair(QObject * parent = nullptr);
36
37 ~ServiceAutoRepair() override;
38
39 QJSValue intervalFunction() const;
40
41 int initialInterval() const;
42
43 void setInitialInterval(int initialInterval);
44
45 void setIntervalFunction(const QJSValue & intervalFunction);
46
47 void setIntervalFunction(const QString & intervalFunction);
48
49 void subscribe(AbstractService * service) override;
50
51 void unsubscribe(AbstractService * service) override;
52
53 void classBegin() override;
54
55 void componentComplete() override;
56
57 signals:
59
61
62 private:
63 struct ServiceEntry {
64 QTimer * timer;
65 QMetaObject::Connection startingEnteredConnection;
66 QMetaObject::Connection startedEnteredConnection;
67 QMetaObject::Connection repairingEnteredConnection;
68 QMetaObject::Connection brokenEnteredConnection;
69 };
70
71 typedef QHash<AbstractService *, ServiceEntry *> ServiceDataContainer;
72
73 // Helper engine to evaulate interval strings (QJSEngine related to QJSValue must be available when QJSValue::call() is
74 // made). If QML engine associated with the object is not available then static instance of QJSEngine is returned. This may
75 // cause problems however, because JSValue values can not be reassigned to another engine, thus try to use QML engine
76 // associated with the object if possible.
77 static QJSEngine & JSEngine(const QObject & object);
78
79 QMetaObject::Connection connectResetIntervalOnStateEntered(const QAbstractState * state, QTimer * timer);
80
81 QMetaObject::Connection connectRepairingEntered(const AbstractService * service, QTimer * timer);
82
83 QMetaObject::Connection connectBrokenEntered(const AbstractService * service, QTimer * timer);
84
85 void clearServiceEntry(AbstractService * service);
86
87 struct Members {
88 int initialInterval;
89 QJSValue intervalFunction;
90 ServiceDataContainer serviceData;
91 };
92
94};
95
96
97}
98}
99
100#endif
101
102//(c)C: Copyright © 2022, Michał Policht <michal@policht.pl>. All rights reserved.
103//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
104//(c)C: This file is a part of CuteHMI.
105//(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.
106//(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.
107//(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/>.
108//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
109//(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:
110//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
111//(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.
Abstract service controller.
Definition: AbstractServiceController.hpp:38
Abstract service.
Definition: AbstractService.hpp:49
Definition: ServiceAutoRepair.hpp:21