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 /****************************************************************************/
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