00001 #include "mrta/history.h" 00002 #include "mrta/robot.h" 00003 #include "mrta/sample_holder.h" 00004 #include "mrta/state_monitor.h" 00005 #include "mrta/system.h" 00006 #include "mrta/task.h" 00007 #include "rqt_mrta/config/application/robot.h" 00008 00009 namespace mrta 00010 { 00011 Robot::Robot(System* parent, Config* config) 00012 : QObject(parent), config_(NULL), state_(Offline), 00013 monitor_(new StateMonitor(this, ros::Duration(5.0), STATE_COUNT)), 00014 history_(new History(this)) 00015 { 00016 setConfig(config); 00017 connect(monitor_, SIGNAL(updated(size_t, bool)), this, 00018 SLOT(monitorUpdated(size_t, bool))); 00019 } 00020 00021 Robot::Robot(const Robot& robot) 00022 : QObject(robot.parent()), config_(NULL), id_(robot.id_), tasks_(robot.tasks_), 00023 state_(robot.state_), history_(robot.history_), monitor_(robot.monitor_) 00024 { 00025 setConfig(robot.config_); 00026 } 00027 00028 Robot::~Robot() 00029 { 00030 ROS_INFO_STREAM("[~Robot] before"); 00031 config_ = NULL; 00032 if (history_) 00033 { 00034 delete history_; 00035 history_ = NULL; 00036 } 00037 if (monitor_) 00038 { 00039 delete monitor_; 00040 monitor_ = NULL; 00041 } 00042 tasks_.clear(); 00043 ROS_INFO_STREAM("[~Robot] after"); 00044 } 00045 00046 Robot::Config* Robot::getConfig() const { return config_; } 00047 00048 void Robot::setConfig(Robot::Config* config) 00049 { 00050 if (config != config_) 00051 { 00052 if (config_) 00053 { 00054 disconnect(config_, SIGNAL(changed()), this, SIGNAL(changed())); 00055 disconnect(config_, SIGNAL(destroyed()), this, SLOT(configDestroyed())); 00056 disconnect(config_, SIGNAL(idChanged(const QString&)), this, 00057 SLOT(setId(const QString&))); 00058 } 00059 config_ = config; 00060 if (config_) 00061 { 00062 connect(config_, SIGNAL(changed()), this, SIGNAL(changed())); 00063 connect(config_, SIGNAL(destroyed()), this, SLOT(configDestroyed())); 00064 connect(config_, SIGNAL(idChanged(const QString&)), this, 00065 SLOT(setId(const QString&))); 00066 setId(config_->getId()); 00067 clearTasks(); 00068 for (size_t index(0); index < config->getTasks()->count(); index++) 00069 { 00070 addTask(new Task(this, config->getTasks()->getTask(index))); 00071 } 00072 } 00073 } 00074 } 00075 00076 QString Robot::getId() const { return id_; } 00077 00078 Robot::Type Robot::getType() const { return type_; } 00079 00080 Robot::State Robot::getState() const { return state_; } 00081 00082 History* Robot::getHistory() const { return history_; } 00083 00084 StateMonitor* Robot::getStateMonitor() const { return monitor_; } 00085 00086 void Robot::setId(const QString& id) 00087 { 00088 if (id != id_) 00089 { 00090 id_ = id; 00091 emit idChanged(id); 00092 emit changed(); 00093 } 00094 } 00095 00096 void Robot::setState(Robot::State state) 00097 { 00098 if (state != state_) 00099 { 00100 state_ = state; 00101 Log::Severity severity; 00102 switch (state_) 00103 { 00104 case Idle: 00105 severity = Log::Info; 00106 emit idle(); 00107 break; 00108 case Busy: 00109 severity = Log::Info; 00110 emit busy(); 00111 break; 00112 case Offline: 00113 severity = Log::Warn; 00114 emit offline(); 00115 break; 00116 } 00117 history_->log(ros::Time::now(), Log::Robot, severity, id_, state_); 00118 emit stateChanged(state); 00119 emit changed(); 00120 } 00121 } 00122 00123 size_t Robot::count() const { return tasks_.count(); } 00124 00125 Task* Robot::getTask(int index) const { return tasks_[index]; } 00126 00127 void Robot::addTask(Task* task) 00128 { 00129 tasks_.append(task); 00130 connect(task, SIGNAL(changed()), this, SLOT(changed())); 00131 connect(task, SIGNAL(changed()), this, SLOT(taskChanged())); 00132 connect(task, SIGNAL(idChanged(const QString&)), this, 00133 SLOT(taskIdChanged(const QString&))); 00134 connect(task, SIGNAL(destroyed()), this, SLOT(taskDestroyed())); 00135 emit added(tasks_.count() - 1); 00136 emit changed(); 00137 } 00138 00139 void Robot::removeTask(Task* task) 00140 { 00141 size_t index(tasks_.indexOf(task)); 00142 QString task_id(task->getId()); 00143 tasks_.remove(index); 00144 emit removed(task_id); 00145 emit changed(); 00146 } 00147 00148 void Robot::clearTasks() { tasks_.clear(); } 00149 00150 Robot& Robot::operator=(const Robot& robot) 00151 { 00152 id_ = robot.id_; 00153 tasks_ = robot.tasks_; 00154 emit changed(); 00155 } 00156 00157 void Robot::configDestroyed() { config_ = NULL; } 00158 00159 void Robot::taskDestroyed() 00160 { 00161 Task* task = static_cast<Task*>(sender()); 00162 int index(tasks_.indexOf(task)); 00163 if (index != -1) 00164 { 00165 QString task_id(task->getId()); 00166 tasks_.remove(index); 00167 emit removed(task_id); 00168 emit changed(); 00169 } 00170 } 00171 00172 void Robot::monitorUpdated(size_t index, bool up_to_date) 00173 { 00174 if (up_to_date) 00175 { 00176 setState(static_cast<State>(index)); 00177 } 00178 else if (index == Busy) 00179 { 00180 bool idle(monitor_->getSampleHolder(Idle)->isUpToDate()); 00181 setState(idle ? Idle : Offline); 00182 } 00183 } 00184 }