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 derived().m->requestedValue = value;
50 if (derived().device() ==
nullptr)
51 CUTEHMI_WARNING(
"Attempting to set value, but no device has been assigned to the controller.");
53 if (derived().m->value != value) {
54 derived().m->adjustingValue =
true;
56 if (derived().writeMode() == DERIVED::WRITE_DELAYED)
57 derived().m->writeTimer.start(derived().writeDelay(), & derived());
58 else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
60 if (!derived().m->requestId.isNull())
61 derived().m->postponedWritePending =
true;
63 derived().writeValue();
64 }
else if (derived().writeMode() == DERIVED::WRITE_IMMEDIATE)
65 derived().writeValue();
68 if (derived().writeMode() == DERIVED::WRITE_DELAYED) {
69 if (derived().m->requestId.isNull()) {
70 derived().m->adjustingValue =
false;
71 derived().m->writeTimer.stop();
73 derived().m->adjustingValue =
true;
74 derived().m->writeTimer.start(derived().writeDelay(), & derived());
76 }
else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
77 if (!derived().m->requestId.isNull()) {
78 derived().m->adjustingValue =
true;
79 derived().m->postponedWritePending =
true;
81 }
else if ((derived().writeMode() == DERIVED::WRITE_IMMEDIATE) && (!derived().m->requestId.isNull()))
82 derived().writeValue();
87template<
typename DERIVED>
90 derived().requestWrite(derived().m->requestedValue);
91 derived().m->adjustingValue =
false;
94template<
typename DERIVED>
99 derived().m->writeTimer.stop();
100 derived().writeValue();
103template<
typename DERIVED>
110 if (function == derived().writeRegisterFunction()) {
111 if (requestId == derived().m->requestId) {
113 if (derived().readOnWrite() && derived().enabled())
115 derived().requestReadRegisters(
static_cast<quint16
>(derived().address()), derived().bytes(), & derived().m->requestId);
117 derived().setBusy(derived().m->postponedWritePending);
119 emit derived().valueWritten();
125 derived().m->requestId =
QUuid();
128 if (!derived().readOnWrite())
129 derived().setBusy(derived().m->postponedWritePending);
131 emit derived().valueFailed();
133 derived().m->requestId =
QUuid();
136 }
else if (function == derived().readRegistersFunction()) {
138 if (
static_cast<quint16
>(derived().address()) >= address &&
static_cast<quint16
>(derived().address()) <= endAddress) {
139 if (requestId == derived().m->requestId) {
142 derived().setBusy(!success || derived().m->postponedWritePending);
145 if (success && (derived().verifyRegisterValue()))
146 emit derived().valueWritten();
148 emit derived().valueMismatch();
150 derived().updateValue();
152 derived().m->requestId =
QUuid();
153 }
else if (derived().m->requestId.isNull()) {
156 derived().setBusy(!success || derived().m->postponedWritePending);
158 derived().updateValue();
162 clearPostponedWrite();
165template<
typename DERIVED>
168 if (derived().m->postponedWritePending)
169 derived().writeValue();
170 derived().m->postponedWritePending =
false;
173template <
typename DERIVED>
176 return static_cast<const DERIVED &
>(*this);
179template <
typename DERIVED>
180DERIVED & RegisterControllerMixin<DERIVED>::derived()
182 return static_cast<DERIVED &
>(*this);
Function
Definition: AbstractDevice.hpp:56
Definition: RegisterControllerMixin.hpp:18
void writeValue()
Definition: RegisterControllerMixin.hpp:88
void onRequestCompleted(QJsonObject request, QJsonObject reply)
Definition: RegisterControllerMixin.hpp:104
void setValue(ValueType value)
Definition: RegisterControllerMixin.hpp:46
void timerEvent(QTimerEvent *event)
Definition: RegisterControllerMixin.hpp:95
RegisterControllerTraits< DERIVED >::ValueType ValueType
Definition: RegisterControllerMixin.hpp:20
RegisterControllerMixin()
Definition: RegisterControllerMixin.hpp:41
void clearPostponedWrite()
Definition: RegisterControllerMixin.hpp:166
#define CUTEHMI_WARNING(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