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_SaveTLSSwitchStates.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date 08.05.2007
18 : ///
19 : // Writes the switch times of a tls into a file when the tls switches
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include "Command_SaveTLSSwitchStates.h"
24 : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
25 : #include <microsim/MSEventControl.h>
26 : #include <microsim/MSNet.h>
27 : #include <utils/common/UtilExceptions.h>
28 : #include <utils/common/MsgHandler.h>
29 : #include <utils/iodevices/OutputDevice.h>
30 :
31 :
32 : // ===========================================================================
33 : // method definitions
34 : // ===========================================================================
35 433 : Command_SaveTLSSwitchStates::Command_SaveTLSSwitchStates(const MSTLLogicControl::TLSLogicVariants& logics,
36 433 : OutputDevice& od)
37 433 : : myOutputDevice(od), myLogics(logics) {
38 433 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
39 866 : myOutputDevice.writeXMLHeader("tlsStates", "tlsstates_file.xsd");
40 433 : }
41 :
42 :
43 862 : Command_SaveTLSSwitchStates::~Command_SaveTLSSwitchStates() {
44 862 : }
45 :
46 :
47 : SUMOTime
48 3142618 : Command_SaveTLSSwitchStates::execute(SUMOTime currentTime) {
49 3142618 : const std::string& state = myLogics.getActive()->getCurrentPhaseDef().getState();
50 3142618 : if (state != myPreviousState || myLogics.getActive()->getProgramID() != myPreviousProgramID) {
51 39909 : myOutputDevice.openTag("tlsState");
52 39909 : myOutputDevice.writeAttr(SUMO_ATTR_TIME, time2string(currentTime));
53 39909 : myOutputDevice.writeAttr(SUMO_ATTR_ID, myLogics.getActive()->getID());
54 39909 : myOutputDevice.writeAttr(SUMO_ATTR_PROGRAMID, myLogics.getActive()->getProgramID());
55 39909 : myOutputDevice.writeAttr(SUMO_ATTR_PHASE, myLogics.getActive()->getCurrentPhaseIndex());
56 39909 : myOutputDevice.writeAttr(SUMO_ATTR_STATE, myLogics.getActive()->getCurrentPhaseDef().getState());
57 39909 : if (!myLogics.getActive()->getCurrentPhaseDef().getName().empty()) {
58 3571 : myOutputDevice.writeAttr(SUMO_ATTR_NAME, myLogics.getActive()->getCurrentPhaseDef().getName());
59 : }
60 79818 : myOutputDevice.closeTag();
61 : myPreviousState = state;
62 39909 : myPreviousProgramID = myLogics.getActive()->getProgramID();
63 : }
64 3142618 : return DELTA_T;
65 : }
66 :
67 :
68 : /****************************************************************************/
|