Path: blob/main/src/traci-server/TraCIServerAPI_TrafficLight.cpp
193772 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2009-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_TrafficLight.cpp14/// @author Daniel Krajzewicz15/// @author Laura Bieker16/// @author Michael Behrisch17/// @author Jakob Erdmann18/// @date 07.05.200919///20// APIs for getting/setting traffic light values via TraCI21/****************************************************************************/22#include <config.h>2324#include <microsim/MSLane.h>25#include <microsim/MSEdge.h>26#include <microsim/traffic_lights/MSTLLogicControl.h>27#include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>28#include <libsumo/TraCIConstants.h>29#include <libsumo/StorageHelper.h>30#include <libsumo/TrafficLight.h>31#include "TraCIServerAPI_TrafficLight.h"323334// ===========================================================================35// method definitions36// ===========================================================================37bool38TraCIServerAPI_TrafficLight::processSet(TraCIServer& server, tcpip::Storage& inputStorage,39tcpip::Storage& outputStorage) {40std::string warning = ""; // additional description for response41// variable42const int variable = inputStorage.readUnsignedByte();43if (variable != libsumo::TL_PHASE_INDEX && variable != libsumo::TL_PROGRAM && variable != libsumo::TL_PHASE_DURATION44&& variable != libsumo::TL_RED_YELLOW_GREEN_STATE && variable != libsumo::TL_COMPLETE_PROGRAM_RYG45&& variable != libsumo::VAR_NAME46&& variable != libsumo::TL_CONSTRAINT_REMOVE47&& variable != libsumo::TL_CONSTRAINT_UPDATE48&& variable != libsumo::TL_CONSTRAINT_ADD49&& variable != libsumo::VAR_PARAMETER) {50return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "Change TLS State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);51}52const std::string id = inputStorage.readString();53try {54switch (variable) {55case libsumo::TL_PHASE_INDEX: {56libsumo::TrafficLight::setPhase(id, StoHelp::readTypedInt(inputStorage, "The phase index must be given as an integer."));57}58break;59case libsumo::VAR_NAME:60libsumo::TrafficLight::setPhaseName(id, StoHelp::readTypedString(inputStorage, "The phase name must be given as a string."));61break;62case libsumo::TL_PROGRAM:63libsumo::TrafficLight::setProgram(id, StoHelp::readTypedString(inputStorage, "The program must be given as a string."));64break;65case libsumo::TL_PHASE_DURATION:66libsumo::TrafficLight::setPhaseDuration(id, StoHelp::readTypedDouble(inputStorage, "The phase duration must be given as a double."));67break;68case libsumo::TL_RED_YELLOW_GREEN_STATE:69libsumo::TrafficLight::setRedYellowGreenState(id, StoHelp::readTypedString(inputStorage, "The phase must be given as a string."));70break;71case libsumo::TL_COMPLETE_PROGRAM_RYG: {72StoHelp::readCompound(inputStorage, -1, "A compound object is needed for setting a new program.");73libsumo::TraCILogic logic;74logic.programID = StoHelp::readTypedString(inputStorage, "set program: 1. parameter (programID) must be a string.");75logic.type = StoHelp::readTypedInt(inputStorage, "set program: 2. parameter (type) must be an int.");76logic.currentPhaseIndex = StoHelp::readTypedInt(inputStorage, "set program: 3. parameter (index) must be an int.");7778const int numPhases = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for the phases.");79for (int j = 0; j < numPhases; ++j) {80const int items = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for every phase.");81if (items != 6 && items != 5) {82return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A phase compound object requires 5 or 6 items.", outputStorage);83}84const double duration = StoHelp::readTypedDouble(inputStorage, "set program: 4.1. parameter (duration) must be a double.");85const std::string state = StoHelp::readTypedString(inputStorage, "set program: 4.2. parameter (phase) must be a string.");86const double minDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.3. parameter (min duration) must be a double.");87const double maxDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.4. parameter (max duration) must be a double.");88const int numNext = StoHelp::readCompound(inputStorage, -1, "set program 4.5 parameter (next) must be a compound (list of ints).");89std::vector<int> next;90for (int k = 0; k < numNext; ++k) {91next.push_back(StoHelp::readTypedInt(inputStorage, "set program: 4.5. parameter (next) must be a list of int."));92}93std::string name;94if (items == 6) {95name = StoHelp::readTypedString(inputStorage, "set program: 4.6. parameter (name) must be a string.");96}97logic.phases.emplace_back(new libsumo::TraCIPhase(duration, state, minDuration, maxDuration, next, name));98}99const int numParams = StoHelp::readCompound(inputStorage, -1, "set program: 5. parameter (subparams) must be a compound object.");100for (int j = 0; j < numParams; ++j) {101const std::vector<std::string> par = StoHelp::readTypedStringList(inputStorage);102logic.subParameter[par[0]] = par[1];103}104libsumo::TrafficLight::setCompleteRedYellowGreenDefinition(id, logic);105}106break;107case libsumo::TL_CONSTRAINT_REMOVE: {108StoHelp::readCompound(inputStorage, 3, "A compound object of size 3 is needed for removing constraints.");109const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");110const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foeSignal id must be given as a string.");111const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");112libsumo::TrafficLight::removeConstraints(id, tripId, foeSignal, foeId);113}114break;115case libsumo::TL_CONSTRAINT_UPDATE:116libsumo::TrafficLight::updateConstraints(id, StoHelp::readTypedString(inputStorage, "The tripId index must be given as a string."));117break;118case libsumo::TL_CONSTRAINT_ADD: {119StoHelp::readCompound(inputStorage, 5, "A compound object of size 5 is needed for adding constraints.");120const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");121const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foe signal must be given as a string.");122const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");123const int type = StoHelp::readTypedInt(inputStorage, "The type must be an int.");124const int limit = StoHelp::readTypedInt(inputStorage, "The limit must be an int.");125libsumo::TrafficLight::addConstraint(id, tripId, foeSignal, foeId, type, limit);126}127break;128case libsumo::VAR_PARAMETER: {129StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");130const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");131const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");132libsumo::TrafficLight::setParameter(id, name, value);133}134break;135default:136break;137}138} catch (libsumo::TraCIException& e) {139return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, e.what(), outputStorage);140}141server.writeStatusCmd(libsumo::CMD_SET_TL_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);142return true;143}144145146void147TraCIServerAPI_TrafficLight::writeConstraint(TraCIServer& server, const libsumo::TraCISignalConstraint& c) {148StoHelp::writeTypedString(server.getWrapperStorage(), c.signalId);149StoHelp::writeTypedString(server.getWrapperStorage(), c.tripId);150StoHelp::writeTypedString(server.getWrapperStorage(), c.foeId);151StoHelp::writeTypedString(server.getWrapperStorage(), c.foeSignal);152StoHelp::writeTypedInt(server.getWrapperStorage(), c.limit);153StoHelp::writeTypedInt(server.getWrapperStorage(), c.type);154StoHelp::writeTypedByte(server.getWrapperStorage(), c.mustWait);155StoHelp::writeTypedByte(server.getWrapperStorage(), c.active);156std::vector<std::string> paramItems;157for (auto item : c.param) {158paramItems.push_back(item.first);159paramItems.push_back(item.second);160}161StoHelp::writeTypedStringList(server.getWrapperStorage(), paramItems);162}163164165/****************************************************************************/166167168