Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 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_SaveTLSState.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date 15 Feb 2004
18 : ///
19 : // Writes the state of the tls to a file (in each second)
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include "Command_SaveTLSState.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 401 : Command_SaveTLSState::Command_SaveTLSState(const MSTLLogicControl::TLSLogicVariants& logics,
36 401 : OutputDevice& od, bool saveDetectors, bool saveConditions) :
37 401 : myOutputDevice(od),
38 401 : myLogics(logics),
39 401 : mySaveDetectors(saveDetectors),
40 401 : mySaveConditions(saveConditions) {
41 401 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
42 401 : }
43 :
44 :
45 800 : Command_SaveTLSState::~Command_SaveTLSState() {
46 800 : }
47 :
48 :
49 : SUMOTime
50 262180 : Command_SaveTLSState::execute(SUMOTime currentTime) {
51 262180 : if (!myOutputDevice.wroteHeader()) {
52 : // delay writing header to ensure tls is initialized
53 : std::map<SumoXMLAttr, std::string> attrs;
54 347 : if (mySaveDetectors) {
55 : std::vector<std::string> IDs;
56 139 : for (auto item : myLogics.getActive()->getDetectorStates()) {
57 126 : IDs.push_back(item.first);
58 : }
59 13 : attrs[SUMO_ATTR_DETECTORS] = joinToString(IDs, " ");
60 :
61 13 : }
62 347 : if (mySaveConditions) {
63 : std::vector<std::string> IDs;
64 105 : for (auto item : myLogics.getActive()->getConditions()) {
65 86 : IDs.push_back(item.first);
66 : }
67 19 : attrs[SUMO_ATTR_CONDITIONS] = joinToString(IDs, " ");
68 19 : }
69 694 : myOutputDevice.writeXMLHeader("tlsStates", "tlsstates_file.xsd", attrs);
70 : }
71 262180 : myOutputDevice.openTag("tlsState");
72 262180 : myOutputDevice.writeAttr(SUMO_ATTR_TIME, time2string(currentTime));
73 262180 : myOutputDevice.writeAttr(SUMO_ATTR_ID, myLogics.getActive()->getID());
74 262180 : myOutputDevice.writeAttr(SUMO_ATTR_PROGRAMID, myLogics.getActive()->getProgramID());
75 262180 : myOutputDevice.writeAttr(SUMO_ATTR_PHASE, myLogics.getActive()->getCurrentPhaseIndex());
76 262180 : myOutputDevice.writeAttr(SUMO_ATTR_STATE, myLogics.getActive()->getCurrentPhaseDef().getState());
77 262180 : if (!myLogics.getActive()->getCurrentPhaseDef().getName().empty()) {
78 14706 : myOutputDevice.writeAttr(SUMO_ATTR_NAME, myLogics.getActive()->getCurrentPhaseDef().getName());
79 : }
80 262180 : if (mySaveDetectors) {
81 : std::vector<int> states;
82 135362 : for (auto item : myLogics.getActive()->getDetectorStates()) {
83 123082 : states.push_back((int)item.second);
84 : }
85 12280 : myOutputDevice.writeAttr(SUMO_ATTR_DETECTORS, joinToString(states, " "));
86 :
87 12280 : }
88 262180 : if (mySaveConditions) {
89 : std::vector<double> states;
90 71440 : for (auto item : myLogics.getActive()->getConditions()) {
91 56686 : states.push_back(item.second);
92 : }
93 14754 : myOutputDevice.writeAttr(SUMO_ATTR_CONDITIONS, joinToString(states, " "));
94 14754 : }
95 262180 : myOutputDevice.closeTag();
96 262180 : return DELTA_T;
97 : }
98 :
99 :
100 : /****************************************************************************/
|