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 // used namespaces
48 // ===========================================================================
49 using namespace traci;
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 bool
57  tcpip::Storage& outputStorage) {
58  // variable & id
59  int variable = inputStorage.readUnsignedByte();
60  std::string id = inputStorage.readString();
61  // check variable
62  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION && variable != ID_COUNT) {
63  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable specified", outputStorage);
64  }
65  // begin response building
66  tcpip::Storage tempMsg;
67  // response-code, variableID, objectID
69  tempMsg.writeUnsignedByte(variable);
70  tempMsg.writeString(id);
71  // process request
72  if (variable == ID_LIST || variable == ID_COUNT) {
73  std::vector<std::string> ids;
75  shapeCont.getPOIs().insertIDs(ids);
76  if (variable == ID_LIST) {
78  tempMsg.writeStringList(ids);
79  } else {
81  tempMsg.writeInt((int) ids.size());
82  }
83  } else {
84  PointOfInterest* p = getPoI(id);
85  if (p == 0) {
86  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
87  }
88  switch (variable) {
89  case VAR_TYPE:
91  tempMsg.writeString(p->getType());
92  break;
93  case VAR_COLOR:
95  tempMsg.writeUnsignedByte(p->getColor().red());
96  tempMsg.writeUnsignedByte(p->getColor().green());
97  tempMsg.writeUnsignedByte(p->getColor().blue());
98  tempMsg.writeUnsignedByte(p->getColor().alpha());
99  break;
100  case VAR_POSITION:
102  tempMsg.writeDouble(p->x());
103  tempMsg.writeDouble(p->y());
104  break;
105  default:
106  break;
107  }
108  }
109  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
110  server.writeResponseWithLength(outputStorage, tempMsg);
111  return true;
112 }
113 
114 
115 bool
117  tcpip::Storage& outputStorage) {
118  std::string warning = ""; // additional description for response
119  // variable
120  int variable = inputStorage.readUnsignedByte();
121  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
122  && variable != ADD && variable != REMOVE) {
123  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable specified", outputStorage);
124  }
125  // id
126  std::string id = inputStorage.readString();
127  PointOfInterest* p = 0;
129  if (variable != ADD && variable != REMOVE) {
130  p = getPoI(id);
131  if (p == 0) {
132  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "POI '" + id + "' is not known", outputStorage);
133  }
134  }
135  // process
136  switch (variable) {
137  case VAR_TYPE: {
138  std::string type;
139  if (!server.readTypeCheckingString(inputStorage, type)) {
140  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
141  }
142  p->setType(type);
143  }
144  break;
145  case VAR_COLOR: {
146  RGBColor col;
147  if (!server.readTypeCheckingColor(inputStorage, col)) {
148  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
149  }
150  p->setColor(col);
151  }
152  break;
153  case VAR_POSITION: {
154  Position pos;
155  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
156  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
157  }
158  shapeCont.movePOI(id, pos);
159  }
160  break;
161  case ADD: {
162  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
163  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
164  }
165  //read itemNo
166  inputStorage.readInt();
167  std::string type;
168  if (!server.readTypeCheckingString(inputStorage, type)) {
169  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
170  }
171  RGBColor col;
172  if (!server.readTypeCheckingColor(inputStorage, col)) {
173  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
174  }
175  int layer = 0;
176  if (!server.readTypeCheckingInt(inputStorage, layer)) {
177  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
178  }
179  Position pos;
180  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
181  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
182  }
183  //
184  if (!shapeCont.addPOI(id, type, col, (SUMOReal)layer,
187  delete p;
188  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
189  }
190  }
191  break;
192  case REMOVE: {
193  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
194  if (!server.readTypeCheckingInt(inputStorage, layer)) {
195  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
196  }
197  if (!shapeCont.removePOI(id)) {
198  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
199  }
200  }
201  break;
202  default:
203  break;
204  }
205  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
206  return true;
207 }
208 
209 
210 bool
211 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
212  PointOfInterest* poi = getPoI(id);
213  if (poi == 0) {
214  return false;
215  }
216  p = *poi;
217  return true;
218 }
219 
220 
222 TraCIServerAPI_POI::getPoI(const std::string& id) {
224 }
225 
226 
227 TraCIRTree*
229  TraCIRTree* t = new TraCIRTree();
231  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
232  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
233  t->addObject((*i).second, *(*i).second);
234  }
235  return t;
236 }
237 
238 
239 #endif
240 
241 
242 /****************************************************************************/
243 
static const std::string DEFAULT_IMG_FILE
Definition: Shape.h:150
#define TYPE_COMPOUND
#define POSITION_2D
#define VAR_POSITION
#define RTYPE_OK
#define VAR_TYPE
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_COLOR
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_COLOR
#define TYPE_STRINGLIST
Storage for geometrical objects.
virtual void writeUnsignedByte(int)
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:149
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
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:349
static const SUMOReal DEFAULT_IMG_HEIGHT
Definition: Shape.h:152
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)
void addObject(Named *o, Boundary &b)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: TraCIRTree.h:115
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
const RGBColor & getColor() const
Returns the color of the Shape.
Definition: Shape.h:78
const IDMap & getMyMap() const
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: TraCIRTree.h:59
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
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:221
A point-of-interest.
static PointOfInterest * getPoI(const std::string &id)
Returns the named PoI.
#define ID_COUNT
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
const std::string & getType() const
Returns the (abstract) type of the Shape.
Definition: Shape.h:70
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_INTEGER
#define ID_LIST
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
void setColor(const RGBColor &col)
Sets a new color.
Definition: Shape.h:120
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
static const SUMOReal DEFAULT_IMG_WIDTH
Definition: Shape.h:151