LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_Edge.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 84.4 % 64 54
Test Date: 2025-06-03 15:29:59 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) 2002-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_Edge.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Jerome Haerri
      18              : /// @author  Michael Behrisch
      19              : /// @author  Laura Bieker
      20              : /// @author  Mario Krumnow
      21              : /// @author  Gregor Laemmel
      22              : /// @date    Sept 2002
      23              : ///
      24              : // APIs for getting/setting edge values via TraCI
      25              : /****************************************************************************/
      26              : #include <config.h>
      27              : 
      28              : #include <utils/common/StdDefs.h>
      29              : #include <microsim/MSNet.h>
      30              : #include <microsim/MSEdgeControl.h>
      31              : #include <microsim/MSEdge.h>
      32              : #include <microsim/MSLane.h>
      33              : #include <microsim/MSVehicle.h>
      34              : #include <microsim/transportables/MSPerson.h>
      35              : #include <libsumo/TraCIConstants.h>
      36              : #include "TraCIServerAPI_Edge.h"
      37              : #include <microsim/MSEdgeWeightsStorage.h>
      38              : #include <utils/emissions/HelpersHarmonoise.h>
      39              : #include <libsumo/StorageHelper.h>
      40              : #include <libsumo/Edge.h>
      41              : 
      42              : 
      43              : // ===========================================================================
      44              : // method definitions
      45              : // ===========================================================================
      46              : bool
      47        14945 : TraCIServerAPI_Edge::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      48              :                                 tcpip::Storage& outputStorage) {
      49        14945 :     const int variable = inputStorage.readUnsignedByte();
      50        14945 :     const std::string id = inputStorage.readString();
      51        14945 :     server.initWrapper(libsumo::RESPONSE_GET_EDGE_VARIABLE, variable, id);
      52              :     try {
      53        14945 :         if (!libsumo::Edge::handleVariable(id, variable, &server, &inputStorage)) {
      54            5 :             return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE,
      55            5 :                                               "Get Edge Variable: unsupported variable " + toHex(variable, 2)
      56            5 :                                               + " specified", outputStorage);
      57              :         }
      58            2 :     } catch (libsumo::TraCIException& e) {
      59            2 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
      60            2 :     }
      61        14938 :     server.writeStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      62        14938 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      63              :     return true;
      64              : }
      65              : 
      66              : 
      67              : bool
      68          164 : TraCIServerAPI_Edge::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      69              :                                 tcpip::Storage& outputStorage) {
      70              :     std::string warning; // additional description for response
      71              :     // variable
      72          164 :     int variable = inputStorage.readUnsignedByte();
      73          164 :     if (variable != libsumo::VAR_EDGE_TRAVELTIME
      74          164 :             && variable != libsumo::VAR_EDGE_EFFORT
      75          164 :             && variable != libsumo::VAR_MAXSPEED
      76              :             && variable != libsumo::LANE_ALLOWED
      77           68 :             && variable != libsumo::LANE_DISALLOWED
      78           62 :             && variable != libsumo::VAR_FRICTION
      79           62 :             && variable != libsumo::VAR_PARAMETER) {
      80            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
      81            0 :                                           "Change Edge State: unsupported variable " + toHex(variable, 2)
      82            0 :                                           + " specified", outputStorage);
      83              :     }
      84              :     // id
      85          164 :     std::string id = inputStorage.readString();
      86              :     try {
      87              :         // process
      88          164 :         switch (variable) {
      89              :             case libsumo::LANE_ALLOWED: {
      90              :                 // read and set allowed vehicle classes
      91            6 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");
      92            6 :                 libsumo::Edge::setAllowed(id, classes);
      93              :                 break;
      94            6 :             }
      95              :             case libsumo::LANE_DISALLOWED: {
      96              :                 // read and set disallowed vehicle classes
      97            0 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");
      98            0 :                 libsumo::Edge::setDisallowed(id, classes);
      99              :                 break;
     100            0 :             }
     101              :             case libsumo::VAR_EDGE_TRAVELTIME: {
     102              :                 // read and set travel time
     103           71 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting travel time requires a compound object.");
     104           71 :                 if (parameterCount == 3) {
     105              :                     // bound by time
     106           15 :                     const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
     107           15 :                     const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
     108           15 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
     109           15 :                     libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
     110           56 :                 } else if (parameterCount == 1) {
     111              :                     // unbound
     112           56 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
     113           56 :                     libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
     114              :                 } else {
     115            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
     116              :                                                       "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
     117              :                                                       outputStorage);
     118              :                 }
     119              :                 break;
     120              :             }
     121              :             case libsumo::VAR_EDGE_EFFORT: {
     122              :                 // read and set effort
     123           12 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting effort requires a compound object.");
     124           12 :                 if (parameterCount == 3) {
     125              :                     // bound by time
     126            9 :                     const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
     127            9 :                     const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
     128            9 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
     129            9 :                     libsumo::Edge::setEffort(id, value, begTime, endTime);
     130            3 :                 } else if (parameterCount == 1) {
     131              :                     // unbound
     132            3 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
     133            3 :                     libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
     134              :                 } else {
     135            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
     136              :                                                       "Setting effort requires either begin time, end time, and value, or only value as parameter.",
     137              :                                                       outputStorage);
     138              :                 }
     139              :                 break;
     140              :             }
     141              :             case libsumo::VAR_MAXSPEED: {
     142              :                 // read and set max. speed
     143           13 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");
     144           13 :                 libsumo::Edge::setMaxSpeed(id, value);
     145              :                 break;
     146              :             }
     147              :             case libsumo::VAR_FRICTION: {
     148              :                 // read and set friction for entire edge
     149            0 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The friction must be given as a double.");
     150            0 :                 libsumo::Edge::setFriction(id, value);
     151              :                 break;
     152              :             }
     153              :             case libsumo::VAR_PARAMETER: {
     154              :                 // read and check item number
     155           62 :                 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
     156           62 :                 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
     157           62 :                 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
     158           62 :                 libsumo::Edge::setParameter(id, name, value);
     159              :                 break;
     160              :             }
     161              :             default:
     162              :                 break;
     163              :         }
     164            2 :     } catch (libsumo::TraCIException& e) {
     165            2 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
     166            2 :     }
     167          162 :     server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     168              :     return true;
     169              : }
     170              : 
     171              : 
     172              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1