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