CuteHMI - Modbus (CuteHMI.Modbus.4)
DataContainerPolling.hpp
1#ifndef H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_INTERNAL_DATACONTAINERPOLLING_HPP
2#define H_EXTENSIONS_CUTEHMI_MODBUS_4_INCLUDE_CUTEHMI_MODBUS_INTERNAL_DATACONTAINERPOLLING_HPP
3
4#include "IterableTasks.hpp"
5#include "../AbstractDevice.hpp"
6
7namespace cutehmi {
8namespace modbus {
9namespace internal {
10
11template <class DERIVED, class DATA>
13 public IterableTasks
14{
15 public:
16 typedef DATA Data;
18
20
22
23 bool runNext() override;
24
25 void reset() override;
26
27 DERIVED & derived();
28
29 const DERIVED & derived() const;
30
31 private:
32 AbstractDevice * m_device;
33 QUuid * m_requestId;
34 typename DataContainer::KeysIterator m_it;
35};
36
37template<class DERIVED, class DATA>
39 m_device(device),
40 m_requestId(requestId),
41 m_it(& derived().container())
42{
43}
44
45template<class DERIVED, class DATA>
47{
48 return m_device;
49}
50
51template<class DERIVED, class DATA>
53{
54 int maxRead = derived().maxRead();
55 quint16 startAddress = 0;
56 quint16 count = 0;
57
58 // Find address of next wakefull register, if any. Value of 'count' set to '1' indicates that such register has been found.
59 while (m_it.hasNext()) {
60 quint16 address = static_cast<quint16>(m_it.next());
61 typename DataContainer::const_pointer data = derived().dataAt(address);
62 if (data && data->wakeful()) {
63 startAddress = address;
64 count = 1;
65 break;
66 }
67 }
68
69 if (count) {
70 // Check if more registers can be read in the same request.
71 while (m_it.hasNext()) {
72 quint16 address = static_cast<quint16>(m_it.next());
73 if ((address - startAddress) < maxRead) {
74 typename DataContainer::const_pointer data = derived().dataAt(address);
75 if (data && data->wakeful())
76 count = address - startAddress + 1;
77 } else {
78 m_it.previous();
79 break;
80 }
81 }
82
83 derived().requestReadData(startAddress, count, m_requestId);
84
85 return true;
86 } else
87 return false;
88}
89
90template<class DERIVED, class DATA>
92{
93 m_it = typename DataContainer::KeysIterator(& derived().container());
94}
95
96template<class DERIVED, class DATA>
98{
99 return static_cast<DERIVED &>(*this);
100}
101
102template<class DERIVED, class DATA>
104{
105 return static_cast<const DERIVED &>(*this);
106}
107
108
109}
110}
111}
112
113
114#endif
115
116//(c)C: Copyright © 2022-2023, Michał Policht <michal@policht.pl>. All rights reserved.
117//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
118//(c)C: This file is a part of CuteHMI.
119//(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.
120//(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.
121//(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/>.
122//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
123//(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:
124//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
125//(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
Keys iterator.
Definition: DataContainer.hpp:43
const T * const_pointer
Definition: DataContainer.hpp:29
friend class KeysIterator
Definition: DataContainer.hpp:22
Definition: DataContainerPolling.hpp:14
bool runNext() override
Run next task.
Definition: DataContainerPolling.hpp:52
void reset() override
Reset tasks iterator.
Definition: DataContainerPolling.hpp:91
DATA Data
Definition: DataContainerPolling.hpp:16
RegisterTraits< Data >::Container DataContainer
Definition: DataContainerPolling.hpp:17
DataContainerPolling(AbstractDevice *device, QUuid *requestId)
Definition: DataContainerPolling.hpp:38
AbstractDevice * device() const
Definition: DataContainerPolling.hpp:46
const DERIVED & derived() const
Definition: DataContainerPolling.hpp:103
DERIVED & derived()
Definition: DataContainerPolling.hpp:97
Definition: IterableTasks.hpp:18
T internal(T... args)
Definition: RegisterTraits.hpp:16