Eclipse SUMO - Simulation of Urban MObility
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
libtraci/TrafficLight.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-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/****************************************************************************/
21// C++ TraCI client API implementation
22/****************************************************************************/
23#include <config.h>
24
25#define LIBTRACI 1
29#include "Domain.h"
30
31
32namespace libtraci {
33
34typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
35
36// ===========================================================================
37// static member definitions
38// ===========================================================================
39std::vector<std::string>
40TrafficLight::getIDList() {
42}
43
44
45int
46TrafficLight::getIDCount() {
48}
49
50
51std::string
52TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
54}
55
56
57std::vector<libsumo::TraCILogic>
58TrafficLight::getAllProgramLogics(const std::string& tlsID) {
59 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
61 std::vector<libsumo::TraCILogic> result;
62 int numLogics = ret.readInt();
63 while (numLogics-- > 0) {
65 StoHelp::readLogic(ret, logic);
66 result.emplace_back(logic);
67 }
68 return result;
69}
70
71
72std::vector<std::string>
73TrafficLight::getControlledJunctions(const std::string& tlsID) {
75}
76
77
78std::vector<std::string>
79TrafficLight::getControlledLanes(const std::string& tlsID) {
81}
82
83
84std::vector<std::vector<libsumo::TraCILink> >
85TrafficLight::getControlledLinks(const std::string& tlsID) {
86 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
88 std::vector< std::vector<libsumo::TraCILink> > result;
89 ret.readInt();
91 return result;
92}
93
94
95std::string
96TrafficLight::getProgram(const std::string& tlsID) {
98}
99
100
101int
102TrafficLight::getPhase(const std::string& tlsID) {
104}
105
106
107std::string
108TrafficLight::getPhaseName(const std::string& tlsID) {
109 return Dom::getString(libsumo::VAR_NAME, tlsID);
110}
111
112
113double
114TrafficLight::getPhaseDuration(const std::string& tlsID) {
116}
117
118
119double
120TrafficLight::getNextSwitch(const std::string& tlsID) {
122}
123
124
125double
126TrafficLight::getSpentDuration(const std::string& tlsID) {
128}
129
130
131int
132TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
133 tcpip::Storage content;
135 content.writeInt(index);
136 return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
137}
138
139std::vector<std::string>
140TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
141 tcpip::Storage content;
143 content.writeInt(linkIndex);
145}
146
147std::vector<std::string>
148TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
149 tcpip::Storage content;
151 content.writeInt(linkIndex);
152 return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
153}
154
155std::vector<std::string>
156TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
157 tcpip::Storage content;
159 content.writeInt(linkIndex);
161}
162
163std::vector<libsumo::TraCISignalConstraint>
164TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
165 std::vector<libsumo::TraCISignalConstraint> result;
166 tcpip::Storage content;
167 StoHelp::writeTypedString(content, tripId);
168 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
169 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
170 ret.readInt(); // components
172 return result;
173}
174
175std::vector<libsumo::TraCISignalConstraint>
176TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
177 std::vector<libsumo::TraCISignalConstraint> result;
178 tcpip::Storage content;
179 StoHelp::writeTypedString(content, foeId);
180 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
181 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
182 ret.readInt(); // components
184 return result;
185}
186
188
189void
190TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
192}
193
194
195void
196TrafficLight::setPhase(const std::string& tlsID, const int index) {
198}
199
200
201void
202TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
203 Dom::setString(libsumo::VAR_NAME, tlsID, name);
204}
205
206
207void
208TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
209 Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
210}
211
212
213void
214TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
215 Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
216}
217
218
219void
220TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
221 tcpip::Storage content;
222 StoHelp::writeCompound(content, 5);
223 StoHelp::writeTypedString(content, logic.programID);
224 StoHelp::writeTypedInt(content, logic.type);
226 StoHelp::writeCompound(content, (int)logic.phases.size());
227 for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
228 StoHelp::writeCompound(content, 6);
229 StoHelp::writeTypedDouble(content, phase->duration);
230 StoHelp::writeTypedString(content, phase->state);
231 StoHelp::writeTypedDouble(content, phase->minDur);
232 StoHelp::writeTypedDouble(content, phase->maxDur);
233 StoHelp::writeCompound(content, (int)phase->next.size());
234 for (int n : phase->next) {
235 StoHelp::writeTypedInt(content, n);
236 }
237 StoHelp::writeTypedString(content, phase->name);
238 }
239 StoHelp::writeCompound(content, (int)logic.subParameter.size());
240 for (const auto& key_value : logic.subParameter) {
241 StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
242 }
244}
245
246
247void
248TrafficLight::addConstraint(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId, const int type, const int limit) {
249 tcpip::Storage content;
250 StoHelp::writeCompound(content, 5);
251 StoHelp::writeTypedString(content, tripId);
252 StoHelp::writeTypedString(content, foeSignal);
253 StoHelp::writeTypedString(content, foeId);
254 StoHelp::writeTypedInt(content, type);
255 StoHelp::writeTypedInt(content, limit);
256 Dom::set(libsumo::TL_CONSTRAINT_ADD, tlsID, &content);
257}
258
259
260std::vector<libsumo::TraCISignalConstraint>
261TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
262 std::vector<libsumo::TraCISignalConstraint> result;
263 tcpip::Storage content;
264 StoHelp::writeCompound(content, 3);
265 StoHelp::writeTypedString(content, tripId);
266 StoHelp::writeTypedString(content, foeSignal);
267 StoHelp::writeTypedString(content, foeId);
268 std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
269 tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
270 ret.readInt(); // components
271 // number of items
272 ret.readUnsignedByte();
273 const int n = ret.readInt();
274 for (int i = 0; i < n; ++i) {
282 c.mustWait = StoHelp::readTypedByte(ret) != 0;
283 c.active = StoHelp::readTypedByte(ret) != 0;
284 const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
285 for (int j = 0; j < (int)paramItems.size(); j += 2) {
286 c.param[paramItems[j]] = paramItems[j + 1];
287 }
288 result.push_back(c);
289 }
290 return result;
291}
292
293
294void
295TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
296 tcpip::Storage content;
297 StoHelp::writeCompound(content, 3);
298 StoHelp::writeTypedString(content, tripId);
299 StoHelp::writeTypedString(content, foeSignal);
300 StoHelp::writeTypedString(content, foeId);
301 Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
302}
303
304void
305TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
307}
308
309std::string
310to_string(const std::vector<double>& value) {
311 std::ostringstream tmp;
312 for (double d : value) {
313 tmp << d << " ";
314 }
315 std::string tmp2 = tmp.str();
316 tmp2.pop_back();
317 return tmp2;
318}
319
320
321void
322TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
323 setParameter(tlsID, "NEMA.splits", to_string(splits));
324}
325
326void
327TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
328 setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
329}
330
331void
332TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
333 setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
334}
335
336void
337TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
338 setParameter(tlsID, "NEMA.offset", std::to_string(offset));
339}
340
341
343
344}
345
346
347/****************************************************************************/
#define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition Domain.h:38
#define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN)
Definition Domain.h:77
#define TL(string)
Definition MsgHandler.h:305
C++ TraCI client API implementation.
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
static void writeTypedDouble(tcpip::Storage &content, double value)
static void readLogic(tcpip::Storage &inputStorage, libsumo::TraCILogic &logic, const std::string &error="")
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
static void readConstraintVector(tcpip::Storage &inputStorage, std::vector< libsumo::TraCISignalConstraint > &result, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static void writeCompound(tcpip::Storage &content, int size)
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static void writeTypedInt(tcpip::Storage &content, int value)
static void writeTypedStringList(tcpip::Storage &content, const std::vector< std::string > &value)
static void writeTypedString(tcpip::Storage &content, const std::string &value)
static void readLinkVectorVector(tcpip::Storage &inputStorage, std::vector< std::vector< libsumo::TraCILink > > &result, const std::string &error="")
static Connection & getActive()
Definition Connection.h:57
std::mutex & getMutex() const
Definition Connection.h:76
static void setDouble(int var, const std::string &id, double value)
Definition Domain.h:221
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:167
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:162
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:125
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition Domain.h:209
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition Domain.h:130
static void setInt(int var, const std::string &id, int value)
Definition Domain.h:214
static void setString(int var, const std::string &id, const std::string &value)
Definition Domain.h:228
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition Domain.h:111
virtual void writeInt(int)
Definition storage.cpp:321
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual void writeUnsignedByte(int)
Definition storage.cpp:165
StorageType::size_type size() const
Definition storage.h:119
virtual int readInt()
Definition storage.cpp:311
TRACI_CONST int VAR_NAME
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int TL_CONSTRAINT_REMOVE
TRACI_CONST int TL_BLOCKING_VEHICLES
TRACI_CONST int TL_CONSTRAINT_SWAP
TRACI_CONST int TL_PRIORITY_VEHICLES
TRACI_CONST int TL_CONTROLLED_LANES
TRACI_CONST int TL_SPENT_DURATION
TRACI_CONST int TL_COMPLETE_DEFINITION_RYG
TRACI_CONST int VAR_PERSON_NUMBER
TRACI_CONST int TL_CONSTRAINT_UPDATE
TRACI_CONST int TL_CONTROLLED_JUNCTIONS
TRACI_CONST int TL_CONTROLLED_LINKS
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int ID_COUNT
TRACI_CONST int TL_CONSTRAINT_BYFOE
TRACI_CONST int TL_CONSTRAINT
TRACI_CONST int TL_NEXT_SWITCH
TRACI_CONST int TL_PROGRAM
TRACI_CONST int TL_PHASE_DURATION
TRACI_CONST int TL_CONSTRAINT_ADD
TRACI_CONST int TL_PHASE_INDEX
TRACI_CONST int TL_CURRENT_PHASE
TRACI_CONST int TL_COMPLETE_PROGRAM_RYG
TRACI_CONST int TL_RED_YELLOW_GREEN_STATE
TRACI_CONST int TL_CURRENT_PROGRAM
TRACI_CONST int TL_RIVAL_VEHICLES
Domain< libsumo::CMD_GET_BUSSTOP_VARIABLE, libsumo::CMD_SET_BUSSTOP_VARIABLE > Dom
std::string to_string(const std::vector< double > &value)
std::map< std::string, std::string > subParameter
Definition TraCIDefs.h:425
std::string programID
Definition TraCIDefs.h:421
std::vector< std::shared_ptr< libsumo::TraCIPhase > > phases
Definition TraCIDefs.h:424
std::string foeId
the tripId or vehicle id of the train that must pass first
Definition TraCIDefs.h:873
std::string tripId
the tripId or vehicle id of the train that is constrained
Definition TraCIDefs.h:871
std::string foeSignal
the tlsID of the rail signla that the foe must pass first
Definition TraCIDefs.h:875
std::string signalId
the idea of the rail signal where this constraint is active
Definition TraCIDefs.h:869
std::map< std::string, std::string > param
additional parameters
Definition TraCIDefs.h:885
bool active
whether this constraint is active
Definition TraCIDefs.h:883
int type
the type of constraint (predecessor:0, insertionPredecessor:1)
Definition TraCIDefs.h:879
bool mustWait
whether tripId must still wait for foeId to pass foeSignal
Definition TraCIDefs.h:881
int limit
the number of trains that must be recorded at the foeSignal
Definition TraCIDefs.h:877