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  // Check if more registers can be read in the same request.
72  while (m_it.hasNext()) {
73  quint16 address = static_cast<quint16>(m_it.next());
74  if ((address - startAddress) < maxRead) {
75  typename DataContainer::const_pointer data = derived().dataAt(address);
76  if (data && data->wakeful())
77  count = address - startAddress + 1;
78  } else {
79  m_it.previous();
80  break;
81  }
82  }
83 
84  derived().requestReadData(startAddress, count, m_requestId);
85 
86  return true;
87  } else
88  return false;
89 }
90 
91 template<class DERIVED, class DATA>
93 {
94  m_it = typename DataContainer::KeysIterator(& derived().container());
95 }
96 
97 template<class DERIVED, class DATA>
99 {
100  return static_cast<DERIVED &>(*this);
101 }
102 
103 template<class DERIVED, class DATA>
104 const DERIVED & DataContainerPolling<DERIVED, DATA>::derived() const
105 {
106  return static_cast<const DERIVED &>(*this);
107 }
108 
109 
110 }
111 }
112 }
113 
114 
115 #endif
116 
117 //(c)C: Copyright © 2019-2020, Michał Policht <michal@policht.pl>. All rights reserved.
118 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
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/>.
123 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
124 //(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:
125 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
126 //(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.
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:33
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:92
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:98
std::internal
T internal(T... args)
cutehmi::modbus::internal::DataContainerPolling::device
AbstractDevice * device() const
Definition: DataContainerPolling.hpp:47