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