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-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 /****************************************************************************/
17 // APIs for getting/setting GUI values via TraCI
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fx.h>
33 #include <libsumo/TraCIConstants.h>
34 #include <guisim/GUINet.h>
35 #include <guisim/GUIVehicle.h>
36 #include <guisim/GUIBaseVehicle.h>
37 #include "TraCIServerAPI_GUI.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
43 bool
45  tcpip::Storage& outputStorage) {
46  // variable & id
47  int variable = inputStorage.readUnsignedByte();
48  std::string id = inputStorage.readString();
49  // check variable
50  if (variable != libsumo::TRACI_ID_LIST && variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
51  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY && variable != libsumo::VAR_HAS_VIEW
52  && variable != libsumo::VAR_TRACK_VEHICLE) {
53  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
54  }
55  // begin response building
56  tcpip::Storage tempMsg;
57  // response-code, variableID, objectID
59  tempMsg.writeUnsignedByte(variable);
60  tempMsg.writeString(id);
61  // process request
62  if (variable == libsumo::TRACI_ID_LIST) {
63  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
65  tempMsg.writeStringList(ids);
66  } else {
68  if (v == nullptr && variable != libsumo::VAR_HAS_VIEW) {
69  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
70  }
71  switch (variable) {
74  tempMsg.writeDouble(v->getChanger().getZoom());
75  break;
78  tempMsg.writeDouble(v->getChanger().getXPos());
79  tempMsg.writeDouble(v->getChanger().getYPos());
80  break;
84  break;
88  tempMsg.writeByte(2);
89  tempMsg.writeDouble(b.xmin());
90  tempMsg.writeDouble(b.ymin());
91  tempMsg.writeDouble(b.xmax());
92  tempMsg.writeDouble(b.ymax());
93  break;
94  }
95  case libsumo::VAR_HAS_VIEW: {
97  tempMsg.writeInt(v != nullptr ? 1 : 0);
98  break;
99  }
101  GUIVehicle* gv = 0;
102  std::string id;
103  GUIGlID gid = v->getTrackedID();
104  if (gid != GUIGlObject::INVALID_ID) {
106  }
107  if (gv == 0) {
108  id = "";
109  } else {
110  id = gv->getID();
111  }
113  tempMsg.writeString(id);
114  if (gid != GUIGlObject::INVALID_ID) {
116  }
117  break;
118  }
119  default:
120  break;
121  }
122  }
124  server.writeResponseWithLength(outputStorage, tempMsg);
125  return true;
126 }
127 
128 
129 bool
131  tcpip::Storage& outputStorage) {
132  std::string warning = ""; // additional description for response
133  // variable
134  int variable = inputStorage.readUnsignedByte();
135  if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
136  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
137  && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
138  ) {
139  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
140  }
141  // id
142  std::string id = inputStorage.readString();
144  if (v == nullptr) {
145  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
146  }
147  // process
148  switch (variable) {
149  case libsumo::VAR_VIEW_ZOOM: {
150  Position off, p;
151  double zoom = 1;
152  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
153  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
154  }
155  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
156  p.set(off.x(), off.y(), 0);
157  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
158  }
159  break;
162  if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
163  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
164  }
165 
166  Position off, p;
167  off.set(tp.x, tp.y, v->getChanger().getZPos());
168  p.set(tp.x, tp.y, 0);
169  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
170  }
171  break;
173  std::string schema;
174  if (!server.readTypeCheckingString(inputStorage, schema)) {
175  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
176  }
177  if (!v->setColorScheme(schema)) {
178  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
179  }
180  }
181  break;
183  PositionVector p;
184  if (!server.readTypeCheckingPolygon(inputStorage, p)) {
185  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
186  }
187  v->centerTo(Boundary(p[0].x(), p[0].y(), p[1].x(), p[1].y()));
188  break;
189  }
191  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
192  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
193  }
194  int parameterCount = inputStorage.readInt();
195  if (parameterCount != 3) {
196  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
197  }
198  std::string filename;
199  if (!server.readTypeCheckingString(inputStorage, filename)) {
200  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
201  }
202  int width = 0, height = 0;
203  if (!server.readTypeCheckingInt(inputStorage, width)) {
204  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
205  }
206  if (!server.readTypeCheckingInt(inputStorage, height)) {
207  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The third variable must be the height given as int.", outputStorage);
208  }
209  // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
210  v->addSnapshot(MSNet::getInstance()->getCurrentTimeStep(), filename, width, height);
211  }
212  break;
214  std::string id;
215  if (!server.readTypeCheckingString(inputStorage, id)) {
216  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
217  }
218  if (id == "") {
219  v->stopTrack();
220  } else {
222  if (veh == nullptr) {
223  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + id + "'.", outputStorage);
224  }
225  if (v->getTrackedID() != static_cast<GUIVehicle*>(veh)->getGlID()) {
226  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
227  }
228  }
229  }
230  default:
231  break;
232  }
233  server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
234  return true;
235 }
236 
237 
239 TraCIServerAPI_GUI::getNamedView(const std::string& id) {
241  if (mw == nullptr) {
242  return nullptr;
243  }
244  GUIGlChildWindow* const c = mw->getViewByID(id);
245  if (c == nullptr) {
246  return nullptr;
247  }
248  return c->getView();
249 }
250 
251 
252 /****************************************************************************/
TRACI_CONST int VAR_VIEW_BOUNDARY
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
TRACI_CONST int VAR_VIEW_SCHEMA
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
TRACI_CONST int RTYPE_OK
double y() const
Returns the y-position.
Definition: Position.h:62
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
double x() const
Returns the x-position.
Definition: Position.h:57
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
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.
virtual double getRotation() const =0
Returns the rotation of the canvas stored in this changer.
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
virtual void writeUnsignedByte(int)
virtual double getZPos() const =0
Returns the camera height corresponding to the current zoom factor.
void set(double x, double y)
set positions x and y
Definition: Position.h:87
virtual void stopTrack()
stop track
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
virtual void writeInt(int)
std::vector< std::string > getViewIDs() const
virtual int readUnsignedByte()
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int TRACI_ID_LIST
virtual GUIGlID getTrackedID() const
get tracked id
virtual void startTrack(int)
star track
std::string name
The name of this setting.
TRACI_CONST int TYPE_POLYGON
Representation of a vehicle.
Definition: SUMOVehicle.h:61
virtual int readInt()
static GUIMainWindow * getInstance()
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
virtual void writeByte(int)
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
virtual void writeStringList(const std::vector< std::string > &s)
GUIPerspectiveChanger & getChanger() const
get changer
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
TRACI_CONST int TYPE_DOUBLE
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
TRACI_CONST int TYPE_STRINGLIST
virtual std::string readString()
TRACI_CONST int VAR_VIEW_ZOOM
TRACI_CONST int TYPE_STRING
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
TRACI_CONST int VAR_TRACK_VEHICLE
TRACI_CONST int RESPONSE_GET_GUI_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual bool setColorScheme(const std::string &)
set color scheme
unsigned int GUIGlID
Definition: GUIGlObject.h:43
virtual void writeString(const std::string &s)
TRACI_CONST int CMD_GET_GUI_VARIABLE
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:70
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
virtual void writeDouble(double)
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
void addSnapshot(SUMOTime time, const std::string &file, const int width=-1, const int height=-1)
Sets the snapshot time to file map.
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Boundary getVisibleBoundary() const
get visible boundary
GUIGlChildWindow * getViewByID(const std::string &id) const
void unblockObject(GUIGlID id)
Marks an object as unblocked.
TRACI_CONST int POSITION_2D
TRACI_CONST int VAR_VIEW_OFFSET
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
A 3D-position.
Definition: TraCIDefs.h:110
const std::string & getID() const
Returns the name of the vehicle.
TRACI_CONST int VAR_HAS_VIEW
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int VAR_SCREENSHOT
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:54