Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2009-2025 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file TraCIServerAPI_TrafficLight.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date 07.05.2009
20 : ///
21 : // APIs for getting/setting traffic light values via TraCI
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/MSLane.h>
26 : #include <microsim/MSEdge.h>
27 : #include <microsim/traffic_lights/MSTLLogicControl.h>
28 : #include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>
29 : #include <libsumo/TraCIConstants.h>
30 : #include <libsumo/StorageHelper.h>
31 : #include <libsumo/TrafficLight.h>
32 : #include "TraCIServerAPI_TrafficLight.h"
33 :
34 :
35 : // ===========================================================================
36 : // method definitions
37 : // ===========================================================================
38 : bool
39 1887 : TraCIServerAPI_TrafficLight::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
40 : tcpip::Storage& outputStorage) {
41 1887 : std::string warning = ""; // additional description for response
42 : // variable
43 1887 : const int variable = inputStorage.readUnsignedByte();
44 1887 : if (variable != libsumo::TL_PHASE_INDEX && variable != libsumo::TL_PROGRAM && variable != libsumo::TL_PHASE_DURATION
45 1887 : && variable != libsumo::TL_RED_YELLOW_GREEN_STATE && variable != libsumo::TL_COMPLETE_PROGRAM_RYG
46 176 : && variable != libsumo::VAR_NAME
47 : && variable != libsumo::TL_CONSTRAINT_REMOVE
48 125 : && variable != libsumo::TL_CONSTRAINT_UPDATE
49 108 : && variable != libsumo::TL_CONSTRAINT_ADD
50 108 : && variable != libsumo::VAR_PARAMETER) {
51 3 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "Change TLS State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
52 : }
53 1886 : const std::string id = inputStorage.readString();
54 : try {
55 1886 : switch (variable) {
56 : case libsumo::TL_PHASE_INDEX: {
57 1646 : libsumo::TrafficLight::setPhase(id, StoHelp::readTypedInt(inputStorage, "The phase index must be given as an integer."));
58 : }
59 1642 : break;
60 : case libsumo::VAR_NAME:
61 8 : libsumo::TrafficLight::setPhaseName(id, StoHelp::readTypedString(inputStorage, "The phase name must be given as a string."));
62 8 : break;
63 : case libsumo::TL_PROGRAM:
64 20 : libsumo::TrafficLight::setProgram(id, StoHelp::readTypedString(inputStorage, "The program must be given as a string."));
65 19 : break;
66 : case libsumo::TL_PHASE_DURATION:
67 20 : libsumo::TrafficLight::setPhaseDuration(id, StoHelp::readTypedDouble(inputStorage, "The phase duration must be given as a double."));
68 19 : break;
69 : case libsumo::TL_RED_YELLOW_GREEN_STATE:
70 25 : libsumo::TrafficLight::setRedYellowGreenState(id, StoHelp::readTypedString(inputStorage, "The phase must be given as a string."));
71 24 : break;
72 : case libsumo::TL_COMPLETE_PROGRAM_RYG: {
73 86 : StoHelp::readCompound(inputStorage, -1, "A compound object is needed for setting a new program.");
74 : libsumo::TraCILogic logic;
75 43 : logic.programID = StoHelp::readTypedString(inputStorage, "set program: 1. parameter (programID) must be a string.");
76 43 : logic.type = StoHelp::readTypedInt(inputStorage, "set program: 2. parameter (type) must be an int.");
77 43 : logic.currentPhaseIndex = StoHelp::readTypedInt(inputStorage, "set program: 3. parameter (index) must be an int.");
78 :
79 43 : const int numPhases = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for the phases.");
80 211 : for (int j = 0; j < numPhases; ++j) {
81 168 : const int items = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for every phase.");
82 168 : if (items != 6 && items != 5) {
83 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A phase compound object requires 5 or 6 items.", outputStorage);
84 : }
85 168 : const double duration = StoHelp::readTypedDouble(inputStorage, "set program: 4.1. parameter (duration) must be a double.");
86 168 : const std::string state = StoHelp::readTypedString(inputStorage, "set program: 4.2. parameter (phase) must be a string.");
87 168 : const double minDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.3. parameter (min duration) must be a double.");
88 168 : const double maxDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.4. parameter (max duration) must be a double.");
89 336 : const int numNext = StoHelp::readCompound(inputStorage, -1, "set program 4.5 parameter (next) must be a compound (list of ints).");
90 : std::vector<int> next;
91 177 : for (int k = 0; k < numNext; ++k) {
92 18 : next.push_back(StoHelp::readTypedInt(inputStorage, "set program: 4.5. parameter (next) must be a list of int."));
93 : }
94 : std::string name;
95 168 : if (items == 6) {
96 300 : name = StoHelp::readTypedString(inputStorage, "set program: 4.6. parameter (name) must be a string.");
97 : }
98 336 : logic.phases.emplace_back(new libsumo::TraCIPhase(duration, state, minDuration, maxDuration, next, name));
99 168 : }
100 43 : const int numParams = StoHelp::readCompound(inputStorage, -1, "set program: 5. parameter (subparams) must be a compound object.");
101 43 : for (int j = 0; j < numParams; ++j) {
102 0 : const std::vector<std::string> par = StoHelp::readTypedStringList(inputStorage);
103 0 : logic.subParameter[par[0]] = par[1];
104 0 : }
105 : libsumo::TrafficLight::setCompleteRedYellowGreenDefinition(id, logic);
106 43 : }
107 : break;
108 : case libsumo::TL_CONSTRAINT_REMOVE: {
109 15 : StoHelp::readCompound(inputStorage, 3, "A compound object of size 3 is needed for removing constraints.");
110 15 : const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
111 15 : const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foeSignal id must be given as a string.");
112 15 : const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
113 15 : libsumo::TrafficLight::removeConstraints(id, tripId, foeSignal, foeId);
114 : }
115 15 : break;
116 : case libsumo::TL_CONSTRAINT_UPDATE:
117 2 : libsumo::TrafficLight::updateConstraints(id, StoHelp::readTypedString(inputStorage, "The tripId index must be given as a string."));
118 2 : break;
119 : case libsumo::TL_CONSTRAINT_ADD: {
120 3 : StoHelp::readCompound(inputStorage, 5, "A compound object of size 5 is needed for adding constraints.");
121 3 : const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
122 3 : const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foe signal must be given as a string.");
123 3 : const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
124 3 : const int type = StoHelp::readTypedInt(inputStorage, "The type must be an int.");
125 3 : const int limit = StoHelp::readTypedInt(inputStorage, "The limit must be an int.");
126 3 : libsumo::TrafficLight::addConstraint(id, tripId, foeSignal, foeId, type, limit);
127 : }
128 3 : break;
129 : case libsumo::VAR_PARAMETER: {
130 104 : StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
131 104 : const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
132 108 : const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
133 104 : libsumo::TrafficLight::setParameter(id, name, value);
134 : }
135 100 : break;
136 : default:
137 : break;
138 : }
139 12 : } catch (libsumo::TraCIException& e) {
140 12 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, e.what(), outputStorage);
141 12 : }
142 1874 : server.writeStatusCmd(libsumo::CMD_SET_TL_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
143 : return true;
144 : }
145 :
146 :
147 : void
148 0 : TraCIServerAPI_TrafficLight::writeConstraint(TraCIServer& server, const libsumo::TraCISignalConstraint& c) {
149 0 : StoHelp::writeTypedString(server.getWrapperStorage(), c.signalId);
150 0 : StoHelp::writeTypedString(server.getWrapperStorage(), c.tripId);
151 0 : StoHelp::writeTypedString(server.getWrapperStorage(), c.foeId);
152 0 : StoHelp::writeTypedString(server.getWrapperStorage(), c.foeSignal);
153 0 : StoHelp::writeTypedInt(server.getWrapperStorage(), c.limit);
154 0 : StoHelp::writeTypedInt(server.getWrapperStorage(), c.type);
155 0 : StoHelp::writeTypedByte(server.getWrapperStorage(), c.mustWait);
156 0 : StoHelp::writeTypedByte(server.getWrapperStorage(), c.active);
157 : std::vector<std::string> paramItems;
158 0 : for (auto item : c.param) {
159 0 : paramItems.push_back(item.first);
160 0 : paramItems.push_back(item.second);
161 : }
162 0 : StoHelp::writeTypedStringList(server.getWrapperStorage(), paramItems);
163 0 : }
164 :
165 :
166 : /****************************************************************************/
|