SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting POI values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 20019-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifndef NO_TRACI
35 
36 #include <microsim/MSNet.h>
39 #include "TraCIConstants.h"
40 #include "TraCIServerAPI_POI.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 & id
54  int variable = inputStorage.readUnsignedByte();
55  std::string id = inputStorage.readString();
56  // check variable
57  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION && variable != ID_COUNT
58  && variable != VAR_PARAMETER) {
59  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
60  }
61  // begin response building
62  tcpip::Storage tempMsg;
63  // response-code, variableID, objectID
65  tempMsg.writeUnsignedByte(variable);
66  tempMsg.writeString(id);
67  // process request
68  if (variable == ID_LIST || variable == ID_COUNT) {
69  std::vector<std::string> ids;
71  shapeCont.getPOIs().insertIDs(ids);
72  if (variable == ID_LIST) {
74  tempMsg.writeStringList(ids);
75  } else {
77  tempMsg.writeInt((int) ids.size());
78  }
79  } else {
80  PointOfInterest* p = getPoI(id);
81  if (p == 0) {
82  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
83  }
84  switch (variable) {
85  case VAR_TYPE:
87  tempMsg.writeString(p->getType());
88  break;
89  case VAR_COLOR:
91  tempMsg.writeUnsignedByte(p->getColor().red());
92  tempMsg.writeUnsignedByte(p->getColor().green());
93  tempMsg.writeUnsignedByte(p->getColor().blue());
94  tempMsg.writeUnsignedByte(p->getColor().alpha());
95  break;
96  case VAR_POSITION:
98  tempMsg.writeDouble(p->x());
99  tempMsg.writeDouble(p->y());
100  break;
101  case VAR_PARAMETER: {
102  std::string paramName = "";
103  if (!server.readTypeCheckingString(inputStorage, paramName)) {
104  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
105  }
107  tempMsg.writeString(p->getParameter(paramName, ""));
108  }
109  break;
110  default:
111  break;
112  }
113  }
114  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
115  server.writeResponseWithLength(outputStorage, tempMsg);
116  return true;
117 }
118 
119 
120 bool
122  tcpip::Storage& outputStorage) {
123  std::string warning = ""; // additional description for response
124  // variable
125  int variable = inputStorage.readUnsignedByte();
126  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
127  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
128  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
129  }
130  // id
131  std::string id = inputStorage.readString();
132  PointOfInterest* p = 0;
134  if (variable != ADD && variable != REMOVE) {
135  p = getPoI(id);
136  if (p == 0) {
137  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
138  }
139  }
140  // process
141  switch (variable) {
142  case VAR_TYPE: {
143  std::string type;
144  if (!server.readTypeCheckingString(inputStorage, type)) {
145  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
146  }
147  p->setType(type);
148  }
149  break;
150  case VAR_COLOR: {
151  RGBColor col;
152  if (!server.readTypeCheckingColor(inputStorage, col)) {
153  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
154  }
155  p->setColor(col);
156  }
157  break;
158  case VAR_POSITION: {
159  Position pos;
160  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
161  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
162  }
163  shapeCont.movePOI(id, pos);
164  }
165  break;
166  case ADD: {
167  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
168  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
169  }
170  //read itemNo
171  inputStorage.readInt();
172  std::string type;
173  if (!server.readTypeCheckingString(inputStorage, type)) {
174  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
175  }
176  RGBColor col;
177  if (!server.readTypeCheckingColor(inputStorage, col)) {
178  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
179  }
180  int layer = 0;
181  if (!server.readTypeCheckingInt(inputStorage, layer)) {
182  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
183  }
184  Position pos;
185  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
186  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
187  }
188  //
189  if (!shapeCont.addPOI(id, type, col, (SUMOReal)layer,
192  delete p;
193  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
194  }
195  }
196  break;
197  case REMOVE: {
198  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
199  if (!server.readTypeCheckingInt(inputStorage, layer)) {
200  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
201  }
202  if (!shapeCont.removePOI(id)) {
203  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
204  }
205  }
206  break;
207  case VAR_PARAMETER: {
208  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
209  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
210  }
211  //readt itemNo
212  inputStorage.readInt();
213  std::string name;
214  if (!server.readTypeCheckingString(inputStorage, name)) {
215  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
216  }
217  std::string value;
218  if (!server.readTypeCheckingString(inputStorage, value)) {
219  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
220  }
221  p->addParameter(name, value);
222  }
223  break;
224  default:
225  break;
226  }
227  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
228  return true;
229 }
230 
231 
232 bool
233 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
234  PointOfInterest* poi = getPoI(id);
235  if (poi == 0) {
236  return false;
237  }
238  p = *poi;
239  return true;
240 }
241 
242 
244 TraCIServerAPI_POI::getPoI(const std::string& id) {
246 }
247 
248 
249 NamedRTree*
251  NamedRTree* t = new NamedRTree();
253  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
254  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
255  const float cmin[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
256  const float cmax[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
257  t->Insert(cmin, cmax, (*i).second);
258  }
259  return t;
260 }
261 
262 
263 #endif
264 
265 
266 /****************************************************************************/
267 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:152
#define TYPE_COMPOUND
#define POSITION_2D
#define VAR_POSITION
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
#define RTYPE_OK
#define VAR_TYPE
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:72
#define VAR_COLOR
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
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
Storage for geometrical objects.
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 TYPE_STRING
virtual int readUnsignedByte()
T get(const std::string &id) const
Retrieves an item.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
static const SUMOReal DEFAULT_ANGLE
Definition: Shape.h:151
static NamedRTree * getTree()
Returns a tree filled with PoI instances.
virtual void movePOI(const std::string &id, const Position &pos)
Assigns a new position to the named PoI.
virtual bool addPOI(const std::string &id, const std::string &type, const RGBColor &color, SUMOReal layer, SUMOReal angle, const std::string &imgFile, const Position &pos, SUMOReal width, SUMOReal height, bool ignorePruning=false)
Builds a POI using the given values and adds it to the container.
virtual int readInt()
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:430
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:154
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
#define CMD_GET_POI_VARIABLE
virtual bool removePOI(const std::string &id)
Removes a PoI from the container.
void insertIDs(std::vector< std::string > &into) const
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
#define CMD_SET_POI_VARIABLE
static bool getPosition(const std::string &id, Position &p)
Returns the named PoI's position.
#define ADD
const POIs & getPOIs() const
Returns all pois.
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:79
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
const IDMap & getMyMap() const
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
virtual void writeString(const std::string &s)
void setType(const std::string &type)
Sets a new type.
Definition: Shape.h:113
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
#define RESPONSE_GET_POI_VARIABLE
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
virtual void writeDouble(double)
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:214
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
A point-of-interest.
static PointOfInterest * getPoI(const std::string &id)
Returns the named PoI.
#define VAR_PARAMETER
#define ID_COUNT
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:71
#define TYPE_INTEGER
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
void setColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:121
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:153