LCOV - code coverage report
Current view: top level - src/gui - TraCIServerAPI_GUI.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 20.0 % 90 18
Test Date: 2024-11-20 15:55:46 Functions: 50.0 % 2 1

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2024 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            0 : TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      37              :                                tcpip::Storage& outputStorage) {
      38            0 :     const int variable = inputStorage.readUnsignedByte();
      39            0 :     const std::string id = inputStorage.readString();
      40            0 :     server.initWrapper(libsumo::RESPONSE_GET_GUI_VARIABLE, variable, id);
      41              :     try {
      42            0 :         if (!libsumo::GUI::handleVariable(id, variable, &server, &inputStorage)) {
      43            0 :             switch (variable) {
      44              :                 case libsumo::VAR_SELECT: {
      45              :                     std::string objType;
      46            0 :                     if (!server.readTypeCheckingString(inputStorage, objType)) {
      47            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
      48              :                     }
      49            0 :                     StoHelp::writeTypedInt(server.getWrapperStorage(), libsumo::GUI::isSelected(id, objType) ? 1 : 0);
      50              :                     break;
      51              :                 }
      52            0 :                 default:
      53            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, "Get GUI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      54              :             }
      55              :         }
      56            0 :     } catch (libsumo::TraCIException& e) {
      57            0 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, e.what(), outputStorage);
      58            0 :     }
      59            0 :     server.writeStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      60            0 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      61              :     return true;
      62              : }
      63              : 
      64              : 
      65              : bool
      66          300 : TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      67              :                                tcpip::Storage& outputStorage) {
      68          300 :     std::string warning = ""; // additional description for response
      69          300 :     const int variable = inputStorage.readUnsignedByte();
      70          300 :     if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
      71          300 :             && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
      72          300 :             && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
      73            0 :             && variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
      74            0 :             && variable != libsumo::ADD && variable != libsumo::REMOVE
      75              :        ) {
      76            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Change GUI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      77              :     }
      78          300 :     const std::string id = inputStorage.readString();
      79              :     try {
      80          300 :         switch (variable) {
      81            0 :             case libsumo::VAR_VIEW_ZOOM: {
      82            0 :                 double zoom = 0.;
      83            0 :                 if (!server.readTypeCheckingDouble(inputStorage, zoom)) {
      84            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The zoom must be given as a double.", outputStorage);
      85              :                 }
      86            0 :                 libsumo::GUI::setZoom(id, zoom);
      87            0 :                 break;
      88              :             }
      89            0 :             case libsumo::VAR_VIEW_OFFSET: {
      90            0 :                 libsumo::TraCIPosition tp;
      91            0 :                 if (!server.readTypeCheckingPosition2D(inputStorage, tp)) {
      92            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The view port must be given as a position.", outputStorage);
      93              :                 }
      94            0 :                 libsumo::GUI::setOffset(id, tp.x, tp.y);
      95              :                 break;
      96              :             }
      97              :             case libsumo::VAR_SELECT: {
      98              :                 std::string objType;
      99            0 :                 if (!server.readTypeCheckingString(inputStorage, objType)) {
     100            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The type of the object must be given as a string.", outputStorage);
     101              :                 }
     102            0 :                 libsumo::GUI::toggleSelection(id, objType);
     103              :                 break;
     104              :             }
     105              :             case libsumo::VAR_VIEW_SCHEMA: {
     106              :                 std::string schema;
     107            0 :                 if (!server.readTypeCheckingString(inputStorage, schema)) {
     108            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The scheme must be specified by a string.", outputStorage);
     109              :                 }
     110            0 :                 libsumo::GUI::setSchema(id, schema);
     111              :                 break;
     112              :             }
     113            0 :             case libsumo::VAR_VIEW_BOUNDARY: {
     114            0 :                 PositionVector p;
     115            0 :                 if (!server.readTypeCheckingPolygon(inputStorage, p)) {
     116            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The boundary must be specified by a bounding box.", outputStorage);
     117              :                 }
     118            0 :                 libsumo::GUI::setBoundary(id, p[0].x(), p[0].y(), p[1].x(), p[1].y());
     119              :                 break;
     120            0 :             }
     121            0 :             case libsumo::VAR_ANGLE: {
     122              :                 double rot;
     123            0 :                 if (!server.readTypeCheckingDouble(inputStorage, rot)) {
     124            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The rotation must be given as a double.", outputStorage);
     125              :                 }
     126            0 :                 libsumo::GUI::setAngle(id, rot);
     127            0 :                 break;
     128              :             }
     129          300 :             case libsumo::VAR_SCREENSHOT: {
     130          300 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     131            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
     132              :                 }
     133          300 :                 int parameterCount = inputStorage.readInt();
     134          300 :                 if (parameterCount != 3) {
     135            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires three values as parameter.", outputStorage);
     136              :                 }
     137              :                 std::string filename;
     138          300 :                 if (!server.readTypeCheckingString(inputStorage, filename)) {
     139            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a file name.", outputStorage);
     140              :                 }
     141          300 :                 int width = 0, height = 0;
     142          300 :                 if (!server.readTypeCheckingInt(inputStorage, width)) {
     143            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the width given as int.", outputStorage);
     144              :                 }
     145          300 :                 if (!server.readTypeCheckingInt(inputStorage, height)) {
     146            0 :                     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          300 :                 libsumo::GUI::screenshot(id, filename, width, height);
     150              :                 break;
     151              :             }
     152              :             case libsumo::VAR_TRACK_VEHICLE: {
     153              :                 std::string objID;
     154            0 :                 if (!server.readTypeCheckingString(inputStorage, objID)) {
     155            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
     156              :                 }
     157            0 :                 libsumo::GUI::trackVehicle(id, objID);
     158              :                 break;
     159              :             }
     160            0 :             case libsumo::ADD: {
     161            0 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     162            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires a compound object.", outputStorage);
     163              :                 }
     164            0 :                 int parameterCount = inputStorage.readInt();
     165            0 :                 if (parameterCount != 2) {
     166            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Adding a view requires two values as parameter.", outputStorage);
     167              :                 }
     168              :                 std::string scheme;
     169            0 :                 if (!server.readTypeCheckingString(inputStorage, scheme)) {
     170            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The first variable must be a scheme name.", outputStorage);
     171              :                 }
     172              :                 int viewType;
     173            0 :                 if (!server.readTypeCheckingInt(inputStorage, viewType)) {
     174            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "The second variable must be the view type given as int.", outputStorage);
     175              :                 }
     176            0 :                 libsumo::GUI::addView(id, scheme,
     177            0 :                                       viewType == 1 ?  GUISUMOViewParent::VIEW_3D_OSG : GUISUMOViewParent::VIEW_2D_OPENGL);
     178              :                 break;
     179              :             }
     180            0 :             case libsumo::REMOVE: {
     181            0 :                 libsumo::GUI::removeView(id);
     182              :                 break;
     183              :             }
     184              :             default:
     185              :                 break;
     186              :         }
     187            0 :     } catch (libsumo::TraCIException& e) {
     188            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, e.what(), outputStorage);
     189            0 :     }
     190          300 :     server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     191              :     return true;
     192              : }
     193              : 
     194              : 
     195              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1