Path: blob/main/src/traci-server/TraCIServerAPI_Person.cpp
193674 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file TraCIServerAPI_Person.cpp14/// @author Daniel Krajzewicz15/// @date 26.05.201416///17// APIs for getting/setting person values via TraCI18/****************************************************************************/19#include <config.h>2021#include <utils/common/StringTokenizer.h>22#include <microsim/transportables/MSTransportableControl.h>23#include <microsim/MSVehicleControl.h>24#include <microsim/transportables/MSPerson.h>25#include <microsim/MSNet.h>26#include <microsim/MSEdge.h>27#include <libsumo/Person.h>28#include <libsumo/StorageHelper.h>29#include <libsumo/TraCIConstants.h>30#include <libsumo/VehicleType.h>31#include "TraCIServer.h"32#include "TraCIServerAPI_VehicleType.h"33#include "TraCIServerAPI_Person.h"34#include "TraCIServerAPI_Simulation.h"353637// ===========================================================================38// method definitions39// ===========================================================================40bool41TraCIServerAPI_Person::processSet(TraCIServer& server, tcpip::Storage& inputStorage,42tcpip::Storage& outputStorage) {43std::string warning = ""; // additional description for response44// variable45int variable = inputStorage.readUnsignedByte();46if (variable != libsumo::VAR_PARAMETER47&& variable != libsumo::ADD48&& variable != libsumo::REMOVE49&& variable != libsumo::APPEND_STAGE50&& variable != libsumo::REPLACE_STAGE51&& variable != libsumo::REMOVE_STAGE52&& variable != libsumo::CMD_REROUTE_TRAVELTIME53&& variable != libsumo::VAR_MOVE_TO54&& variable != libsumo::MOVE_TO_XY55&& variable != libsumo::VAR_SPEED56&& variable != libsumo::VAR_TYPE57&& variable != libsumo::VAR_SPEED_FACTOR58&& variable != libsumo::VAR_LENGTH59&& variable != libsumo::VAR_WIDTH60&& variable != libsumo::VAR_HEIGHT61&& variable != libsumo::VAR_MINGAP62&& variable != libsumo::VAR_COLOR63) {64return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);65}6667try {68// TODO: remove declaration of c after completion69MSTransportableControl& c = MSNet::getInstance()->getPersonControl();70// id71std::string id = inputStorage.readString();72// TODO: remove declaration of p after completion73const bool shouldExist = variable != libsumo::ADD;74MSTransportable* p = c.get(id);75if (p == nullptr && shouldExist) {76return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);77}78// process79switch (variable) {80case libsumo::VAR_SPEED: {81// set the speed for all present and future (walking) stages and modify the vType so that stages added later are also affected82libsumo::Person::setSpeed(id, StoHelp::readTypedDouble(inputStorage, "Setting speed requires a double."));83}84break;85case libsumo::VAR_TYPE: {86libsumo::Person::setType(id, StoHelp::readTypedString(inputStorage, "The vehicle type id must be given as a string."));87break;88}89case libsumo::VAR_SPEED_FACTOR: {90libsumo::Person::setSpeedFactor(id, StoHelp::readTypedDouble(inputStorage, "Setting SpeedFactor requires a double."));91}92break;93case libsumo::VAR_COLOR: {94libsumo::Person::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));95break;96}97case libsumo::ADD: {98StoHelp::readCompound(inputStorage, 4, "Adding a person needs four parameters.");99const std::string vTypeID = StoHelp::readTypedString(inputStorage, "First parameter (type) requires a string.");100const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");101const double depart = StoHelp::readTypedDouble(inputStorage, "Third parameter (depart) requires a double.");102const double pos = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (position) requires a double.");103libsumo::Person::add(id, edgeID, pos, depart, vTypeID);104}105break;106case libsumo::REMOVE: {107libsumo::Person::remove(id, (char)StoHelp::readTypedByte(inputStorage, "Removing a person requires a byte."));108}109break;110case libsumo::APPEND_STAGE: {111const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Adding a person stage requires a compound object.");112if (parameterCount == 13) {113libsumo::TraCIStage stage;114StoHelp::readStage(inputStorage, stage);115libsumo::Person::appendStage(id, stage);116} else {117const int stageType = StoHelp::readTypedInt(inputStorage, "The first parameter for adding a stage must be the stage type given as int.");118if (stageType == libsumo::STAGE_DRIVING) {119// append driving stage120if (parameterCount != 4) {121return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a driving stage needs four parameters.", outputStorage);122}123const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");124const std::string lines = StoHelp::readTypedString(inputStorage, "Third parameter (lines) requires a string.");125const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");126libsumo::Person::appendDrivingStage(id, edgeID, lines, stopID);127} else if (stageType == libsumo::STAGE_WAITING) {128// append waiting stage129if (parameterCount != 4) {130return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a waiting stage needs four parameters.", outputStorage);131}132const double duration = StoHelp::readTypedDouble(inputStorage, "Second parameter (duration) requires a double.");133const std::string description = StoHelp::readTypedString(inputStorage, "Third parameter (description) requires a string.");134const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");135libsumo::Person::appendWaitingStage(id, duration, description, stopID);136} else if (stageType == libsumo::STAGE_WALKING) {137// append walking stage138if (parameterCount != 6) {139return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a walking stage needs six parameters.", outputStorage);140}141const std::vector<std::string> edgeIDs = StoHelp::readTypedStringList(inputStorage, "Second parameter (edges) route must be defined as a list of edge ids.");142const double arrivalPos = StoHelp::readTypedDouble(inputStorage, "Third parameter (arrivalPos) requires a double.");143const double duration = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (duration) requires a double.");144const double speed = StoHelp::readTypedDouble(inputStorage, "Fifth parameter (speed) requires a double.");145const std::string stopID = StoHelp::readTypedString(inputStorage, "Sixth parameter (stopID) requires a string.");146libsumo::Person::appendWalkingStage(id, edgeIDs, arrivalPos, duration, speed, stopID);147} else {148return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Invalid stage type for person '" + id + "'", outputStorage);149}150}151}152break;153case libsumo::REPLACE_STAGE: {154StoHelp::readCompound(inputStorage, 2, "Replacing a person stage requires a compound object of size 2.");155const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "First parameter of replace stage should be an integer");156StoHelp::readCompound(inputStorage, 13, "Second parameter of replace stage should be a compound object of size 13");157libsumo::TraCIStage stage;158StoHelp::readStage(inputStorage, stage);159libsumo::Person::replaceStage(id, nextStageIndex, stage);160}161break;162case libsumo::REMOVE_STAGE: {163libsumo::Person::removeStage(id, StoHelp::readTypedInt(inputStorage, "The message must contain the stage index."));164}165break;166case libsumo::CMD_REROUTE_TRAVELTIME: {167StoHelp::readCompound(inputStorage, 0, "Resuming requires an empty compound object.");168libsumo::Person::rerouteTraveltime(id);169}170break;171case libsumo::VAR_MOVE_TO: {172StoHelp::readCompound(inputStorage, 3, "Setting position should obtain the edge id, the position and the lateral position.");173const std::string laneID = StoHelp::readTypedString(inputStorage, "The first parameter for setting a position must be the laneID given as a string.");174const double position = StoHelp::readTypedDouble(inputStorage, "The second parameter for setting a position must be the position given as a double.");175const double posLat = StoHelp::readTypedDouble(inputStorage, "The third parameter for setting a position must be the lateral position given as a double.");176libsumo::Person::moveTo(id, laneID, position, posLat);177}178break;179case libsumo::MOVE_TO_XY: {180const int parameterCount = StoHelp::readCompound(inputStorage, -1, "MoveToXY person requires a compound object.");181if (parameterCount != 5 && parameterCount != 6) {182return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person should obtain: edgeID, x, y, angle, keepRouteFlag and optionally matchThreshold.", outputStorage);183}184const std::string edgeID = StoHelp::readTypedString(inputStorage, "The first parameter for moveToXY must be the edge ID given as a string.");185const double x = StoHelp::readTypedDouble(inputStorage, "The second parameter for moveToXY must be the x-position given as a double.");186const double y = StoHelp::readTypedDouble(inputStorage, "The third parameter for moveToXY must be the y-position given as a double.");187const double angle = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for moveToXY must be the angle given as a double.");188const int keepRouteFlag = StoHelp::readTypedByte(inputStorage, "The fifth parameter for moveToXY must be the keepRouteFlag given as a byte.");189double matchThreshold = 100.;190if (parameterCount == 6) {191matchThreshold = StoHelp::readTypedDouble(inputStorage, "The sixth parameter for moveToXY must be the matchThreshold given as a double.");192}193libsumo::Person::moveToXY(id, edgeID, x, y, angle, keepRouteFlag, matchThreshold);194}195break;196case libsumo::VAR_PARAMETER: {197StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");198const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");199const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");200libsumo::Person::setParameter(id, name, value);201}202break;203default:204try {205if (!TraCIServerAPI_VehicleType::setVariable(libsumo::CMD_SET_PERSON_VARIABLE, variable, p->getSingularType().getID(), server, inputStorage, outputStorage)) {206return false;207}208} catch (ProcessError& e) {209return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);210}211break;212}213} catch (libsumo::TraCIException& e) {214return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);215}216server.writeStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);217return true;218}219220221/****************************************************************************/222223224