Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_GUI.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 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 const int width = StoHelp::readTypedInt(inputStorage, "The second variable must be the width given as int.");
142 const int height = StoHelp::readTypedInt(inputStorage, "The third variable must be the height given as int.");
143 // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
144 libsumo::GUI::screenshot(id, filename, width, height);
145 break;
146 }
148 std::string objID;
149 if (!server.readTypeCheckingString(inputStorage, objID)) {
150 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
151 }
152 libsumo::GUI::trackVehicle(id, objID);
153 break;
154 }
155 case libsumo::ADD: {
156 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
157 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires a compound object.", outputStorage);
158 }
159 int parameterCount = inputStorage.readInt();
160 if (parameterCount != 2) {
161 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires two values as parameter.", outputStorage);
162 }
163 std::string scheme;
164 if (!server.readTypeCheckingString(inputStorage, scheme)) {
165 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a scheme name.", outputStorage);
166 }
167 const int viewType = StoHelp::readTypedInt(inputStorage, "The second variable must be the view type given as int.");
168 libsumo::GUI::addView(id, scheme,
170 break;
171 }
172 case libsumo::REMOVE: {
173 libsumo::GUI::removeView(id);
174 break;
175 }
176 default:
177 break;
178 }
179 } catch (libsumo::TraCIException& e) {
180 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
181 }
182 server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
183 return true;
184}
185
186
187/****************************************************************************/
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 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 int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static void writeTypedInt(tcpip::Storage &content, int value)
An error which allows to continue.
Definition TraCIDefs.h:145
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:179