00001 #include "mrta/monitor.h" 00002 #include "mrta/sample_holder.h" 00003 #include "mrta/state_monitor.h" 00004 #include "mrta/system.h" 00005 #include "rqt_mrta/config/architecture/topic.h" 00006 #include "utilities/message_subscriber_registry.h" 00007 00008 namespace mrta 00009 { 00010 Monitor::Monitor(System* system, utilities::MessageSubscriberRegistry* registry, 00011 Config* config) 00012 : QObject(system), subscriber_(NULL), registry_(NULL), config_(NULL) 00013 { 00014 connect(this, SIGNAL(changed()), this, SLOT(updateSubscriber())); 00015 setRegistry(registry); 00016 setConfig(config); 00017 } 00018 00019 Monitor::~Monitor() 00020 { 00021 ROS_INFO_STREAM("[~Monitor] before"); 00022 config_ = NULL; 00023 registry_ = NULL; 00024 unsubscribe(); 00025 if (subscriber_) 00026 { 00027 delete subscriber_; 00028 subscriber_ = NULL; 00029 } 00030 monitors_.clear(); 00031 ROS_INFO_STREAM("[~Monitor] after"); 00032 } 00033 00034 Monitor::Config* Monitor::getConfig() const { return config_; } 00035 00036 QString Monitor::getName() const { return name_; } 00037 00038 QString Monitor::getType() const { return type_; } 00039 00040 size_t Monitor::getQueueSize() const { return queue_size_; } 00041 00042 QString Monitor::getField() const { return field_; } 00043 00044 ros::Duration Monitor::getTimeout() const { return timeout_; } 00045 00046 ros::Duration Monitor::getHorizon() const { return horizon_; } 00047 00048 System* Monitor::getSystem() const { return static_cast<System*>(parent()); } 00049 00050 void Monitor::setConfig(Monitor::Config* config) 00051 { 00052 if (config != config_) 00053 { 00054 if (config_) 00055 { 00056 disconnect(config_, SIGNAL(nameChanged(const QString&)), this, 00057 SLOT(setName(const QString&))); 00058 disconnect(config_, SIGNAL(typeChanged(const QString&)), this, 00059 SLOT(setType(const QString&))); 00060 disconnect(config_, SIGNAL(queueSizeChanged(const size_t&)), this, 00061 SLOT(setQueueSize(const size_t&))); 00062 disconnect(config_, SIGNAL(fieldChanged(const QString&)), this, 00063 SLOT(setField(const QString&))); 00064 disconnect(config_, SIGNAL(timeoutChanged(const ros::Duration&)), this, 00065 SLOT(setTimeout(const ros::Duration&))); 00066 disconnect(config_, SIGNAL(horizonChanged(const ros::Duration&)), this, 00067 SLOT(setHorizon(const ros::Duration&))); 00068 } 00069 config_ = config; 00070 if (config_) 00071 { 00072 connect(config_, SIGNAL(nameChanged(const QString&)), this, 00073 SLOT(setName(const QString&))); 00074 connect(config_, SIGNAL(typeChanged(const QString&)), this, 00075 SLOT(setType(const QString&))); 00076 connect(config_, SIGNAL(queueSizeChanged(const size_t&)), this, 00077 SLOT(setQueueSize(const size_t&))); 00078 connect(config_, SIGNAL(fieldChanged(const QString&)), this, 00079 SLOT(setField(const QString&))); 00080 connect(config_, SIGNAL(timeoutChanged(const ros::Duration&)), this, 00081 SLOT(setTimeout(const ros::Duration&))); 00082 connect(config_, SIGNAL(horizonChanged(const ros::Duration&)), this, 00083 SLOT(setHorizon(const ros::Duration&))); 00084 setName(config_->getName()); 00085 setType(config_->getType()); 00086 setQueueSize(config_->getQueueSize()); 00087 setField(config_->getField()); 00088 setTimeout(config_->getTimeout()); 00089 setHorizon(config_->getHorizon()); 00090 } 00091 emit changed(); 00092 } 00093 } 00094 00095 void Monitor::setName(const QString& name) 00096 { 00097 if (name != name_) 00098 { 00099 name_ = name; 00100 emit changed(); 00101 } 00102 } 00103 00104 void Monitor::setType(const QString& type) 00105 { 00106 if (type != type_) 00107 { 00108 type_ = type; 00109 emit changed(); 00110 } 00111 } 00112 00113 void Monitor::setQueueSize(const size_t& queue_size) 00114 { 00115 if (queue_size != queue_size_) 00116 { 00117 queue_size_ = queue_size; 00118 emit changed(); 00119 } 00120 } 00121 00122 void Monitor::setField(const QString& field) 00123 { 00124 if (field != field_) 00125 { 00126 field_ = field; 00127 emit changed(); 00128 } 00129 } 00130 00131 void Monitor::setTimeout(const ros::Duration& timeout) 00132 { 00133 if (timeout != timeout_) 00134 { 00135 timeout_ = timeout; 00136 for (StateMonitorMap::iterator it(monitors_.begin()); it != monitors_.end(); 00137 it++) 00138 { 00139 monitors_[it.key()]->setTimeout(timeout); 00140 } 00141 emit changed(); 00142 } 00143 } 00144 00145 void Monitor::setHorizon(const ros::Duration& horizon) 00146 { 00147 if (horizon != horizon_) 00148 { 00149 horizon_ = horizon; 00150 emit changed(); 00151 } 00152 } 00153 00154 void Monitor::setRegistry(utilities::MessageSubscriberRegistry* registry) 00155 { 00156 if (registry != registry_) 00157 { 00158 registry_ = registry; 00159 if (subscriber_) 00160 { 00161 subscriber_->setRegistry(registry); 00162 } 00163 } 00164 } 00165 00166 void Monitor::addStateMonitor(const QString& id, StateMonitor* monitor) 00167 { 00168 if (!monitors_.contains(id)) 00169 { 00170 monitors_.insert(id, monitor); 00171 } 00172 } 00173 00174 void Monitor::update(const QString& id, int state) 00175 { 00176 if (monitors_.contains(id)) 00177 { 00178 monitors_[id]->update(state); 00179 } 00180 } 00181 00182 void Monitor::updateSubscriber() 00183 { 00184 unsubscribe(); 00185 subscribe(); 00186 } 00187 00188 bool Monitor::subscribe() 00189 { 00190 if (name_.isEmpty() || type_.isEmpty() || field_.isEmpty() || !registry_) 00191 { 00192 return false; 00193 } 00194 subscriber_ = 00195 new utilities::MessageFieldSubscriber(this, type_, field_, registry_); 00196 subscriber_->subscribe(name_, queue_size_); 00197 if (subscriber_->isSubscribed()) 00198 { 00199 connect( 00200 subscriber_, 00201 SIGNAL(received(const variant_topic_tools::BuiltinVariant&)), this, 00202 SLOT(subscriberReceived(const variant_topic_tools::BuiltinVariant&))); 00203 } 00204 else 00205 { 00206 delete subscriber_; 00207 subscriber_ = NULL; 00208 } 00209 return subscriber_; 00210 } 00211 00212 bool Monitor::unsubscribe() 00213 { 00214 if (!subscriber_) 00215 { 00216 return false; 00217 } 00218 disconnect( 00219 subscriber_, SIGNAL(received(const variant_topic_tools::BuiltinVariant&)), 00220 this, 00221 SLOT(subscriberReceived(const variant_topic_tools::BuiltinVariant&))); 00222 if (subscriber_->isSubscribed()) 00223 { 00224 subscriber_->unsubscribe(); 00225 } 00226 delete subscriber_; 00227 subscriber_ = NULL; 00228 return true; 00229 } 00230 00231 void Monitor::subscriberReceived( 00232 variant_topic_tools::BuiltinVariant field_value) 00233 { 00234 QString id(QString::fromStdString(field_value.getValue<std::string>())); 00235 update(id); 00236 emit received(id); 00237 } 00238 }