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) {
105  gv = static_cast<GUIVehicle*>(GUIGlObjectStorage::gIDStorage.getObjectBlocking(gid));
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 /****************************************************************************/
GUISUMOAbstractView::getVisibleBoundary
Boundary getVisibleBoundary() const
get visible boundary
Definition: GUISUMOAbstractView.cpp:266
libsumo::VAR_VIEW_SCHEMA
TRACI_CONST int VAR_VIEW_SCHEMA
Definition: TraCIConstants.h:995
GUIPerspectiveChanger::getYPos
virtual double getYPos() const =0
Returns the y-offset of the field to show stored in this changer.
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:353
GUIVisualizationSettings::name
std::string name
The name of this setting.
Definition: GUIVisualizationSettings.h:397
GUISUMOAbstractView::setViewportFromToRot
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
Definition: GUISUMOAbstractView.cpp:1328
MFXImageHelper.h
libsumo::CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_SET_GUI_VARIABLE
Definition: TraCIConstants.h:274
GUISUMOAbstractView::stopTrack
virtual void stopTrack()
stop track
Definition: GUISUMOAbstractView.cpp:1392
GUIVehicle.h
libsumo::VAR_HAS_VIEW
TRACI_CONST int VAR_HAS_VIEW
Definition: TraCIConstants.h:1007
GUISUMOAbstractView
Definition: GUISUMOAbstractView.h:73
TraCIServerAPI_GUI::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcc: Change GUI State)
Definition: TraCIServerAPI_GUI.cpp:130
Boundary::ymin
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
GUIPerspectiveChanger::getZPos
virtual double getZPos() const =0
Returns the camera height corresponding to the current zoom factor.
libsumo::TraCIPosition
A 3D-position.
Definition: TraCIDefs.h:110
TraCIServerAPI_GUI::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
Definition: TraCIServerAPI_GUI.cpp:44
libsumo::TraCIPosition::x
double x
Definition: TraCIDefs.h:116
GUISUMOAbstractView::setColorScheme
virtual bool setColorScheme(const std::string &)
set color scheme
Definition: GUISUMOAbstractView.cpp:1351
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1416
GUISUMOAbstractView::centerTo
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
Definition: GUISUMOAbstractView.cpp:769
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1368
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:61
Boundary::xmax
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
GUIMainWindow::getViewIDs
std::vector< std::string > getViewIDs() const
Definition: GUIMainWindow.cpp:125
GUIPerspectiveChanger::getRotation
virtual double getRotation() const =0
Returns the rotation of the canvas stored in this changer.
GUINet.h
PositionVector
A list of positions.
Definition: PositionVector.h:46
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1406
GUIMainWindow.h
tcpip::Storage::writeByte
virtual void writeByte(int)
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:967
GUISUMOAbstractView.h
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
tcpip::Storage::writeInt
virtual void writeInt(int)
MSVehicleControl::getVehicle
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Definition: MSVehicleControl.cpp:235
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1491
GUIGlObject::INVALID_ID
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:70
libsumo::TYPE_DOUBLE
TRACI_CONST int TYPE_DOUBLE
Definition: TraCIConstants.h:336
libsumo::TYPE_POLYGON
TRACI_CONST int TYPE_POLYGON
Definition: TraCIConstants.h:328
GUIMainWindow::getInstance
static GUIMainWindow * getInstance()
Definition: GUIMainWindow.cpp:183
tcpip::Storage::readString
virtual std::string readString()
TraCIConstants.h
libsumo::VAR_VIEW_ZOOM
TRACI_CONST int VAR_VIEW_ZOOM
Definition: TraCIConstants.h:989
Boundary::xmin
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
tcpip::Storage::readInt
virtual int readInt()
libsumo::TYPE_INTEGER
TRACI_CONST int TYPE_INTEGER
Definition: TraCIConstants.h:334
GUIPerspectiveChanger.h
GUISUMOAbstractView::getVisualisationSettings
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
Definition: GUISUMOAbstractView.cpp:1357
Position::set
void set(double x, double y)
set positions x and y
Definition: Position.h:87
GUIVehicle
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:54
tcpip::Storage::writeStringList
virtual void writeStringList(const std::vector< std::string > &s)
GUIMainWindow::getViewByID
GUIGlChildWindow * getViewByID(const std::string &id) const
Definition: GUIMainWindow.cpp:135
GUISUMOAbstractView::addSnapshot
void addSnapshot(SUMOTime time, const std::string &file, const int width=-1, const int height=-1)
Sets the snapshot time to file map.
Definition: GUISUMOAbstractView.cpp:1059
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
Position::x
double x() const
Returns the x-position.
Definition: Position.h:57
GUIPerspectiveChanger::getZoom
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
libsumo::TRACI_ID_LIST
TRACI_CONST int TRACI_ID_LIST
Definition: TraCIConstants.h:496
libsumo::RESPONSE_GET_GUI_VARIABLE
TRACI_CONST int RESPONSE_GET_GUI_VARIABLE
Definition: TraCIConstants.h:272
GUISUMOAbstractView::startTrack
virtual void startTrack(int)
star track
Definition: GUISUMOAbstractView.cpp:1387
libsumo::TYPE_STRINGLIST
TRACI_CONST int TYPE_STRINGLIST
Definition: TraCIConstants.h:340
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
GUIGlObjectStorage::unblockObject
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Definition: GUIGlObjectStorage.cpp:120
libsumo::CMD_GET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE
Definition: TraCIConstants.h:270
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1396
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
GUIMainWindow
Definition: GUIMainWindow.h:47
Position::y
double y() const
Returns the y-position.
Definition: Position.h:62
GUISUMOAbstractView::getChanger
GUIPerspectiveChanger & getChanger() const
get changer
Definition: GUISUMOAbstractView.cpp:173
libsumo::POSITION_2D
TRACI_CONST int POSITION_2D
Definition: TraCIConstants.h:315
GUIPerspectiveChanger::zoom2ZPos
virtual double zoom2ZPos(double zoom) const =0
Returns the camera height at which the given zoom level is reached.
GUIGlID
unsigned int GUIGlID
Definition: GUIGlObject.h:43
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:338
GUIGlChildWindow.h
libsumo::VAR_TRACK_VEHICLE
TRACI_CONST int VAR_TRACK_VEHICLE
Definition: TraCIConstants.h:1004
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:141
TraCIServerAPI_GUI::getNamedView
static GUISUMOAbstractView * getNamedView(const std::string &id)
Returns the named view.
Definition: TraCIServerAPI_GUI.cpp:239
MSBaseVehicle::getID
const std::string & getID() const
Returns the name of the vehicle.
Definition: MSBaseVehicle.cpp:137
GUIBaseVehicle.h
libsumo::VAR_VIEW_OFFSET
TRACI_CONST int VAR_VIEW_OFFSET
Definition: TraCIConstants.h:992
config.h
tcpip::Storage::writeDouble
virtual void writeDouble(double)
GUIPerspectiveChanger::getXPos
virtual double getXPos() const =0
Returns the x-offset of the field to show stored in this changer.
libsumo::VAR_VIEW_BOUNDARY
TRACI_CONST int VAR_VIEW_BOUNDARY
Definition: TraCIConstants.h:998
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
GUIGlChildWindow
Definition: GUIGlChildWindow.h:41
MSVehicleControl.h
TraCIServer::readTypeCheckingPosition2D
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
Definition: TraCIServer.cpp:1459
libsumo::VAR_SCREENSHOT
TRACI_CONST int VAR_SCREENSHOT
Definition: TraCIConstants.h:1001
tcpip::Storage
Definition: storage.h:36
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
GUISUMOAbstractView::getTrackedID
virtual GUIGlID getTrackedID() const
get tracked id
Definition: GUISUMOAbstractView.cpp:1397
TraCIServerAPI_GUI.h
GUIGlChildWindow::getView
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
Definition: GUIGlChildWindow.cpp:100
Boundary::ymax
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
libsumo::TraCIPosition::y
double y
Definition: TraCIDefs.h:116
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:342
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:981