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:
51  break;
52  case VAR_PARAMETER: {
53  std::string paramName = "";
54  if (!server.readTypeCheckingString(inputStorage, paramName)) {
55  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
56  }
59  break;
60  }
61  default:
62  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
63  }
64  }
65  } catch (libsumo::TraCIException& e) {
66  return server.writeErrorStatusCmd(CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
67  }
68  server.writeStatusCmd(CMD_GET_POLYGON_VARIABLE, RTYPE_OK, "", outputStorage);
69  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
70  return true;
71 }
72 
73 bool
75  tcpip::Storage& outputStorage) {
76  std::string warning = ""; // additional description for response
77  // variable
78  int variable = inputStorage.readUnsignedByte();
79  if (variable != VAR_TYPE && variable != VAR_COLOR && variable != VAR_SHAPE && variable != VAR_FILL
80  && variable != VAR_WIDTH
81  && variable != ADD && variable != REMOVE && variable != VAR_PARAMETER) {
83  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
84  }
85  // id
86  std::string id = inputStorage.readString();
87  try {
88  // process
89  switch (variable) {
90  case VAR_TYPE: {
91  std::string type;
92  if (!server.readTypeCheckingString(inputStorage, type)) {
93  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
94  }
95  libsumo::Polygon::setType(id, type);
96  }
97  break;
98  case VAR_COLOR: {
100  if (!server.readTypeCheckingColor(inputStorage, col)) {
101  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
102  }
104  }
105  break;
106  case VAR_SHAPE: {
107  PositionVector shape;
108  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
109  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
110  }
112  }
113  break;
114  case VAR_FILL: {
115  int value = 0;
116  if (!server.readTypeCheckingInt(inputStorage, value)) {
117  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an integer.", outputStorage);
118  }
119  libsumo::Polygon::setFilled(id, value != 0);
120  }
121  break;
122  case VAR_WIDTH: {
123  double value = 0;
124  if (!server.readTypeCheckingDouble(inputStorage, value)) {
125  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
126  }
128  }
129  break;
130  case ADD: {
131  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
132  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
133  }
134  int itemNo = inputStorage.readInt();
135  if (itemNo != 5 && itemNo != 6) {
136  return server.writeErrorStatusCmd(CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
137  }
138  std::string type;
139  if (!server.readTypeCheckingString(inputStorage, type)) {
140  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
141  }
143  if (!server.readTypeCheckingColor(inputStorage, col)) {
144  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
145  }
146  int value = 0;
147  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
148  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
149  }
150  bool fill = value != 0;
151  int layer = 0;
152  if (!server.readTypeCheckingInt(inputStorage, layer)) {
153  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
154  }
155  PositionVector shape;
156  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
157  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
158  }
159  double lineWidth = 1;
160  if (itemNo == 6) {
161  if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {
162  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
163  }
164  }
166 
167  libsumo::Polygon::add(id, tp, col, fill, lineWidth, type, layer);
168 
169  }
170  break;
171  case REMOVE: {
172  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
173  if (!server.readTypeCheckingInt(inputStorage, layer)) {
174  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
175  }
176 
177  libsumo::Polygon::remove(id, layer);
178 
179  }
180  break;
181  case VAR_PARAMETER: {
182  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
183  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
184  }
185  //readt itemNo
186  inputStorage.readInt();
187  std::string name;
188  if (!server.readTypeCheckingString(inputStorage, name)) {
189  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
190  }
191  std::string value;
192  if (!server.readTypeCheckingString(inputStorage, value)) {
193  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
194  }
195  libsumo::Polygon::setParameter(id, name, value);
196 
197  }
198  break;
199  default:
200  break;
201  }
202  } catch (libsumo::TraCIException& e) {
203  return server.writeErrorStatusCmd(CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
204  }
205  server.writeStatusCmd(CMD_SET_POLYGON_VARIABLE, RTYPE_OK, warning, outputStorage);
206  return true;
207 }
208 
209 
210 /****************************************************************************/
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:110
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
static void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:143
static std::string getParameter(const std::string &polygonID, const std::string &key)
Definition: Polygon.cpp:88
#define TYPE_COMPOUND
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &color, bool fill=false, double lineWidth=1, const std::string &polygonType="", int layer=0)
Definition: Polygon.cpp:116
#define RTYPE_OK
#define VAR_TYPE
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:137
#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.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, 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 TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:210
#define TYPE_STRING
virtual int readUnsignedByte()
virtual int readInt()
A list of positions.
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:127
#define CMD_SET_VEHICLE_VARIABLE
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:64
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:101
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
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define CMD_SET_POLYGON_VARIABLE
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
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:196
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 setParameter(const std::string &polygonID, const std::string &key, const std::string &value)
Definition: Polygon.cpp:160
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:94
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
#define VAR_WIDTH