LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_Person.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 91.7 % 120 110
Test Date: 2025-12-06 15:35:27 Functions: 100.0 % 1 1

            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_Person.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @date    26.05.2014
      17              : ///
      18              : // APIs for getting/setting person values via TraCI
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include <utils/common/StringTokenizer.h>
      23              : #include <microsim/transportables/MSTransportableControl.h>
      24              : #include <microsim/MSVehicleControl.h>
      25              : #include <microsim/transportables/MSPerson.h>
      26              : #include <microsim/MSNet.h>
      27              : #include <microsim/MSEdge.h>
      28              : #include <libsumo/Person.h>
      29              : #include <libsumo/StorageHelper.h>
      30              : #include <libsumo/TraCIConstants.h>
      31              : #include <libsumo/VehicleType.h>
      32              : #include "TraCIServer.h"
      33              : #include "TraCIServerAPI_VehicleType.h"
      34              : #include "TraCIServerAPI_Person.h"
      35              : #include "TraCIServerAPI_Simulation.h"
      36              : 
      37              : 
      38              : // ===========================================================================
      39              : // method definitions
      40              : // ===========================================================================
      41              : bool
      42         2668 : TraCIServerAPI_Person::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      43              :                                   tcpip::Storage& outputStorage) {
      44         2668 :     std::string warning = ""; // additional description for response
      45              :     // variable
      46         2668 :     int variable = inputStorage.readUnsignedByte();
      47         2668 :     if (variable != libsumo::VAR_PARAMETER
      48         2668 :             && variable != libsumo::ADD
      49         2484 :             && variable != libsumo::REMOVE
      50         2484 :             && variable != libsumo::APPEND_STAGE
      51         2308 :             && variable != libsumo::REPLACE_STAGE
      52         2308 :             && variable != libsumo::REMOVE_STAGE
      53         2195 :             && variable != libsumo::CMD_REROUTE_TRAVELTIME
      54         2195 :             && variable != libsumo::VAR_MOVE_TO
      55         1603 :             && variable != libsumo::MOVE_TO_XY
      56         1603 :             && variable != libsumo::VAR_SPEED
      57           48 :             && variable != libsumo::VAR_TYPE
      58           48 :             && variable != libsumo::VAR_SPEED_FACTOR
      59           20 :             && variable != libsumo::VAR_LENGTH
      60           20 :             && variable != libsumo::VAR_WIDTH
      61           12 :             && variable != libsumo::VAR_HEIGHT
      62           12 :             && variable != libsumo::VAR_MINGAP
      63            4 :             && variable != libsumo::VAR_COLOR
      64              :        ) {
      65            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      66              :     }
      67              : 
      68              :     try {
      69              :         // TODO: remove declaration of c after completion
      70         2668 :         MSTransportableControl& c = MSNet::getInstance()->getPersonControl();
      71              :         // id
      72         2668 :         std::string id = inputStorage.readString();
      73              :         // TODO: remove declaration of p after completion
      74              :         const bool shouldExist = variable != libsumo::ADD;
      75         2668 :         MSTransportable* p = c.get(id);
      76         2668 :         if (p == nullptr && shouldExist) {
      77            0 :             return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
      78              :         }
      79              :         // process
      80         2668 :         switch (variable) {
      81              :             case libsumo::VAR_SPEED: {
      82              :                 // set the speed for all present and future (walking) stages and modify the vType so that stages added later are also affected
      83           10 :                 libsumo::Person::setSpeed(id, StoHelp::readTypedDouble(inputStorage, "Setting speed requires a double."));
      84              :             }
      85           10 :             break;
      86              :             case libsumo::VAR_TYPE: {
      87           10 :                 libsumo::Person::setType(id, StoHelp::readTypedString(inputStorage, "The vehicle type id must be given as a string."));
      88           10 :                 break;
      89              :             }
      90              :             case libsumo::VAR_SPEED_FACTOR: {
      91           18 :                 libsumo::Person::setSpeedFactor(id, StoHelp::readTypedDouble(inputStorage, "Setting SpeedFactor requires a double."));
      92              :             }
      93           18 :             break;
      94              :             case libsumo::VAR_COLOR: {
      95            4 :                 libsumo::Person::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
      96            4 :                 break;
      97              :             }
      98              :             case libsumo::ADD: {
      99          115 :                 StoHelp::readCompound(inputStorage, 4, "Adding a person needs four parameters.");
     100          115 :                 const std::string vTypeID = StoHelp::readTypedString(inputStorage, "First parameter (type) requires a string.");
     101          115 :                 const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
     102          115 :                 const double depart = StoHelp::readTypedDouble(inputStorage, "Third parameter (depart) requires a double.");
     103          230 :                 const double pos = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (position) requires a double.");
     104          230 :                 libsumo::Person::add(id, edgeID, pos, depart, vTypeID);
     105              :             }
     106          115 :             break;
     107              :             case libsumo::REMOVE: {
     108           15 :                 libsumo::Person::remove(id, (char)StoHelp::readTypedByte(inputStorage, "Removing a person requires a byte."));
     109              :             }
     110           15 :             break;
     111              :             case libsumo::APPEND_STAGE: {
     112          161 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Adding a person stage requires a compound object.");
     113          161 :                 if (parameterCount == 13) {
     114           44 :                     libsumo::TraCIStage stage;
     115           22 :                     StoHelp::readStage(inputStorage, stage);
     116           22 :                     libsumo::Person::appendStage(id, stage);
     117           22 :                 } else {
     118          139 :                     const int stageType = StoHelp::readTypedInt(inputStorage, "The first parameter for adding a stage must be the stage type given as int.");
     119          139 :                     if (stageType == libsumo::STAGE_DRIVING) {
     120              :                         // append driving stage
     121           22 :                         if (parameterCount != 4) {
     122            0 :                             return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a driving stage needs four parameters.", outputStorage);
     123              :                         }
     124           22 :                         const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
     125           22 :                         const std::string lines = StoHelp::readTypedString(inputStorage, "Third parameter (lines) requires a string.");
     126           22 :                         const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
     127           22 :                         libsumo::Person::appendDrivingStage(id, edgeID, lines, stopID);
     128          117 :                     } else if (stageType == libsumo::STAGE_WAITING) {
     129              :                         // append waiting stage
     130           16 :                         if (parameterCount != 4) {
     131            0 :                             return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a waiting stage needs four parameters.", outputStorage);
     132              :                         }
     133           16 :                         const double duration = StoHelp::readTypedDouble(inputStorage, "Second parameter (duration) requires a double.");
     134           16 :                         const std::string description = StoHelp::readTypedString(inputStorage, "Third parameter (description) requires a string.");
     135           16 :                         const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
     136           16 :                         libsumo::Person::appendWaitingStage(id, duration, description, stopID);
     137          101 :                     } else if (stageType == libsumo::STAGE_WALKING) {
     138              :                         // append walking stage
     139          101 :                         if (parameterCount != 6) {
     140            0 :                             return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a walking stage needs six parameters.", outputStorage);
     141              :                         }
     142          101 :                         const std::vector<std::string> edgeIDs = StoHelp::readTypedStringList(inputStorage, "Second parameter (edges) route must be defined as a list of edge ids.");
     143          101 :                         const double arrivalPos = StoHelp::readTypedDouble(inputStorage, "Third parameter (arrivalPos) requires a double.");
     144          101 :                         const double duration = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (duration) requires a double.");
     145          101 :                         const double speed = StoHelp::readTypedDouble(inputStorage, "Fifth parameter (speed) requires a double.");
     146          101 :                         const std::string stopID = StoHelp::readTypedString(inputStorage, "Sixth parameter (stopID) requires a string.");
     147          101 :                         libsumo::Person::appendWalkingStage(id, edgeIDs, arrivalPos, duration, speed, stopID);
     148          101 :                     } else {
     149            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Invalid stage type for person '" + id + "'", outputStorage);
     150              :                     }
     151              :                 }
     152              :             }
     153              :             break;
     154              :             case libsumo::REPLACE_STAGE: {
     155            6 :                 StoHelp::readCompound(inputStorage, 2, "Replacing a person stage requires a compound object of size 2.");
     156            6 :                 const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "First parameter of replace stage should be an integer");
     157            6 :                 StoHelp::readCompound(inputStorage, 13, "Second parameter of replace stage should be a compound object of size 13");
     158           12 :                 libsumo::TraCIStage stage;
     159            6 :                 StoHelp::readStage(inputStorage, stage);
     160            6 :                 libsumo::Person::replaceStage(id, nextStageIndex, stage);
     161            6 :             }
     162            6 :             break;
     163              :             case libsumo::REMOVE_STAGE: {
     164          107 :                 libsumo::Person::removeStage(id, StoHelp::readTypedInt(inputStorage, "The message must contain the stage index."));
     165              :             }
     166          107 :             break;
     167              :             case libsumo::CMD_REROUTE_TRAVELTIME: {
     168          580 :                 StoHelp::readCompound(inputStorage, 0, "Resuming requires an empty compound object.");
     169          580 :                 libsumo::Person::rerouteTraveltime(id);
     170              :             }
     171              :             break;
     172              :             case libsumo::VAR_MOVE_TO: {
     173           12 :                 StoHelp::readCompound(inputStorage, 3, "Setting position should obtain the edge id, the position and the lateral position.");
     174           12 :                 const std::string laneID = StoHelp::readTypedString(inputStorage, "The first parameter for setting a position must be the laneID given as a string.");
     175           12 :                 const double position = StoHelp::readTypedDouble(inputStorage, "The second parameter for setting a position must be the position given as a double.");
     176           12 :                 const double posLat = StoHelp::readTypedDouble(inputStorage, "The third parameter for setting a position must be the lateral position given as a double.");
     177           12 :                 libsumo::Person::moveTo(id, laneID, position, posLat);
     178              :             }
     179           12 :             break;
     180              :             case libsumo::MOVE_TO_XY: {
     181         1545 :                 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "MoveToXY person requires a compound object.");
     182         1545 :                 if (parameterCount != 5 && parameterCount != 6) {
     183            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person should obtain: edgeID, x, y, angle, keepRouteFlag and optionally matchThreshold.", outputStorage);
     184              :                 }
     185         1545 :                 const std::string edgeID = StoHelp::readTypedString(inputStorage, "The first parameter for moveToXY must be the edge ID given as a string.");
     186         1545 :                 const double x = StoHelp::readTypedDouble(inputStorage, "The second parameter for moveToXY must be the x-position given as a double.");
     187         1545 :                 const double y = StoHelp::readTypedDouble(inputStorage, "The third parameter for moveToXY must be the y-position given as a double.");
     188         1545 :                 const double angle = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for moveToXY must be the angle given as a double.");
     189         1545 :                 const int keepRouteFlag = StoHelp::readTypedByte(inputStorage, "The fifth parameter for moveToXY must be the keepRouteFlag given as a byte.");
     190              :                 double matchThreshold = 100.;
     191         1545 :                 if (parameterCount == 6) {
     192         3093 :                     matchThreshold = StoHelp::readTypedDouble(inputStorage, "The sixth parameter for moveToXY must be the matchThreshold given as a double.");
     193              :                 }
     194         1545 :                 libsumo::Person::moveToXY(id, edgeID, x, y, angle, keepRouteFlag, matchThreshold);
     195              :             }
     196         1542 :             break;
     197              :             case libsumo::VAR_PARAMETER: {
     198           69 :                 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
     199           75 :                 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
     200           72 :                 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
     201           69 :                 libsumo::Person::setParameter(id, name, value);
     202              :             }
     203           66 :             break;
     204           16 :             default:
     205              :                 try {
     206           16 :                     if (!TraCIServerAPI_VehicleType::setVariable(libsumo::CMD_SET_PERSON_VARIABLE, variable, p->getSingularType().getID(), server, inputStorage, outputStorage)) {
     207              :                         return false;
     208              :                     }
     209            0 :                 } catch (ProcessError& e) {
     210            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
     211            0 :                 }
     212              :                 break;
     213              :         }
     214            6 :     } catch (libsumo::TraCIException& e) {
     215            6 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
     216            6 :     }
     217         2662 :     server.writeStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     218              :     return true;
     219              : }
     220              : 
     221              : 
     222              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1