1 |
|
|
#include "dynamic_graph_bridge/ros_interpreter.hh" |
2 |
|
|
|
3 |
|
|
namespace dynamicgraph { |
4 |
|
|
static const int queueSize = 5; |
5 |
|
|
|
6 |
|
|
Interpreter::Interpreter(ros::NodeHandle& nodeHandle) |
7 |
|
|
: interpreter_(), |
8 |
|
|
nodeHandle_(nodeHandle), |
9 |
|
|
runCommandSrv_(), |
10 |
|
|
runPythonFileSrv_() {} |
11 |
|
|
|
12 |
|
|
void Interpreter::startRosService() { |
13 |
|
|
runCommandCallback_t runCommandCb = |
14 |
|
|
boost::bind(&Interpreter::runCommandCallback, this, _1, _2); |
15 |
|
|
runCommandSrv_ = nodeHandle_.advertiseService("run_command", runCommandCb); |
16 |
|
|
|
17 |
|
|
runPythonFileCallback_t runPythonFileCb = |
18 |
|
|
boost::bind(&Interpreter::runPythonFileCallback, this, _1, _2); |
19 |
|
|
runPythonFileSrv_ = |
20 |
|
|
nodeHandle_.advertiseService("run_script", runPythonFileCb); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
bool Interpreter::runCommandCallback( |
24 |
|
|
dynamic_graph_bridge_msgs::RunCommand::Request& req, |
25 |
|
|
dynamic_graph_bridge_msgs::RunCommand::Response& res) { |
26 |
|
|
interpreter_.python(req.input, res.result, res.standardoutput, |
27 |
|
|
res.standarderror); |
28 |
|
|
return true; |
29 |
|
|
} |
30 |
|
|
|
31 |
|
|
bool Interpreter::runPythonFileCallback( |
32 |
|
|
dynamic_graph_bridge_msgs::RunPythonFile::Request& req, |
33 |
|
|
dynamic_graph_bridge_msgs::RunPythonFile::Response& res) { |
34 |
|
|
interpreter_.runPythonFile(req.input); |
35 |
|
|
res.result = "File parsed"; // FIX: It is just an echo, is there a way to |
36 |
|
|
// have a feedback? |
37 |
|
|
return true; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
void Interpreter::runCommand(const std::string& command, std::string& result, |
41 |
|
|
std::string& out, std::string& err) { |
42 |
|
|
interpreter_.python(command, result, out, err); |
43 |
|
|
if (err.size() > 0) { |
44 |
|
|
ROS_ERROR(err.c_str()); |
45 |
|
|
} |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
void Interpreter::runPythonFile(std::string ifilename) { |
49 |
|
|
interpreter_.runPythonFile(ifilename); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
} // end of namespace dynamicgraph. |