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 /****************************************************************************/
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 <libsumo/TraCIConstants.h>
35 #include "TraCIServerAPI_Polygon.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 bool
43  tcpip::Storage& outputStorage) {
44  const int variable = inputStorage.readUnsignedByte();
45  const std::string id = inputStorage.readString();
47  try {
48  if (!libsumo::Polygon::handleVariable(id, variable, &server)) {
49  switch (variable) {
50  case libsumo::VAR_SHAPE:
52  break;
54  std::string paramName = "";
55  if (!server.readTypeCheckingString(inputStorage, paramName)) {
56  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
57  }
60  break;
61  }
62  default:
63  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
64  }
65  }
66  } catch (libsumo::TraCIException& e) {
67  return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
68  }
70  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
71  return true;
72 }
73 
74 bool
76  tcpip::Storage& outputStorage) {
77  std::string warning = ""; // additional description for response
78  // variable
79  int variable = inputStorage.readUnsignedByte();
80  if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
81  && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
82  && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
84  "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
85  }
86  // id
87  std::string id = inputStorage.readString();
88  try {
89  // process
90  switch (variable) {
91  case libsumo::VAR_TYPE: {
92  std::string type;
93  if (!server.readTypeCheckingString(inputStorage, type)) {
94  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
95  }
96  libsumo::Polygon::setType(id, type);
97  }
98  break;
99  case libsumo::VAR_COLOR: {
101  if (!server.readTypeCheckingColor(inputStorage, col)) {
102  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
103  }
105  }
106  break;
107  case libsumo::VAR_SHAPE: {
108  PositionVector shape;
109  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
110  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
111  }
113  }
114  break;
115  case libsumo::VAR_FILL: {
116  int value = 0;
117  if (!server.readTypeCheckingInt(inputStorage, value)) {
118  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an integer.", outputStorage);
119  }
120  libsumo::Polygon::setFilled(id, value != 0);
121  }
122  break;
123  case libsumo::VAR_WIDTH: {
124  double value = 0;
125  if (!server.readTypeCheckingDouble(inputStorage, value)) {
126  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
127  }
129  }
130  break;
131  case libsumo::ADD: {
132  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
133  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
134  }
135  int itemNo = inputStorage.readInt();
136  if (itemNo != 5 && itemNo != 6) {
137  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
138  }
139  std::string type;
140  if (!server.readTypeCheckingString(inputStorage, type)) {
141  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
142  }
144  if (!server.readTypeCheckingColor(inputStorage, col)) {
145  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
146  }
147  int value = 0;
148  if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
149  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
150  }
151  bool fill = value != 0;
152  int layer = 0;
153  if (!server.readTypeCheckingInt(inputStorage, layer)) {
154  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
155  }
156  PositionVector shape;
157  if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
158  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
159  }
160  double lineWidth = 1;
161  if (itemNo == 6) {
162  if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {
163  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
164  }
165  }
167  libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
168  }
169  break;
171  // Add dynamics to polygon.
172  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
173  return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
174  }
175  int itemNo = inputStorage.readInt();
176  if (itemNo != 5) {
177  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
178  }
179 
180  std::string trackedID;
181  if (!server.readTypeCheckingString(inputStorage, trackedID)) {
182  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);
183  }
184 
185  std::vector<double> timeSpan;
186  if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {
187  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);
188  }
189 
190  std::vector<double> alphaSpan;
191  if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {
192  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);
193  }
194 
195  int looped;
196  if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {
197  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);
198  }
199 
200  int rotate;
201  if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {
202  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);
203  }
204 
205  libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);
206  }
207  break;
208  case libsumo::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(libsumo::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 libsumo::VAR_PARAMETER: {
219  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
220  return server.writeErrorStatusCmd(libsumo::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(libsumo::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(libsumo::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(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
241  }
242  server.writeStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
243  return true;
244 }
245 
246 
247 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:353
libsumo::CMD_GET_POLYGON_VARIABLE
TRACI_CONST int CMD_GET_POLYGON_VARIABLE
Definition: TraCIConstants.h:210
libsumo::Polygon::setColor
static void setColor(const std::string &polygonID, const TraCIColor &c)
Definition: Polygon.cpp:116
libsumo::RESPONSE_GET_POLYGON_VARIABLE
TRACI_CONST int RESPONSE_GET_POLYGON_VARIABLE
Definition: TraCIConstants.h:212
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:122
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:140
TraCIServer::readTypeCheckingUnsignedByte
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
Definition: TraCIServer.cpp:1481
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
libsumo::VAR_COLOR
TRACI_CONST int VAR_COLOR
Definition: TraCIConstants.h:628
TraCIServer::readTypeCheckingColor
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
Definition: TraCIServer.cpp:1446
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1416
libsumo::VAR_WIDTH
TRACI_CONST int VAR_WIDTH
Definition: TraCIConstants.h:664
TraCIServerAPI_Polygon.h
libsumo::CMD_SET_POLYGON_VARIABLE
TRACI_CONST int CMD_SET_POLYGON_VARIABLE
Definition: TraCIConstants.h:214
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1368
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:75
libsumo::Polygon::getParameter
static std::string getParameter(const std::string &polygonID, const std::string &key)
Definition: Polygon.cpp:94
libsumo::Polygon::remove
static void remove(const std::string &polygonID, int layer=0)
Definition: Polygon.cpp:185
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:928
libsumo::VAR_MOVE_TO
TRACI_CONST int VAR_MOVE_TO
Definition: TraCIConstants.h:715
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:136
PositionVector
A list of positions.
Definition: PositionVector.h:46
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:175
libsumo::CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
Definition: TraCIConstants.h:154
libsumo::ADD
TRACI_CONST int ADD
Definition: TraCIConstants.h:932
libsumo::Polygon::setShape
static void setShape(const std::string &polygonID, const TraCIPositionVector &shape)
Definition: Polygon.cpp:107
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1406
libsumo::Polygon::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Polygon.cpp:274
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:967
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:103
libsumo::VAR_SHAPE
TRACI_CONST int VAR_SHAPE
Definition: TraCIConstants.h:667
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
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:1436
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1491
tcpip::Storage::readString
virtual std::string readString()
TraCIConstants.h
tcpip::Storage::readInt
virtual int readInt()
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
libsumo::REMOVE
TRACI_CONST int REMOVE
Definition: TraCIConstants.h:935
Helper.h
libsumo::Polygon::setFilled
static void setFilled(std::string polygonID, bool filled)
Definition: Polygon.cpp:195
libsumo::Helper::makeTraCIPositionVector
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition: Helper.cpp:224
libsumo::TraCIException
Definition: TraCIDefs.h:90
TraCIServer::writePositionVector
void writePositionVector(tcpip::Storage &outputStorage, const libsumo::TraCIPositionVector &shape)
Definition: TraCIServer.cpp:1380
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
Polygon.h
libsumo::VAR_TYPE
TRACI_CONST int VAR_TYPE
Definition: TraCIConstants.h:670
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1396
libsumo::VAR_ADD_DYNAMICS
TRACI_CONST int VAR_ADD_DYNAMICS
Definition: TraCIConstants.h:718
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:338
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:42
libsumo::Polygon::setParameter
static void setParameter(const std::string &polygonID, const std::string &key, const std::string &value)
Definition: Polygon.cpp:238
libsumo::Polygon::setType
static void setType(const std::string &polygonID, const std::string &setType)
Definition: Polygon.cpp:100
libsumo::Polygon::getShape
static TraCIPositionVector getShape(const std::string &polygonID)
Definition: Polygon.cpp:70
config.h
ShapeContainer.h
StdDefs.h
libsumo::TraCIPositionVector
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:150
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
libsumo::Polygon::setLineWidth
static void setLineWidth(std::string polygonID, double lineWidth)
Definition: Polygon.cpp:201
libsumo::VAR_FILL
TRACI_CONST int VAR_FILL
Definition: TraCIConstants.h:691
tcpip::Storage
Definition: storage.h:36
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:342
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:981