Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
23 // APIs for getting/setting polygon values via TraCI
24 /****************************************************************************/
25 #include <config.h>
26 
27 #include <utils/common/StdDefs.h>
28 #include <microsim/MSNet.h>
30 #include <libsumo/Polygon.h>
31 #include <libsumo/Helper.h>
32 #include <libsumo/TraCIConstants.h>
33 #include "TraCIServerAPI_Polygon.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 bool
41  tcpip::Storage& outputStorage) {
42  const int variable = inputStorage.readUnsignedByte();
43  const std::string id = inputStorage.readString();
45  try {
46  if (!libsumo::Polygon::handleVariable(id, variable, &server)) {
47  switch (variable) {
48  case libsumo::VAR_SHAPE:
50  break;
52  std::string paramName = "";
53  if (!server.readTypeCheckingString(inputStorage, paramName)) {
54  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
55  }
57  server.getWrapperStorage().writeString(libsumo::Polygon::getParameter(id, paramName));
58  break;
59  }
61  std::string paramName = "";
62  if (!server.readTypeCheckingString(inputStorage, paramName)) {
63  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
64  }
66  server.getWrapperStorage().writeInt(2);
68  server.getWrapperStorage().writeString(paramName);
70  server.getWrapperStorage().writeString(libsumo::Polygon::getParameter(id, paramName));
71  break;
72  }
73  default:
74  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
75  }
76  }
77  } catch (libsumo::TraCIException& e) {
78  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
79  }
81  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
82  return true;
83 }
84 
85 bool
87  tcpip::Storage& outputStorage) {
88  std::string warning = ""; // additional description for response
89  // variable
90  int variable = inputStorage.readUnsignedByte();
91  if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
92  && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
93  && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
95  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
96  }
97  // id
98  std::string id = inputStorage.readString();
99  try {
100  // process
101  switch (variable) {
102  case libsumo::VAR_TYPE: {
103  std::string type;
104  if (!server.readTypeCheckingString(inputStorage, type)) {
105  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
106  }
107  libsumo::Polygon::setType(id, type);
108  }
109  break;
110  case libsumo::VAR_COLOR: {
112  if (!server.readTypeCheckingColor(inputStorage, col)) {
113  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
114  }
116  }
117  break;
118  case libsumo::VAR_SHAPE: {
119  PositionVector shape;
120  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
121  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
122  }
124  }
125  break;
126  case libsumo::VAR_FILL: {
127  int value = 0;
128  if (!server.readTypeCheckingInt(inputStorage, value)) {
129  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an integer.", outputStorage);
130  }
131  libsumo::Polygon::setFilled(id, value != 0);
132  }
133  break;
134  case libsumo::VAR_WIDTH: {
135  double value = 0;
136  if (!server.readTypeCheckingDouble(inputStorage, value)) {
137  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
138  }
140  }
141  break;
142  case libsumo::ADD: {
143  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
144  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
145  }
146  int itemNo = inputStorage.readInt();
147  if (itemNo != 5 && itemNo != 6) {
148  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
149  }
150  std::string type;
151  if (!server.readTypeCheckingString(inputStorage, type)) {
152  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
153  }
155  if (!server.readTypeCheckingColor(inputStorage, col)) {
156  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
157  }
158  int value = 0;
159  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
160  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
161  }
162  bool fill = value != 0;
163  int layer = 0;
164  if (!server.readTypeCheckingInt(inputStorage, layer)) {
165  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
166  }
167  PositionVector shape;
168  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
169  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
170  }
171  double lineWidth = 1;
172  if (itemNo == 6) {
173  if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {
174  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
175  }
176  }
178  libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
179  }
180  break;
182  // Add dynamics to polygon.
183  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
184  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
185  }
186  int itemNo = inputStorage.readInt();
187  if (itemNo != 5) {
188  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
189  }
190 
191  std::string trackedID;
192  if (!server.readTypeCheckingString(inputStorage, trackedID)) {
193  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The first parameter for adding polygon dynamics must be ID of the tracked object as a string ('' to disregard tracking).", outputStorage);
194  }
195 
196  std::vector<double> timeSpan;
197  if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {
198  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second parameter for adding polygon dynamics must be the timespan of the animation (length=0 to disregard animation).", outputStorage);
199  }
200 
201  std::vector<double> alphaSpan;
202  if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {
203  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third parameter for adding polygon dynamics must be the alphaSpanStr of the animation (length=0 to disregard alpha animation).", outputStorage);
204  }
205 
206  int looped;
207  if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {
208  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth parameter for adding polygon dynamics must be boolean indicating whether the animation should be looped.", outputStorage);
209  }
210 
211  int rotate;
212  if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {
213  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth parameter for adding polygon dynamics must be boolean indicating whether the tracking polygon should be rotated.", outputStorage);
214  }
215 
216  libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);
217  }
218  break;
219  case libsumo::REMOVE: {
220  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
221  if (!server.readTypeCheckingInt(inputStorage, layer)) {
222  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
223  }
224 
225  libsumo::Polygon::remove(id, layer);
226 
227  }
228  break;
229  case libsumo::VAR_PARAMETER: {
230  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
231  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
232  }
233  //readt itemNo
234  inputStorage.readInt();
235  std::string name;
236  if (!server.readTypeCheckingString(inputStorage, name)) {
237  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
238  }
239  std::string value;
240  if (!server.readTypeCheckingString(inputStorage, value)) {
241  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
242  }
243  libsumo::Polygon::setParameter(id, name, value);
244 
245  }
246  break;
247  default:
248  break;
249  }
250  } catch (libsumo::TraCIException& e) {
251  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
252  }
253  server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
254  return true;
255 }
256 
257 
258 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:54
A list of positions.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
tcpip::Storage & getWrapperStorage()
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:348
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:109
static void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:219
static void addDynamics(const std::string &polygonID, const std::string &trackedID="", const std::vector< double > &timeSpan=std::vector< double >(), const std::vector< double > &alphaSpan=std::vector< double >(), bool looped=false, bool rotate=true)
Definition: Polygon.cpp:149
static void setColor(const std::string &polygonID, const TraCIColor &color)
Definition: Polygon.cpp:118
static void add(const std::string &polygonID, const TraCIPositionVector &shape, const TraCIColor &color, bool fill=false, const std::string &polygonType="", int layer=0, double lineWidth=1)
Definition: Polygon.cpp:124
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:194
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:69
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:213
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:299
LIBSUMO_ID_PARAMETER_API static LIBSUMO_SUBSCRIPTION_API void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:102
virtual std::string readString()
Definition: storage.cpp:175
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
virtual void writeInt(int)
Definition: storage.cpp:316
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
virtual int readInt()
Definition: storage.cpp:306
TRACI_CONST int RESPONSE_GET_POLYGON_VARIABLE
TRACI_CONST int VAR_TYPE
TRACI_CONST int TYPE_COMPOUND
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:196
TRACI_CONST int VAR_MOVE_TO
TRACI_CONST int CMD_SET_POLYGON_VARIABLE
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_SHAPE
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int VAR_ADD_DYNAMICS
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int CMD_GET_POLYGON_VARIABLE
TRACI_CONST int VAR_FILL
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
TRACI_CONST int TYPE_STRING