Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 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 /****************************************************************************/
20 // APIs for getting/setting GUI values via TraCI
21 /****************************************************************************/
22 #include <config.h>
23 
34 #include <libsumo/TraCIConstants.h>
35 #include <guisim/GUINet.h>
36 #include <guisim/GUIVehicle.h>
37 #include <guisim/GUIPerson.h>
38 #include <guisim/GUIContainer.h>
39 #include <guisim/GUIBaseVehicle.h>
40 #include "TraCIServerAPI_GUI.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 bool
48  tcpip::Storage& outputStorage) {
49  // variable & id
50  int variable = inputStorage.readUnsignedByte();
51  std::string id = inputStorage.readString();
52  // check variable
53  if (variable != libsumo::TRACI_ID_LIST && variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
54  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY && variable != libsumo::VAR_HAS_VIEW
55  && variable != libsumo::VAR_SELECT
56  && variable != libsumo::VAR_TRACK_VEHICLE) {
57  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58  }
59  // begin response building
60  tcpip::Storage tempMsg;
61  // response-code, variableID, objectID
63  tempMsg.writeUnsignedByte(variable);
64  tempMsg.writeString(id);
65  // process request
66  if (variable == libsumo::TRACI_ID_LIST) {
67  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
69  tempMsg.writeStringList(ids);
70  } else {
72  if (v == nullptr && variable != libsumo::VAR_HAS_VIEW && variable != libsumo::VAR_SELECT) {
73  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
74  }
75  switch (variable) {
78  tempMsg.writeDouble(v->getChanger().getZoom());
79  break;
82  tempMsg.writeDouble(v->getChanger().getXPos());
83  tempMsg.writeDouble(v->getChanger().getYPos());
84  break;
88  break;
92  tempMsg.writeByte(2);
93  tempMsg.writeDouble(b.xmin());
94  tempMsg.writeDouble(b.ymin());
95  tempMsg.writeDouble(b.xmax());
96  tempMsg.writeDouble(b.ymax());
97  }
98  break;
99  case libsumo::VAR_HAS_VIEW: {
101  tempMsg.writeInt(v != nullptr ? 1 : 0);
102  }
103  break;
104  case libsumo::VAR_SELECT: {
105  std::string objType;
106  if (!server.readTypeCheckingString(inputStorage, objType)) {
107  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
108  }
109  const std::string fullName = objType + ":" + id;
111  if (obj == nullptr) {
112  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "The " + objType + " " + id + " is not known.", outputStorage);
113  } else {
115  tempMsg.writeInt(gSelected.isSelected(obj) ? 1 : 0);
117  }
118  }
119  break;
121  GUIGlObject* tracked = nullptr;
122  GUIGlID gid = v->getTrackedID();
123  if (gid != GUIGlObject::INVALID_ID) {
125  }
127  tempMsg.writeString(tracked == nullptr ? "" : tracked->getMicrosimID());
128  if (gid != GUIGlObject::INVALID_ID) {
130  }
131  }
132  break;
133  default:
134  break;
135  }
136  }
138  server.writeResponseWithLength(outputStorage, tempMsg);
139  return true;
140 }
141 
142 
143 bool
145  tcpip::Storage& outputStorage) {
146  std::string warning = ""; // additional description for response
147  // variable
148  int variable = inputStorage.readUnsignedByte();
149  if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
150  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
151  && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
152  && variable != libsumo::VAR_SELECT
153  ) {
154  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
155  }
156  // id
157  const std::string id = inputStorage.readString();
159  if (v == nullptr && variable != libsumo::VAR_SELECT) {
160  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
161  }
162  // process
163  switch (variable) {
164  case libsumo::VAR_VIEW_ZOOM: {
165  Position off, p;
166  double zoom = 1;
167  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
168  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
169  }
170  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
171  p.set(off.x(), off.y(), 0);
172  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
173  }
174  break;
177  if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
178  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
179  }
180 
181  Position off, p;
182  off.set(tp.x, tp.y, v->getChanger().getZPos());
183  p.set(tp.x, tp.y, 0);
184  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
185  }
186  break;
187  case libsumo::VAR_SELECT: {
188  std::string objType;
189  if (!server.readTypeCheckingString(inputStorage, objType)) {
190  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
191  }
192  const std::string fullName = objType + ":" + id;
194  if (obj == nullptr) {
195  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The " + objType + " " + id + " is not known.", outputStorage);
196  } else {
199  }
200  }
201  break;
203  std::string schema;
204  if (!server.readTypeCheckingString(inputStorage, schema)) {
205  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
206  }
207  if (!v->setColorScheme(schema)) {
208  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
209  }
210  }
211  break;
213  PositionVector p;
214  if (!server.readTypeCheckingPolygon(inputStorage, p)) {
215  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
216  }
217  v->centerTo(Boundary(p[0].x(), p[0].y(), p[1].x(), p[1].y()));
218  }
219  break;
221  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
222  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
223  }
224  int parameterCount = inputStorage.readInt();
225  if (parameterCount != 3) {
226  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
227  }
228  std::string filename;
229  if (!server.readTypeCheckingString(inputStorage, filename)) {
230  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
231  }
232  int width = 0, height = 0;
233  if (!server.readTypeCheckingInt(inputStorage, width)) {
234  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
235  }
236  if (!server.readTypeCheckingInt(inputStorage, height)) {
237  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The third variable must be the height given as int.", outputStorage);
238  }
239  // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
240  v->addSnapshot(MSNet::getInstance()->getCurrentTimeStep(), filename, width, height);
241  }
242  break;
244  std::string objID;
245  if (!server.readTypeCheckingString(inputStorage, objID)) {
246  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
247  }
248  if (objID == "") {
249  v->stopTrack();
250  } else {
251  GUIGlID glID = 0;
253  if (veh != nullptr) {
254  glID = static_cast<GUIVehicle*>(veh)->getGlID();
255  } else {
257  if (person != nullptr) {
258  glID = static_cast<GUIPerson*>(person)->getGlID();
259  } else {
261  if (container != nullptr) {
262  glID = static_cast<GUIContainer*>(container)->getGlID();
263  } else {
264  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Could not find vehicle or person '" + objID + "'.", outputStorage);
265  }
266  }
267  }
268  if (v->getTrackedID() != glID) {
269  v->startTrack(glID);
270  }
271  }
272  }
273  break;
274  default:
275  break;
276  }
277  server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
278  return true;
279 }
280 
281 
283 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
285  if (mw == nullptr) {
286  return nullptr;
287  }
288  GUIGlChildWindow* const c = mw->getViewByID(id);
289  if (c == nullptr) {
290  return nullptr;
291  }
292  return c->getView();
293 }
294 
295 
296 /****************************************************************************/
unsigned int GUIGlID
Definition: GUIGlObject.h:40
GUISelectedStorage gSelected
A global holder of selected objects.
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:129
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:117
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:135
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:123
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:67
GUIGlID getGlID() const
Returns the numerical id of the object.
void unblockObject(GUIGlID id)
Marks an object as unblocked.
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
GUIGlChildWindow * getViewByID(const std::string &id) const
get specific view by ID
std::vector< std::string > getViewIDs() const
get view IDs
static GUIMainWindow * getInstance()
get instance
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
virtual double getRotation() const =0
Returns the rotation of the canvas stored in this changer.
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
virtual double getZPos() const =0
Returns the camera height corresponding to the current zoom factor.
GUIVisualizationSettings & getVisualisationSettings() const
get visualization settings
Boundary getVisibleBoundary() const
get visible boundary
void addSnapshot(SUMOTime time, const std::string &file, const int w=-1, const int h=-1)
Sets the snapshot time to file map.
GUIPerspectiveChanger & getChanger() const
get changer
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
virtual void stopTrack()
stop track
virtual void startTrack(int)
star track
virtual bool setColorScheme(const std::string &)
set color scheme
virtual GUIGlID getTrackedID() const
get tracked id
void toggleSelection(GUIGlID id)
Toggles selection of an object.
bool isSelected(GUIGlObjectType type, GUIGlID id)
Returns the information whether the object with the given type and id is selected.
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:51
std::string name
The name of this setting.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
virtual MSTransportableControl & getContainerControl()
Returns the container control.
Definition: MSNet.cpp:1068
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:376
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition: MSNet.cpp:1059
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
void set(double x, double y)
set positions x and y
Definition: Position.h:85
double x() const
Returns the x-position.
Definition: Position.h:55
double y() const
Returns the y-position.
Definition: Position.h:60
A list of positions.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
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.
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 readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeInt(int)
Definition: storage.cpp:321
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:247
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual void writeByte(int)
Definition: storage.cpp:140
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_VIEW_BOUNDARY
TRACI_CONST int RESPONSE_GET_GUI_VARIABLE
TRACI_CONST int VAR_SCREENSHOT
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_VIEW_OFFSET
TRACI_CONST int VAR_VIEW_SCHEMA
TRACI_CONST int POSITION_2D
TRACI_CONST int TYPE_POLYGON
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int VAR_VIEW_ZOOM
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int VAR_TRACK_VEHICLE
TRACI_CONST int VAR_HAS_VIEW
TRACI_CONST int CMD_GET_GUI_VARIABLE
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int VAR_SELECT
TRACI_CONST int RTYPE_OK
TRACI_CONST int TYPE_STRING
A 3D-position.
Definition: TraCIDefs.h:164