SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting POI values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include <microsim/MSNet.h>
38 #include "TraCIConstants.h"
39 #include "TraCIServerAPI_POI.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
51  tcpip::Storage& outputStorage) {
52  // variable & id
53  int variable = inputStorage.readUnsignedByte();
54  std::string id = inputStorage.readString();
55  // check variable
56  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION && variable != ID_COUNT) {
57  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable specified", outputStorage);
58  }
59  // begin response building
60  tcpip::Storage tempMsg;
61  // response-code, variableID, objectID
63  tempMsg.writeUnsignedByte(variable);
64  tempMsg.writeString(id);
65  // process request
66  if (variable == ID_LIST || variable == ID_COUNT) {
67  std::vector<std::string> ids;
69  shapeCont.getPOIs().insertIDs(ids);
70  if (variable == ID_LIST) {
72  tempMsg.writeStringList(ids);
73  } else {
75  tempMsg.writeInt((int) ids.size());
76  }
77  } else {
78  PointOfInterest* p = getPoI(id);
79  if (p == 0) {
80  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
81  }
82  switch (variable) {
83  case VAR_TYPE:
85  tempMsg.writeString(p->getType());
86  break;
87  case VAR_COLOR:
89  tempMsg.writeUnsignedByte(p->getColor().red());
90  tempMsg.writeUnsignedByte(p->getColor().green());
91  tempMsg.writeUnsignedByte(p->getColor().blue());
92  tempMsg.writeUnsignedByte(p->getColor().alpha());
93  break;
94  case VAR_POSITION:
96  tempMsg.writeDouble(p->x());
97  tempMsg.writeDouble(p->y());
98  break;
99  default:
100  break;
101  }
102  }
103  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
104  server.writeResponseWithLength(outputStorage, tempMsg);
105  return true;
106 }
107 
108 
109 bool
111  tcpip::Storage& outputStorage) {
112  std::string warning = ""; // additional description for response
113  // variable
114  int variable = inputStorage.readUnsignedByte();
115  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
116  && variable != ADD && variable != REMOVE) {
117  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable specified", outputStorage);
118  }
119  // id
120  std::string id = inputStorage.readString();
121  PointOfInterest* p = 0;
123  if (variable != ADD && variable != REMOVE) {
124  p = getPoI(id);
125  if (p == 0) {
126  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
127  }
128  }
129  // process
130  switch (variable) {
131  case VAR_TYPE: {
132  std::string type;
133  if (!server.readTypeCheckingString(inputStorage, type)) {
134  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
135  }
136  p->setType(type);
137  }
138  break;
139  case VAR_COLOR: {
140  RGBColor col;
141  if (!server.readTypeCheckingColor(inputStorage, col)) {
142  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
143  }
144  p->setColor(col);
145  }
146  break;
147  case VAR_POSITION: {
148  Position pos;
149  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
150  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
151  }
152  shapeCont.movePOI(id, pos);
153  }
154  break;
155  case ADD: {
156  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
157  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
158  }
159  //read itemNo
160  inputStorage.readInt();
161  std::string type;
162  if (!server.readTypeCheckingString(inputStorage, type)) {
163  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
164  }
165  RGBColor col;
166  if (!server.readTypeCheckingColor(inputStorage, col)) {
167  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
168  }
169  int layer = 0;
170  if (!server.readTypeCheckingInt(inputStorage, layer)) {
171  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
172  }
173  Position pos;
174  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
175  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
176  }
177  //
178  if (!shapeCont.addPOI(id, type, col, (SUMOReal)layer,
181  delete p;
182  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
183  }
184  }
185  break;
186  case REMOVE: {
187  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
188  if (!server.readTypeCheckingInt(inputStorage, layer)) {
189  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
190  }
191  if (!shapeCont.removePOI(id)) {
192  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
193  }
194  }
195  break;
196  default:
197  break;
198  }
199  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
200  return true;
201 }
202 
203 
204 bool
205 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
206  PointOfInterest* poi = getPoI(id);
207  if (poi == 0) {
208  return false;
209  }
210  p = *poi;
211  return true;
212 }
213 
214 
216 TraCIServerAPI_POI::getPoI(const std::string& id) {
218 }
219 
220 
221 NamedRTree*
223  NamedRTree* t = new NamedRTree();
225  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
226  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
227  const float cmin[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
228  const float cmax[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
229  t->Insert(cmin, cmax, (*i).second);
230  }
231  return t;
232 }
233 
234 
235 #endif
236 
237 
238 /****************************************************************************/
239 
void Insert(const float a_min[2], const float a_max[2], Named *a_data)
Insert entry.
Definition: NamedRTree.h:79
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:151
#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&#39;s Named objects.
Definition: NamedRTree.h:60
#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:150
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:150
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:354
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:153
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&#39;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:78
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:112
#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:70
#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:120
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:152