Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-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 /****************************************************************************/
18 // APIs for getting/setting POI values via TraCI
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <microsim/MSNet.h>
30 #include <libsumo/POI.h>
31 #include <libsumo/TraCIConstants.h>
32 #include "TraCIServerAPI_POI.h"
33 
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 bool
40  tcpip::Storage& outputStorage) {
41  const int variable = inputStorage.readUnsignedByte();
42  const std::string id = inputStorage.readString();
43  server.initWrapper(libsumo::RESPONSE_GET_POI_VARIABLE, variable, id);
44  try {
45  if (!libsumo::POI::handleVariable(id, variable, &server)) {
46  switch (variable) {
48  std::string paramName = "";
49  if (!server.readTypeCheckingString(inputStorage, paramName)) {
50  return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
51  }
54  break;
55  }
56  default:
57  return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58  }
59  }
60  } catch (libsumo::TraCIException& e) {
61  return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, e.what(), outputStorage);
62  }
64  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
65  return true;
66 }
67 
68 
69 bool
71  tcpip::Storage& outputStorage) {
72  std::string warning = ""; // additional description for response
73  // variable & id
74  int variable = inputStorage.readUnsignedByte();
75  std::string id = inputStorage.readString();
76  // check variable
77  if (variable != libsumo::VAR_TYPE &&
78  variable != libsumo::VAR_COLOR &&
79  variable != libsumo::VAR_POSITION &&
80  variable != libsumo::VAR_WIDTH &&
81  variable != libsumo::VAR_HEIGHT &&
82  variable != libsumo::VAR_ANGLE &&
83  variable != libsumo::VAR_IMAGEFILE &&
84  variable != libsumo::VAR_HIGHLIGHT &&
85  variable != libsumo::ADD &&
86  variable != libsumo::REMOVE &&
87  variable != libsumo::VAR_PARAMETER) {
88  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
89  }
90  // process
91  try {
92  switch (variable) {
93  case libsumo::VAR_TYPE: {
94  std::string type;
95  if (!server.readTypeCheckingString(inputStorage, type)) {
96  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
97  }
98  libsumo::POI::setType(id, type);
99  }
100  break;
101  case libsumo::VAR_COLOR: {
103  if (!server.readTypeCheckingColor(inputStorage, col)) {
104  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
105  }
106  libsumo::POI::setColor(id, col);
107  }
108  break;
109  case libsumo::VAR_POSITION: {
111  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
112  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The position must be given using an according type.", outputStorage);
113  }
114  libsumo::POI::setPosition(id, pos.x, pos.y);
115  }
116  break;
117  case libsumo::VAR_WIDTH: {
118  double width;
119  if (!server.readTypeCheckingDouble(inputStorage, width)) {
120  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The width must be given using an according type.", outputStorage);
121  }
122  libsumo::POI::setWidth(id, width);
123  }
124  break;
125  case libsumo::VAR_HEIGHT: {
126  double height;
127  if (!server.readTypeCheckingDouble(inputStorage, height)) {
128  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The height must be given using an according type.", outputStorage);
129  }
130  libsumo::POI::setHeight(id, height);
131  }
132  break;
133  case libsumo::VAR_ANGLE: {
134  double angle;
135  if (!server.readTypeCheckingDouble(inputStorage, angle)) {
136  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The angle must be given using an according type.", outputStorage);
137  }
138  libsumo::POI::setAngle(id, angle);
139  }
140  break;
141  case libsumo::VAR_IMAGEFILE: {
142  std::string imageFile;
143  if (!server.readTypeCheckingString(inputStorage, imageFile)) {
144  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
145  }
146  libsumo::POI::setImageFile(id, imageFile);
147  }
148  break;
149  case libsumo::VAR_HIGHLIGHT: {
150  // Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
151  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
152  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for highlighting an object.", outputStorage);
153  }
154  int itemNo = inputStorage.readUnsignedByte();
155  if (itemNo > 5) {
156  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
157  }
158  libsumo::TraCIColor col = libsumo::TraCIColor(255, 0, 0);
159  if (itemNo > 0) {
160  if (!server.readTypeCheckingColor(inputStorage, col)) {
161  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first parameter for highlighting must be the highlight color.", outputStorage);
162  }
163  }
164  double size = -1;
165  if (itemNo > 1) {
166  if (!server.readTypeCheckingDouble(inputStorage, size)) {
167  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second parameter for highlighting must be the highlight size.", outputStorage);
168  }
169  }
170  int alphaMax = -1;
171  if (itemNo > 2) {
172  if (!server.readTypeCheckingUnsignedByte(inputStorage, alphaMax)) {
173  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third parameter for highlighting must be maximal alpha.", outputStorage);
174  }
175  }
176  double duration = -1;
177  if (itemNo > 3) {
178  if (!server.readTypeCheckingDouble(inputStorage, duration)) {
179  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fourth parameter for highlighting must be the highlight duration.", outputStorage);
180  }
181  }
182  int type = 0;
183  if (itemNo > 4) {
184  if (!server.readTypeCheckingUnsignedByte(inputStorage, type)) {
185  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fifth parameter for highlighting must be the highlight type id as ubyte.", outputStorage);
186  }
187  }
188  libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
189  }
190  break;
191  case libsumo::ADD: {
192  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
193  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
194  }
195  //read itemNo
196  const int parameterCount = inputStorage.readInt();
197  std::string type;
198  if (!server.readTypeCheckingString(inputStorage, type)) {
199  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
200  }
202  if (!server.readTypeCheckingColor(inputStorage, col)) {
203  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
204  }
205  int layer = 0;
206  if (!server.readTypeCheckingInt(inputStorage, layer)) {
207  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
208  }
210  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
211  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
212  }
213  if (parameterCount == 4) {
214  if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
215  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
216  }
217  } else if (parameterCount == 8) {
218  std::string imgFile;
219  if (!server.readTypeCheckingString(inputStorage, imgFile)) {
220  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fifth PoI parameter must be the imgFile encoded as a string.", outputStorage);
221  }
222  double width;
223  if (!server.readTypeCheckingDouble(inputStorage, width)) {
224  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The sixth PoI parameter must be the width encoded as a double.", outputStorage);
225  }
226  double height;
227  if (!server.readTypeCheckingDouble(inputStorage, height)) {
228  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The seventh PoI parameter must be the height encoded as a double.", outputStorage);
229  }
230  double angle;
231  if (!server.readTypeCheckingDouble(inputStorage, angle)) {
232  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The eighth PoI parameter must be the angle encoded as a double.", outputStorage);
233  }
234  //
235  if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle)) {
236  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
237  }
238  } else {
240  "Adding a PoI requires either only type, color, layer and position parameters or these and imageFile, width, height and angle parameters.",
241  outputStorage);
242  }
243  }
244  break;
245  case libsumo::REMOVE: {
246  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
247  if (!server.readTypeCheckingInt(inputStorage, layer)) {
248  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
249  }
250  if (!libsumo::POI::remove(id, layer)) {
251  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
252  }
253  }
254  break;
255  case libsumo::VAR_PARAMETER: {
256  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
257  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
258  }
259  //readt itemNo
260  inputStorage.readInt();
261  std::string name;
262  if (!server.readTypeCheckingString(inputStorage, name)) {
263  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
264  }
265  std::string value;
266  if (!server.readTypeCheckingString(inputStorage, value)) {
267  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
268  }
269  libsumo::POI::setParameter(id, name, value);
270  }
271  break;
272  default:
273  break;
274  }
275  } catch (libsumo::TraCIException& e) {
276  return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
277  }
278  server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
279  return true;
280 }
281 
282 
283 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:352
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
libsumo::VAR_HEIGHT
TRACI_CONST int VAR_HEIGHT
Definition: TraCIConstants.h:772
MSNet.h
libsumo::VAR_POSITION
TRACI_CONST int VAR_POSITION
Definition: TraCIConstants.h:618
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::TraCIPosition
A 3D-position.
Definition: TraCIDefs.h:109
libsumo::TraCIPosition::x
double x
Definition: TraCIDefs.h:115
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
libsumo::CMD_SET_POI_VARIABLE
TRACI_CONST int CMD_SET_POI_VARIABLE
Definition: TraCIConstants.h:198
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
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
libsumo::VAR_HIGHLIGHT
TRACI_CONST int VAR_HIGHLIGHT
Definition: TraCIConstants.h:723
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:939
libsumo::POI::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: POI.cpp:275
libsumo::TraCIColor
A color.
Definition: TraCIDefs.h:135
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
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
libsumo::POI::setHeight
static void setHeight(const std::string &poiID, double height)
Definition: POI.cpp:138
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
libsumo::POI::setPosition
static void setPosition(const std::string &poiID, double x, double y)
Definition: POI.cpp:118
libsumo::RESPONSE_GET_POI_VARIABLE
TRACI_CONST int RESPONSE_GET_POI_VARIABLE
Definition: TraCIConstants.h:196
libsumo::POI::setAngle
static void setAngle(const std::string &poiID, double angle)
Definition: POI.cpp:144
TraCIServerAPI_POI::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
Definition: TraCIServerAPI_POI.cpp:70
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
libsumo::POI::add
static bool add(const std::string &poiID, double x, double y, const TraCIColor &color, const std::string &poiType="", int layer=0, const std::string &imgFile=Shape::DEFAULT_IMG_FILE, double width=Shape::DEFAULT_IMG_WIDTH, double height=Shape::DEFAULT_IMG_HEIGHT, double angle=Shape::DEFAULT_ANGLE)
Definition: POI.cpp:156
libsumo::CMD_GET_POI_VARIABLE
TRACI_CONST int CMD_GET_POI_VARIABLE
Definition: TraCIConstants.h:194
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
Definition: storage.cpp:150
TraCIConstants.h
libsumo::VAR_ANGLE
TRACI_CONST int VAR_ANGLE
Definition: TraCIConstants.h:624
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
libsumo::POI::setType
static void setType(const std::string &poiID, const std::string &setType)
Definition: POI.cpp:112
libsumo::TraCIException
Definition: TraCIDefs.h:89
libsumo::POI::setWidth
static void setWidth(const std::string &poiID, double width)
Definition: POI.cpp:132
libsumo::VAR_TYPE
TRACI_CONST int VAR_TYPE
Definition: TraCIConstants.h:672
PointOfInterest.h
libsumo::POI::setColor
static void setColor(const std::string &poiID, const TraCIColor &c)
Definition: POI.cpp:126
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1394
TraCIServerAPI_POI.h
libsumo::POI::getParameter
static std::string getParameter(const std::string &poiID, const std::string &key)
Definition: POI.cpp:106
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
POI.h
libsumo::POI::setImageFile
static void setImageFile(const std::string &poiID, const std::string &imageFile)
Definition: POI.cpp:150
config.h
ShapeContainer.h
tcpip::Storage::readString
virtual std::string readString()
Definition: storage.cpp:175
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
TraCIServerAPI_POI::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
Definition: TraCIServerAPI_POI.cpp:39
libsumo::VAR_IMAGEFILE
TRACI_CONST int VAR_IMAGEFILE
Definition: TraCIConstants.h:696
TraCIServer::readTypeCheckingPosition2D
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
Definition: TraCIServer.cpp:1457
libsumo::POI::highlight
static void highlight(const std::string &poiID, const TraCIColor &col, double size, const int alphaMax, const double duration, const int type)
Definition: POI.cpp:175
libsumo::POI::setParameter
static void setParameter(const std::string &poiID, const std::string &key, const std::string &value)
Definition: POI.cpp:229
tcpip::Storage
Definition: storage.h:38
tcpip::Storage::readInt
virtual int readInt()
Definition: storage.cpp:306
libsumo::POI::remove
static bool remove(const std::string &poiID, int layer=0)
Definition: POI.cpp:168
libsumo::TraCIPosition::y
double y
Definition: TraCIDefs.h:115
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