SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // APIs for getting/setting POI values via TraCI
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 <libsumo/POI.h>
41 #include "TraCIServerAPI_POI.h"
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 bool
48  tcpip::Storage& outputStorage) {
49 
50  // variable & id
51  int variable = inputStorage.readUnsignedByte();
52  std::string id = inputStorage.readString();
53  // check variable
54  if (variable != ID_LIST &&
55  variable != VAR_TYPE &&
56  variable != VAR_COLOR &&
57  variable != VAR_POSITION &&
58  variable != VAR_POSITION3D &&
59  variable != ID_COUNT &&
60  variable != VAR_PARAMETER) {
61  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
62  }
63  // begin response building
64  tcpip::Storage tempMsg;
65  // response-code, variableID, objectID
67  tempMsg.writeUnsignedByte(variable);
68  tempMsg.writeString(id);
69  // process request
70  try {
71  switch (variable) {
72  case ID_LIST:
75  break;
76  case ID_COUNT:
78  tempMsg.writeInt((int) libsumo::POI::getIDCount());
79  break;
80  case VAR_TYPE:
83  break;
84  case VAR_COLOR:
90  break;
91  case VAR_POSITION:
95  break;
96  case VAR_POSITION3D:
100  tempMsg.writeDouble(libsumo::POI::getPosition(id).z);
101  break;
102  case VAR_PARAMETER: {
103  std::string paramName = "";
104  if (!server.readTypeCheckingString(inputStorage, paramName)) {
105  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
106  }
108  tempMsg.writeString(libsumo::POI::getParameter(id, paramName));
109  break;
110  }
111  default:
112  break;
113  }
114  } catch (libsumo::TraCIException& e) {
115  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, e.what(), outputStorage);
116  }
117 
118  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
119  server.writeResponseWithLength(outputStorage, tempMsg);
120 
121  return true;
122 }
123 
124 
125 bool
127  tcpip::Storage& outputStorage) {
128  std::string warning = ""; // additional description for response
129  // variable & id
130  int variable = inputStorage.readUnsignedByte();
131  std::string id = inputStorage.readString();
132  // check variable
133  if (variable != VAR_TYPE &&
134  variable != VAR_COLOR &&
135  variable != VAR_POSITION &&
136  variable != ADD &&
137  variable != REMOVE &&
138  variable != VAR_PARAMETER) {
139  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
140  }
141  // process
142  try {
143  switch (variable) {
144  case VAR_TYPE: {
145  std::string type;
146  if (!server.readTypeCheckingString(inputStorage, type)) {
147  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
148  }
149  libsumo::POI::setType(id, type);
150  }
151  break;
152  case VAR_COLOR: {
154  if (!server.readTypeCheckingColor(inputStorage, col)) {
155  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
156  }
157  libsumo::POI::setColor(id, col);
158  }
159  break;
160  case VAR_POSITION: {
162  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
163  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
164  }
165  libsumo::POI::setPosition(id, pos);
166  }
167  break;
168  case ADD: {
169  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
170  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
171  }
172  //read itemNo
173  inputStorage.readInt();
174  std::string type;
175  if (!server.readTypeCheckingString(inputStorage, type)) {
176  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
177  }
179  if (!server.readTypeCheckingColor(inputStorage, col)) {
180  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
181  }
182  int layer = 0;
183  if (!server.readTypeCheckingInt(inputStorage, layer)) {
184  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
185  }
187  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
188  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
189  }
190  //
191  if (!libsumo::POI::add(id, pos, col, type, layer)) {
192  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
193  }
194  }
195  break;
196  case REMOVE: {
197  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
198  if (!server.readTypeCheckingInt(inputStorage, layer)) {
199  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
200  }
201  if (!libsumo::POI::remove(id, layer)) {
202  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
203  }
204  }
205  break;
206  case VAR_PARAMETER: {
207  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
208  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
209  }
210  //readt itemNo
211  inputStorage.readInt();
212  std::string name;
213  if (!server.readTypeCheckingString(inputStorage, name)) {
214  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
215  }
216  std::string value;
217  if (!server.readTypeCheckingString(inputStorage, value)) {
218  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
219  }
220  libsumo::POI::setParameter(id, name, value);
221  }
222  break;
223  default:
224  break;
225  }
226  } catch (libsumo::TraCIException& e) {
227  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, e.what(), outputStorage);
228  }
229  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
230  return true;
231 }
232 
233 
234 bool
235 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
236  PointOfInterest* poi = getPoI(id);
237  if (poi == 0) {
238  return false;
239  }
240  p = *poi;
241  return true;
242 }
243 
244 
246 TraCIServerAPI_POI::getPoI(const std::string& id) {
248 }
249 
250 
251 #endif
252 
253 
254 /****************************************************************************/
255 
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_COMPOUND
#define POSITION_2D
static bool remove(const std::string &poiID, int layer=0)
Definition: POI.cpp:117
#define VAR_POSITION
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
static void setColor(const std::string &poiID, const TraCIColor &c)
Definition: POI.cpp:101
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
#define VAR_TYPE
#define VAR_COLOR
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
#define POSITION_3D
virtual void writeUnsignedByte(int)
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 writeInt(int)
#define VAR_POSITION3D
#define TYPE_STRING
virtual int readUnsignedByte()
static bool add(const std::string &poiID, const TraCIPosition &pos, const TraCIColor &c, const std::string &type, int layer)
Definition: POI.cpp:107
static int getIDCount()
Definition: POI.cpp:56
virtual int readInt()
static void setPosition(const std::string &poiID, const TraCIPosition &pos)
Definition: POI.cpp:95
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:429
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
#define CMD_GET_POI_VARIABLE
static TraCIPosition getPosition(const std::string &poiID)
Definition: POI.cpp:73
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
static std::string getParameter(const std::string &poiID, const std::string &param)
Definition: POI.cpp:83
#define CMD_SET_POI_VARIABLE
static void setType(const std::string &poiID, const std::string &setType)
Definition: POI.cpp:89
static bool getPosition(const std::string &id, Position &p)
Returns the named PoI&#39;s position.
#define ADD
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static std::vector< std::string > getIDList()
Definition: POI.cpp:48
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
#define RESPONSE_GET_POI_VARIABLE
static std::string getType(const std::string &poiID)
Definition: POI.cpp:61
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
A point-of-interest.
static PointOfInterest * getPoI(const std::string &id)
Returns the named PoI.
#define VAR_PARAMETER
#define ID_COUNT
static void setParameter(const std::string &poiID, const std::string &param, const std::string &value)
Definition: POI.cpp:123
A 3D-position.
Definition: TraCIDefs.h:71
#define TYPE_INTEGER
#define ID_LIST
static TraCIColor getColor(const std::string &poiID)
Definition: POI.cpp:66
const POIs & getPOIs() const
Returns all pois.