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-2023 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/****************************************************************************/
21// APIs for getting/setting GUI values via TraCI
22/****************************************************************************/
23#include <config.h>
24
25#include <libsumo/GUI.h>
28#include "GUISUMOViewParent.h"
29#include "TraCIServerAPI_GUI.h"
30
31
32// ===========================================================================
33// method definitions
34// ===========================================================================
35bool
37 tcpip::Storage& outputStorage) {
38 const int variable = inputStorage.readUnsignedByte();
39 const std::string id = inputStorage.readString();
41 try {
42 if (!libsumo::GUI::handleVariable(id, variable, &server, &inputStorage)) {
43 switch (variable) {
45 std::string objType;
46 if (!server.readTypeCheckingString(inputStorage, objType)) {
47 return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
48 }
49 StoHelp::writeTypedInt(server.getWrapperStorage(), libsumo::GUI::isSelected(id, objType) ? 1 : 0);
50 break;
51 }
52 default:
53 return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
54 }
55 }
56 } catch (libsumo::TraCIException& e) {
57 return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
58 }
60 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
61 return true;
62}
63
64
65bool
67 tcpip::Storage& outputStorage) {
68 std::string warning = ""; // additional description for response
69 const int variable = inputStorage.readUnsignedByte();
70 if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
71 && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
72 && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
73 && variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
74 && variable != libsumo::ADD && variable != libsumo::REMOVE
75 ) {
76 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
77 }
78 const std::string id = inputStorage.readString();
79 try {
80 switch (variable) {
82 double zoom = 0.;
83 if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
84 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
85 }
86 libsumo::GUI::setZoom(id, zoom);
87 break;
88 }
91 if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
92 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
93 }
94 libsumo::GUI::setOffset(id, tp.x, tp.y);
95 break;
96 }
98 std::string objType;
99 if (!server.readTypeCheckingString(inputStorage, objType)) {
100 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
101 }
102 libsumo::GUI::toggleSelection(id, objType);
103 break;
104 }
106 std::string schema;
107 if (!server.readTypeCheckingString(inputStorage, schema)) {
108 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
109 }
110 libsumo::GUI::setSchema(id, schema);
111 break;
112 }
115 if (!server.readTypeCheckingPolygon(inputStorage, p)) {
116 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
117 }
118 libsumo::GUI::setBoundary(id, p[0].x(), p[0].y(), p[1].x(), p[1].y());
119 break;
120 }
121 case libsumo::VAR_ANGLE: {
122 double rot;
123 if (!server.readTypeCheckingDouble(inputStorage, rot)) {
124 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The rotation must be given as a double.", outputStorage);
125 }
126 libsumo::GUI::setAngle(id, rot);
127 break;
128 }
130 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
131 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
132 }
133 int parameterCount = inputStorage.readInt();
134 if (parameterCount != 3) {
135 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
136 }
137 std::string filename;
138 if (!server.readTypeCheckingString(inputStorage, filename)) {
139 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
140 }
141 int width = 0, height = 0;
142 if (!server.readTypeCheckingInt(inputStorage, width)) {
143 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
144 }
145 if (!server.readTypeCheckingInt(inputStorage, height)) {
146 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The third variable must be the height given as int.", outputStorage);
147 }
148 // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
149 libsumo::GUI::screenshot(id, filename, width, height);
150 break;
151 }
153 std::string objID;
154 if (!server.readTypeCheckingString(inputStorage, objID)) {
155 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
156 }
157 libsumo::GUI::trackVehicle(id, objID);
158 break;
159 }
160 case libsumo::ADD: {
161 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
162 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires a compound object.", outputStorage);
163 }
164 int parameterCount = inputStorage.readInt();
165 if (parameterCount != 2) {
166 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires two values as parameter.", outputStorage);
167 }
168 std::string scheme;
169 if (!server.readTypeCheckingString(inputStorage, scheme)) {
170 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a scheme name.", outputStorage);
171 }
172 int viewType;
173 if (!server.readTypeCheckingInt(inputStorage, viewType)) {
174 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the view type given as int.", outputStorage);
175 }
176 libsumo::GUI::addView(id, scheme,
178 break;
179 }
180 case libsumo::REMOVE: {
181 libsumo::GUI::removeView(id);
182 break;
183 }
184 default:
185 break;
186 }
187 } catch (libsumo::TraCIException& e) {
188 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
189 }
190 server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
191 return true;
192}
193
194
195/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
@ VIEW_3D_OSG
plain 3D OSG view (
@ VIEW_2D_OPENGL
plain 2D openGL view (
A list of positions.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xac: Get GUI Variable)
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.
tcpip::Storage & getWrapperStorage()
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 readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
static void writeTypedInt(tcpip::Storage &content, int value)
An error which allows to continue.
Definition: TraCIDefs.h:144
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual int readInt()
Definition: storage.cpp:311
TRACI_CONST int VAR_VIEW_BOUNDARY
TRACI_CONST int RESPONSE_GET_GUI_VARIABLE
TRACI_CONST int VAR_SCREENSHOT
TRACI_CONST int VAR_ANGLE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_VIEW_OFFSET
TRACI_CONST int VAR_VIEW_SCHEMA
TRACI_CONST int VAR_VIEW_ZOOM
TRACI_CONST int CMD_SET_GUI_VARIABLE
TRACI_CONST int VAR_TRACK_VEHICLE
TRACI_CONST int REMOVE
TRACI_CONST int CMD_GET_GUI_VARIABLE
TRACI_CONST int VAR_SELECT
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition: TraCIDefs.h:178