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