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-2016 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  && variable != VAR_NEXT_EDGE
64  ) {
65  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Get Person Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
66  }
67  // begin response building
68  tcpip::Storage tempMsg;
69  // response-code, variableID, objectID
71  tempMsg.writeUnsignedByte(variable);
72  tempMsg.writeString(id);
74  if (variable == ID_LIST || variable == ID_COUNT) {
75  if (variable == ID_LIST) {
76  std::vector<std::string> ids;
77  for (MSTransportableControl::constVehIt i = c.loadedBegin(); i != c.loadedEnd(); ++i) {
78  if (i->second->getCurrentStageType() != MSTransportable::WAITING_FOR_DEPART) {
79  ids.push_back(i->first);
80  }
81  }
83  tempMsg.writeStringList(ids);
84  } else {
86  tempMsg.writeInt((int) c.size());
87  }
88  } else {
89  MSTransportable* p = c.get(id);
90  if (p == 0) {
91  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
92  }
93  switch (variable) {
94  case VAR_POSITION: {
96  tempMsg.writeDouble(p->getPosition().x());
97  tempMsg.writeDouble(p->getPosition().y());
98  }
99  break;
100  case VAR_POSITION3D:
102  tempMsg.writeDouble(p->getPosition().x());
103  tempMsg.writeDouble(p->getPosition().y());
104  tempMsg.writeDouble(p->getPosition().z());
105  break;
106  case VAR_ANGLE:
109  break;
110  case VAR_SPEED:
112  tempMsg.writeDouble(p->getSpeed());
113  break;
114  case VAR_ROAD_ID:
116  tempMsg.writeString(p->getEdge()->getID());
117  break;
118  case VAR_LANEPOSITION:
120  tempMsg.writeDouble(p->getEdgePos());
121  break;
122  case VAR_COLOR:
123  tempMsg.writeUnsignedByte(TYPE_COLOR);
124  tempMsg.writeUnsignedByte(p->getParameter().color.red());
125  tempMsg.writeUnsignedByte(p->getParameter().color.green());
126  tempMsg.writeUnsignedByte(p->getParameter().color.blue());
127  tempMsg.writeUnsignedByte(p->getParameter().color.alpha());
128  break;
129  case VAR_WAITING_TIME:
131  tempMsg.writeDouble(p->getWaitingSeconds());
132  break;
133  case VAR_TYPE:
135  tempMsg.writeString(p->getVehicleType().getID());
136  break;
137  case VAR_NEXT_EDGE:
139  tempMsg.writeString(dynamic_cast<MSPerson*>(p)->getNextEdge());
140  break;
141  case VAR_PARAMETER: {
142  std::string paramName = "";
143  if (!server.readTypeCheckingString(inputStorage, paramName)) {
144  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
145  }
147  tempMsg.writeString(p->getParameter().getParameter(paramName, ""));
148  }
149  default:
151  break;
152  }
153  }
154  server.writeStatusCmd(CMD_GET_PERSON_VARIABLE, RTYPE_OK, "", outputStorage);
155  server.writeResponseWithLength(outputStorage, tempMsg);
156  return true;
157 }
158 
159 
160 bool
162  tcpip::Storage& outputStorage) {
163  std::string warning = ""; // additional description for response
164  // variable
165  int variable = inputStorage.readUnsignedByte();
166  if (variable != VAR_PARAMETER
167  ) {
168  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
169  }
170  // id
172  std::string id = inputStorage.readString();
173  MSTransportable* p = c.get(id);
174  if (p == 0) {
175  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
176  }
177  // process
178  switch (variable) {
179  case VAR_PARAMETER: {
180  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
181  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
182  }
183  //readt itemNo
184  inputStorage.readInt();
185  std::string name;
186  if (!server.readTypeCheckingString(inputStorage, name)) {
187  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
188  }
189  std::string value;
190  if (!server.readTypeCheckingString(inputStorage, value)) {
191  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
192  }
193  ((SUMOVehicleParameter&) p->getParameter()).addParameter(name, value);
194  }
195  break;
196  default:
197  /*
198  try {
199  if (!TraCIServerAPI_VehicleType::setVariable(CMD_SET_PERSON_VARIABLE, variable, getSingularType(v), server, inputStorage, outputStorage)) {
200  return false;
201  }
202  } catch (ProcessError& e) {
203  return server.writeErrorStatusCmd(CMD_SET_VEHICLE_VARIABLE, e.what(), outputStorage);
204  }
205  */
206  break;
207  }
208  server.writeStatusCmd(CMD_SET_PERSON_VARIABLE, RTYPE_OK, warning, outputStorage);
209  return true;
210 }
211 
212 
213 bool
214 TraCIServerAPI_Person::getPosition(const std::string& id, Position& p) {
215  MSPerson* person = dynamic_cast<MSPerson*>(MSNet::getInstance()->getPersonControl().get(id));
216  if (person == 0) {
217  return false;
218  }
219  p = person->getPosition();
220  return true;
221 }
222 
223 
224 /*
225 MSVehicleType&
226 TraCIServerAPI_Person::getSingularType(MSPerson* const person) {
227  const MSVehicleType& oType = person->getVehicleType();
228  std::string newID = oType.getID().find('@') == std::string::npos ? oType.getID() + "@" + person->getID() : oType.getID();
229  MSVehicleType* type = MSVehicleType::build(newID, &oType);
230  person->replaceVehicleType(type);
231  return *type;
232 }
233 */
234 
235 
236 
237 #endif
238 
239 
240 /****************************************************************************/
241 
#define VAR_ROAD_ID
RGBColor color
The vehicle's color.
#define VAR_LENGTH
std::map< std::string, MSTransportable * >::const_iterator constVehIt
Definition of the internal transportables map iterator.
#define TYPE_COMPOUND
#define POSITION_2D
#define VAR_POSITION
const SUMOVehicleParameter & getParameter() const
virtual SUMOReal getWaitingSeconds() const
the time this transportable spent waiting in seconds
#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
#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 SUMOReal getEdgePos() const
Return the position on the edge.
virtual void writeUnsignedByte(int)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
virtual SUMOReal getAngle() const
return the current angle of the transportable
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
#define VAR_NEXT_EDGE
virtual void writeInt(int)
#define VAR_POSITION3D
#define TYPE_STRING
virtual int readUnsignedByte()
static bool getPosition(const std::string &id, Position &p)
Returns the named persons's position.
int size() const
Returns the number of known transportables.
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:66
const MSVehicleType & getVehicleType() const
#define VAR_SHAPECLASS
const MSEdge * getEdge() const
Returns the current edge.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xce: Change Person State)
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
virtual int readInt()
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:699
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)
virtual std::string readString()
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xae: Get Person Variable)
static SUMOReal naviDegree(const SUMOReal angle)
Definition: GeomHelper.cpp:191
#define VAR_SPEED
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
constVehIt loadedEnd() const
Returns the end of the internal transportables map.
virtual SUMOReal getSpeed() const
the current speed of the transportable
virtual void writeString(const std::string &s)
Structure representing possible vehicle parameter.
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
#define RESPONSE_GET_PERSON_VARIABLE
const std::string & getID() const
Returns the name of the vehicle type.
virtual Position getPosition() const
Return the Network coordinate of the transportable.
virtual void writeDouble(double)
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.
constVehIt loadedBegin() const
Returns the begin of the internal transportables map.
#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