GuiHealthClientWidgetController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * \package RobotAPI::gui-plugins::GuiHealthClientWidgetController
17  * \author Simon Ottenhaus ( simon dot ottenhaus at kit dot edu )
18  * \date 2018
19  * \copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 
25 #include <SimoxUtility/algorithm/get_map_keys_values.h>
26 #include <qcolor.h>
27 #include <qnamespace.h>
28 #include <qrgb.h>
29 #include <qtablewidget.h>
30 #include <string>
31 #include <iomanip>
32 
33 #include <QLabel>
34 #include <QPushButton>
35 
39 
40 namespace armarx
41 {
42  std::string serializeList(const std::vector<std::string>& l)
43  {
44  std::string s;
45  for(std::size_t i = 0; i < l.size(); i++)
46  {
47  const auto& tag = l.at(i);
48  s += tag;
49 
50  if(i != l.size() - 1)
51  {
52  s += ", ";
53  }
54  }
55 
56  return s;
57  }
58 
60  {
61  widget.setupUi(getWidget());
62 
63  connect(this,
64  SIGNAL(invokeConnectComponentQt()),
65  this,
66  SLOT(onConnectComponentQt()),
67  Qt::QueuedConnection);
68  connect(this,
70  this,
72  Qt::QueuedConnection);
73 
74  connect(widget.pushButtonUnrequireAll,
75  SIGNAL(clicked()),
76  this,
77  SLOT(unrequireAll()));
78 
79  updateSummaryTimer = new QTimer(this);
80  updateSummaryTimer->setInterval(100);
81  connect(updateSummaryTimer, SIGNAL(timeout()), this, SLOT(updateSummaryTimerClb()));
82  }
83 
85  {
86  }
87 
88  void
90  {
91  }
92 
93  void
95  {
96  }
97 
98  void
100  {
101  //ARMARX_IMPORTANT << "onInitComponent";
102  usingProxy("RobotHealth");
103  offeringTopic("RobotHealthTopic");
104  }
105 
106  void
108  {
109  armarx::core::time::dto::DateTime now;
111  robotHealthTopicPrx->heartbeat(getName(), now);
112  }
113 
114 
115  std::string to_string_rounded(float value, int decimals = 100)
116  {
117  std::stringstream ss;
118  ss << std::fixed << std::setprecision(3) << value;
119  return ss.str();
120  }
121 
122 
123  QTableWidgetItem* make_item(const std::string& text,
124  Qt::AlignmentFlag horAlignment = Qt::AlignLeft)
125  {
126  auto* item = new QTableWidgetItem(QString::fromStdString(text));
127  item->setTextAlignment(horAlignment | Qt::AlignVCenter);
128  return item;
129  }
130 
131 
132  void
134  {
135  //ARMARX_IMPORTANT << "updateSummaryTimerClb";
136  if (robotHealthComponentPrx)
137  {
138  //ARMARX_IMPORTANT << "has robotHealthComponentPrx";
139  try
140  {
141  auto summary = robotHealthComponentPrx->getSummary();
142 
143  const std::size_t nCols = 10;
144 
145  auto& tableWidget = widget.tableHealthState;
146  tableWidget->setRowCount(summary.entries.size());
147  tableWidget->setColumnCount(nCols);
148 
149  const auto summaryVals = simox::alg::get_values(summary.entries);
150 
151  tableWidget->setHorizontalHeaderLabels({
152  "identifier", "required", "keeps frequency", "tags", "time since\nlast arrival [ms]", "time to\nreference [ms]", "time sync \n+ Ice [ms]", "warning\nthreshold [ms]", "error\nthreshold [ms]", "hostname"
153  });
154 
155  tableWidget->setColumnWidth(0, 250);
156  tableWidget->setColumnWidth(1, 80);
157  tableWidget->setColumnWidth(2, 120);
158  tableWidget->setColumnWidth(3, 180);
159  tableWidget->setColumnWidth(4, 120);
160  tableWidget->setColumnWidth(5, 120);
161  tableWidget->setColumnWidth(6, 120);
162  tableWidget->setColumnWidth(7, 120);
163 
164  for(std::size_t i = 0; i < summary.entries.size(); i++)
165  {
166  const auto& entry = summaryVals.at(i);
167 
168  std::string stateRepr;
169  QColor stateColor;
170  switch(entry.state)
171  {
172  case HealthOK:
173  stateRepr = "yes";
174  stateColor.setRgb(0, 255, 0); // green
175  break;
176  case HealthWarning:
177  stateRepr = "yes";
178  stateColor.setRgb(255, 165, 0); // orange
179  break;
180  case HealthError:
181  stateRepr = "no";
182  stateColor.setRgb(255, 0, 0); // red
183  break;
184  }
185 
186  const std::string hostname = entry.lastReferenceTimestamp.hostname;
187 
188  const std::string timeSinceLastArrivalRepr = to_string_rounded(entry.timeSinceLastArrival.microSeconds / 1000.0);
189  const std::string timeToLastReferenceRepr = to_string_rounded(entry.timeSinceLastUpdateReference.microSeconds / 1000.0);
190  const std::string tagsRepr = serializeList(entry.tags);
191 
192  const float syncErrorMilliSeconds = std::abs(entry.timeSinceLastArrival.microSeconds - entry.timeSinceLastUpdateReference.microSeconds) / 1000.0;
193 
194  tableWidget->setItem(i, 0, new QTableWidgetItem(QString::fromStdString(entry.identifier)));
195  tableWidget->setItem(i, 1, make_item(entry.required ? "yes" : "no", Qt::AlignHCenter));
196 
197  {
198  auto* item = make_item(stateRepr, Qt::AlignHCenter);
199  item->setBackgroundColor(stateColor);
200  tableWidget->setItem(i, 2, item);
201  }
202 
203  tableWidget->setItem(i, 3, new QTableWidgetItem(QString::fromStdString(tagsRepr)));
204  tableWidget->setItem(i, 4, make_item(timeSinceLastArrivalRepr, Qt::AlignRight));
205  tableWidget->setItem(i, 5, make_item(timeToLastReferenceRepr, Qt::AlignRight));
206 
207  {
208  auto* item = make_item(to_string_rounded(syncErrorMilliSeconds), Qt::AlignRight);
209 
210  QColor timeSyncColor;
211  if (syncErrorMilliSeconds > 20.)
212  {
213  timeSyncColor.setRgb(255, 0, 0);
214  }
215  else
216  {
217  timeSyncColor.setRgb(0, 255, 0);
218  }
219  item->setBackgroundColor(timeSyncColor);
220 
221  tableWidget->setItem(i, 6, item);
222  }
223 
224  tableWidget->setItem(i, 7, make_item(to_string_rounded(entry.maximumCycleTimeWarning.microSeconds / 1000.), Qt::AlignRight));
225  tableWidget->setItem(i, 8, make_item(to_string_rounded(entry.maximumCycleTimeError.microSeconds / 1000.), Qt::AlignRight));
226  tableWidget->setItem(i, 9, new QTableWidgetItem(QString::fromStdString(hostname)));
227  }
228 
229  std::string tagsText = "Active tags: [";
230  {
231  std::sort(summary.activeTags.begin(), summary.activeTags.end());
232 
233  tagsText += serializeList(summary.activeTags);
234 
235  tagsText += "]";
236  }
237 
238  widget.labelTags->setText(QString::fromStdString(tagsText));
239  }
240  catch (...)
241  {
242  }
243  }
244  }
245 
246  void
248  {
249  ARMARX_INFO << "UnrequireAll.";
250  if (robotHealthComponentPrx)
251  {
252  try
253  {
254  robotHealthComponentPrx->resetRequiredTags();
255  }
256  catch(...){}
257  }
258  }
259 
260  void
262  {
263  //ARMARX_IMPORTANT << "onConnectComponent";
264  robotHealthTopicPrx = getTopic<RobotHealthInterfacePrx>("RobotHealthTopic");
265  robotHealthComponentPrx = getProxy<RobotHealthComponentInterfacePrx>("RobotHealth");
267  }
268 
269  void
271  {
272  //healthTimer->start();
273  //ARMARX_IMPORTANT <<ame "updateSummaryTimer->start";
274  updateSummaryTimer->start();
275  }
276 
277  void
279  {
281  }
282 
283  void
285  {
286  updateSummaryTimer->stop();
287  }
288 } // namespace armarx
armarx::GuiHealthClientWidgetController::updateSummaryTimerClb
void updateSummaryTimerClb()
Definition: GuiHealthClientWidgetController.cpp:133
armarx::serializeList
std::string serializeList(const std::vector< std::string > &l)
Definition: GuiHealthClientWidgetController.cpp:42
armarx::GuiHealthClientWidgetController::unrequireAll
void unrequireAll()
Definition: GuiHealthClientWidgetController.cpp:247
armarx::GuiHealthClientWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Definition: GuiHealthClientWidgetController.cpp:89
armarx::GuiHealthClientWidgetController::onDisconnectComponentQt
void onDisconnectComponentQt()
Definition: GuiHealthClientWidgetController.cpp:284
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
DateTime.h
armarx::core::time::toIce
void toIce(dto::ClockType::ClockTypeEnum &dto, const ClockType &bo)
Definition: ice_conversions.cpp:33
Duration.h
ice_conversions.h
armarx::GuiHealthClientWidgetController::GuiHealthClientWidgetController
GuiHealthClientWidgetController()
Controller Constructor.
Definition: GuiHealthClientWidgetController.cpp:59
armarx::make_item
QTableWidgetItem * make_item(const std::string &text, Qt::AlignmentFlag horAlignment=Qt::AlignLeft)
Definition: GuiHealthClientWidgetController.cpp:123
armarx::GuiHealthClientWidgetController::invokeConnectComponentQt
void invokeConnectComponentQt()
armarx::GuiHealthClientWidgetController::invokeDisconnectComponentQt
void invokeDisconnectComponentQt()
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
armarx::GuiHealthClientWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: GuiHealthClientWidgetController.cpp:94
armarx::GuiHealthClientWidgetController::onConnectComponent
void onConnectComponent() override
Definition: GuiHealthClientWidgetController.cpp:261
armarx::GuiHealthClientWidgetController::healthTimerClb
void healthTimerClb()
Definition: GuiHealthClientWidgetController.cpp:107
armarx::GuiHealthClientWidgetController::onConnectComponentQt
void onConnectComponentQt()
Definition: GuiHealthClientWidgetController.cpp:270
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
armarx::GuiHealthClientWidgetController::onInitComponent
void onInitComponent() override
Definition: GuiHealthClientWidgetController.cpp:99
GuiHealthClientWidgetController.h
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::GuiHealthClientWidgetController::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: GuiHealthClientWidgetController.cpp:278
armarx::to_string_rounded
std::string to_string_rounded(float value, int decimals=100)
Definition: GuiHealthClientWidgetController.cpp:115
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::GuiHealthClientWidgetController::~GuiHealthClientWidgetController
virtual ~GuiHealthClientWidgetController()
Controller destructor.
Definition: GuiHealthClientWidgetController.cpp:84
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28