1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    MSBatteryExport.cpp
11 /// @author  Mario Krumnow
12 /// @author  Tamas Kurczveil
13 /// @author  Pablo Alvarez Lopez
14 /// @date    20-12-13
15 /// @version $Id$
16 ///
17 // Realises dumping Battery Data
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <microsim/MSEdgeControl.h>
27 #include <microsim/MSEdge.h>
28 #include <microsim/MSLane.h>
29 #include <microsim/MSGlobals.h>
30 #include <utils/iodevices/OutputDevice.h>
31 #include <microsim/MSNet.h>
32 #include <microsim/MSVehicle.h>
33 #include <microsim/MSVehicleControl.h>
34 #include <microsim/devices/MSDevice_Battery.h>
35 #include "MSBatteryExport.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 void
write(OutputDevice & of,SUMOTime timestep,int precision)42 MSBatteryExport::write(OutputDevice& of, SUMOTime timestep, int precision) {
43     of.openTag(SUMO_TAG_TIMESTEP).writeAttr(SUMO_ATTR_TIME, time2string(timestep));
44     of.setPrecision(precision);
45 
46     MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
47     MSVehicleControl::constVehIt it = vc.loadedVehBegin();
48     MSVehicleControl::constVehIt end = vc.loadedVehEnd();
49     for (; it != end; ++it) {
50         const MSVehicle* veh = static_cast<const MSVehicle*>((*it).second);
51 
52         if (!veh->isOnRoad()) {
53             continue;
54         }
55 
56         std::string fclass = veh->getVehicleType().getID();
57         fclass = fclass.substr(0, fclass.find_first_of("@"));
58 
59         if (static_cast<MSDevice_Battery*>(veh->getDevice(typeid(MSDevice_Battery))) != nullptr) {
60             MSDevice_Battery* batteryToExport = dynamic_cast<MSDevice_Battery*>(veh->getDevice(typeid(MSDevice_Battery)));
61             if (batteryToExport->getMaximumBatteryCapacity() > 0) {
62                 // Open Row
63                 of.openTag(SUMO_TAG_VEHICLE);
64                 // Write ID
65                 of.writeAttr(SUMO_ATTR_ID, veh->getID());
66                 // Write consum
67                 of.writeAttr(SUMO_ATTR_ENERGYCONSUMED, batteryToExport->getConsum());
68                 // Write Actual battery capacity
69                 of.writeAttr(SUMO_ATTR_ACTUALBATTERYCAPACITY, batteryToExport->getActualBatteryCapacity());
70                 // Write Maximum battery capacity
71                 of.writeAttr(SUMO_ATTR_MAXIMUMBATTERYCAPACITY, batteryToExport->getMaximumBatteryCapacity());
72                 // Write Charging Station ID
73                 of.writeAttr(SUMO_ATTR_CHARGINGSTATIONID, batteryToExport->getChargingStationID());
74                 // Write Charge charged in the Battery
75                 of.writeAttr(SUMO_ATTR_ENERGYCHARGED, batteryToExport->getEnergyCharged());
76                 // Write ChargeInTransit
77                 if (batteryToExport->isChargingInTransit()) {
78                     of.writeAttr(SUMO_ATTR_ENERGYCHARGEDINTRANSIT, batteryToExport->getEnergyCharged());
79                 } else {
80                     of.writeAttr(SUMO_ATTR_ENERGYCHARGEDINTRANSIT, 0.00);
81                 }
82                 // Write ChargingStopped
83                 if (batteryToExport->isChargingStopped()) {
84                     of.writeAttr(SUMO_ATTR_ENERGYCHARGEDSTOPPED, batteryToExport->getEnergyCharged());
85                 } else {
86                     of.writeAttr(SUMO_ATTR_ENERGYCHARGEDSTOPPED, 0.00);
87                 }
88                 // Write Speed
89                 of.writeAttr(SUMO_ATTR_SPEED, veh->getSpeed());
90                 // Write Acceleration
91                 of.writeAttr(SUMO_ATTR_ACCELERATION, veh->getAcceleration());
92 
93                 Position pos = veh->getPosition();
94                 of.writeAttr(SUMO_ATTR_X, veh->getPosition().x());
95                 of.writeAttr(SUMO_ATTR_Y, veh->getPosition().y());
96 
97                 // Write Lane ID / edge ID
98                 if (MSGlobals::gUseMesoSim) {
99                     of.writeAttr(SUMO_ATTR_EDGE, veh->getEdge()->getID());
100                 } else {
101                     of.writeAttr(SUMO_ATTR_LANE, veh->getLane()->getID());
102                 }
103                 // Write vehicle position in the lane
104                 of.writeAttr(SUMO_ATTR_POSONLANE, veh->getPositionOnLane());
105                 // Write Time stopped (In all cases)
106                 of.writeAttr(SUMO_ATTR_TIMESTOPPED, batteryToExport->getVehicleStopped());
107                 // Close Row
108                 of.closeTag();
109             }
110         }
111     }
112     of.closeTag();
113 }
114