CuteHMI - Modbus (CuteHMI.Modbus.4)
RegisterControllerMixin.hpp
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
3
4#include "common.hpp"
5#include "functions.hpp"
6#include "RegisterControllerTraits.hpp"
7#include "../AbstractDevice.hpp"
8
9#include <QBasicTimer>
10#include <QJsonObject>
11
12namespace cutehmi {
13namespace modbus {
14namespace internal {
15
16template <typename DERIVED>
18{
19 protected:
21
23
24 void setValue(ValueType value);
25
26 void writeValue();
27
28 void timerEvent(QTimerEvent * event);
29
31
33
34 private:
35 const DERIVED & derived() const;
36
37 DERIVED & derived();
38};
39
40template <typename DERIVED>
42{
43}
44
45template <typename DERIVED>
47{
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.");
50 return;
51 }
52
53 derived().m->requestedValue = value;
54
55 if (derived().device() == nullptr)
56 CUTEHMI_WARNING("Attempting to set value, but no device has been assigned to the controller.");
57 else {
58 if (derived().m->value != value) {
59 derived().m->adjustingValue = true;
60
61 if (derived().writeMode() == DERIVED::WRITE_DELAYED)
62 derived().m->writeTimer.start(derived().writeDelay(), & derived());
63 else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
64 // If m->requestId is not null, then controller has not finished with previous request yet.
65 if (!derived().m->requestId.isNull())
66 derived().m->postponedWritePending = true;
67 else
68 derived().writeValue();
69 } else if (derived().writeMode() == DERIVED::WRITE_IMMEDIATE)
70 derived().writeValue();
71 // If write mode is WRITE_EXPLICIT, then do nothing.
72 } else {
73 if (derived().writeMode() == DERIVED::WRITE_DELAYED) {
74 if (derived().m->requestId.isNull()) {
75 derived().m->adjustingValue = false;
76 derived().m->writeTimer.stop();
77 } else {
78 derived().m->adjustingValue = true;
79 derived().m->writeTimer.start(derived().writeDelay(), & derived());
80 }
81 } else if (derived().writeMode() == DERIVED::WRITE_POSTPONED) {
82 if (!derived().m->requestId.isNull()) {
83 derived().m->adjustingValue = true;
84 derived().m->postponedWritePending = true;
85 }
86 } else if ((derived().writeMode() == DERIVED::WRITE_IMMEDIATE) && (!derived().m->requestId.isNull()))
87 derived().writeValue();
88 }
89 }
90}
91
92template<typename DERIVED>
94{
95 derived().requestWrite(derived().m->requestedValue);
96 derived().m->adjustingValue = false;
97}
98
99template<typename DERIVED>
101{
102 Q_UNUSED(event)
103
104 derived().m->writeTimer.stop();
105 derived().writeValue();
106}
107
108template<typename DERIVED>
110{
111 AbstractDevice::Function function = static_cast<AbstractDevice::Function>(request.value("function").toInt());
112 QUuid requestId = QUuid::fromString(request.value("id").toString());
113 bool success = reply.value("success").toBool();
114 quint16 address = static_cast<quint16>(request.value("payload").toObject().value("address").toDouble());
115 if (function == derived().writeRegisterFunction()) {
116 if (requestId == derived().m->requestId) {
117 if (success) {
118 if (derived().readOnWrite() && derived().enabled())
119 // Non-null requestId implies that device is not null (see setDevice() and setupRegister()).
120 derived().requestReadRegisters(static_cast<quint16>(derived().address()), derived().bytes(), & derived().m->requestId);
121 else {
122 derived().setBusy(derived().m->postponedWritePending);
123
124 emit derived().valueWritten();
125
126 // Without readOnWrite verification, written value acts as one, which is currently set in register.
128 derived().updateValue(request.value("payload").toObject().value("value"));
129
130 derived().m->requestId = QUuid();
131 }
132 } else {
133 if (!derived().readOnWrite())
134 derived().setBusy(derived().m->postponedWritePending);
135
136 emit derived().valueFailed();
137
138 derived().m->requestId = QUuid();
139 }
140 }
141 } else if (function == derived().readRegistersFunction()) {
142 quint16 endAddress = address + static_cast<quint16>(request.value("payload").toObject().value("amount").toDouble()) - 1;
143 if (static_cast<quint16>(derived().address()) >= address && static_cast<quint16>(derived().address()) <= endAddress) {
144 if (requestId == derived().m->requestId) {
145 // If requestId == m->requestId, then request must have been made by controller due to readOnWrite.
146
147 derived().setBusy(!success || derived().m->postponedWritePending);
148
149 // Non-null requestId implies that register is not null (see setDevice() and setupRegister()).
150 if (success && (derived().verifyRegisterValue()))
151 emit derived().valueWritten();
152 else
153 emit derived().valueMismatch(); // In case of read failure we can't verify value. Even though write request must have succeeded assume valueMismatch() in such case.
154
155 derived().updateValue();
156
157 if (success)
158 derived().setInitialized(true);
159
160 derived().m->requestId = QUuid();
161 } else if (derived().m->requestId.isNull()) {
162 // Standard update, if controller is not waiting for its own request made due to readOnWrite.
163
164 derived().setBusy(!success || derived().m->postponedWritePending);
165
166 derived().updateValue();
167
168 if (success)
169 derived().setInitialized(true);
170 }
171 }
172 }
173 clearPostponedWrite();
174}
175
176template<typename DERIVED>
178{
179 if (derived().m->postponedWritePending)
180 derived().writeValue();
181 derived().m->postponedWritePending = false;
182}
183
184template <typename DERIVED>
185const DERIVED & RegisterControllerMixin<DERIVED>::derived() const
186{
187 return static_cast<const DERIVED &>(*this);
188}
189
190template <typename DERIVED>
191DERIVED & RegisterControllerMixin<DERIVED>::derived()
192{
193 return static_cast<DERIVED &>(*this);
194}
195
196}
197}
198}
199
200#endif
201
202//(c)C: Copyright © 2022-2024, Michał Policht <michal@policht.pl>. All rights reserved.
203//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
204//(c)C: This file is a part of CuteHMI.
205//(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.
206//(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.
207//(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/>.
208//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
209//(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:
210//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
211//(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.
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)
T internal(T... args)
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