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