1#ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_INTERNAL_REGISTERCONTROLLERMIXIN_HPP
2#define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_INTERNAL_REGISTERCONTROLLERMIXIN_HPP
5#include "functions.hpp"
6#include "RegisterControllerTraits.hpp"
7#include "../AbstractDevice.hpp"
16template <
typename DERIVED>
35 const DERIVED & derived()
const;
40template <
typename DERIVED>
45template <
typename DERIVED>
48 if (derived().valueSettingPolicy() == DERIVED::VALUE_SETTING_INITIALIZED && !derived().initialized()) {
49 CUTEHMI_DEBUG(
"Ignoring set value request as controller value has not been initialized and value setting policy is VALUE_SETTING_INITIALIZED.");
53 derived().m->requestedValue = value;
55 if (derived().device() ==
nullptr)
56 CUTEHMI_WARNING(
"Attempting to set value, but no device has been assigned to the controller.");
58 if (derived().m->value != value) {
59 derived().m->adjustingValue =
true;
61 if (derived().writeMode() == DERIVED::WRITE_DELAYED)
62 derived().m->writeTimer.start(derived().writeDelay(), & derived());
63 else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
65 if (!derived().m->requestId.isNull())
66 derived().m->postponedWritePending =
true;
68 derived().writeValue();
69 }
else if (derived().writeMode() == DERIVED::WRITE_IMMEDIATE)
70 derived().writeValue();
73 if (derived().writeMode() == DERIVED::WRITE_DELAYED) {
74 if (derived().m->requestId.isNull()) {
75 derived().m->adjustingValue =
false;
76 derived().m->writeTimer.stop();
78 derived().m->adjustingValue =
true;
79 derived().m->writeTimer.start(derived().writeDelay(), & derived());
81 }
else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
82 if (!derived().m->requestId.isNull()) {
83 derived().m->adjustingValue =
true;
84 derived().m->postponedWritePending =
true;
86 }
else if ((derived().writeMode() == DERIVED::WRITE_IMMEDIATE) && (!derived().m->requestId.isNull()))
87 derived().writeValue();
92template<
typename DERIVED>
95 derived().requestWrite(derived().m->requestedValue);
96 derived().m->adjustingValue =
false;
99template<
typename DERIVED>
104 derived().m->writeTimer.stop();
105 derived().writeValue();
108template<
typename DERIVED>
115 if (function == derived().writeRegisterFunction()) {
116 if (requestId == derived().m->requestId) {
118 if (derived().readOnWrite() && derived().enabled())
120 derived().requestReadRegisters(
static_cast<quint16
>(derived().address()), derived().bytes(), & derived().m->requestId);
122 derived().setBusy(derived().m->postponedWritePending);
124 emit derived().valueWritten();
130 derived().m->requestId =
QUuid();
133 if (!derived().readOnWrite())
134 derived().setBusy(derived().m->postponedWritePending);
136 emit derived().valueFailed();
138 derived().m->requestId =
QUuid();
141 }
else if (function == derived().readRegistersFunction()) {
143 if (
static_cast<quint16
>(derived().address()) >= address &&
static_cast<quint16
>(derived().address()) <= endAddress) {
144 if (requestId == derived().m->requestId) {
147 derived().setBusy(!success || derived().m->postponedWritePending);
150 if (success && (derived().verifyRegisterValue()))
151 emit derived().valueWritten();
153 emit derived().valueMismatch();
155 derived().updateValue();
158 derived().setInitialized(
true);
160 derived().m->requestId =
QUuid();
161 }
else if (derived().m->requestId.isNull()) {
164 derived().setBusy(!success || derived().m->postponedWritePending);
166 derived().updateValue();
169 derived().setInitialized(
true);
173 clearPostponedWrite();
176template<
typename DERIVED>
179 if (derived().m->postponedWritePending)
180 derived().writeValue();
181 derived().m->postponedWritePending =
false;
184template <
typename DERIVED>
187 return static_cast<const DERIVED &
>(*this);
190template <
typename DERIVED>
191DERIVED & RegisterControllerMixin<DERIVED>::derived()
193 return static_cast<DERIVED &
>(*this);
Function
Definition: AbstractDevice.hpp:56
Definition: RegisterControllerMixin.hpp:18
void writeValue()
Definition: RegisterControllerMixin.hpp:93
void onRequestCompleted(QJsonObject request, QJsonObject reply)
Definition: RegisterControllerMixin.hpp:109
void setValue(ValueType value)
Definition: RegisterControllerMixin.hpp:46
void timerEvent(QTimerEvent *event)
Definition: RegisterControllerMixin.hpp:100
RegisterControllerTraits< DERIVED >::ValueType ValueType
Definition: RegisterControllerMixin.hpp:20
RegisterControllerMixin()
Definition: RegisterControllerMixin.hpp:41
void clearPostponedWrite()
Definition: RegisterControllerMixin.hpp:177
#define CUTEHMI_WARNING(EXPR)
#define CUTEHMI_DEBUG(EXPR)
QJsonValue value(const QString &key) const const
bool toBool(bool defaultValue) const const
double toDouble(double defaultValue) const const
int toInt(int defaultValue) const const
QJsonObject toObject() const const
QString toString() const const
QUuid fromString(QStringView text)
Definition: RegisterControllerTraits.hpp:16