Line data Source code
1 : /****************************************************************************/ 2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3 : // Copyright (C) 2001-2024 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 Command_SaveTLSSwitches.cpp 15 : /// @author Daniel Krajzewicz 16 : /// @author Jakob Erdmann 17 : /// @author Michael Behrisch 18 : /// @date 06 Jul 2006 19 : /// 20 : // Writes information about the green durations of a tls 21 : /****************************************************************************/ 22 : #include <config.h> 23 : 24 : #include "Command_SaveTLSSwitches.h" 25 : #include <microsim/traffic_lights/MSTrafficLightLogic.h> 26 : #include <microsim/MSEventControl.h> 27 : #include <microsim/MSNet.h> 28 : #include <microsim/MSLink.h> 29 : #include <microsim/MSLane.h> 30 : #include <utils/common/UtilExceptions.h> 31 : #include <utils/common/MsgHandler.h> 32 : #include <utils/iodevices/OutputDevice.h> 33 : 34 : 35 : // =========================================================================== 36 : // method definitions 37 : // =========================================================================== 38 265 : Command_SaveTLSSwitches::Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants& logics, 39 265 : OutputDevice& od) 40 265 : : myOutputDevice(od), myLogics(logics) { 41 265 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this); 42 795 : myOutputDevice.writeXMLHeader("tlsSwitches", "tlsswitches_file.xsd"); 43 265 : } 44 : 45 : 46 528 : Command_SaveTLSSwitches::~Command_SaveTLSSwitches() { 47 528 : } 48 : 49 : 50 : SUMOTime 51 323756 : Command_SaveTLSSwitches::execute(SUMOTime currentTime) { 52 323756 : MSTrafficLightLogic* light = myLogics.getActive(); 53 : const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks(); 54 323756 : const std::string& state = light->getCurrentPhaseDef().getState(); 55 1667276 : for (int i = 0; i < (int) links.size(); i++) { 56 1343520 : if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) { 57 520364 : if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) { 58 : // was not saved before 59 19026 : myPreviousLinkStates[i] = currentTime; 60 19026 : continue; 61 : } 62 : } else { 63 823156 : if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) { 64 : // was not yet green 65 804675 : continue; 66 : } 67 : const MSTrafficLightLogic::LinkVector& currLinks = links[i]; 68 : const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i); 69 18481 : SUMOTime lastOn = myPreviousLinkStates[i]; 70 36962 : for (int j = 0; j < (int) currLinks.size(); j++) { 71 18481 : MSLink* link = currLinks[j]; 72 18481 : myOutputDevice << " <tlsSwitch id=\"" << light->getID() 73 18481 : << "\" programID=\"" << light->getProgramID() 74 18481 : << "\" fromLane=\"" << currLanes[j]->getID() 75 18481 : << "\" toLane=\"" << link->getLane()->getID() 76 36962 : << "\" begin=\"" << time2string(lastOn) 77 36962 : << "\" end=\"" << time2string(currentTime) 78 36962 : << "\" duration=\"" << time2string(currentTime - lastOn) 79 18481 : << "\"/>\n"; 80 : } 81 : myPreviousLinkStates.erase(myPreviousLinkStates.find(i)); 82 : } 83 : } 84 323756 : return DELTA_T; 85 : } 86 : 87 : 88 : /****************************************************************************/