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-2026 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 StoHelp::writeTypedInt(server.getWrapperStorage(), libsumo::GUI::isSelected(id, StoHelp::readTypedString(inputStorage, "The type of the object must be given as a string.")) ? 1 : 0);
46 break;
47 }
48 default:
49 return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
50 }
51 }
52 } catch (libsumo::TraCIException& e) {
53 return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
54 }
56 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
57 return true;
58}
59
60
61bool
63 tcpip::Storage& outputStorage) {
64 std::string warning = ""; // additional description for response
65 const int variable = inputStorage.readUnsignedByte();
66 if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
67 && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
68 && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
69 && variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
70 && variable != libsumo::ADD && variable != libsumo::REMOVE
71 ) {
72 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
73 }
74 const std::string id = inputStorage.readString();
75 try {
76 switch (variable) {
78 libsumo::GUI::setZoom(id, StoHelp::readTypedDouble(inputStorage, "The zoom must be given as a double."));
79 break;
80 }
82 const libsumo::TraCIPosition tp = StoHelp::readTypedPosition2D(inputStorage, "The view port must be given as a position.");
83 libsumo::GUI::setOffset(id, tp.x, tp.y);
84 break;
85 }
87 libsumo::GUI::toggleSelection(id, StoHelp::readTypedString(inputStorage, "The type of the object must be given as a string."));
88 break;
89 }
91 libsumo::GUI::setSchema(id, StoHelp::readTypedString(inputStorage, "The scheme must be specified by a string."));
92 break;
93 }
95 const libsumo::TraCIPositionVector tp = StoHelp::readTypedPolygon(inputStorage, "The boundary must be specified by a bounding box.");
96 libsumo::GUI::setBoundary(id, tp.value[0].x, tp.value[0].y, tp.value[1].x, tp.value[1].y);
97 break;
98 }
99 case libsumo::VAR_ANGLE: {
100 libsumo::GUI::setAngle(id, StoHelp::readTypedDouble(inputStorage, "The rotation must be given as a double."));
101 break;
102 }
104 StoHelp::readCompound(inputStorage, 3, "Screenshot requires three values as parameter.");
105 const std::string filename = StoHelp::readTypedString(inputStorage, "The first variable must be a file name.");
106 const int width = StoHelp::readTypedInt(inputStorage, "The second variable must be the width given as int.");
107 const int height = StoHelp::readTypedInt(inputStorage, "The third variable must be the height given as int.");
108 // take screenshot after the current step is finished (showing the same state as sumo-gui and netstate-output)
109 libsumo::GUI::screenshot(id, filename, width, height);
110 break;
111 }
113 libsumo::GUI::trackVehicle(id, StoHelp::readTypedString(inputStorage, "Tracking requires a string ID."));
114 break;
115 }
116 case libsumo::ADD: {
117 StoHelp::readCompound(inputStorage, 2, "Adding a view requires two values as parameter.");
118 const std::string scheme = StoHelp::readTypedString(inputStorage, "The first variable must be a scheme name.");
119 const int viewType = StoHelp::readTypedInt(inputStorage, "The second variable must be the view type given as int.");
120 libsumo::GUI::addView(id, scheme,
122 break;
123 }
124 case libsumo::REMOVE: {
125 libsumo::GUI::removeView(id);
126 break;
127 }
128 default:
129 break;
130 }
131 } catch (libsumo::TraCIException& e) {
132 return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
133 }
134 server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
135 return true;
136}
137
138
139/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:81
@ VIEW_3D_OSG
plain 3D OSG view (
@ VIEW_2D_OPENGL
plain 2D openGL view (
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.
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.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static void writeTypedInt(tcpip::Storage &content, int value)
static const libsumo::TraCIPosition readTypedPosition2D(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIPositionVector readTypedPolygon(tcpip::Storage &ret, const std::string &error="")
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
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
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 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
A list of positions.
Definition TraCIDefs.h:240
std::vector< TraCIPosition > value
Definition TraCIDefs.h:250