LCOV - code coverage report
Current view: top level - src/libtraci - TrafficLight.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 92.6 % 162 150
Test Date: 2025-11-13 15:38:19 Functions: 89.1 % 46 41

            Line data    Source code
       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              : /****************************************************************************/
      14              : /// @file    TrafficLight.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Mario Krumnow
      17              : /// @author  Jakob Erdmann
      18              : /// @author  Michael Behrisch
      19              : /// @date    30.05.2012
      20              : ///
      21              : // C++ TraCI client API implementation
      22              : /****************************************************************************/
      23              : #include <config.h>
      24              : 
      25              : #define LIBTRACI 1
      26              : #include <libsumo/StorageHelper.h>
      27              : #include <libsumo/TraCIConstants.h>
      28              : #include <libsumo/TrafficLight.h>
      29              : #include "Domain.h"
      30              : 
      31              : 
      32              : namespace libtraci {
      33              : 
      34              : typedef Domain<libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE> Dom;
      35              : 
      36              : // ===========================================================================
      37              : // static member definitions
      38              : // ===========================================================================
      39              : std::vector<std::string>
      40          106 : TrafficLight::getIDList() {
      41          211 :     return Dom::getStringVector(libsumo::TRACI_ID_LIST, "");
      42              : }
      43              : 
      44              : 
      45              : int
      46            3 : TrafficLight::getIDCount() {
      47            6 :     return Dom::getInt(libsumo::ID_COUNT, "");
      48              : }
      49              : 
      50              : 
      51              : std::string
      52           22 : TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
      53           22 :     return Dom::getString(libsumo::TL_RED_YELLOW_GREEN_STATE, tlsID);
      54              : }
      55              : 
      56              : 
      57              : std::vector<libsumo::TraCILogic>
      58           19 : TrafficLight::getAllProgramLogics(const std::string& tlsID) {
      59           19 :     std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
      60              :     tcpip::Storage& ret = Dom::get(libsumo::TL_COMPLETE_DEFINITION_RYG, tlsID);
      61              :     std::vector<libsumo::TraCILogic> result;
      62           19 :     int numLogics = ret.readInt();
      63           52 :     while (numLogics-- > 0) {
      64              :         libsumo::TraCILogic logic;
      65           33 :         StoHelp::readLogic(ret, logic);
      66           33 :         result.emplace_back(logic);
      67           33 :     }
      68           19 :     return result;
      69            0 : }
      70              : 
      71              : 
      72              : std::vector<std::string>
      73            1 : TrafficLight::getControlledJunctions(const std::string& tlsID) {
      74            1 :     return Dom::getStringVector(libsumo::TL_CONTROLLED_JUNCTIONS, tlsID);
      75              : }
      76              : 
      77              : 
      78              : std::vector<std::string>
      79            9 : TrafficLight::getControlledLanes(const std::string& tlsID) {
      80            9 :     return Dom::getStringVector(libsumo::TL_CONTROLLED_LANES, tlsID);
      81              : }
      82              : 
      83              : 
      84              : std::vector<std::vector<libsumo::TraCILink> >
      85            9 : TrafficLight::getControlledLinks(const std::string& tlsID) {
      86            9 :     std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
      87              :     tcpip::Storage& ret = Dom::get(libsumo::TL_CONTROLLED_LINKS, tlsID);
      88              :     std::vector< std::vector<libsumo::TraCILink> > result;
      89            9 :     ret.readInt();
      90           18 :     StoHelp::readLinkVectorVector(ret, result);
      91            9 :     return result;
      92            0 : }
      93              : 
      94              : 
      95              : std::string
      96          194 : TrafficLight::getProgram(const std::string& tlsID) {
      97          194 :     return Dom::getString(libsumo::TL_CURRENT_PROGRAM, tlsID);
      98              : }
      99              : 
     100              : 
     101              : int
     102        18141 : TrafficLight::getPhase(const std::string& tlsID) {
     103        18141 :     return Dom::getInt(libsumo::TL_CURRENT_PHASE, tlsID);
     104              : }
     105              : 
     106              : 
     107              : std::string
     108        13778 : TrafficLight::getPhaseName(const std::string& tlsID) {
     109        13778 :     return Dom::getString(libsumo::VAR_NAME, tlsID);
     110              : }
     111              : 
     112              : 
     113              : double
     114           10 : TrafficLight::getPhaseDuration(const std::string& tlsID) {
     115           10 :     return Dom::getDouble(libsumo::TL_PHASE_DURATION, tlsID);
     116              : }
     117              : 
     118              : 
     119              : double
     120          233 : TrafficLight::getNextSwitch(const std::string& tlsID) {
     121          233 :     return Dom::getDouble(libsumo::TL_NEXT_SWITCH, tlsID);
     122              : }
     123              : 
     124              : 
     125              : double
     126          185 : TrafficLight::getSpentDuration(const std::string& tlsID) {
     127          185 :     return Dom::getDouble(libsumo::TL_SPENT_DURATION, tlsID);
     128              : }
     129              : 
     130              : 
     131              : int
     132          802 : TrafficLight::getServedPersonCount(const std::string& tlsID, int index) {
     133          802 :     tcpip::Storage content;
     134          802 :     content.writeUnsignedByte(libsumo::TYPE_INTEGER);
     135          802 :     content.writeInt(index);
     136         1604 :     return Dom::getInt(libsumo::VAR_PERSON_NUMBER, tlsID, &content);
     137          802 : }
     138              : 
     139              : std::vector<std::string>
     140         1600 : TrafficLight::getBlockingVehicles(const std::string& tlsID, int linkIndex) {
     141         1600 :     tcpip::Storage content;
     142         1600 :     content.writeUnsignedByte(libsumo::TYPE_INTEGER);
     143         1600 :     content.writeInt(linkIndex);
     144         3200 :     return Dom::getStringVector(libsumo::TL_BLOCKING_VEHICLES, tlsID, &content);
     145         1600 : }
     146              : 
     147              : std::vector<std::string>
     148         1598 : TrafficLight::getRivalVehicles(const std::string& tlsID, int linkIndex) {
     149         1598 :     tcpip::Storage content;
     150         1598 :     content.writeUnsignedByte(libsumo::TYPE_INTEGER);
     151         1598 :     content.writeInt(linkIndex);
     152         3196 :     return Dom::getStringVector(libsumo::TL_RIVAL_VEHICLES, tlsID, &content);
     153         1598 : }
     154              : 
     155              : std::vector<std::string>
     156         1598 : TrafficLight::getPriorityVehicles(const std::string& tlsID, int linkIndex) {
     157         1598 :     tcpip::Storage content;
     158         1598 :     content.writeUnsignedByte(libsumo::TYPE_INTEGER);
     159         1598 :     content.writeInt(linkIndex);
     160         3196 :     return Dom::getStringVector(libsumo::TL_PRIORITY_VEHICLES, tlsID, &content);
     161         1598 : }
     162              : 
     163              : std::vector<libsumo::TraCISignalConstraint>
     164          247 : TrafficLight::getConstraints(const std::string& tlsID, const std::string& tripId) {
     165              :     std::vector<libsumo::TraCISignalConstraint> result;
     166          247 :     tcpip::Storage content;
     167              :     StoHelp::writeTypedString(content, tripId);
     168          247 :     std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     169              :     tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT, tlsID, &content);
     170          247 :     ret.readInt(); // components
     171          494 :     StoHelp::readConstraintVector(ret, result);
     172          247 :     return result;
     173          247 : }
     174              : 
     175              : std::vector<libsumo::TraCISignalConstraint>
     176            2 : TrafficLight::getConstraintsByFoe(const std::string& foeSignal, const std::string& foeId) {
     177              :     std::vector<libsumo::TraCISignalConstraint> result;
     178            2 :     tcpip::Storage content;
     179              :     StoHelp::writeTypedString(content, foeId);
     180            2 :     std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     181              :     tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_BYFOE, foeSignal, &content);
     182            2 :     ret.readInt(); // components
     183            4 :     StoHelp::readConstraintVector(ret, result);
     184            2 :     return result;
     185            2 : }
     186              : 
     187         1091 : LIBTRACI_PARAMETER_IMPLEMENTATION(TrafficLight, TL)
     188              : 
     189              : void
     190            8 : TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
     191            8 :     Dom::setString(libsumo::TL_RED_YELLOW_GREEN_STATE, tlsID, state);
     192            8 : }
     193              : 
     194              : 
     195              : void
     196            9 : TrafficLight::setPhase(const std::string& tlsID, const int index) {
     197            9 :     Dom::setInt(libsumo::TL_PHASE_INDEX, tlsID, index);
     198            9 : }
     199              : 
     200              : 
     201              : void
     202            3 : TrafficLight::setPhaseName(const std::string& tlsID, const std::string& name) {
     203            3 :     Dom::setString(libsumo::VAR_NAME, tlsID, name);
     204            3 : }
     205              : 
     206              : 
     207              : void
     208            5 : TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
     209            5 :     Dom::setString(libsumo::TL_PROGRAM, tlsID, programID);
     210            5 : }
     211              : 
     212              : 
     213              : void
     214            7 : TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
     215            7 :     Dom::setDouble(libsumo::TL_PHASE_DURATION, tlsID, phaseDuration);
     216            7 : }
     217              : 
     218              : 
     219              : void
     220           12 : TrafficLight::setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) {
     221           12 :     tcpip::Storage content;
     222              :     StoHelp::writeCompound(content, 5);
     223           12 :     StoHelp::writeTypedString(content, logic.programID);
     224           12 :     StoHelp::writeTypedInt(content, logic.type);
     225           12 :     StoHelp::writeTypedInt(content, logic.currentPhaseIndex);
     226           12 :     StoHelp::writeCompound(content, (int)logic.phases.size());
     227           66 :     for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
     228              :         StoHelp::writeCompound(content, 6);
     229           54 :         StoHelp::writeTypedDouble(content, phase->duration);
     230           54 :         StoHelp::writeTypedString(content, phase->state);
     231           54 :         StoHelp::writeTypedDouble(content, phase->minDur);
     232           54 :         StoHelp::writeTypedDouble(content, phase->maxDur);
     233           54 :         StoHelp::writeCompound(content, (int)phase->next.size());
     234           57 :         for (int n : phase->next) {
     235              :             StoHelp::writeTypedInt(content, n);
     236              :         }
     237           54 :         StoHelp::writeTypedString(content, phase->name);
     238              :     }
     239           12 :     StoHelp::writeCompound(content, (int)logic.subParameter.size());
     240           12 :     for (const auto& key_value : logic.subParameter) {
     241            0 :         StoHelp::writeTypedStringList(content, std::vector<std::string> {key_value.first, key_value.second});
     242              :     }
     243           12 :     Dom::set(libsumo::TL_COMPLETE_PROGRAM_RYG, tlsID, &content);
     244           12 : }
     245              : 
     246              : 
     247              : void
     248            1 : TrafficLight::addConstraint(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId, const int type, const int limit) {
     249            1 :     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            1 :     Dom::set(libsumo::TL_CONSTRAINT_ADD, tlsID, &content);
     257            1 : }
     258              : 
     259              : 
     260              : std::vector<libsumo::TraCISignalConstraint>
     261           22 : TrafficLight::swapConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
     262              :     std::vector<libsumo::TraCISignalConstraint> result;
     263           22 :     tcpip::Storage content;
     264              :     StoHelp::writeCompound(content, 3);
     265              :     StoHelp::writeTypedString(content, tripId);
     266              :     StoHelp::writeTypedString(content, foeSignal);
     267              :     StoHelp::writeTypedString(content, foeId);
     268           22 :     std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     269              :     tcpip::Storage& ret = Dom::get(libsumo::TL_CONSTRAINT_SWAP, tlsID, &content);
     270           22 :     ret.readInt(); // components
     271              :     // number of items
     272           22 :     ret.readUnsignedByte();
     273           22 :     const int n = ret.readInt();
     274           42 :     for (int i = 0; i < n; ++i) {
     275              :         libsumo::TraCISignalConstraint c;
     276           20 :         c.signalId = StoHelp::readTypedString(ret);
     277           20 :         c.tripId = StoHelp::readTypedString(ret);
     278           20 :         c.foeId = StoHelp::readTypedString(ret);
     279           20 :         c.foeSignal = StoHelp::readTypedString(ret);
     280           20 :         c.limit = StoHelp::readTypedInt(ret);
     281           20 :         c.type = StoHelp::readTypedInt(ret);
     282           20 :         c.mustWait = StoHelp::readTypedByte(ret) != 0;
     283           20 :         c.active = StoHelp::readTypedByte(ret) != 0;
     284           20 :         const std::vector<std::string> paramItems = StoHelp::readTypedStringList(ret);
     285           36 :         for (int j = 0; j < (int)paramItems.size(); j += 2) {
     286           16 :             c.param[paramItems[j]] = paramItems[j + 1];
     287              :         }
     288           20 :         result.push_back(c);
     289           20 :     }
     290           22 :     return result;
     291           22 : }
     292              : 
     293              : 
     294              : void
     295            5 : TrafficLight::removeConstraints(const std::string& tlsID, const std::string& tripId, const std::string& foeSignal, const std::string& foeId) {
     296            5 :     tcpip::Storage content;
     297              :     StoHelp::writeCompound(content, 3);
     298              :     StoHelp::writeTypedString(content, tripId);
     299              :     StoHelp::writeTypedString(content, foeSignal);
     300              :     StoHelp::writeTypedString(content, foeId);
     301            5 :     Dom::set(libsumo::TL_CONSTRAINT_REMOVE, tlsID, &content);
     302            5 : }
     303              : 
     304              : void
     305            1 : TrafficLight::updateConstraints(const std::string& vehID, std::string tripId) {
     306            1 :     Dom::setString(libsumo::TL_CONSTRAINT_UPDATE, vehID, tripId);
     307            1 : }
     308              : 
     309              : std::string
     310            1 : to_string(const std::vector<double>& value) {
     311            1 :     std::ostringstream tmp;
     312            9 :     for (double d : value) {
     313            8 :         tmp << d << " ";
     314              :     }
     315              :     std::string tmp2 = tmp.str();
     316              :     tmp2.pop_back();
     317            1 :     return tmp2;
     318            1 : }
     319              : 
     320              : 
     321              : void
     322            0 : TrafficLight::setNemaSplits(const std::string& tlsID, const std::vector<double>& splits) {
     323            0 :     setParameter(tlsID, "NEMA.splits", to_string(splits));
     324            0 : }
     325              : 
     326              : void
     327            1 : TrafficLight::setNemaMaxGreens(const std::string& tlsID, const std::vector<double>& maxGreens) {
     328            2 :     setParameter(tlsID, "NEMA.maxGreens", to_string(maxGreens));
     329            1 : }
     330              : 
     331              : void
     332            0 : TrafficLight::setNemaCycleLength(const std::string& tlsID, double cycleLength) {
     333            0 :     setParameter(tlsID, "NEMA.cycleLength", std::to_string(cycleLength));
     334            0 : }
     335              : 
     336              : void
     337            0 : TrafficLight::setNemaOffset(const std::string& tlsID, double offset) {
     338            0 :     setParameter(tlsID, "NEMA.offset", std::to_string(offset));
     339            0 : }
     340              : 
     341              : 
     342          280 : LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(TrafficLight, TL)
     343              : 
     344              : }
     345              : 
     346              : 
     347              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1