icub-client
sensationManager.cpp
Go to the documentation of this file.
1 #include "sensationManager.h"
2 
4  for(auto& sens : sensations) {
5  sens->close_ports();
6  delete sens;
7  }
8  sensations.clear();
9 
10  rpc_in_port.interrupt();
11  rpc_in_port.close();
12 
13  return true;
14 }
15 
16 
17 bool SensationManager::configure(yarp::os::ResourceFinder &rf)
18 {
19  moduleName = rf.check("name",Value("SensationManager")).asString();
20  setName(moduleName.c_str());
21  cout<<moduleName<<": finding configuration files..."<<endl;
22  period = rf.check("period",Value(0.1)).asDouble();
23 
24  Bottle grp = rf.findGroup("SENSATIONS");
25  if (!grp.isNull()){
26  sensationList = *grp.find("sensations").asList();
27  for (unsigned int i = 0; i < sensationList.size(); i++)
28  {
29  string sensation_name = sensationList.get(i).asString();
30  if (sensation_name == "opcSensation") {
31  sensations.push_back(new OpcSensation());
32  } else if (sensation_name == "test") {
33  sensations.push_back(new TestSensation());
34  } else{
35  yError() << "Sensation " + sensation_name + " not implemented";
36  return false;
37  }
38  sensations.back()->configure();
39  }
40  }else{
41  yError()<<"Didn't find any sensation. Please revise your configuration files...";
42  return 0;
43  }
44 
45  rpc_in_port.open("/" + moduleName + "/rpc");
46  attach(rpc_in_port);
47  yInfo("Init done");
48 
49  return true;
50 }
51 
52 
54 {
55  for(auto& sensation : sensations) {
56  sensation->publish();
57  }
58  return true;
59 }
60 
61 bool SensationManager::respond(const Bottle& cmd, Bottle& reply)
62 {
63  yInfo() << "RPC received in sensationsManager";
64  yDebug() << cmd.toString();
65 
66  reply.clear();
67  bool rpl;
68  if (cmd.get(0).asString() == "help" )
69  { string help = "\n";
70  help += " ['is' + entity_name + entity_tag] : Turns on/off manual mode (for manual control of drives) \n";
71  reply.addString(help);
72  }
73  else if (cmd.get(0).asString() == "is") {
74  for (unsigned int i = 0; i < sensationList.size(); i++)
75  {
76  string sensation_name = sensationList.get(i).asString();
77  if (sensation_name == "opcSensation") {
78  rpl = dynamic_cast<OpcSensation*>(sensations[i])->get_property(cmd.get(1).asString(),cmd.get(2).asString());
79  reply.addString("ack");
80  reply.addInt(rpl);
81  return true;
82  }
83  }
84  if (reply.size()==0)
85  {
86  reply.addString("nack");
87  }
88  } else {
89  reply.addString("nack");
90  reply.addString("Unknown rpc command");
91  }
92  return true;
93 }
94 
bool configure(yarp::os::ResourceFinder &rf)
bool respond(const Bottle &cmd, Bottle &reply)