status_item.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
39 #ifndef DIAGNOSTIC_STATUS_ITEM_H
40 #define DIAGNOSTIC_STATUS_ITEM_H
41 
42 #include <map>
43 #include <string>
44 #include <vector>
45 #include <ros/ros.h>
46 #include <diagnostic_msgs/DiagnosticStatus.h>
47 #include <diagnostic_msgs/KeyValue.h>
48 #include <boost/shared_ptr.hpp>
49 
50 namespace diagnostic_aggregator {
51 
55 inline std::string getOutputName(const std::string item_name)
56 {
57  std::string output_name = item_name;
58  std::string slash_str = "/";
59  std::string::size_type pos = 0;
60  while ((pos = output_name.find(slash_str, pos)) != std::string::npos)
61  {
62  output_name.replace(pos, slash_str.size(), " ");
63  pos++;
64  }
65 
66  return output_name;
67 }
68 
72 enum DiagnosticLevel
73 {
74  Level_OK = diagnostic_msgs::DiagnosticStatus::OK,
75  Level_Warn = diagnostic_msgs::DiagnosticStatus::WARN,
76  Level_Error = diagnostic_msgs::DiagnosticStatus::ERROR,
77  Level_Stale = 3
78 };
79 
83 inline DiagnosticLevel valToLevel(const int val)
84 {
85  if (val == diagnostic_msgs::DiagnosticStatus::OK)
86  return Level_OK;
87  if (val == diagnostic_msgs::DiagnosticStatus::WARN)
88  return Level_Warn;
89  if (val == diagnostic_msgs::DiagnosticStatus::ERROR)
90  return Level_Error;
91  if (val == 3)
92  return Level_Stale;
93 
94  ROS_ERROR("Attempting to convert %d into DiagnosticLevel. Values are: {0: OK, 1: Warning, 2: Error, 3: Stale}", val);
95  return Level_Error;
96 }
97 
101 inline std::string valToMsg(const int val)
102 {
103  if (val == diagnostic_msgs::DiagnosticStatus::OK)
104  return "OK";
105  if (val == diagnostic_msgs::DiagnosticStatus::WARN)
106  return "Warning";
107  if (val == diagnostic_msgs::DiagnosticStatus::ERROR)
108  return "Error";
109  if (val == 3)
110  return "Stale";
111 
112  ROS_ERROR("Attempting to convert diagnostic level %d into string. Values are: {0: \"OK\", 1: \"Warning\", 2: \"Error\", 3: \"Stale\"}", val);
113  return "Error";
114 }
115 
127 inline std::string removeLeadingNameChaff(const std::string &input_name, const std::string &chaff)
128 {
129  std::string output_name = input_name;
130 
131  if (chaff.size() == 0)
132  return output_name;
133 
134  // Remove start name from all output names
135  // Turns "/PREFIX/base_hokuyo_node: Connection Status" to "/PREFIX/Connection Status"
136  std::size_t last_slash = output_name.rfind("/");
137  std::string start_of_name = output_name.substr(0, last_slash) + std::string("/");
138 
139  if (output_name.find(chaff) == last_slash + 1)
140  output_name.replace(last_slash + 1, chaff.size(), "");
141 
142  if (output_name.find(":", last_slash) == last_slash + 1)
143  output_name= start_of_name + output_name.substr(last_slash + 2);
144 
145  while (output_name.find(" ", last_slash) == last_slash + 1)
146  output_name = start_of_name + output_name.substr(last_slash + 2);
147 
148  return output_name;
149 }
150 
158 class StatusItem
159 {
160 public:
164  StatusItem(const diagnostic_msgs::DiagnosticStatus *status);
165 
169  StatusItem(const std::string item_name, const std::string message = "Missing", const DiagnosticLevel level = Level_Stale);
170 
171  ~StatusItem();
172 
178  bool update(const diagnostic_msgs::DiagnosticStatus *status);
179 
191  boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> toStatusMsg(const std::string &path, const bool stale = false) const;
192 
193  /*
194  *\brief Returns level of DiagnosticStatus message
195  */
196  DiagnosticLevel getLevel() const { return level_; }
197 
201  std::string getMessage() const { return message_; }
202 
206  std::string getName() const { return name_; }
207 
211  std::string getHwId() const { return hw_id_; }
212 
216  const ros::Time getLastUpdateTime() const { return update_time_; }
217 
223  bool hasKey(const std::string &key) const
224  {
225  for (unsigned int i = 0; i < values_.size(); ++i)
226  {
227  if (values_[i].key == key)
228  return true;
229  }
230 
231  return false;
232  }
233 
239  std::string getValue(const std::string &key) const
240  {
241  for (unsigned int i = 0; i < values_.size(); ++i)
242  {
243  if (values_[i].key == key)
244  return values_[i].value;
245  }
246 
247  return std::string("");
248  }
249 
250 private:
252 
258  std::vector<diagnostic_msgs::KeyValue> values_;
259 };
260 
261 }
262 
263 #endif //DIAGNOSTIC_STATUS_ITEM_H
diagnostic_aggregator::StatusItem::level_
DiagnosticLevel level_
Definition: status_item.h:285
diagnostic_aggregator::StatusItem::~StatusItem
~StatusItem()
Definition: status_item.cpp:67
diagnostic_aggregator::StatusItem::name_
std::string name_
Definition: status_item.h:287
diagnostic_aggregator
Definition: aggregator.h:61
diagnostic_aggregator::StatusItem::values_
std::vector< diagnostic_msgs::KeyValue > values_
Definition: status_item.h:290
diagnostic_aggregator::StatusItem::toStatusMsg
boost::shared_ptr< diagnostic_msgs::DiagnosticStatus > toStatusMsg(const std::string &path, const bool stale=false) const
Prepends "path/" to name, makes item stale if "stale" true.
Definition: status_item.cpp:91
boost::shared_ptr
ros.h
diagnostic_aggregator::Level_Error
@ Level_Error
Definition: status_item.h:108
diagnostic_aggregator::valToLevel
DiagnosticLevel valToLevel(const int val)
Converts in to DiagnosticLevel. Values: [0, 3].
Definition: status_item.h:115
diagnostic_aggregator::StatusItem::getHwId
std::string getHwId() const
Returns hardware ID field of DiagnosticStatus message.
Definition: status_item.h:243
diagnostic_aggregator::StatusItem::update_time_
ros::Time update_time_
Definition: status_item.h:283
diagnostic_aggregator::StatusItem::hw_id_
std::string hw_id_
Definition: status_item.h:289
testing::internal::string
::std::string string
Definition: gtest.h:1979
diagnostic_aggregator::StatusItem::getName
std::string getName() const
Returns name of DiagnosticStatus message.
Definition: status_item.h:238
diagnostic_aggregator::StatusItem::getLastUpdateTime
const ros::Time getLastUpdateTime() const
Returns the time since last update for this item.
Definition: status_item.h:248
diagnostic_aggregator::DiagnosticLevel
DiagnosticLevel
Level of StatusItem. OK, Warn, Error, Stale.
Definition: status_item.h:104
diagnostic_aggregator::Level_Warn
@ Level_Warn
Definition: status_item.h:107
diagnostic_aggregator::StatusItem::getMessage
std::string getMessage() const
Get message field of DiagnosticStatus.
Definition: status_item.h:233
diagnostic_aggregator::getOutputName
std::string getOutputName(const std::string item_name)
Replace "/" with "" in output name, to avoid confusing robot monitor.
Definition: status_item.h:87
diagnostic_aggregator::StatusItem::update
bool update(const diagnostic_msgs::DiagnosticStatus *status)
Must have same name as original status or it won't update.
Definition: status_item.cpp:69
diagnostic_aggregator::Level_OK
@ Level_OK
Definition: status_item.h:106
diagnostic_aggregator::removeLeadingNameChaff
std::string removeLeadingNameChaff(const std::string &input_name, const std::string &chaff)
Removes redundant prefixes from status name.
Definition: status_item.h:159
diagnostic_aggregator::StatusItem::StatusItem
StatusItem(const diagnostic_msgs::DiagnosticStatus *status)
Constructed from const DiagnosticStatus*.
Definition: status_item.cpp:42
diagnostic_aggregator::valToMsg
std::string valToMsg(const int val)
Converts int to message {0: 'OK', 1: 'Warning', 2: 'Error', 3: 'Stale' }.
Definition: status_item.h:133
diagnostic_aggregator::StatusItem::message_
std::string message_
Definition: status_item.h:288
diagnostic_aggregator::StatusItem::hasKey
bool hasKey(const std::string &key) const
Returns true if item has key in values KeyValues.
Definition: status_item.h:255
ros::Time
diagnostic_aggregator::StatusItem::output_name_
std::string output_name_
Definition: status_item.h:286
ROS_ERROR
#define ROS_ERROR(...)
diagnostic_aggregator::StatusItem::getValue
std::string getValue(const std::string &key) const
Returns value for given key, "" if doesn't exist.
Definition: status_item.h:271
diagnostic_aggregator::Level_Stale
@ Level_Stale
Definition: status_item.h:109
diag_pub.status
status
Definition: diag_pub.py:54
diagnostic_aggregator::StatusItem::getLevel
DiagnosticLevel getLevel() const
Definition: status_item.h:228


diagnostic_aggregator
Author(s): Kevin Watts, Brice Rebsamen
autogenerated on Wed Aug 3 2022 02:15:35