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 
73 {
74  Level_OK = diagnostic_msgs::DiagnosticStatus::OK,
75  Level_Warn = diagnostic_msgs::DiagnosticStatus::WARN,
76  Level_Error = diagnostic_msgs::DiagnosticStatus::ERROR,
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 
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
::std::string string
Definition: gtest.h:1979
std::string valToMsg(const int val)
Converts int to message {0: &#39;OK&#39;, 1: &#39;Warning&#39;, 2: &#39;Error&#39;, 3: &#39;Stale&#39; }.
Definition: status_item.h:101
DiagnosticLevel valToLevel(const int val)
Converts in to DiagnosticLevel. Values: [0, 3].
Definition: status_item.h:83
std::string getValue(const std::string &key) const
Returns value for given key, "" if doens&#39;t exist.
Definition: status_item.h:239
std::string getName() const
Returns name of DiagnosticStatus message.
Definition: status_item.h:206
std::string getMessage() const
Get message field of DiagnosticStatus.
Definition: status_item.h:201
StatusItem(const diagnostic_msgs::DiagnosticStatus *status)
Constructed from const DiagnosticStatus*.
Definition: status_item.cpp:42
std::string removeLeadingNameChaff(const std::string &input_name, const std::string &chaff)
Removes redundant prefixes from status name.
Definition: status_item.h:127
DiagnosticLevel getLevel() const
Definition: status_item.h:196
DiagnosticLevel
Level of StatusItem. OK, Warn, Error, Stale.
Definition: status_item.h:72
const ros::Time getLastUpdateTime() const
Returns the time since last update for this item.
Definition: status_item.h:216
bool hasKey(const std::string &key) const
Returns true if item has key in values KeyValues.
Definition: status_item.h:223
std::string getHwId() const
Returns hardware ID field of DiagnosticStatus message.
Definition: status_item.h:211
std::vector< diagnostic_msgs::KeyValue > values_
Definition: status_item.h:258
Helper class to hold, store DiagnosticStatus messages.
Definition: status_item.h:158
std::string getOutputName(const std::string item_name)
Replace "/" with "" in output name, to avoid confusing robot monitor.
Definition: status_item.h:55
#define ROS_ERROR(...)
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
bool update(const diagnostic_msgs::DiagnosticStatus *status)
Must have same name as original status or it won&#39;t update.
Definition: status_item.cpp:69


diagnostic_aggregator
Author(s): Kevin Watts, Brice Rebsamen
autogenerated on Mon Feb 28 2022 22:18:07