File indexing completed on 2024-06-23 05:36:21

0001 #ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_REGISTER1CONTROLLER_HPP
0002 #define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_REGISTER1CONTROLLER_HPP
0003 
0004 #include "internal/common.hpp"
0005 #include "internal/RegisterControllerMixin.hpp"
0006 #include "AbstractDevice.hpp"
0007 #include "Register1.hpp"
0008 #include "AbstractRegisterController.hpp"
0009 
0010 #include <QObject>
0011 #include <QBasicTimer>
0012 #include <QQmlEngine>
0013 
0014 namespace cutehmi {
0015 namespace modbus {
0016 
0017 /**
0018  * Register controller for 1 bit registers.
0019  */
0020 class CUTEHMI_MODBUS_API Register1Controller:
0021     public cutehmi::modbus::AbstractRegisterController,
0022     protected internal::RegisterControllerMixin<Register1Controller>
0023 {
0024         Q_OBJECT
0025         QML_NAMED_ELEMENT(Register1Controller)
0026         QML_UNCREATABLE("Register1Controller is an abstract class")
0027 
0028         friend class internal::RegisterControllerMixin<Register1Controller>;
0029         typedef internal::RegisterControllerMixin<Register1Controller> Mixin;
0030 
0031     public:
0032         static constexpr bool INITIAL_VALUE = false;
0033 
0034         Q_PROPERTY(bool value READ value WRITE setValue NOTIFY valueChanged)
0035 
0036         Register1Controller(QObject * parent = nullptr);
0037 
0038         ~Register1Controller() override;
0039 
0040         bool value() const;
0041 
0042         void setValue(bool value);
0043 
0044     public slots:
0045         void writeValue();
0046 
0047     signals:
0048         void valueChanged();
0049 
0050         void valueUpdated();
0051 
0052         void valueWritten();
0053 
0054         void valueFailed();
0055 
0056         void valueMismatch();
0057 
0058     protected:
0059         virtual Register1 * registerAt(quint16 address) const = 0;
0060 
0061         virtual AbstractDevice::Function readRegistersFunction() const = 0;
0062 
0063         virtual AbstractDevice::Function writeRegisterFunction() const = 0;
0064 
0065         virtual void requestWriteRegister(quint16 address, bool value, QUuid * requestId) const = 0;
0066 
0067         void timerEvent(QTimerEvent * event) override;
0068 
0069         quint16 bytes() const override;
0070 
0071         void onDeviceDestroyed() override;
0072 
0073     protected slots:
0074         void onRequestCompleted(QJsonObject request, QJsonObject reply) override;
0075 
0076         void resetRegister();
0077 
0078     private:
0079         void updateValue();
0080 
0081         void updateValue(bool value);
0082 
0083         void updateValue(const QJsonValue & value);
0084 
0085         void requestWrite(bool value);
0086 
0087         bool verifyRegisterValue() const;
0088 
0089     private:
0090         struct Members {
0091             bool value;
0092             bool postponedWritePending;
0093             bool adjustingValue;
0094             bool requestedValue;
0095             Register1 * register1;
0096             QUuid requestId;
0097             QBasicTimer writeTimer;
0098 
0099             Members():
0100                 value(INITIAL_VALUE),
0101                 postponedWritePending(false),
0102                 adjustingValue(false),
0103                 requestedValue(0.0),
0104                 register1(nullptr)
0105             {
0106             }
0107         };
0108 
0109         MPtr<Members> m;
0110 };
0111 
0112 }
0113 }
0114 
0115 #endif
0116 
0117 //(c)C: Copyright © 2022, Michał Policht <michal@policht.pl>. All rights reserved.
0118 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
0119 //(c)C: This file is a part of CuteHMI.
0120 //(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.
0121 //(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.
0122 //(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/>.
0123 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
0124 //(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:
0125 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0126 //(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.