1 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_INTERNAL_REGISTERCONTROLLERMIXIN_HPP
2 #define H_EXTENSIONS_CUTEHMI_MODBUS_2_INCLUDE_CUTEHMI_MODBUS_INTERNAL_REGISTERCONTROLLERMIXIN_HPP
5 #include "functions.hpp"
6 #include "RegisterControllerTraits.hpp"
7 #include "../AbstractDevice.hpp"
10 #include <QJsonObject>
16 template <
typename DERIVED>
35 const DERIVED & derived()
const;
40 template <
typename DERIVED>
45 template <
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 !=
nullptr)
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 ==
nullptr) {
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 !=
nullptr) {
78 derived().m->adjustingValue =
true;
79 derived().m->postponedWritePending =
true;
81 }
else if ((derived().writeMode() == DERIVED::WRITE_IMMEDIATE) && (derived().m->requestId !=
nullptr))
82 derived().writeValue();
87 template<
typename DERIVED>
90 derived().requestWrite(derived().m->requestedValue);
91 derived().m->adjustingValue =
false;
94 template<
typename DERIVED>
99 derived().m->writeTimer.stop();
100 derived().writeValue();
103 template<
typename DERIVED>
110 if (
function == derived().writeRegisterFunction()) {
111 if (requestId == derived().m->requestId) {
113 if (derived().readOnWrite())
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 =
nullptr;
128 if (!derived().readOnWrite())
129 derived().setBusy(derived().m->postponedWritePending);
131 emit derived().valueFailed();
133 derived().m->requestId =
nullptr;
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 =
nullptr;
153 }
else if (derived().m->requestId.isNull()) {
156 derived().setBusy(!success || derived().m->postponedWritePending);
158 derived().updateValue();
162 clearPostponedWrite();
165 template<
typename DERIVED>
168 if (derived().m->postponedWritePending)
169 derived().writeValue();
170 derived().m->postponedWritePending =
false;
173 template <
typename DERIVED>
176 return static_cast<const DERIVED &
>(*this);
179 template <
typename DERIVED>
180 DERIVED & RegisterControllerMixin<DERIVED>::derived()
182 return static_cast<DERIVED &
>(*this);