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.sourceforge.net/
12 // Copyright (C) 2001-2012 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  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "Get PoI Variable: unsupported variable specified", outputStorage);
64  return false;
65  }
66  // begin response building
67  tcpip::Storage tempMsg;
68  // response-code, variableID, objectID
70  tempMsg.writeUnsignedByte(variable);
71  tempMsg.writeString(id);
72  // process request
73  if (variable == ID_LIST || variable == ID_COUNT) {
74  std::vector<std::string> ids;
76  shapeCont.getPOIs().insertIDs(ids);
77  if (variable == ID_LIST) {
79  tempMsg.writeStringList(ids);
80  } else {
82  tempMsg.writeInt((int) ids.size());
83  }
84  } else {
85  int layer;
86  PointOfInterest* p = getPoI(id, layer);
87  if (p == 0) {
88  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage);
89  return false;
90  }
91  switch (variable) {
92  case VAR_TYPE:
94  tempMsg.writeString(p->getType());
95  break;
96  case VAR_COLOR:
98  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().red() * 255. + .5));
99  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().green() * 255. + .5));
100  tempMsg.writeUnsignedByte(static_cast<int>(p->getColor().blue() * 255. + .5));
101  tempMsg.writeUnsignedByte(255);
102  break;
103  case VAR_POSITION:
105  tempMsg.writeDouble(p->x());
106  tempMsg.writeDouble(p->y());
107  break;
108  default:
109  break;
110  }
111  }
112  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
113  server.writeResponseWithLength(outputStorage, tempMsg);
114  return true;
115 }
116 
117 
118 bool
120  tcpip::Storage& outputStorage) {
121  std::string warning = ""; // additional description for response
122  // variable
123  int variable = inputStorage.readUnsignedByte();
124  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_POSITION
125  && variable != ADD && variable != REMOVE) {
126  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Change PoI State: unsupported variable specified", outputStorage);
127  return false;
128  }
129  // id
130  std::string id = inputStorage.readString();
131  PointOfInterest* p = 0;
132  int layer = 0;
134  if (variable != ADD && variable != REMOVE) {
135  p = getPoI(id, layer);
136  if (p == 0) {
137  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "POI '" + id + "' is not known", outputStorage);
138  return false;
139  }
140  }
141  // process
142  int valueDataType = inputStorage.readUnsignedByte();
143  switch (variable) {
144  case VAR_TYPE: {
145  if (valueDataType != TYPE_STRING) {
146  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The type must be given as a string.", outputStorage);
147  return false;
148  }
149  std::string type = inputStorage.readString();
150  p->setType(type);
151  }
152  break;
153  case VAR_COLOR: {
154  if (valueDataType != TYPE_COLOR) {
155  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The color must be given using an accoring type.", outputStorage);
156  return false;
157  }
158  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
159  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
160  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
161  //read SUMOReal a
162  inputStorage.readUnsignedByte();
163  p->setColor(RGBColor(r, g, b));
164  }
165  break;
166  case VAR_POSITION: {
167  if (valueDataType != POSITION_2D) {
168  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The position must be given using an accoring type.", outputStorage);
169  return false;
170  }
171  SUMOReal x = inputStorage.readDouble();
172  SUMOReal y = inputStorage.readDouble();
173  shapeCont.movePOI(id, Position(x, y));
174  }
175  break;
176  case ADD: {
177  if (valueDataType != TYPE_COMPOUND) {
178  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "A compound object is needed for setting a new PoI.", outputStorage);
179  return false;
180  }
181  //read itemNo
182  inputStorage.readInt();
183  // type
184  if (inputStorage.readUnsignedByte() != TYPE_STRING) {
185  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The first PoI parameter must be the type encoded as a string.", outputStorage);
186  return false;
187  }
188  std::string type = inputStorage.readString();
189  // color
190  if (inputStorage.readUnsignedByte() != TYPE_COLOR) {
191  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The second PoI parameter must be the color.", outputStorage);
192  return false;
193  }
194  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
195  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
196  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
197  //read SUMOReal a
198  inputStorage.readUnsignedByte();
199  // layer
200  if (inputStorage.readUnsignedByte() != TYPE_INTEGER) {
201  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The third PoI parameter must be the layer encoded as int.", outputStorage);
202  return false;
203  }
204  layer = inputStorage.readInt();
205  // pos
206  if (inputStorage.readUnsignedByte() != POSITION_2D) {
207  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The fourth PoI parameter must be the position.", outputStorage);
208  return false;
209  }
210  SUMOReal x = inputStorage.readDouble();
211  SUMOReal y = inputStorage.readDouble();
212  //
213  if (!shapeCont.addPOI(id, type, RGBColor(r, g, b), (SUMOReal)layer,
215  Position(x, y),
217  delete p;
218  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not add PoI.", outputStorage);
219  return false;
220  }
221  }
222  break;
223  case REMOVE: {
224  if (valueDataType != TYPE_INTEGER) {
225  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "The layer must be given using an int.", outputStorage);
226  return false;
227  }
228  layer = inputStorage.readInt();
229  if (!shapeCont.removePOI(id)) {
230  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_ERR, "Could not remove PoI '" + id + "'", outputStorage);
231  }
232  }
233  break;
234  default:
235  break;
236  }
237  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
238  return true;
239 }
240 
241 
242 bool
243 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
244  int layer;
245  PointOfInterest* poi = getPoI(id, layer);
246  if (poi == 0) {
247  return false;
248  }
249  p = *poi;
250  return true;
251 }
252 
253 
255 TraCIServerAPI_POI::getPoI(const std::string& id, int& layer) {
257  return shapeCont.getPOIs().get(id);
258 }
259 
260 
261 TraCIRTree*
263  TraCIRTree* t = new TraCIRTree();
265  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
266  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
267  t->addObject((*i).second, *(*i).second);
268  }
269  return t;
270 }
271 
272 
273 #endif
274 
275 
276 /****************************************************************************/
277