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 /****************************************************************************/
16 // APIs for getting/setting GUI values via TraCI
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <fx.h>
32 #include <libsumo/TraCIConstants.h>
33 #include <guisim/GUINet.h>
34 #include <guisim/GUIVehicle.h>
35 #include <guisim/GUIBaseVehicle.h>
36 #include "TraCIServerAPI_GUI.h"
37 
38 
39 // ===========================================================================
40 // method definitions
41 // ===========================================================================
42 bool
44  tcpip::Storage& outputStorage) {
45  // variable & id
46  int variable = inputStorage.readUnsignedByte();
47  std::string id = inputStorage.readString();
48  // check variable
49  if (variable != libsumo::TRACI_ID_LIST && variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
50  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY && variable != libsumo::VAR_HAS_VIEW
51  && variable != libsumo::VAR_TRACK_VEHICLE) {
52  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
53  }
54  // begin response building
55  tcpip::Storage tempMsg;
56  // response-code, variableID, objectID
58  tempMsg.writeUnsignedByte(variable);
59  tempMsg.writeString(id);
60  // process request
61  if (variable == libsumo::TRACI_ID_LIST) {
62  std::vector<std::string> ids = GUIMainWindow::getInstance()->getViewIDs();
64  tempMsg.writeStringList(ids);
65  } else {
67  if (v == nullptr && variable != libsumo::VAR_HAS_VIEW) {
68  return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
69  }
70  switch (variable) {
73  tempMsg.writeDouble(v->getChanger().getZoom());
74  break;
77  tempMsg.writeDouble(v->getChanger().getXPos());
78  tempMsg.writeDouble(v->getChanger().getYPos());
79  break;
83  break;
87  tempMsg.writeByte(2);
88  tempMsg.writeDouble(b.xmin());
89  tempMsg.writeDouble(b.ymin());
90  tempMsg.writeDouble(b.xmax());
91  tempMsg.writeDouble(b.ymax());
92  }
93  break;
94  case libsumo::VAR_HAS_VIEW: {
96  tempMsg.writeInt(v != nullptr ? 1 : 0);
97  }
98  break;
100  GUIVehicle* gv = 0;
101  std::string id;
102  GUIGlID gid = v->getTrackedID();
103  if (gid != GUIGlObject::INVALID_ID) {
105  }
106  if (gv == 0) {
107  id = "";
108  } else {
109  id = gv->getID();
110  }
112  tempMsg.writeString(id);
113  if (gid != GUIGlObject::INVALID_ID) {
115  }
116  }
117  break;
118  default:
119  break;
120  }
121  }
123  server.writeResponseWithLength(outputStorage, tempMsg);
124  return true;
125 }
126 
127 
128 bool
130  tcpip::Storage& outputStorage) {
131  std::string warning = ""; // additional description for response
132  // variable
133  int variable = inputStorage.readUnsignedByte();
134  if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
135  && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
136  && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
137  ) {
138  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
139  }
140  // id
141  const std::string id = inputStorage.readString();
143  if (v == nullptr) {
144  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "View '" + id + "' is not known", outputStorage);
145  }
146  // process
147  switch (variable) {
148  case libsumo::VAR_VIEW_ZOOM: {
149  Position off, p;
150  double zoom = 1;
151  if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
152  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
153  }
154  off.set(v->getChanger().getXPos(), v->getChanger().getYPos(), v->getChanger().zoom2ZPos(zoom));
155  p.set(off.x(), off.y(), 0);
156  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
157  }
158  break;
161  if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
162  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
163  }
164 
165  Position off, p;
166  off.set(tp.x, tp.y, v->getChanger().getZPos());
167  p.set(tp.x, tp.y, 0);
168  v->setViewportFromToRot(off, p, v->getChanger().getRotation());
169  }
170  break;
172  std::string schema;
173  if (!server.readTypeCheckingString(inputStorage, schema)) {
174  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
175  }
176  if (!v->setColorScheme(schema)) {
177  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme is not known.", outputStorage);
178  }
179  }
180  break;
182  PositionVector p;
183  if (!server.readTypeCheckingPolygon(inputStorage, p)) {
184  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
185  }
186  v->centerTo(Boundary(p[0].x(), p[0].y(), p[1].x(), p[1].y()));
187  }
188  break;
190  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
191  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
192  }
193  int parameterCount = inputStorage.readInt();
194  if (parameterCount != 3) {
195  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
196  }
197  std::string filename;
198  if (!server.readTypeCheckingString(inputStorage, filename)) {
199  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
200  }
201  int width = 0, height = 0;
202  if (!server.readTypeCheckingInt(inputStorage, width)) {
203  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
204  }
205  if (!server.readTypeCheckingInt(inputStorage, height)) {
206  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The third variable must be the height given as int.", outputStorage);
207  }
208  // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
209  v->addSnapshot(MSNet::getInstance()->getCurrentTimeStep(), filename, width, height);
210  }
211  break;
213  std::string vehID;
214  if (!server.readTypeCheckingString(inputStorage, vehID)) {
215  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string vehicle ID.", outputStorage);
216  }
217  if (id == "") {
218  v->stopTrack();
219  } else {
221  if (veh == nullptr) {
222  return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Could not find vehicle '" + vehID + "'.", outputStorage);
223  }
224  if (v->getTrackedID() != static_cast<GUIVehicle*>(veh)->getGlID()) {
225  v->startTrack(static_cast<GUIVehicle*>(veh)->getGlID());
226  }
227  }
228  }
229  break;
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:268
libsumo::VAR_VIEW_SCHEMA
TRACI_CONST int VAR_VIEW_SCHEMA
Definition: TraCIConstants.h:1006
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:352
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
GUIVisualizationSettings::name
std::string name
The name of this setting.
Definition: GUIVisualizationSettings.h:399
GUISUMOAbstractView::setViewportFromToRot
virtual void setViewportFromToRot(const Position &lookFrom, const Position &lookAt, double rotation)
applies the given viewport settings
Definition: GUISUMOAbstractView.cpp:1366
MFXImageHelper.h
libsumo::CMD_SET_GUI_VARIABLE
TRACI_CONST int CMD_SET_GUI_VARIABLE
Definition: TraCIConstants.h:273
GUISUMOAbstractView::stopTrack
virtual void stopTrack()
stop track
Definition: GUISUMOAbstractView.cpp:1430
GUIVehicle.h
libsumo::VAR_HAS_VIEW
TRACI_CONST int VAR_HAS_VIEW
Definition: TraCIConstants.h:1018
GUISUMOAbstractView
Definition: GUISUMOAbstractView.h:72
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:129
Boundary::ymin
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
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:109
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:43
libsumo::TraCIPosition::x
double x
Definition: TraCIDefs.h:115
GUISUMOAbstractView::setColorScheme
virtual bool setColorScheme(const std::string &)
set color scheme
Definition: GUISUMOAbstractView.cpp:1389
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
GUISUMOAbstractView::centerTo
virtual void centerTo(GUIGlID id, bool applyZoom, double zoomDist=20)
centers to the chosen artifact
Definition: GUISUMOAbstractView.cpp:789
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
Boundary::xmax
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
GUISUMOAbstractView::addSnapshot
void addSnapshot(SUMOTime time, const std::string &file, const int w=-1, const int h=-1)
Sets the snapshot time to file map.
Definition: GUISUMOAbstractView.cpp:1097
GUIMainWindow::getViewIDs
std::vector< std::string > getViewIDs() const
Definition: GUIMainWindow.cpp:124
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:45
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
GUIMainWindow.h
tcpip::Storage::writeDouble
virtual void writeDouble(double)
Definition: storage.cpp:349
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:968
GUISUMOAbstractView.h
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
MSVehicleControl::getVehicle
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
Definition: MSVehicleControl.cpp:240
TraCIServer::readTypeCheckingPolygon
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
Definition: TraCIServer.cpp:1489
tcpip::Storage::writeInt
virtual void writeInt(int)
Definition: storage.cpp:316
GUIGlObject::INVALID_ID
static const GUIGlID INVALID_ID
Definition: GUIGlObject.h:69
libsumo::TYPE_DOUBLE
TRACI_CONST int TYPE_DOUBLE
Definition: TraCIConstants.h:335
libsumo::TYPE_POLYGON
TRACI_CONST int TYPE_POLYGON
Definition: TraCIConstants.h:327
GUIMainWindow::getInstance
static GUIMainWindow * getInstance()
Definition: GUIMainWindow.cpp:182
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
Definition: storage.cpp:150
TraCIConstants.h
libsumo::VAR_VIEW_ZOOM
TRACI_CONST int VAR_VIEW_ZOOM
Definition: TraCIConstants.h:1000
Boundary::xmin
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
libsumo::TYPE_INTEGER
TRACI_CONST int TYPE_INTEGER
Definition: TraCIConstants.h:333
GUIPerspectiveChanger.h
GUISUMOAbstractView::getVisualisationSettings
GUIVisualizationSettings * getVisualisationSettings() const
get visualitation settings
Definition: GUISUMOAbstractView.cpp:1395
tcpip::Storage::writeStringList
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:242
Position::set
void set(double x, double y)
set positions x and y
Definition: Position.h:86
GUIVehicle
A MSVehicle extended by some values for usage within the gui.
Definition: GUIVehicle.h:53
GUIMainWindow::getViewByID
GUIGlChildWindow * getViewByID(const std::string &id) const
Definition: GUIMainWindow.cpp:134
GUIGlObject::getGlID
GUIGlID getGlID() const
Returns the numerical id of the object.
Definition: GUIGlObject.cpp:149
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:57
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
Position::x
double x() const
Returns the x-position.
Definition: Position.h:56
GUIPerspectiveChanger::getZoom
virtual double getZoom() const =0
Returns the zoom factor computed stored in this changer.
tcpip::Storage::writeByte
virtual void writeByte(int)
Definition: storage.cpp:135
libsumo::TRACI_ID_LIST
TRACI_CONST int TRACI_ID_LIST
Definition: TraCIConstants.h:498
libsumo::RESPONSE_GET_GUI_VARIABLE
TRACI_CONST int RESPONSE_GET_GUI_VARIABLE
Definition: TraCIConstants.h:271
GUIGlObjectStorage::getObjectBlocking
GUIGlObject * getObjectBlocking(GUIGlID id)
Returns the object from the container locking it.
Definition: GUIGlObjectStorage.cpp:62
GUISUMOAbstractView::startTrack
virtual void startTrack(int)
star track
Definition: GUISUMOAbstractView.cpp:1425
libsumo::TYPE_STRINGLIST
TRACI_CONST int TYPE_STRINGLIST
Definition: TraCIConstants.h:339
GUIGlObjectStorage::unblockObject
void unblockObject(GUIGlID id)
Marks an object as unblocked.
Definition: GUIGlObjectStorage.cpp:119
libsumo::CMD_GET_GUI_VARIABLE
TRACI_CONST int CMD_GET_GUI_VARIABLE
Definition: TraCIConstants.h:269
TraCIServer::readTypeCheckingInt
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
Definition: TraCIServer.cpp:1394
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
GUIMainWindow
Definition: GUIMainWindow.h:46
Position::y
double y() const
Returns the y-position.
Definition: Position.h:61
GUISUMOAbstractView::getChanger
GUIPerspectiveChanger & getChanger() const
get changer
Definition: GUISUMOAbstractView.cpp:175
libsumo::POSITION_2D
TRACI_CONST int POSITION_2D
Definition: TraCIConstants.h:314
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:42
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
GUIGlChildWindow.h
libsumo::VAR_TRACK_VEHICLE
TRACI_CONST int VAR_TRACK_VEHICLE
Definition: TraCIConstants.h:1015
GUIGlObjectStorage::gIDStorage
static GUIGlObjectStorage gIDStorage
A single static instance of this class.
Definition: GUIGlObjectStorage.h:140
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:138
GUIBaseVehicle.h
libsumo::VAR_VIEW_OFFSET
TRACI_CONST int VAR_VIEW_OFFSET
Definition: TraCIConstants.h:1003
config.h
tcpip::Storage::readString
virtual std::string readString()
Definition: storage.cpp:175
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:1009
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
GUIGlChildWindow
Definition: GUIGlChildWindow.h:40
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:1457
libsumo::VAR_SCREENSHOT
TRACI_CONST int VAR_SCREENSHOT
Definition: TraCIConstants.h:1012
tcpip::Storage
Definition: storage.h:38
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
GUISUMOAbstractView::getTrackedID
virtual GUIGlID getTrackedID() const
get tracked id
Definition: GUISUMOAbstractView.cpp:1435
TraCIServerAPI_GUI.h
GUIGlChildWindow::getView
GUISUMOAbstractView * getView() const
return GUISUMOAbstractView
Definition: GUIGlChildWindow.cpp:99
tcpip::Storage::readInt
virtual int readInt()
Definition: storage.cpp:306
Boundary::ymax
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136
libsumo::TraCIPosition::y
double y
Definition: TraCIDefs.h:115
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:341
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:982