Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00037 #ifndef GENERIC_ANALYZER_BASE_H
00038 #define GENERIC_ANALYZER_BASE_H
00039
00040 #include <map>
00041 #include <ros/ros.h>
00042 #include <vector>
00043 #include <string>
00044 #include <sstream>
00045 #include <boost/shared_ptr.hpp>
00046 #include <boost/regex.hpp>
00047 #include <pluginlib/class_list_macros.h>
00048 #include "diagnostic_msgs/DiagnosticStatus.h"
00049 #include "diagnostic_msgs/KeyValue.h"
00050 #include "diagnostic_aggregator/analyzer.h"
00051 #include "diagnostic_aggregator/status_item.h"
00052
00053 namespace diagnostic_aggregator {
00054
00065 class GenericAnalyzerBase : public Analyzer
00066 {
00067 public:
00068 GenericAnalyzerBase() :
00069 nice_name_(""), path_(""), timeout_(-1.0), num_items_expected_(-1),
00070 discard_stale_(false), has_initialized_(false), has_warned_(false)
00071 { }
00072
00073 virtual ~GenericAnalyzerBase() { items_.clear(); }
00074
00075
00076
00077
00078 bool init(const std::string path, const ros::NodeHandle &n) = 0;
00079
00080
00081
00082
00083
00084
00085 bool init(const std::string path, const std::string nice_name,
00086 double timeout = -1.0, int num_items_expected = -1, bool discard_stale = false)
00087 {
00088 num_items_expected_ = num_items_expected;
00089 timeout_ = timeout;
00090 nice_name_ = nice_name;
00091 path_ = path;
00092 discard_stale_ = discard_stale;
00093
00094 if (discard_stale_ and timeout <= 0)
00095 {
00096 ROS_WARN("Cannot discard stale items if no timeout specified. No items will be discarded");
00097 discard_stale_ = false;
00098 }
00099
00100 has_initialized_ = true;
00101
00102 return true;
00103 }
00104
00108 virtual bool analyze(const boost::shared_ptr<StatusItem> item)
00109 {
00110 if (!has_initialized_ && !has_warned_)
00111 {
00112 has_warned_ = true;
00113 ROS_ERROR("GenericAnalyzerBase is asked to analyze diagnostics without being initialized. init() must be called in order to correctly use this class.");
00114 }
00115
00116 if (!has_initialized_)
00117 return false;
00118
00119 items_[item->getName()] = item;
00120
00121 return has_initialized_;
00122 }
00123
00129 virtual std::vector<boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> > report()
00130 {
00131 if (!has_initialized_ && !has_warned_)
00132 {
00133 has_warned_ = true;
00134 ROS_ERROR("GenericAnalyzerBase is asked to report diagnostics without being initialized. init() must be called in order to correctly use this class.");
00135 }
00136 if (!has_initialized_)
00137 {
00138 std::vector<boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> > vec;
00139 return vec;
00140 }
00141
00142 boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> header_status(new diagnostic_msgs::DiagnosticStatus());
00143 header_status->name = path_;
00144 header_status->level = 0;
00145 header_status->message = "OK";
00146
00147 std::vector<boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> > processed;
00148 processed.push_back(header_status);
00149
00150 bool all_stale = true;
00151
00152 std::map<std::string, boost::shared_ptr<StatusItem> >::iterator it = items_.begin();
00153 while(it != items_.end())
00154 {
00155 std::string name = it->first;
00156 boost::shared_ptr<StatusItem> item = it->second;
00157
00158 bool stale = false;
00159 if (timeout_ > 0)
00160 stale = (ros::Time::now() - item->getLastUpdateTime()).toSec() > timeout_;
00161
00162
00163 if (discard_stale_ and stale)
00164 {
00165 items_.erase(it++);
00166 continue;
00167 }
00168
00169 int8_t level = item->getLevel();
00170 header_status->level = std::max(header_status->level, level);
00171
00172 diagnostic_msgs::KeyValue kv;
00173 kv.key = name;
00174 kv.value = item->getMessage();
00175
00176 header_status->values.push_back(kv);
00177
00178 all_stale = all_stale && ((level == 3) || stale);
00179
00180
00181
00182 processed.push_back(item->toStatusMsg(path_, stale));
00183
00184 if (stale)
00185 header_status->level = 3;
00186
00187 ++it;
00188 }
00189
00190
00191 if (all_stale)
00192 header_status->level = 3;
00193 else if (header_status->level == 3)
00194 header_status->level = 2;
00195
00196 header_status->message = valToMsg(header_status->level);
00197
00198
00199 if (num_items_expected_ == 0 && items_.size() == 0)
00200 {
00201 header_status->level = 0;
00202 header_status->message = "OK";
00203 }
00204 else if (num_items_expected_ > 0 and int(items_.size()) != num_items_expected_)
00205 {
00206 int8_t lvl = 2;
00207 header_status->level = std::max(lvl, header_status->level);
00208
00209 std::stringstream expec, item;
00210 expec << num_items_expected_;
00211 item << items_.size();
00212
00213 if (items_.size() > 0)
00214 header_status->message = "Expected " + expec.str() + ", found " + item.str();
00215 else
00216 header_status->message = "No items found, expected " + expec.str();
00217 }
00218
00219 return processed;
00220 }
00221
00225 virtual bool match(const std::string name) = 0;
00226
00230 virtual std::string getPath() const { return path_; }
00231
00235 virtual std::string getName() const { return nice_name_; }
00236
00237 protected:
00238 std::string nice_name_;
00239 std::string path_;
00240
00241 double timeout_;
00242 int num_items_expected_;
00243
00247 void addItem(std::string name, boost::shared_ptr<StatusItem> item) { items_[name] = item; }
00248
00249 private:
00253 std::map<std::string, boost::shared_ptr<StatusItem> > items_;
00254
00255 bool discard_stale_, has_initialized_, has_warned_;
00256 };
00257
00258 }
00259 #endif //GENERIC_ANALYZER_BASE_H