Path: blob/main/src/traci-server/TraCIServerAPI_ChargingStation.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_ChargingStation.cpp14/// @author Jakob Erdmann15/// @date 16.03.202016///17// APIs for getting/setting charging station values via TraCI18/****************************************************************************/19#include <config.h>2021#include <microsim/MSNet.h>22#include <microsim/MSEdge.h>23#include <microsim/MSStoppingPlace.h>24#include <libsumo/ChargingStation.h>25#include <libsumo/TraCIConstants.h>26#include <libsumo/StorageHelper.h>27#include "TraCIServerAPI_ChargingStation.h"282930// ===========================================================================31// method definitions32// ===========================================================================33bool34TraCIServerAPI_ChargingStation::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_CHARGINGSTATION_VARIABLE, variable, id);39try {40if (!libsumo::ChargingStation::handleVariable(id, variable, &server, &inputStorage)) {41return server.writeErrorStatusCmd(libsumo::CMD_GET_CHARGINGSTATION_VARIABLE, "Get ChargingStation Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);42}43} catch (libsumo::TraCIException& e) {44return server.writeErrorStatusCmd(libsumo::CMD_GET_CHARGINGSTATION_VARIABLE, e.what(), outputStorage);45}46server.writeStatusCmd(libsumo::CMD_GET_CHARGINGSTATION_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);47server.writeResponseWithLength(outputStorage, server.getWrapperStorage());48return true;49}505152bool53TraCIServerAPI_ChargingStation::processSet(TraCIServer& server, tcpip::Storage& inputStorage,54tcpip::Storage& outputStorage) {55std::string warning = ""; // additional description for response56// variable57int variable = inputStorage.readUnsignedByte();58if (variable != libsumo::VAR_PARAMETER &&59variable != libsumo::VAR_CS_POWER &&60variable != libsumo::VAR_CS_EFFICIENCY &&61variable != libsumo::VAR_CS_CHARGE_DELAY &&62variable != libsumo::VAR_CS_CHARGE_IN_TRANSIT) {63return server.writeErrorStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, "Change ChargingStation State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);64}65// id66std::string id = inputStorage.readString();6768try {69// process70switch (variable) {71case libsumo::VAR_PARAMETER: {72StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");73const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");74const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");75libsumo::ChargingStation::setParameter(id, name, value);76}77break;78case libsumo::VAR_CS_POWER: {79double value = 0;80if (!server.readTypeCheckingDouble(inputStorage, value)) {81return server.writeErrorStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, "Setting chargingPower requires a double.", outputStorage);82}83libsumo::ChargingStation::setChargingPower(id, value);84}85break;86case libsumo::VAR_CS_EFFICIENCY: {87double value = 0;88if (!server.readTypeCheckingDouble(inputStorage, value)) {89return server.writeErrorStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, "Setting efficiency requires a double.", outputStorage);90}91libsumo::ChargingStation::setEfficiency(id, value);92}93break;94case libsumo::VAR_CS_CHARGE_DELAY: {95double value = 0;96if (!server.readTypeCheckingDouble(inputStorage, value)) {97return server.writeErrorStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, "Setting charge delay requires a double.", outputStorage);98}99libsumo::ChargingStation::setChargeDelay(id, value);100}101break;102case libsumo::VAR_CS_CHARGE_IN_TRANSIT: {103const int value = StoHelp::readTypedInt(inputStorage, "Setting charge in transit requires an integer.");104libsumo::ChargingStation::setChargeInTransit(id, value != 0);105}106break;107default:108break;109}110} catch (libsumo::TraCIException& e) {111return server.writeErrorStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, e.what(), outputStorage);112}113server.writeStatusCmd(libsumo::CMD_SET_CHARGINGSTATION_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);114return true;115}116117118/****************************************************************************/119120121