Path: blob/main/src/traci-server/TraCIServerAPI_Edge.cpp
169665 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2025 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_Edge.cpp14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Jerome Haerri17/// @author Michael Behrisch18/// @author Laura Bieker19/// @author Mario Krumnow20/// @author Gregor Laemmel21/// @date Sept 200222///23// APIs for getting/setting edge values via TraCI24/****************************************************************************/25#include <config.h>2627#include <utils/common/StdDefs.h>28#include <microsim/MSNet.h>29#include <microsim/MSEdgeControl.h>30#include <microsim/MSEdge.h>31#include <microsim/MSLane.h>32#include <microsim/MSVehicle.h>33#include <microsim/transportables/MSPerson.h>34#include <libsumo/TraCIConstants.h>35#include "TraCIServerAPI_Edge.h"36#include <microsim/MSEdgeWeightsStorage.h>37#include <utils/emissions/HelpersHarmonoise.h>38#include <libsumo/StorageHelper.h>39#include <libsumo/Edge.h>404142// ===========================================================================43// method definitions44// ===========================================================================45bool46TraCIServerAPI_Edge::processGet(TraCIServer& server, tcpip::Storage& inputStorage,47tcpip::Storage& outputStorage) {48const int variable = inputStorage.readUnsignedByte();49const std::string id = inputStorage.readString();50server.initWrapper(libsumo::RESPONSE_GET_EDGE_VARIABLE, variable, id);51try {52if (!libsumo::Edge::handleVariable(id, variable, &server, &inputStorage)) {53return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE,54"Get Edge Variable: unsupported variable " + toHex(variable, 2)55+ " specified", outputStorage);56}57} catch (libsumo::TraCIException& e) {58return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);59}60server.writeStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);61server.writeResponseWithLength(outputStorage, server.getWrapperStorage());62return true;63}646566bool67TraCIServerAPI_Edge::processSet(TraCIServer& server, tcpip::Storage& inputStorage,68tcpip::Storage& outputStorage) {69std::string warning; // additional description for response70// variable71int variable = inputStorage.readUnsignedByte();72if (variable != libsumo::VAR_EDGE_TRAVELTIME73&& variable != libsumo::VAR_EDGE_EFFORT74&& variable != libsumo::VAR_MAXSPEED75&& variable != libsumo::LANE_ALLOWED76&& variable != libsumo::LANE_DISALLOWED77&& variable != libsumo::VAR_FRICTION78&& variable != libsumo::VAR_PARAMETER) {79return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,80"Change Edge State: unsupported variable " + toHex(variable, 2)81+ " specified", outputStorage);82}83// id84std::string id = inputStorage.readString();85try {86// process87switch (variable) {88case libsumo::LANE_ALLOWED: {89// read and set allowed vehicle classes90const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");91libsumo::Edge::setAllowed(id, classes);92break;93}94case libsumo::LANE_DISALLOWED: {95// read and set disallowed vehicle classes96const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");97libsumo::Edge::setDisallowed(id, classes);98break;99}100case libsumo::VAR_EDGE_TRAVELTIME: {101// read and set travel time102const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting travel time requires a compound object.");103if (parameterCount == 3) {104// bound by time105const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");106const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");107const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");108libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);109} else if (parameterCount == 1) {110// unbound111const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");112libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());113} else {114return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,115"Setting travel time requires either begin time, end time, and value, or only value as parameter.",116outputStorage);117}118break;119}120case libsumo::VAR_EDGE_EFFORT: {121// read and set effort122const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting effort requires a compound object.");123if (parameterCount == 3) {124// bound by time125const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");126const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");127const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");128libsumo::Edge::setEffort(id, value, begTime, endTime);129} else if (parameterCount == 1) {130// unbound131const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");132libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());133} else {134return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE,135"Setting effort requires either begin time, end time, and value, or only value as parameter.",136outputStorage);137}138break;139}140case libsumo::VAR_MAXSPEED: {141// read and set max. speed142const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");143libsumo::Edge::setMaxSpeed(id, value);144break;145}146case libsumo::VAR_FRICTION: {147// read and set friction for entire edge148const double value = StoHelp::readTypedDouble(inputStorage, "The friction must be given as a double.");149libsumo::Edge::setFriction(id, value);150break;151}152case libsumo::VAR_PARAMETER: {153// read and check item number154StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");155const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");156const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");157libsumo::Edge::setParameter(id, name, value);158break;159}160default:161break;162}163} catch (libsumo::TraCIException& e) {164return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);165}166server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);167return true;168}169170171/****************************************************************************/172173174