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