CuteHMI - Data Acquisition (CuteHMI.DataAcquisition.0)
DbServiceableMixin.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_DBSERVICEABLEMIXIN_HPP
2 #define H_EXTENSIONS_CUTEHMI_DATAACQUISITION_0_INCLUDE_CUTEHMI_DATAACQUISITION_INTERNAL_DBSERVICEABLEMIXIN_HPP
3 
4 #include <cutehmi/services/Serviceable.hpp>
5 
6 #include <cutehmi/shareddatabase/Database.hpp>
7 
8 #include <QState>
9 #include <QTimer>
10 
11 namespace cutehmi {
12 namespace dataacquisition {
13 namespace internal {
14 
15 template <class DERIVED>
17 {
18  protected:
19  QState * createWaitingForDatabaseConnectedSate(QState * parent, services::Serviceable::ServiceStatuses * statuses = nullptr, QState * target = nullptr);
20 
21  QState * createValidatingSchemaSate(QState * parent, services::Serviceable::ServiceStatuses * statuses = nullptr, QState * target = nullptr);
22 
23  private:
24  const DERIVED & derived() const;
25 
26  DERIVED & derived();
27 };
28 
29 template <typename DERIVED>
31 {
32  QState * state = new QState(parent);
33  QObject::connect(state, & QState::entered, & derived(), [this, state]() {
34  if (derived().schema()) {
35  QTimer * timer = new QTimer(derived().schema());
36  QObject::connect(timer, & QTimer::timeout, [this]() {
37  if (shareddatabase::Database::IsConnected(derived().schema()->connectionName()))
38  emit derived().databaseConnected();
39  });
40  QObject::connect(state, & QState::exited, timer, & QTimer::stop);
42  timer->start(250);
43  } else {
44  CUTEHMI_CRITICAL("Schema is not set for '" << this << "' object.");
45  emit derived().broke();
46  }
47  });
48 
49  if (statuses)
50  statuses->insert(state, QCoreApplication::translate("cutehmi::dataacquisition::internal::DbServiceableMixin", "Waiting for database"));
51 
52  if (target)
53  state->addTransition(& derived(), & DERIVED::databaseConnected, target);
54 
55  return state;
56 }
57 
58 template <typename DERIVED>
60 {
61  QState * state = new QState(parent);
62  QObject::connect(state, & QState::entered, & derived(), [this]() {
63  if (!derived().schema()) {
64  CUTEHMI_CRITICAL("Schema is not set for '" << this << "' object.");
65  emit derived().broke();
66  } else
67  derived().schema()->validate();
68  });
69 
70  if (statuses)
71  statuses->insert(state, QCoreApplication::translate("cutehmi::dataacquisition::internal::DbServiceableMixin", "Validating schema"));
72 
73  if (target)
74  state->addTransition(& derived(), & DERIVED::schemaValidated, target);
75 
76  return state;
77 }
78 
79 template <typename DERIVED>
80 const DERIVED & DbServiceableMixin<DERIVED>::derived() const
81 {
82  return static_cast<const DERIVED &>(*this);
83 }
84 
85 template <typename DERIVED>
86 DERIVED & DbServiceableMixin<DERIVED>::derived()
87 {
88  return static_cast<DERIVED &>(*this);
89 }
90 
91 }
92 }
93 }
94 
95 #endif
96 
97 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
98 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
99 //(c)C: This file is a part of CuteHMI.
100 //(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.
101 //(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.
102 //(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/>.
103 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
104 //(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:
105 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
106 //(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
cutehmi::shareddatabase::Database::IsConnected
static bool IsConnected(const QString &connectionName)
QCoreApplication::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QState
cutehmi::dataacquisition::internal::DbServiceableMixin::createValidatingSchemaSate
QState * createValidatingSchemaSate(QState *parent, services::Serviceable::ServiceStatuses *statuses=nullptr, QState *target=nullptr)
Definition: DbServiceableMixin.hpp:59
QAbstractState::entered
void entered()
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QHash::insert
QHash::iterator insert(const Key &key, const T &value)
QObject::deleteLater
void deleteLater()
QTimer::start
void start(int msec)
cutehmi
QTimer::timeout
void timeout()
QState::addTransition
void addTransition(QAbstractTransition *transition)
QTimer::stop
void stop()
QAbstractState::exited
void exited()
cutehmi::dataacquisition::internal::DbServiceableMixin::createWaitingForDatabaseConnectedSate
QState * createWaitingForDatabaseConnectedSate(QState *parent, services::Serviceable::ServiceStatuses *statuses=nullptr, QState *target=nullptr)
Definition: DbServiceableMixin.hpp:30
QHash
cutehmi::dataacquisition::internal::DbServiceableMixin
Definition: DbServiceableMixin.hpp:16
CUTEHMI_CRITICAL
#define CUTEHMI_CRITICAL(EXPR)