SUMO - Simulation of Urban MObility
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;
78  ids.push_back((*i).first);
79  }
81  tempMsg.writeStringList(ids);
82  } else {
84  tempMsg.writeInt((int) c.size());
85  }
86  } else {
87  MSTransportable* p = c.get(id);
88  if (p == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
90  }
91  switch (variable) {
92  case VAR_POSITION: {
94  tempMsg.writeDouble(p->getPosition().x());
95  tempMsg.writeDouble(p->getPosition().y());
96  }
97  break;
98  case VAR_POSITION3D:
100  tempMsg.writeDouble(p->getPosition().x());
101  tempMsg.writeDouble(p->getPosition().y());
102  tempMsg.writeDouble(p->getPosition().z());
103  break;
104  case VAR_ANGLE:
107  break;
108  case VAR_SPEED:
110  tempMsg.writeDouble(p->getSpeed());
111  break;
112  case VAR_ROAD_ID:
114  tempMsg.writeString(p->getEdge()->getID());
115  break;
116  case VAR_LANEPOSITION:
118  tempMsg.writeDouble(p->getEdgePos());
119  break;
120  case VAR_COLOR:
121  tempMsg.writeUnsignedByte(TYPE_COLOR);
122  tempMsg.writeUnsignedByte(p->getParameter().color.red());
123  tempMsg.writeUnsignedByte(p->getParameter().color.green());
124  tempMsg.writeUnsignedByte(p->getParameter().color.blue());
125  tempMsg.writeUnsignedByte(p->getParameter().color.alpha());
126  break;
127  case VAR_WAITING_TIME:
129  tempMsg.writeDouble(p->getWaitingSeconds());
130  break;
131  case VAR_TYPE:
133  tempMsg.writeString(p->getVehicleType().getID());
134  break;
135  case VAR_NEXT_EDGE:
137  tempMsg.writeString(dynamic_cast<MSPerson*>(p)->getNextEdge());
138  break;
139  case VAR_PARAMETER: {
140  std::string paramName = "";
141  if (!server.readTypeCheckingString(inputStorage, paramName)) {
142  return server.writeErrorStatusCmd(CMD_GET_PERSON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
143  }
145  tempMsg.writeString(p->getParameter().getParameter(paramName, ""));
146  }
147  default:
149  break;
150  }
151  }
152  server.writeStatusCmd(CMD_GET_PERSON_VARIABLE, RTYPE_OK, "", outputStorage);
153  server.writeResponseWithLength(outputStorage, tempMsg);
154  return true;
155 }
156 
157 
158 bool
160  tcpip::Storage& outputStorage) {
161  std::string warning = ""; // additional description for response
162  // variable
163  int variable = inputStorage.readUnsignedByte();
164  if (variable != VAR_PARAMETER
165  ) {
166  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
167  }
168  // id
170  std::string id = inputStorage.readString();
171  MSTransportable* p = c.get(id);
172  if (p == 0) {
173  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
174  }
175  // process
176  switch (variable) {
177  case VAR_PARAMETER: {
178  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
179  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
180  }
181  //readt itemNo
182  inputStorage.readInt();
183  std::string name;
184  if (!server.readTypeCheckingString(inputStorage, name)) {
185  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
186  }
187  std::string value;
188  if (!server.readTypeCheckingString(inputStorage, value)) {
189  return server.writeErrorStatusCmd(CMD_SET_PERSON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
190  }
191  ((SUMOVehicleParameter&) p->getParameter()).addParameter(name, value);
192  }
193  break;
194  default:
195  /*
196  try {
197  if (!TraCIServerAPI_VehicleType::setVariable(CMD_SET_PERSON_VARIABLE, variable, getSingularType(v), server, inputStorage, outputStorage)) {
198  return false;
199  }
200  } catch (ProcessError& e) {
201  return server.writeErrorStatusCmd(CMD_SET_VEHICLE_VARIABLE, e.what(), outputStorage);
202  }
203  */
204  break;
205  }
206  server.writeStatusCmd(CMD_SET_PERSON_VARIABLE, RTYPE_OK, warning, outputStorage);
207  return true;
208 }
209 
210 
211 bool
212 TraCIServerAPI_Person::getPosition(const std::string& id, Position& p) {
213  MSPerson* person = dynamic_cast<MSPerson*>(MSNet::getInstance()->getPersonControl().get(id));
214  if (person == 0) {
215  return false;
216  }
217  p = person->getPosition();
218  return true;
219 }
220 
221 
222 /*
223 MSVehicleType&
224 TraCIServerAPI_Person::getSingularType(MSPerson* const person) {
225  const MSVehicleType& oType = person->getVehicleType();
226  std::string newID = oType.getID().find('@') == std::string::npos ? oType.getID() + "@" + person->getID() : oType.getID();
227  MSVehicleType* type = MSVehicleType::build(newID, &oType);
228  person->replaceVehicleType(type);
229  return *type;
230 }
231 */
232 
233 
234 
235 #endif
236 
237 
238 /****************************************************************************/
239 
#define VAR_ROAD_ID
RGBColor color
The vehicle&#39;s color.
#define VAR_LENGTH
#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
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:160
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&#39;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:65
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)
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)
virtual std::string readString()
unsigned int size() const
Returns the number of known persons.
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
MSTransportable * get(const std::string &id) const
Returns the named person, if existing.
#define VAR_SPEED
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
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:64
virtual MSPersonControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:690
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.
#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
std::map< std::string, MSTransportable * >::const_iterator constVehIt
Definition of the internal persons map iterator.
#define VAR_WIDTH
constVehIt loadedPersonsBegin() const
Returns the begin of the internal persons map.