Path: blob/main/src/traci-server/TraCIServerAPI_Calibrator.cpp
169665 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-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_Calibrator.cpp14/// @author Jakob Erdmann15/// @date 16.03.202016///17// APIs for getting/setting Calibrator values via TraCI18/****************************************************************************/19#include <config.h>2021#include <microsim/MSNet.h>22#include <microsim/MSEdge.h>23#include <microsim/trigger/MSCalibrator.h>24#include <libsumo/Calibrator.h>25#include <libsumo/TraCIConstants.h>26#include <libsumo/StorageHelper.h>27#include "TraCIServerAPI_Calibrator.h"282930// ===========================================================================31// method definitions32// ===========================================================================33bool34TraCIServerAPI_Calibrator::processGet(TraCIServer& server, tcpip::Storage& inputStorage,35tcpip::Storage& outputStorage) {36const int variable = inputStorage.readUnsignedByte();37const std::string id = inputStorage.readString();38server.initWrapper(libsumo::RESPONSE_GET_CALIBRATOR_VARIABLE, variable, id);39try {40if (!libsumo::Calibrator::handleVariable(id, variable, &server, &inputStorage)) {41return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, "Get Calibrator Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);42}43} catch (libsumo::TraCIException& e) {44return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, e.what(), outputStorage);45}46server.writeStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);47server.writeResponseWithLength(outputStorage, server.getWrapperStorage());48return true;49}505152bool53TraCIServerAPI_Calibrator::processSet(TraCIServer& server, tcpip::Storage& inputStorage,54tcpip::Storage& outputStorage) {55std::string warning = ""; // additional description for response56// variable57int variable = inputStorage.readUnsignedByte();58if (variable != libsumo::CMD_SET_FLOW && variable != libsumo::VAR_PARAMETER) {59return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Change Calibrator State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);60}61// id62std::string id = inputStorage.readString();6364try {65// process66switch (variable) {67case libsumo::CMD_SET_FLOW: {68StoHelp::readCompound(inputStorage, 8, "A compound object of size 8 is needed for setting calibrator flow.");69const double begin = StoHelp::readTypedDouble(inputStorage, "Setting flow requires the begin time as the first (double) value.");70const double end = StoHelp::readTypedDouble(inputStorage, "Setting flow requires the end time as the second (double) value.");71const double vehsPerHour = StoHelp::readTypedDouble(inputStorage, "Setting flow requires the number of vehicles per hour as the third (double) value.");72const double speed = StoHelp::readTypedDouble(inputStorage, "Setting flow requires the speed as the fourth (double) value.");73const std::string typeID = StoHelp::readTypedString(inputStorage, "Setting flow requires the type id as the fifth (string) value.");74const std::string routeID = StoHelp::readTypedString(inputStorage, "Setting flow requires the route id as the sixth (string) value.");75const std::string departLane = StoHelp::readTypedString(inputStorage, "Setting flow requires the departLane as the seventh (string) value.");76const std::string departSpeed = StoHelp::readTypedString(inputStorage, "Setting flow requires the departSpeed as the eigth (string) value.");77libsumo::Calibrator::setFlow(id, begin, end, vehsPerHour, speed, typeID, routeID, departLane, departSpeed);78break;79}80case libsumo::VAR_PARAMETER: {81StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");82const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");83const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");84libsumo::Calibrator::setParameter(id, name, value);85break;86}87default:88break;89}90} catch (libsumo::TraCIException& e) {91return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, e.what(), outputStorage);92}93server.writeStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);94return true;95}969798/****************************************************************************/99100101