CuteHMI - Modbus (CuteHMI.Modbus.4)
AbstractRegisterController.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
2#define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_ABSTRACTREGISTERCONTROLLER_HPP
3
4#include "internal/common.hpp"
5#include "AbstractDevice.hpp"
6
7#include <QObject>
8#include <QQmlParserStatus>
9#include <QQmlEngine>
10
11namespace cutehmi {
12namespace modbus {
13
17class CUTEHMI_MODBUS_API AbstractRegisterController:
18 public QObject,
19 public QQmlParserStatus
20{
21 Q_OBJECT
22 Q_INTERFACES(QQmlParserStatus)
23 QML_NAMED_ELEMENT(AbstractRegisterController)
24 QML_UNCREATABLE("AbstractRegisterController is an abstract class")
25
26 public:
30 enum WriteMode {
35 };
36 Q_ENUM(WriteMode)
37
38
44 };
45 Q_ENUM(ValueSettingPolicy)
46
47 static constexpr unsigned int INITIAL_ADDRESS = 0;
48 static constexpr WriteMode INITIAL_WRITE_MODE = WRITE_DELAYED;
49 static constexpr ValueSettingPolicy INITIAL_VALUE_SETTING_POLICY = VALUE_SETTING_INITIALIZED;
50 static constexpr int INITIAL_WRITE_DELAY = 500;
51 static constexpr bool INITIAL_BUSY = true;
52 static constexpr bool INITIAL_READ_ON_WRITE = true;
53 static constexpr bool INITIAL_ENABLED = true;
54
58 Q_PROPERTY(cutehmi::modbus::AbstractDevice * device READ device WRITE setDevice NOTIFY deviceChanged)
59
63 // Note: unsigned int is guaranteed to be at least 16 bits wide by the standard. Using unsigned int instead of quint16 (aka
64 // ushort), because when using quint16 QML throws an error (unsupported type "ushort") for an alias to quint16 property.
65 Q_PROPERTY(unsigned int address READ address WRITE setAddress NOTIFY addressChanged)
66
70 Q_PROPERTY(bool busy READ busy NOTIFY busyChanged)
71
75 Q_PROPERTY(bool initialized READ initialized NOTIFY initializedChanged)
76
80 Q_PROPERTY(bool readOnWrite READ readOnWrite WRITE setReadOnWrite NOTIFY readOnWriteChanged)
81
85 Q_PROPERTY(WriteMode writeMode READ writeMode WRITE setWriteMode NOTIFY writeModeChanged)
86
90 Q_PROPERTY(ValueSettingPolicy valueSettingPolicy READ valueSettingPolicy WRITE setValueSettingPolicy NOTIFY valueSettingPolicyChanged)
91
95 Q_PROPERTY(int writeDelay READ writeDelay WRITE setWriteDelay NOTIFY writeDelayChanged)
96
100 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
101
102 AbstractRegisterController(QObject * parent = nullptr);
103
104 AbstractDevice * device() const;
105
106 void setDevice(AbstractDevice * device);
107
108 unsigned int address() const;
109
110 void setAddress(unsigned int address);
111
112 bool busy() const;
113
114 bool initialized() const;
115
116 bool readOnWrite() const;
117
118 void setReadOnWrite(bool readOnWrite);
119
120 WriteMode writeMode() const;
121
122 void setWriteMode(WriteMode writeMode);
123
124 ValueSettingPolicy valueSettingPolicy() const;
125
126 void setValueSettingPolicy(ValueSettingPolicy valueSettingPolicy);
127
128 int writeDelay() const;
129
130 void setWriteDelay(int writeDelay);
131
132 bool enabled() const;
133
134 void setEnabled(bool enabled);
135
136 signals:
137 void deviceChanged();
138
139 void addressChanged();
140
141 void busyChanged();
142
143 void initializedChanged();
144
145 void readOnWriteChanged();
146
147 void writeModeChanged();
148
149 void valueSettingPolicyChanged();
150
151 void writeDelayChanged();
152
153 void enabledChanged();
154
155 protected:
156 virtual void requestReadRegisters(quint16 address, quint16 amount, QUuid * requestId) const = 0;
157
158 virtual quint16 bytes() const = 0;
159
160 virtual void onDeviceDestroyed() = 0;
161
162 void classBegin() override;
163
164 void componentComplete() override;
165
166 void setBusy(bool busy);
167
168 void setInitialized(bool initialized);
169
170 protected slots:
171 virtual void onRequestCompleted(QJsonObject request, QJsonObject reply) = 0;
172
173 private:
174 bool deviceReady() const;
175
176 struct Members
177 {
178 AbstractDevice * device;
179 unsigned int address;
180 bool busy;
181 bool initialized;
182 bool readOnWrite;
183 WriteMode writeMode;
184 ValueSettingPolicy valueSettingPolicy;
185 int writeDelay;
186 bool enabled;
187 bool deferRequestRead;
188
189 Members():
190 device(nullptr),
191 address(INITIAL_ADDRESS),
192 busy(INITIAL_BUSY),
193 initialized(false),
194 readOnWrite(INITIAL_READ_ON_WRITE),
195 writeMode(INITIAL_WRITE_MODE),
196 valueSettingPolicy(INITIAL_VALUE_SETTING_POLICY),
197 writeDelay(INITIAL_WRITE_DELAY),
198 enabled(INITIAL_ENABLED),
199 deferRequestRead(false)
200 {
201 }
202 };
203
205};
206
207}
208}
209
210#endif
211
212//(c)C: Copyright © 2022-2024, Michał Policht <michal@policht.pl>. All rights reserved.
213//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
214//(c)C: This file is a part of CuteHMI.
215//(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.
216//(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.
217//(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/>.
218//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
219//(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:
220//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
221//(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.
Abstract Modbus device.
Definition: AbstractDevice.hpp:38
Abstract register controller.
Definition: AbstractRegisterController.hpp:20
WriteMode
Write mode enum.
Definition: AbstractRegisterController.hpp:30
@ WRITE_DELAYED
Writes are delayed. Write will be requested after amount of time specified by writeDelay property has...
Definition: AbstractRegisterController.hpp:31
@ WRITE_POSTPONED
Writes are postponed, which means that write will not be requested until previous write has finished.
Definition: AbstractRegisterController.hpp:32
@ WRITE_IMMEDIATE
Writes are requested immediately, which may creates a queue of pending writes if they are not proceed...
Definition: AbstractRegisterController.hpp:33
@ WRITE_EXPLICIT
Writes are explicit, i.e. write requires an explicit call to write value function.
Definition: AbstractRegisterController.hpp:34
ValueSettingPolicy
Value setting policy enum.
Definition: AbstractRegisterController.hpp:41
@ VALUE_SETTING_ALWAYS
Always handle setting a value and create write requests according to selected writeMode.
Definition: AbstractRegisterController.hpp:42
@ VALUE_SETTING_INITIALIZED
Handle set value and create write requests only when controller value has been initialized with value...
Definition: AbstractRegisterController.hpp:43