LCOV - code coverage report
Current view: top level - src/gui - TraCIServerAPI_GUI.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 80.0 % 55 44
Test Date: 2025-12-06 15:35:27 Functions: 100.0 % 2 2

            Line data    Source code
       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              : /****************************************************************************/
      14              : /// @file    TraCIServerAPI_GUI.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @author  Mirko Barthauer
      19              : /// @date    07.05.2009
      20              : ///
      21              : // APIs for getting/setting GUI values via TraCI
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #include <libsumo/GUI.h>
      26              : #include <libsumo/StorageHelper.h>
      27              : #include <libsumo/TraCIConstants.h>
      28              : #include "GUISUMOViewParent.h"
      29              : #include "TraCIServerAPI_GUI.h"
      30              : 
      31              : 
      32              : // ===========================================================================
      33              : // method definitions
      34              : // ===========================================================================
      35              : bool
      36          660 : TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      37              :                                tcpip::Storage& outputStorage) {
      38          660 :     const int variable = inputStorage.readUnsignedByte();
      39          660 :     const std::string id = inputStorage.readString();
      40          660 :     server.initWrapper(libsumo::RESPONSE_GET_GUI_VARIABLE, variable, id);
      41              :     try {
      42          660 :         if (!libsumo::GUI::handleVariable(id, variable, &server, &inputStorage)) {
      43           12 :             switch (variable) {
      44              :                 case libsumo::VAR_SELECT: {
      45           30 :                     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            0 :                 default:
      49            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      50              :             }
      51              :         }
      52            0 :     } catch (libsumo::TraCIException& e) {
      53            0 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
      54            0 :     }
      55          660 :     server.writeStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      56          660 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      57              :     return true;
      58              : }
      59              : 
      60              : 
      61              : bool
      62          336 : TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      63              :                                tcpip::Storage& outputStorage) {
      64          336 :     std::string warning = ""; // additional description for response
      65          336 :     const int variable = inputStorage.readUnsignedByte();
      66          336 :     if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
      67          336 :             && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
      68          336 :             && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
      69           15 :             && variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
      70            6 :             && variable != libsumo::ADD && variable != libsumo::REMOVE
      71              :        ) {
      72            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      73              :     }
      74          336 :     const std::string id = inputStorage.readString();
      75              :     try {
      76          336 :         switch (variable) {
      77              :             case libsumo::VAR_VIEW_ZOOM: {
      78            3 :                 libsumo::GUI::setZoom(id, StoHelp::readTypedDouble(inputStorage, "The zoom must be given as a double."));
      79            3 :                 break;
      80              :             }
      81              :             case libsumo::VAR_VIEW_OFFSET: {
      82            3 :                 const libsumo::TraCIPosition tp = StoHelp::readTypedPosition2D(inputStorage,  "The view port must be given as a position.");
      83            3 :                 libsumo::GUI::setOffset(id, tp.x, tp.y);
      84              :                 break;
      85              :             }
      86              :             case libsumo::VAR_SELECT: {
      87            6 :                 libsumo::GUI::toggleSelection(id, StoHelp::readTypedString(inputStorage, "The type of the object must be given as a string."));
      88            6 :                 break;
      89              :             }
      90              :             case libsumo::VAR_VIEW_SCHEMA: {
      91            6 :                 libsumo::GUI::setSchema(id, StoHelp::readTypedString(inputStorage, "The scheme must be specified by a string."));
      92            6 :                 break;
      93              :             }
      94              :             case libsumo::VAR_VIEW_BOUNDARY: {
      95            6 :                 const libsumo::TraCIPositionVector tp = StoHelp::readTypedPolygon(inputStorage, "The boundary must be specified by a bounding box.");
      96            3 :                 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            0 :                 libsumo::GUI::setAngle(id, StoHelp::readTypedDouble(inputStorage, "The rotation must be given as a double."));
     101            0 :                 break;
     102              :             }
     103              :             case libsumo::VAR_SCREENSHOT: {
     104          306 :                 StoHelp::readCompound(inputStorage, 3, "Screenshot requires three values as parameter.");
     105          306 :                 const std::string filename = StoHelp::readTypedString(inputStorage, "The first variable must be a file name.");
     106          306 :                 const int width = StoHelp::readTypedInt(inputStorage, "The second variable must be the width given as int.");
     107          306 :                 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          306 :                 libsumo::GUI::screenshot(id, filename, width, height);
     110              :                 break;
     111              :             }
     112              :             case libsumo::VAR_TRACK_VEHICLE: {
     113            3 :                 libsumo::GUI::trackVehicle(id, StoHelp::readTypedString(inputStorage, "Tracking requires a string ID."));
     114            3 :                 break;
     115              :             }
     116              :             case libsumo::ADD: {
     117            3 :                 StoHelp::readCompound(inputStorage, 2, "Adding a view requires two values as parameter.");
     118            3 :                 const std::string scheme = StoHelp::readTypedString(inputStorage, "The first variable must be a scheme name.");
     119            3 :                 const int viewType = StoHelp::readTypedInt(inputStorage, "The second variable must be the view type given as int.");
     120            3 :                 libsumo::GUI::addView(id, scheme,
     121            3 :                                       viewType == 1 ?  GUISUMOViewParent::VIEW_3D_OSG : GUISUMOViewParent::VIEW_2D_OPENGL);
     122              :                 break;
     123              :             }
     124            3 :             case libsumo::REMOVE: {
     125            3 :                 libsumo::GUI::removeView(id);
     126              :                 break;
     127              :             }
     128              :             default:
     129              :                 break;
     130              :         }
     131            0 :     } catch (libsumo::TraCIException& e) {
     132            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
     133            0 :     }
     134          336 :     server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     135              :     return true;
     136              : }
     137              : 
     138              : 
     139              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1