SUMO - Simulation of Urban MObility
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-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 /****************************************************************************/
22 // APIs for getting/setting polygon values via TraCI
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifndef NO_TRACI
36 
37 #include <utils/common/StdDefs.h>
38 #include <microsim/MSNet.h>
40 #include <libsumo/Polygon.h>
41 #include <libsumo/Helper.h>
42 #include "TraCIConstants.h"
43 #include "TraCIServerAPI_Polygon.h"
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
48 bool
50  tcpip::Storage& outputStorage) {
51  // variable & id
52  int variable = inputStorage.readUnsignedByte();
53  std::string id = inputStorage.readString();
54  // check variable
55  if (variable != ID_LIST && variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
56  && variable != ID_COUNT && variable != VAR_PARAMETER) {
58  "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " 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 = libsumo::Polygon::getIDList();
69  if (variable == ID_LIST) {
71  tempMsg.writeStringList(ids);
72  } else {
74  tempMsg.writeInt((int) ids.size());
75  }
76  } else {
77  try {
78  switch (variable) {
79  case VAR_TYPE: {
82  }
83  break;
84  case VAR_COLOR: {
87  tempMsg.writeUnsignedByte(tc.r);
88  tempMsg.writeUnsignedByte(tc.g);
89  tempMsg.writeUnsignedByte(tc.b);
90  tempMsg.writeUnsignedByte(tc.a);
91  }
92  break;
93  case VAR_SHAPE: {
96  tempMsg.writeUnsignedByte((int) tp.size());
97  for (int iPoint = 0; iPoint < (int)tp.size(); ++iPoint) {
98  tempMsg.writeDouble(tp[iPoint].x);
99  tempMsg.writeDouble(tp[iPoint].y);
100  }
101  }
102  break;
103  case VAR_FILL: {
104  tempMsg.writeUnsignedByte(TYPE_UBYTE);
105  tempMsg.writeUnsignedByte(libsumo::Polygon::getFilled(id) ? 1 : 0);
106  }
107  break;
108  case VAR_PARAMETER: {
109  std::string paramName = "";
110  if (!server.readTypeCheckingString(inputStorage, paramName)) {
111  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
112  }
114  tempMsg.writeString(libsumo::Polygon::getParameter(id, paramName));
115  }
116  break;
117  }
118  } catch (libsumo::TraCIException& e) {
119  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
120  }
121  }
122  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
123  server.writeResponseWithLength(outputStorage, tempMsg);
124  return true;
125 }
126 
127 bool
129  tcpip::Storage& outputStorage) {
130  std::string warning = ""; // additional description for response
131  // variable
132  int variable = inputStorage.readUnsignedByte();
133  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
134  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
136  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
137  }
138  // id
139  std::string id = inputStorage.readString();
140  try {
141  // process
142  switch (variable) {
143  case VAR_TYPE: {
144  std::string type;
145  if (!server.readTypeCheckingString(inputStorage, type)) {
146  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
147  }
148  libsumo::Polygon::setType(id, type);
149  }
150  break;
151  case VAR_COLOR: {
153  if (!server.readTypeCheckingColor(inputStorage, col)) {
154  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
155  }
157  }
158  break;
159  case VAR_SHAPE: {
160  PositionVector shape;
161  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
162  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an accoring type.", outputStorage);
163  }
165  }
166  break;
167  case VAR_FILL: {
168  int value = 0;
169  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
170  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an unsigned byte.", outputStorage);
171  }
172  libsumo::Polygon::setFilled(id, value != 0);
173  }
174  break;
175  case ADD: {
176  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
177  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
178  }
179  //readt itemNo
180  inputStorage.readInt();
181  std::string type;
182  if (!server.readTypeCheckingString(inputStorage, type)) {
183  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
184  }
186  if (!server.readTypeCheckingColor(inputStorage, col)) {
187  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
188  }
189  int value = 0;
190  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
191  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
192  }
193  bool fill = value != 0;
194  int layer = 0;
195  if (!server.readTypeCheckingInt(inputStorage, layer)) {
196  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
197  }
198  PositionVector shape;
199  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
200  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
201  }
203 
204  libsumo::Polygon::add(id, tp, col, fill, type, layer);
205 
206  }
207  break;
208  case REMOVE: {
209  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
210  if (!server.readTypeCheckingInt(inputStorage, layer)) {
211  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
212  }
213 
214  libsumo::Polygon::remove(id, layer);
215 
216  }
217  break;
218  case VAR_PARAMETER: {
219  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
220  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
221  }
222  //readt itemNo
223  inputStorage.readInt();
224  std::string name;
225  if (!server.readTypeCheckingString(inputStorage, name)) {
226  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
227  }
228  std::string value;
229  if (!server.readTypeCheckingString(inputStorage, value)) {
230  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
231  }
232  libsumo::Polygon::setParameter(id, name, value);
233 
234  }
235  break;
236  default:
237  break;
238  }
239  } catch (libsumo::TraCIException& e) {
240  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
241  }
242  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
243  return true;
244 }
245 
246 bool
247 TraCIServerAPI_Polygon::getShape(const std::string& id, PositionVector& shape) {
248  SUMOPolygon* poly = getPolygon(id);
249  if (poly == 0) {
250  return false;
251  }
252  shape = poly->getShape();
253  return true;
254 }
255 
257 TraCIServerAPI_Polygon::getPolygon(const std::string& id) {
259 }
260 
261 #endif
262 
263 
264 /****************************************************************************/
265 
unsigned char g
Definition: TraCIDefs.h:79
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:85
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_COMPOUND
const Polygons & getPolygons() const
Returns all polygons.
#define TYPE_UBYTE
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
#define VAR_TYPE
#define TYPE_POLYGON
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:111
#define VAR_COLOR
#define RESPONSE_GET_POLYGON_VARIABLE
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
const PositionVector & getShape() const
Returns whether the shape of the polygon.
Definition: SUMOPolygon.h:84
#define TYPE_STRINGLIST
#define CMD_GET_POLYGON_VARIABLE
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:110
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
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.
#define VAR_SHAPE
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
static std::string getType(const std::string &polygonID)
Definition: Polygon.cpp:44
unsigned char b
Definition: TraCIDefs.h:79
virtual int readInt()
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:429
A list of positions.
unsigned char a
Definition: TraCIDefs.h:79
static std::string getParameter(const std::string &polygonID, const std::string &paramName)
Definition: Polygon.cpp:66
virtual void writeStringList(const std::vector< std::string > &s)
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:101
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:49
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:77
virtual std::string readString()
unsigned char r
Definition: TraCIDefs.h:79
#define ADD
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
static void setParameter(std::string &name, std::string &value, std::string &string)
Definition: Polygon.cpp:128
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define CMD_SET_POLYGON_VARIABLE
virtual void writeString(const std::string &s)
static std::vector< std::string > getIDList()
Definition: Polygon.cpp:36
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:85
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &c, bool fill, const std::string &type, int layer)
Definition: Polygon.cpp:91
static SUMOPolygon * getPolygon(const std::string &id)
Returns the named polygon.
static TraCIColor getColor(const std::string &polygonID)
Definition: Polygon.cpp:60
static bool getFilled(const std::string &polygonID)
Definition: Polygon.cpp:55
virtual void writeDouble(double)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
#define VAR_FILL
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_PARAMETER
#define ID_COUNT
#define TYPE_INTEGER
#define ID_LIST
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named polygons&#39;s shape.
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:71
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)