LCOV - code coverage report
Current view: top level - src/gui - TraCIServerAPI_GUI.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 60 90 66.7 %
Date: 2024-05-18 15:37:58 Functions: 2 2 100.0 %

          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         880 : TraCIServerAPI_GUI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      37             :                                tcpip::Storage& outputStorage) {
      38         880 :     const int variable = inputStorage.readUnsignedByte();
      39         880 :     const std::string id = inputStorage.readString();
      40         880 :     server.initWrapper(libsumo::RESPONSE_GET_GUI_VARIABLE, variable, id);
      41             :     try {
      42         880 :         if (!libsumo::GUI::handleVariable(id, variable, &server, &inputStorage)) {
      43          16 :             switch (variable) {
      44             :                 case libsumo::VAR_SELECT: {
      45             :                     std::string objType;
      46          16 :                     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          24 :                     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         880 :     server.writeStatusCmd(libsumo::CMD_GET_GUI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      60         880 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      61             :     return true;
      62             : }
      63             : 
      64             : 
      65             : bool
      66         448 : TraCIServerAPI_GUI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      67             :                                tcpip::Storage& outputStorage) {
      68         448 :     std::string warning = ""; // additional description for response
      69         448 :     const int variable = inputStorage.readUnsignedByte();
      70         448 :     if (variable != libsumo::VAR_VIEW_ZOOM && variable != libsumo::VAR_VIEW_OFFSET
      71         448 :             && variable != libsumo::VAR_VIEW_SCHEMA && variable != libsumo::VAR_VIEW_BOUNDARY
      72         448 :             && variable != libsumo::VAR_SCREENSHOT && variable != libsumo::VAR_TRACK_VEHICLE
      73          20 :             && variable != libsumo::VAR_SELECT && variable != libsumo::VAR_ANGLE
      74           8 :             && 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         448 :     const std::string id = inputStorage.readString();
      79             :     try {
      80         448 :         switch (variable) {
      81           4 :             case libsumo::VAR_VIEW_ZOOM: {
      82           4 :                 double zoom = 0.;
      83           4 :                 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           4 :                 libsumo::GUI::setZoom(id, zoom);
      87           4 :                 break;
      88             :             }
      89           4 :             case libsumo::VAR_VIEW_OFFSET: {
      90           4 :                 libsumo::TraCIPosition tp;
      91           4 :                 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           4 :                 libsumo::GUI::setOffset(id, tp.x, tp.y);
      95             :                 break;
      96             :             }
      97             :             case libsumo::VAR_SELECT: {
      98             :                 std::string objType;
      99           8 :                 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           8 :                 libsumo::GUI::toggleSelection(id, objType);
     103             :                 break;
     104             :             }
     105             :             case libsumo::VAR_VIEW_SCHEMA: {
     106             :                 std::string schema;
     107           8 :                 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           8 :                 libsumo::GUI::setSchema(id, schema);
     111             :                 break;
     112             :             }
     113           4 :             case libsumo::VAR_VIEW_BOUNDARY: {
     114           4 :                 PositionVector p;
     115           4 :                 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           4 :                 libsumo::GUI::setBoundary(id, p[0].x(), p[0].y(), p[1].x(), p[1].y());
     119             :                 break;
     120           4 :             }
     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         408 :             case libsumo::VAR_SCREENSHOT: {
     130         408 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     131           0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Screenshot requires a compound object.", outputStorage);
     132             :                 }
     133         408 :                 int parameterCount = inputStorage.readInt();
     134         408 :                 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         408 :                 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         408 :                 int width = 0, height = 0;
     142         408 :                 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         408 :                 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         408 :                 libsumo::GUI::screenshot(id, filename, width, height);
     150             :                 break;
     151             :             }
     152             :             case libsumo::VAR_TRACK_VEHICLE: {
     153             :                 std::string objID;
     154           4 :                 if (!server.readTypeCheckingString(inputStorage, objID)) {
     155           0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, "Tracking requires a string ID.", outputStorage);
     156             :                 }
     157           4 :                 libsumo::GUI::trackVehicle(id, objID);
     158             :                 break;
     159             :             }
     160           4 :             case libsumo::ADD: {
     161           4 :                 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           4 :                 int parameterCount = inputStorage.readInt();
     165           4 :                 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           4 :                 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           4 :                 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           4 :                 libsumo::GUI::addView(id, scheme,
     177           4 :                                       viewType == 1 ?  GUISUMOViewParent::VIEW_3D_OSG : GUISUMOViewParent::VIEW_2D_OPENGL);
     178             :                 break;
     179             :             }
     180           4 :             case libsumo::REMOVE: {
     181           4 :                 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         448 :     server.writeStatusCmd(libsumo::CMD_SET_GUI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     191             :     return true;
     192             : }
     193             : 
     194             : 
     195             : /****************************************************************************/

Generated by: LCOV version 1.14