LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_Edge.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 60 70 85.7 %
Date: 2024-05-08 15:29:52 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) 2002-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_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       22698 : TraCIServerAPI_Edge::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      48             :                                 tcpip::Storage& outputStorage) {
      49       22698 :     const int variable = inputStorage.readUnsignedByte();
      50       22698 :     const std::string id = inputStorage.readString();
      51       22698 :     server.initWrapper(libsumo::RESPONSE_GET_EDGE_VARIABLE, variable, id);
      52             :     try {
      53       22698 :         if (!libsumo::Edge::handleVariable(id, variable, &server, &inputStorage)) {
      54         132 :             switch (variable) {
      55             :                 case libsumo::VAR_EDGE_TRAVELTIME: {
      56          89 :                     const double time = StoHelp::readTypedDouble(inputStorage, "The message must contain the time definition.");
      57          89 :                     StoHelp::writeTypedDouble(server.getWrapperStorage(), libsumo::Edge::getAdaptedTraveltime(id, time));
      58             :                     break;
      59             :                 }
      60             :                 case libsumo::VAR_EDGE_EFFORT: {
      61          41 :                     const double time = StoHelp::readTypedDouble(inputStorage, "The message must contain the time definition.");
      62          41 :                     StoHelp::writeTypedDouble(server.getWrapperStorage(), libsumo::Edge::getEffort(id, time));
      63             :                     break;
      64             :                 }
      65           2 :                 default:
      66           2 :                     return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE,
      67           4 :                                                       "Get Edge Variable: unsupported variable " + toHex(variable, 2)
      68           4 :                                                       + " specified", outputStorage);
      69             :             }
      70             :         }
      71           2 :     } catch (libsumo::TraCIException& e) {
      72           2 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
      73           2 :     }
      74       22694 :     server.writeStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      75       22694 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      76             :     return true;
      77             : }
      78             : 
      79             : 
      80             : bool
      81         168 : TraCIServerAPI_Edge::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      82             :                                 tcpip::Storage& outputStorage) {
      83             :     std::string warning; // additional description for response
      84             :     // variable
      85         168 :     int variable = inputStorage.readUnsignedByte();
      86         168 :     if (variable != libsumo::VAR_EDGE_TRAVELTIME
      87         168 :             && variable != libsumo::VAR_EDGE_EFFORT
      88         168 :             && variable != libsumo::VAR_MAXSPEED
      89             :             && variable != libsumo::LANE_ALLOWED
      90          56 :             && variable != libsumo::LANE_DISALLOWED
      91          48 :             && variable != libsumo::VAR_FRICTION
      92          48 :             && variable != libsumo::VAR_PARAMETER) {
      93           0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
      94           0 :                                           "Change Edge State: unsupported variable " + toHex(variable, 2)
      95           0 :                                           + " specified", outputStorage);
      96             :     }
      97             :     // id
      98         168 :     std::string id = inputStorage.readString();
      99             :     try {
     100             :         // process
     101         168 :         switch (variable) {
     102             :             case libsumo::LANE_ALLOWED: {
     103             :                 // read and set allowed vehicle classes
     104           8 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");
     105           8 :                 libsumo::Edge::setAllowed(id, classes);
     106             :                 break;
     107           8 :             }
     108             :             case libsumo::LANE_DISALLOWED: {
     109             :                 // read and set disallowed vehicle classes
     110           0 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");
     111           0 :                 libsumo::Edge::setDisallowed(id, classes);
     112             :                 break;
     113           0 :             }
     114             :             case libsumo::VAR_EDGE_TRAVELTIME: {
     115             :                 // read and set travel time
     116          79 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting travel time requires a compound object.");
     117          79 :                 if (parameterCount == 3) {
     118             :                     // bound by time
     119          20 :                     const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
     120          20 :                     const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
     121          20 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
     122          20 :                     libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
     123          59 :                 } else if (parameterCount == 1) {
     124             :                     // unbound
     125          59 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
     126          59 :                     libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
     127             :                 } else {
     128           0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
     129             :                                                       "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
     130             :                                                       outputStorage);
     131             :                 }
     132             :                 break;
     133             :             }
     134             :             case libsumo::VAR_EDGE_EFFORT: {
     135             :                 // read and set effort
     136          16 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting effort requires a compound object.");
     137          16 :                 if (parameterCount == 3) {
     138             :                     // bound by time
     139          12 :                     const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
     140          12 :                     const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
     141          12 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
     142          12 :                     libsumo::Edge::setEffort(id, value, begTime, endTime);
     143           4 :                 } else if (parameterCount == 1) {
     144             :                     // unbound
     145           4 :                     const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
     146           4 :                     libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
     147             :                 } else {
     148           0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,
     149             :                                                       "Setting effort requires either begin time, end time, and value, or only value as parameter.",
     150             :                                                       outputStorage);
     151             :                 }
     152             :                 break;
     153             :             }
     154             :             case libsumo::VAR_MAXSPEED: {
     155             :                 // read and set max. speed
     156          17 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");
     157          17 :                 libsumo::Edge::setMaxSpeed(id, value);
     158             :                 break;
     159             :             }
     160             :             case libsumo::VAR_FRICTION: {
     161             :                 // read and set friction for entire edge
     162           0 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The friction must be given as a double.");
     163           0 :                 libsumo::Edge::setFriction(id, value);
     164             :                 break;
     165             :             }
     166             :             case libsumo::VAR_PARAMETER: {
     167             :                 // read and check item number
     168          48 :                 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
     169          48 :                 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
     170          48 :                 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
     171          48 :                 libsumo::Edge::setParameter(id, name, value);
     172             :                 break;
     173             :             }
     174             :             default:
     175             :                 break;
     176             :         }
     177           2 :     } catch (libsumo::TraCIException& e) {
     178           2 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
     179           2 :     }
     180         166 :     server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     181             :     return true;
     182             : }
     183             : 
     184             : 
     185             : /****************************************************************************/

Generated by: LCOV version 1.14