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