SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TraCIServerAPI_Person.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // APIs for getting/setting person values via TraCI
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #ifndef NO_TRACI
32 
35 #include <microsim/MSNet.h>
36 #include <microsim/MSEdge.h>
37 #include "TraCIConstants.h"
38 #include "TraCIServer.h"
39 #include "TraCIServerAPI_Person.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 bool
52  tcpip::Storage& outputStorage) {
53  // variable
54  int variable = inputStorage.readUnsignedByte();
55  std::string id = inputStorage.readString();
56  // check variable
57  if (variable != ID_LIST && variable != ID_COUNT
58  && variable != VAR_POSITION && variable != VAR_POSITION3D && variable != VAR_ANGLE && variable != VAR_SPEED
59  && variable != VAR_ROAD_ID && variable != VAR_LANEPOSITION
60  && variable != VAR_WIDTH && variable != VAR_LENGTH && variable != VAR_MINGAP
61  && variable != VAR_TYPE && variable != VAR_SHAPECLASS && variable != VAR_COLOR
62  && variable != VAR_WAITING_TIME && variable != VAR_PARAMETER
63  ) {
64  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Get Person Variable: unsupported variable specified", outputStorage);
65  }
66  // begin response building
67  tcpip::Storage tempMsg;
68  // response-code, variableID, objectID
70  tempMsg.writeUnsignedByte(variable);
71  tempMsg.writeString(id);
73  if (variable == ID_LIST || variable == ID_COUNT) {
74  if (variable == ID_LIST) {
75  std::vector<std::string> ids;
77  ids.push_back((*i).first);
78  }
80  tempMsg.writeStringList(ids);
81  } else {
83  tempMsg.writeInt((int) c.size());
84  }
85  } else {
86  MSPerson* p = c.get(id);
87  if (p == 0) {
88  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
89  }
90  switch (variable) {
91  case VAR_POSITION: {
93  tempMsg.writeDouble(p->getPosition().x());
94  tempMsg.writeDouble(p->getPosition().y());
95  }
96  break;
97  case VAR_POSITION3D:
99  tempMsg.writeDouble(p->getPosition().x());
100  tempMsg.writeDouble(p->getPosition().y());
101  tempMsg.writeDouble(p->getPosition().z());
102  break;
103  case VAR_ANGLE:
105  tempMsg.writeDouble(p->getAngle());
106  break;
107  case VAR_SPEED:
109  tempMsg.writeDouble(p->getSpeed());
110  break;
111  case VAR_ROAD_ID:
113  tempMsg.writeString(p->getEdge()->getID());
114  break;
115  case VAR_LANEPOSITION:
117  tempMsg.writeDouble(p->getEdgePos());
118  break;
119  case VAR_COLOR:
120  tempMsg.writeUnsignedByte(TYPE_COLOR);
121  tempMsg.writeUnsignedByte(p->getParameter().color.red());
122  tempMsg.writeUnsignedByte(p->getParameter().color.green());
123  tempMsg.writeUnsignedByte(p->getParameter().color.blue());
124  tempMsg.writeUnsignedByte(p->getParameter().color.alpha());
125  break;
126  case VAR_WAITING_TIME:
128  tempMsg.writeDouble(p->getWaitingSeconds());
129  break;
130  case VAR_TYPE:
132  tempMsg.writeString(p->getVehicleType().getID());
133  break;
134  case VAR_PARAMETER: {
135  std::string paramName = "";
136  if (!server.readTypeCheckingString(inputStorage, paramName)) {
137  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
138  }
140  tempMsg.writeString(p->getParameter().getParameter(paramName, ""));
141  }
142  default:
144  break;
145  }
146  }
147  server.writeStatusCmd(CMD_GET_PERSON_VARIABLE, RTYPE_OK, "", outputStorage);
148  server.writeResponseWithLength(outputStorage, tempMsg);
149  return true;
150 }
151 
152 
153 bool
155  tcpip::Storage& outputStorage) {
156  std::string warning = ""; // additional description for response
157  // variable
158  int variable = inputStorage.readUnsignedByte();
159  if (variable != VAR_PARAMETER
160  ) {
161  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable specified", outputStorage);
162  }
163  // id
165  std::string id = inputStorage.readString();
166  MSPerson* p = c.get(id);
167  if (p == 0) {
168  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
169  }
170  // process
171  switch (variable) {
172  case VAR_PARAMETER: {
173  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
174  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
175  }
176  //readt itemNo
177  inputStorage.readInt();
178  std::string name;
179  if (!server.readTypeCheckingString(inputStorage, name)) {
180  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
181  }
182  std::string value;
183  if (!server.readTypeCheckingString(inputStorage, value)) {
184  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
185  }
186  ((SUMOVehicleParameter&) p->getParameter()).addParameter(name, value);
187  }
188  break;
189  default:
190  /*
191  try {
192  if (!TraCIServerAPI_VehicleType::setVariable(CMD_SET_PERSON_VARIABLE, variable, getSingularType(v), server, inputStorage, outputStorage)) {
193  return false;
194  }
195  } catch (ProcessError& e) {
196  return server.writeErrorStatusCmd(CMD_SET_VEHICLE_VARIABLE, e.what(), outputStorage);
197  }
198  */
199  break;
200  }
201  server.writeStatusCmd(CMD_SET_PERSON_VARIABLE, RTYPE_OK, warning, outputStorage);
202  return true;
203 }
204 
205 
206 bool
207 TraCIServerAPI_Person::getPosition(const std::string& id, Position& p) {
208  MSPerson* person = dynamic_cast<MSPerson*>(MSNet::getInstance()->getPersonControl().get(id));
209  if (person == 0) {
210  return false;
211  }
212  p = person->getPosition();
213  return true;
214 }
215 
216 
217 /*
218 MSVehicleType&
219 TraCIServerAPI_Person::getSingularType(MSPerson* const person) {
220  const MSVehicleType& oType = person->getVehicleType();
221  std::string newID = oType.getID().find('@') == std::string::npos ? oType.getID() + "@" + person->getID() : oType.getID();
222  MSVehicleType* type = MSVehicleType::build(newID, &oType);
223  person->replaceVehicleType(type);
224  return *type;
225 }
226 */
227 
228 
229 
230 #endif
231 
232 
233 /****************************************************************************/
234 
#define VAR_ROAD_ID
RGBColor color
The vehicle's color.
#define VAR_LENGTH
const MSVehicleType & getVehicleType() const
Definition: MSPerson.h:610
#define TYPE_COMPOUND
#define POSITION_2D
#define VAR_POSITION
virtual Position getPosition() const
return the Network coordinate of the person
Definition: MSPerson.cpp:621
const MSEdge * getEdge() const
Returns the current edge.
Definition: MSPerson.h:542
#define RTYPE_OK
static bool getVariable(const int variable, const MSVehicleType &v, tcpip::Storage &tempMsg)
Processes a value request for the given type.
#define VAR_WAITING_TIME
#define CMD_GET_PERSON_VARIABLE
#define VAR_TYPE
virtual SUMOReal getWaitingSeconds() const
the time this person spent waiting in seconds
Definition: MSPerson.cpp:632
constVehIt loadedPersonsEnd() const
Returns the end of the internal persons map.
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
#define POSITION_3D
virtual void writeUnsignedByte(int)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
virtual void writeInt(int)
#define VAR_POSITION3D
#define TYPE_STRING
virtual int readUnsignedByte()
const SUMOVehicleParameter & getParameter() const
Definition: MSPerson.h:605
static bool getPosition(const std::string &id, Position &p)
Returns the named persons's position.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
#define VAR_ANGLE
const std::string & getID() const
Returns the id.
Definition: Named.h:60
#define VAR_SHAPECLASS
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xce: Change Person State)
virtual int readInt()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
#define VAR_LANEPOSITION
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
virtual void writeStringList(const std::vector< std::string > &s)
std::map< std::string, MSPerson * >::const_iterator constVehIt
Definition of the internal persons map iterator.
virtual std::string readString()
unsigned int size() const
Returns the number of known persons.
virtual SUMOReal getAngle() const
return the current angle of the person
Definition: MSPerson.cpp:627
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xae: Get Person Variable)
#define VAR_SPEED
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
Structure representing possible vehicle parameter.
#define TYPE_DOUBLE
virtual MSPersonControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:658
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
#define RESPONSE_GET_PERSON_VARIABLE
MSPerson * get(const std::string &id) const
Returns the named person, if existing.
const std::string & getID() const
Returns the name of the vehicle type.
virtual void writeDouble(double)
virtual SUMOReal getSpeed() const
the current speed of the person
Definition: MSPerson.cpp:637
virtual SUMOReal getEdgePos() const
return the offset from the start of the current edge
Definition: MSPerson.cpp:616
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define CMD_SET_PERSON_VARIABLE
#define VAR_PARAMETER
#define ID_COUNT
#define TYPE_INTEGER
#define VAR_MINGAP
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
#define VAR_WIDTH
constVehIt loadedPersonsBegin() const
Returns the begin of the internal persons map.