SUMO - Simulation of Urban MObility
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-sim.org/
13 // Copyright (C) 20019-2014 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  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable specified", outputStorage);
59  }
60  // begin response building
61  tcpip::Storage tempMsg;
62  // response-code, variableID, objectID
64  tempMsg.writeUnsignedByte(variable);
65  tempMsg.writeString(id);
66  // process request
67  if (variable == ID_LIST || variable == ID_COUNT) {
68  std::vector<std::string> ids;
70  shapeCont.getPOIs().insertIDs(ids);
71  if (variable == ID_LIST) {
73  tempMsg.writeStringList(ids);
74  } else {
76  tempMsg.writeInt((int) ids.size());
77  }
78  } else {
79  PointOfInterest* p = getPoI(id);
80  if (p == 0) {
81  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
82  }
83  switch (variable) {
84  case VAR_TYPE:
86  tempMsg.writeString(p->getType());
87  break;
88  case VAR_COLOR:
90  tempMsg.writeUnsignedByte(p->getColor().red());
91  tempMsg.writeUnsignedByte(p->getColor().green());
92  tempMsg.writeUnsignedByte(p->getColor().blue());
93  tempMsg.writeUnsignedByte(p->getColor().alpha());
94  break;
95  case VAR_POSITION:
97  tempMsg.writeDouble(p->x());
98  tempMsg.writeDouble(p->y());
99  break;
100  default:
101  break;
102  }
103  }
104  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
105  server.writeResponseWithLength(outputStorage, tempMsg);
106  return true;
107 }
108 
109 
110 bool
112  tcpip::Storage& outputStorage) {
113  std::string warning = ""; // additional description for response
114  // variable
115  int variable = inputStorage.readUnsignedByte();
116  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
117  && variable != ADD && variable != REMOVE) {
118  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable specified", outputStorage);
119  }
120  // id
121  std::string id = inputStorage.readString();
122  PointOfInterest* p = 0;
124  if (variable != ADD && variable != REMOVE) {
125  p = getPoI(id);
126  if (p == 0) {
127  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
128  }
129  }
130  // process
131  switch (variable) {
132  case VAR_TYPE: {
133  std::string type;
134  if (!server.readTypeCheckingString(inputStorage, type)) {
135  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
136  }
137  p->setType(type);
138  }
139  break;
140  case VAR_COLOR: {
141  RGBColor col;
142  if (!server.readTypeCheckingColor(inputStorage, col)) {
143  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
144  }
145  p->setColor(col);
146  }
147  break;
148  case VAR_POSITION: {
149  Position pos;
150  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
151  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
152  }
153  shapeCont.movePOI(id, pos);
154  }
155  break;
156  case ADD: {
157  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
158  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
159  }
160  //read itemNo
161  inputStorage.readInt();
162  std::string type;
163  if (!server.readTypeCheckingString(inputStorage, type)) {
164  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
165  }
166  RGBColor col;
167  if (!server.readTypeCheckingColor(inputStorage, col)) {
168  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
169  }
170  int layer = 0;
171  if (!server.readTypeCheckingInt(inputStorage, layer)) {
172  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
173  }
174  Position pos;
175  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
176  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
177  }
178  //
179  if (!shapeCont.addPOI(id, type, col, (SUMOReal)layer,
182  delete p;
183  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
184  }
185  }
186  break;
187  case REMOVE: {
188  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
189  if (!server.readTypeCheckingInt(inputStorage, layer)) {
190  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
191  }
192  if (!shapeCont.removePOI(id)) {
193  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
194  }
195  }
196  break;
197  default:
198  break;
199  }
200  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
201  return true;
202 }
203 
204 
205 bool
206 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
207  PointOfInterest* poi = getPoI(id);
208  if (poi == 0) {
209  return false;
210  }
211  p = *poi;
212  return true;
213 }
214 
215 
217 TraCIServerAPI_POI::getPoI(const std::string& id) {
219 }
220 
221 
222 NamedRTree*
224  NamedRTree* t = new NamedRTree();
226  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
227  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
228  const float cmin[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
229  const float cmax[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
230  t->Insert(cmin, cmax, (*i).second);
231  }
232  return t;
233 }
234 
235 
236 #endif
237 
238 
239 /****************************************************************************/
240 
void Insert(const float a_min[2], const float a_max[2], Named *a_data)
Insert entry.
Definition: NamedRTree.h:91
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)
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 int readInt()
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:370
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
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)
Builds a POI using the given values and adds it to the container.
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:74
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.
virtual void writeString(const std::string &s)
void setType(const std::string &type)
Sets a new type.
Definition: Shape.h:113
#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:215
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 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