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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
20 // APIs for getting/setting polygon values via TraCI
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <utils/common/StdDefs.h>
30 #include <microsim/MSNet.h>
32 #include <libsumo/Polygon.h>
33 #include <libsumo/Helper.h>
34 #include "TraCIConstants.h"
35 #include "TraCIServerAPI_Polygon.h"
36 
37 // ===========================================================================
38 // method definitions
39 // ===========================================================================
40 bool
42  tcpip::Storage& outputStorage) {
43  const int variable = inputStorage.readUnsignedByte();
44  const std::string id = inputStorage.readString();
45  server.initWrapper(RESPONSE_GET_POLYGON_VARIABLE, variable, id);
46  try {
47  if (!libsumo::Polygon::handleVariable(id, variable, &server)) {
48  switch (variable) {
49  case VAR_SHAPE: {
52  server.getWrapperStorage().writeUnsignedByte((int)tp.size());
53  for (int iPoint = 0; iPoint < (int)tp.size(); ++iPoint) {
54  server.getWrapperStorage().writeDouble(tp[iPoint].x);
55  server.getWrapperStorage().writeDouble(tp[iPoint].y);
56  }
57  break;
58  }
59  case VAR_PARAMETER: {
60  std::string paramName = "";
61  if (!server.readTypeCheckingString(inputStorage, paramName)) {
62  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
63  }
66  break;
67  }
68  default:
69  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
70  }
71  }
72  } catch (libsumo::TraCIException& e) {
73  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
74  }
75  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
76  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
77  return true;
78 }
79 
80 bool
82  tcpip::Storage& outputStorage) {
83  std::string warning = ""; // additional description for response
84  // variable
85  int variable = inputStorage.readUnsignedByte();
86  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
87  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
89  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
90  }
91  // id
92  std::string id = inputStorage.readString();
93  try {
94  // process
95  switch (variable) {
96  case VAR_TYPE: {
97  std::string type;
98  if (!server.readTypeCheckingString(inputStorage, type)) {
99  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
100  }
101  libsumo::Polygon::setType(id, type);
102  }
103  break;
104  case VAR_COLOR: {
106  if (!server.readTypeCheckingColor(inputStorage, col)) {
107  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
108  }
110  }
111  break;
112  case VAR_SHAPE: {
113  PositionVector shape;
114  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
115  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
116  }
118  }
119  break;
120  case VAR_FILL: {
121  int value = 0;
122  if (!server.readTypeCheckingInt(inputStorage, value)) {
123  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an integer.", outputStorage);
124  }
125  libsumo::Polygon::setFilled(id, value != 0);
126  }
127  break;
128  case ADD: {
129  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
130  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
131  }
132  //readt itemNo
133  inputStorage.readInt();
134  std::string type;
135  if (!server.readTypeCheckingString(inputStorage, type)) {
136  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
137  }
139  if (!server.readTypeCheckingColor(inputStorage, col)) {
140  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
141  }
142  int value = 0;
143  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
144  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
145  }
146  bool fill = value != 0;
147  int layer = 0;
148  if (!server.readTypeCheckingInt(inputStorage, layer)) {
149  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
150  }
151  PositionVector shape;
152  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
153  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
154  }
156 
157  libsumo::Polygon::add(id, tp, col, fill, type, layer);
158 
159  }
160  break;
161  case REMOVE: {
162  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
163  if (!server.readTypeCheckingInt(inputStorage, layer)) {
164  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
165  }
166 
167  libsumo::Polygon::remove(id, layer);
168 
169  }
170  break;
171  case VAR_PARAMETER: {
172  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
173  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
174  }
175  //readt itemNo
176  inputStorage.readInt();
177  std::string name;
178  if (!server.readTypeCheckingString(inputStorage, name)) {
179  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
180  }
181  std::string value;
182  if (!server.readTypeCheckingString(inputStorage, value)) {
183  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
184  }
185  libsumo::Polygon::setParameter(id, name, value);
186 
187  }
188  break;
189  default:
190  break;
191  }
192  } catch (libsumo::TraCIException& e) {
193  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
194  }
195  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
196  return true;
197 }
198 
199 
200 /****************************************************************************/
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:106
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_COMPOUND
#define RTYPE_OK
#define VAR_TYPE
#define TYPE_POLYGON
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:133
#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.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define CMD_GET_POLYGON_VARIABLE
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
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &color, bool fill=false, const std::string &polygonType="", int layer=0)
Definition: Polygon.cpp:112
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:188
#define TYPE_STRING
virtual int readUnsignedByte()
virtual int readInt()
A list of positions.
static std::string getParameter(const std::string &polygonID, const std::string &paramName)
Definition: Polygon.cpp:84
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:123
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:64
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:97
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
#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:62
static void setParameter(std::string &name, std::string &value, std::string &string)
Definition: Polygon.cpp:150
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define CMD_SET_POLYGON_VARIABLE
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:145
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:186
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.
void initWrapper(const int domainID, const int variable, const std::string &objID)
#define VAR_PARAMETER
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:90
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)