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-2019 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 /****************************************************************************/
19 // APIs for getting/setting polygon values via TraCI
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <utils/common/StdDefs.h>
29 #include <microsim/MSNet.h>
31 #include <libsumo/Polygon.h>
32 #include <libsumo/Helper.h>
33 #include <libsumo/TraCIConstants.h>
34 #include "TraCIServerAPI_Polygon.h"
35 
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();
46  try {
47  if (!libsumo::Polygon::handleVariable(id, variable, &server)) {
48  switch (variable) {
49  case libsumo::VAR_SHAPE:
51  break;
53  std::string paramName = "";
54  if (!server.readTypeCheckingString(inputStorage, paramName)) {
55  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
56  }
59  break;
60  }
61  default:
62  return server.writeErrorStatusCmd(libsumo::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(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
67  }
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 != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
80  && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
81  && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::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 libsumo::VAR_TYPE: {
91  std::string type;
92  if (!server.readTypeCheckingString(inputStorage, type)) {
93  return server.writeErrorStatusCmd(libsumo::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 libsumo::VAR_COLOR: {
100  if (!server.readTypeCheckingColor(inputStorage, col)) {
101  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
102  }
104  }
105  break;
106  case libsumo::VAR_SHAPE: {
107  PositionVector shape;
108  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
109  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
110  }
112  }
113  break;
114  case libsumo::VAR_FILL: {
115  int value = 0;
116  if (!server.readTypeCheckingInt(inputStorage, value)) {
117  return server.writeErrorStatusCmd(libsumo::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 libsumo::VAR_WIDTH: {
123  double value = 0;
124  if (!server.readTypeCheckingDouble(inputStorage, value)) {
125  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
126  }
128  }
129  break;
130  case libsumo::ADD: {
131  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
132  return server.writeErrorStatusCmd(libsumo::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(libsumo::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(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
141  }
143  if (!server.readTypeCheckingColor(inputStorage, col)) {
144  return server.writeErrorStatusCmd(libsumo::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(libsumo::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(libsumo::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(libsumo::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(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
163  }
164  }
166  libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
167  }
168  break;
170  // Add dynamics to polygon.
171  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
172  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
173  }
174  int itemNo = inputStorage.readInt();
175  if (itemNo != 5) {
176  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
177  }
178 
179  std::string trackedID;
180  if (!server.readTypeCheckingString(inputStorage, trackedID)) {
181  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);
182  }
183 
184  std::vector<double> timeSpan;
185  if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {
186  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);
187  }
188 
189  std::vector<double> alphaSpan;
190  if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {
191  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);
192  }
193 
194  int looped;
195  if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {
196  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);
197  }
198 
199  int rotate;
200  if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {
201  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);
202  }
203 
204  libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);
205  }
206  break;
207  case libsumo::REMOVE: {
208  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
209  if (!server.readTypeCheckingInt(inputStorage, layer)) {
210  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
211  }
212 
213  libsumo::Polygon::remove(id, layer);
214 
215  }
216  break;
217  case libsumo::VAR_PARAMETER: {
218  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
219  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
220  }
221  //readt itemNo
222  inputStorage.readInt();
223  std::string name;
224  if (!server.readTypeCheckingString(inputStorage, name)) {
225  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
226  }
227  std::string value;
228  if (!server.readTypeCheckingString(inputStorage, value)) {
229  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
230  }
231  libsumo::Polygon::setParameter(id, name, value);
232 
233  }
234  break;
235  default:
236  break;
237  }
238  } catch (libsumo::TraCIException& e) {
239  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
240  }
241  server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
242  return true;
243 }
244 
245 
246 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:352
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
libsumo::CMD_GET_POLYGON_VARIABLE
TRACI_CONST int CMD_GET_POLYGON_VARIABLE
Definition: TraCIConstants.h:209
libsumo::Polygon::setColor
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:115
libsumo::RESPONSE_GET_POLYGON_VARIABLE
TRACI_CONST int RESPONSE_GET_POLYGON_VARIABLE
Definition: TraCIConstants.h:211
libsumo::Polygon::add
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:121
MSNet.h
libsumo::Polygon::addDynamics
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:139
TraCIServer::readTypeCheckingUnsignedByte
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
Definition: TraCIServer.cpp:1479
libsumo::VAR_COLOR
TRACI_CONST int VAR_COLOR
Definition: TraCIConstants.h:630
TraCIServer::readTypeCheckingColor
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
Definition: TraCIServer.cpp:1444
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
libsumo::VAR_WIDTH
TRACI_CONST int VAR_WIDTH
Definition: TraCIConstants.h:666
TraCIServerAPI_Polygon.h
libsumo::CMD_SET_POLYGON_VARIABLE
TRACI_CONST int CMD_SET_POLYGON_VARIABLE
Definition: TraCIConstants.h:213
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
TraCIServerAPI_Polygon::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
Definition: TraCIServerAPI_Polygon.cpp:74
libsumo::Polygon::getParameter
static std::string getParameter(const std::string &polygonID, const std::string &key)
Definition: Polygon.cpp:93
libsumo::Polygon::remove
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:184
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:939
libsumo::VAR_MOVE_TO
TRACI_CONST int VAR_MOVE_TO
Definition: TraCIConstants.h:717
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:135
PositionVector
A list of positions.
Definition: PositionVector.h:45
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:174
libsumo::CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
Definition: TraCIConstants.h:153
libsumo::ADD
TRACI_CONST int ADD
Definition: TraCIConstants.h:943
libsumo::Polygon::setShape
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:106
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
libsumo::Polygon::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:273
TraCIServer::writeStatusCmd
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Definition: TraCIServer.cpp:968
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
libsumo::VAR_SHAPE
TRACI_CONST int VAR_SHAPE
Definition: TraCIConstants.h:669
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
TraCIServer::readTypeCheckingDoubleList
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, verifying the type.
Definition: TraCIServer.cpp:1434
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1489
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
Definition: storage.cpp:150
TraCIConstants.h
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:57
libsumo::REMOVE
TRACI_CONST int REMOVE
Definition: TraCIConstants.h:946
Helper.h
libsumo::Polygon::setFilled
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:194
libsumo::Helper::makeTraCIPositionVector
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:269
libsumo::TraCIException
Definition: TraCIDefs.h:89
TraCIServer::writePositionVector
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
Definition: TraCIServer.cpp:1378
Polygon.h
libsumo::VAR_TYPE
TRACI_CONST int VAR_TYPE
Definition: TraCIConstants.h:672
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1394
libsumo::VAR_ADD_DYNAMICS
TRACI_CONST int VAR_ADD_DYNAMICS
Definition: TraCIConstants.h:720
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
TraCIServerAPI_Polygon::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
Definition: TraCIServerAPI_Polygon.cpp:41
libsumo::Polygon::setParameter
static void setParameter(const std::string &polygonID, const std::string &key, const std::string &value)
Definition: Polygon.cpp:237
libsumo::Polygon::setType
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:99
libsumo::Polygon::getShape
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:69
config.h
ShapeContainer.h
tcpip::Storage::readString
virtual std::string readString()
Definition: storage.cpp:175
StdDefs.h
libsumo::TraCIPositionVector
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:149
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
libsumo::Polygon::setLineWidth
static void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:200
libsumo::VAR_FILL
TRACI_CONST int VAR_FILL
Definition: TraCIConstants.h:693
tcpip::Storage
Definition: storage.h:38
tcpip::Storage::readInt
virtual int readInt()
Definition: storage.cpp:306
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:341
TraCIServer::writeErrorStatusCmd
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
Definition: TraCIServer.cpp:982