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_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 377 : Command_SaveTLSState::Command_SaveTLSState(const MSTLLogicControl::TLSLogicVariants& logics,
36 377 : OutputDevice& od, bool saveDetectors, bool saveConditions) :
37 377 : myOutputDevice(od),
38 377 : myLogics(logics),
39 377 : mySaveDetectors(saveDetectors),
40 377 : mySaveConditions(saveConditions) {
41 377 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(this);
42 377 : }
43 :
44 :
45 752 : Command_SaveTLSState::~Command_SaveTLSState() {
46 752 : }
47 :
48 :
49 : SUMOTime
50 240252 : Command_SaveTLSState::execute(SUMOTime currentTime) {
51 240252 : if (!myOutputDevice.wroteHeader()) {
52 : // delay writing header to ensure tls is initialized
53 : std::map<SumoXMLAttr, std::string> attrs;
54 324 : if (mySaveDetectors) {
55 : std::vector<std::string> IDs;
56 88 : for (auto item : myLogics.getActive()->getDetectorStates()) {
57 79 : IDs.push_back(item.first);
58 : }
59 9 : attrs[SUMO_ATTR_DETECTORS] = joinToString(IDs, " ");
60 :
61 9 : }
62 324 : if (mySaveConditions) {
63 : std::vector<std::string> IDs;
64 61 : for (auto item : myLogics.getActive()->getConditions()) {
65 50 : IDs.push_back(item.first);
66 : }
67 11 : attrs[SUMO_ATTR_CONDITIONS] = joinToString(IDs, " ");
68 11 : }
69 648 : myOutputDevice.writeXMLHeader("tlsStates", "tlsstates_file.xsd", attrs);
70 : }
71 240252 : myOutputDevice.openTag("tlsState");
72 240252 : myOutputDevice.writeAttr(SUMO_ATTR_TIME, time2string(currentTime));
73 240252 : myOutputDevice.writeAttr(SUMO_ATTR_ID, myLogics.getActive()->getID());
74 240252 : myOutputDevice.writeAttr(SUMO_ATTR_PROGRAMID, myLogics.getActive()->getProgramID());
75 240252 : myOutputDevice.writeAttr(SUMO_ATTR_PHASE, myLogics.getActive()->getCurrentPhaseIndex());
76 240252 : myOutputDevice.writeAttr(SUMO_ATTR_STATE, myLogics.getActive()->getCurrentPhaseDef().getState());
77 240252 : if (!myLogics.getActive()->getCurrentPhaseDef().getName().empty()) {
78 10440 : myOutputDevice.writeAttr(SUMO_ATTR_NAME, myLogics.getActive()->getCurrentPhaseDef().getName());
79 : }
80 240252 : if (mySaveDetectors) {
81 : std::vector<int> states;
82 70981 : for (auto item : myLogics.getActive()->getDetectorStates()) {
83 64394 : states.push_back((int)item.second);
84 : }
85 6587 : myOutputDevice.writeAttr(SUMO_ATTR_DETECTORS, joinToString(states, " "));
86 :
87 6587 : }
88 240252 : if (mySaveConditions) {
89 : std::vector<double> states;
90 35490 : for (auto item : myLogics.getActive()->getConditions()) {
91 28723 : states.push_back(item.second);
92 : }
93 6767 : myOutputDevice.writeAttr(SUMO_ATTR_CONDITIONS, joinToString(states, " "));
94 6767 : }
95 240252 : myOutputDevice.closeTag();
96 240252 : return DELTA_T;
97 : }
98 :
99 :
100 : /****************************************************************************/
|