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 137 : Command_SaveTLSSwitches::Command_SaveTLSSwitches(const MSTLLogicControl::TLSLogicVariants& logics,
39 137 : OutputDevice& od)
40 137 : : myOutputDevice(od), myLogics(logics) {
41 137 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
42 274 : myOutputDevice.writeXMLHeader("tlsSwitches", "tlsswitches_file.xsd");
43 137 : }
44 :
45 :
46 272 : Command_SaveTLSSwitches::~Command_SaveTLSSwitches() {
47 272 : }
48 :
49 :
50 : SUMOTime
51 164607 : Command_SaveTLSSwitches::execute(SUMOTime currentTime) {
52 164607 : MSTrafficLightLogic* light = myLogics.getActive();
53 : const MSTrafficLightLogic::LinkVectorVector& links = light->getLinks();
54 164607 : const std::string& state = light->getCurrentPhaseDef().getState();
55 871515 : for (int i = 0; i < (int) links.size(); i++) {
56 706908 : if (state[i] == LINKSTATE_TL_GREEN_MAJOR || state[i] == LINKSTATE_TL_GREEN_MINOR) {
57 273452 : if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
58 : // was not saved before
59 9852 : myPreviousLinkStates[i] = currentTime;
60 9852 : continue;
61 : }
62 : } else {
63 433456 : if (myPreviousLinkStates.find(i) == myPreviousLinkStates.end()) {
64 : // was not yet green
65 423905 : continue;
66 : }
67 : const MSTrafficLightLogic::LinkVector& currLinks = links[i];
68 : const MSTrafficLightLogic::LaneVector& currLanes = light->getLanesAt(i);
69 9551 : SUMOTime lastOn = myPreviousLinkStates[i];
70 19102 : for (int j = 0; j < (int) currLinks.size(); j++) {
71 9551 : MSLink* link = currLinks[j];
72 9551 : myOutputDevice << " <tlsSwitch id=\"" << light->getID()
73 9551 : << "\" programID=\"" << light->getProgramID()
74 9551 : << "\" fromLane=\"" << currLanes[j]->getID()
75 9551 : << "\" toLane=\"" << link->getLane()->getID()
76 19102 : << "\" begin=\"" << time2string(lastOn)
77 19102 : << "\" end=\"" << time2string(currentTime)
78 19102 : << "\" duration=\"" << time2string(currentTime - lastOn)
79 9551 : << "\"/>\n";
80 : }
81 : myPreviousLinkStates.erase(myPreviousLinkStates.find(i));
82 : }
83 : }
84 164607 : return DELTA_T;
85 : }
86 :
87 :
88 : /****************************************************************************/
|