Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/traci-server/TraCIServerAPI_TrafficLight.cpp
193772 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2009-2026 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
TraCIServerAPI_TrafficLight::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
40
tcpip::Storage& outputStorage) {
41
std::string warning = ""; // additional description for response
42
// variable
43
const int variable = inputStorage.readUnsignedByte();
44
if (variable != libsumo::TL_PHASE_INDEX && variable != libsumo::TL_PROGRAM && variable != libsumo::TL_PHASE_DURATION
45
&& variable != libsumo::TL_RED_YELLOW_GREEN_STATE && variable != libsumo::TL_COMPLETE_PROGRAM_RYG
46
&& variable != libsumo::VAR_NAME
47
&& variable != libsumo::TL_CONSTRAINT_REMOVE
48
&& variable != libsumo::TL_CONSTRAINT_UPDATE
49
&& variable != libsumo::TL_CONSTRAINT_ADD
50
&& variable != libsumo::VAR_PARAMETER) {
51
return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "Change TLS State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
52
}
53
const std::string id = inputStorage.readString();
54
try {
55
switch (variable) {
56
case libsumo::TL_PHASE_INDEX: {
57
libsumo::TrafficLight::setPhase(id, StoHelp::readTypedInt(inputStorage, "The phase index must be given as an integer."));
58
}
59
break;
60
case libsumo::VAR_NAME:
61
libsumo::TrafficLight::setPhaseName(id, StoHelp::readTypedString(inputStorage, "The phase name must be given as a string."));
62
break;
63
case libsumo::TL_PROGRAM:
64
libsumo::TrafficLight::setProgram(id, StoHelp::readTypedString(inputStorage, "The program must be given as a string."));
65
break;
66
case libsumo::TL_PHASE_DURATION:
67
libsumo::TrafficLight::setPhaseDuration(id, StoHelp::readTypedDouble(inputStorage, "The phase duration must be given as a double."));
68
break;
69
case libsumo::TL_RED_YELLOW_GREEN_STATE:
70
libsumo::TrafficLight::setRedYellowGreenState(id, StoHelp::readTypedString(inputStorage, "The phase must be given as a string."));
71
break;
72
case libsumo::TL_COMPLETE_PROGRAM_RYG: {
73
StoHelp::readCompound(inputStorage, -1, "A compound object is needed for setting a new program.");
74
libsumo::TraCILogic logic;
75
logic.programID = StoHelp::readTypedString(inputStorage, "set program: 1. parameter (programID) must be a string.");
76
logic.type = StoHelp::readTypedInt(inputStorage, "set program: 2. parameter (type) must be an int.");
77
logic.currentPhaseIndex = StoHelp::readTypedInt(inputStorage, "set program: 3. parameter (index) must be an int.");
78
79
const int numPhases = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for the phases.");
80
for (int j = 0; j < numPhases; ++j) {
81
const int items = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for every phase.");
82
if (items != 6 && items != 5) {
83
return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A phase compound object requires 5 or 6 items.", outputStorage);
84
}
85
const double duration = StoHelp::readTypedDouble(inputStorage, "set program: 4.1. parameter (duration) must be a double.");
86
const std::string state = StoHelp::readTypedString(inputStorage, "set program: 4.2. parameter (phase) must be a string.");
87
const double minDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.3. parameter (min duration) must be a double.");
88
const double maxDuration = StoHelp::readTypedDouble(inputStorage, "set program: 4.4. parameter (max duration) must be a double.");
89
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
for (int k = 0; k < numNext; ++k) {
92
next.push_back(StoHelp::readTypedInt(inputStorage, "set program: 4.5. parameter (next) must be a list of int."));
93
}
94
std::string name;
95
if (items == 6) {
96
name = StoHelp::readTypedString(inputStorage, "set program: 4.6. parameter (name) must be a string.");
97
}
98
logic.phases.emplace_back(new libsumo::TraCIPhase(duration, state, minDuration, maxDuration, next, name));
99
}
100
const int numParams = StoHelp::readCompound(inputStorage, -1, "set program: 5. parameter (subparams) must be a compound object.");
101
for (int j = 0; j < numParams; ++j) {
102
const std::vector<std::string> par = StoHelp::readTypedStringList(inputStorage);
103
logic.subParameter[par[0]] = par[1];
104
}
105
libsumo::TrafficLight::setCompleteRedYellowGreenDefinition(id, logic);
106
}
107
break;
108
case libsumo::TL_CONSTRAINT_REMOVE: {
109
StoHelp::readCompound(inputStorage, 3, "A compound object of size 3 is needed for removing constraints.");
110
const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
111
const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foeSignal id must be given as a string.");
112
const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
113
libsumo::TrafficLight::removeConstraints(id, tripId, foeSignal, foeId);
114
}
115
break;
116
case libsumo::TL_CONSTRAINT_UPDATE:
117
libsumo::TrafficLight::updateConstraints(id, StoHelp::readTypedString(inputStorage, "The tripId index must be given as a string."));
118
break;
119
case libsumo::TL_CONSTRAINT_ADD: {
120
StoHelp::readCompound(inputStorage, 5, "A compound object of size 5 is needed for adding constraints.");
121
const std::string tripId = StoHelp::readTypedString(inputStorage, "The tripId must be given as a string.");
122
const std::string foeSignal = StoHelp::readTypedString(inputStorage, "The foe signal must be given as a string.");
123
const std::string foeId = StoHelp::readTypedString(inputStorage, "The foe tripId must be given as a string.");
124
const int type = StoHelp::readTypedInt(inputStorage, "The type must be an int.");
125
const int limit = StoHelp::readTypedInt(inputStorage, "The limit must be an int.");
126
libsumo::TrafficLight::addConstraint(id, tripId, foeSignal, foeId, type, limit);
127
}
128
break;
129
case libsumo::VAR_PARAMETER: {
130
StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
131
const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
132
const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
133
libsumo::TrafficLight::setParameter(id, name, value);
134
}
135
break;
136
default:
137
break;
138
}
139
} catch (libsumo::TraCIException& e) {
140
return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, e.what(), outputStorage);
141
}
142
server.writeStatusCmd(libsumo::CMD_SET_TL_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
143
return true;
144
}
145
146
147
void
148
TraCIServerAPI_TrafficLight::writeConstraint(TraCIServer& server, const libsumo::TraCISignalConstraint& c) {
149
StoHelp::writeTypedString(server.getWrapperStorage(), c.signalId);
150
StoHelp::writeTypedString(server.getWrapperStorage(), c.tripId);
151
StoHelp::writeTypedString(server.getWrapperStorage(), c.foeId);
152
StoHelp::writeTypedString(server.getWrapperStorage(), c.foeSignal);
153
StoHelp::writeTypedInt(server.getWrapperStorage(), c.limit);
154
StoHelp::writeTypedInt(server.getWrapperStorage(), c.type);
155
StoHelp::writeTypedByte(server.getWrapperStorage(), c.mustWait);
156
StoHelp::writeTypedByte(server.getWrapperStorage(), c.active);
157
std::vector<std::string> paramItems;
158
for (auto item : c.param) {
159
paramItems.push_back(item.first);
160
paramItems.push_back(item.second);
161
}
162
StoHelp::writeTypedStringList(server.getWrapperStorage(), paramItems);
163
}
164
165
166
/****************************************************************************/
167
168