LCOV - code coverage report
Current view: top level - src/libsumo - ChargingStation.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 98.9 % 87 86
Test Date: 2025-05-20 15:36:59 Functions: 87.1 % 31 27

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2017-2025 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    ChargingStation.cpp
      15              : /// @author  Jakob Erdmann
      16              : /// @date    16.03.2020
      17              : ///
      18              : // C++ TraCI client API implementation
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include <microsim/MSNet.h>
      23              : #include <microsim/MSLane.h>
      24              : #include <microsim/MSStoppingPlace.h>
      25              : #include <microsim/trigger/MSChargingStation.h>
      26              : #include <libsumo/TraCIConstants.h>
      27              : #include <utils/common/SUMOTime.h>
      28              : #include "Helper.h"
      29              : #include "ChargingStation.h"
      30              : 
      31              : 
      32              : namespace libsumo {
      33              : // ===========================================================================
      34              : // static member initializations
      35              : // ===========================================================================
      36              : 
      37              : SubscriptionResults ChargingStation::mySubscriptionResults;
      38              : ContextSubscriptionResults ChargingStation::myContextSubscriptionResults;
      39              : 
      40              : // ===========================================================================
      41              : // static member definitions
      42              : // ===========================================================================
      43              : 
      44              : std::vector<std::string>
      45          849 : ChargingStation::getIDList() {
      46              :     std::vector<std::string> ids;
      47         2723 :     for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_CHARGING_STATION)) {
      48         1874 :         ids.push_back(item.first);
      49              :     }
      50          847 :     std::sort(ids.begin(), ids.end());
      51          847 :     return ids;
      52            2 : }
      53              : 
      54              : 
      55              : int
      56           20 : ChargingStation::getIDCount() {
      57           20 :     return (int)getIDList().size();
      58              : }
      59              : 
      60              : 
      61              : std::string
      62           40 : ChargingStation::getLaneID(const std::string& stopID) {
      63           40 :     return getChargingStation(stopID)->getLane().getID();
      64              : }
      65              : 
      66              : double
      67           39 : ChargingStation::getStartPos(const std::string& stopID) {
      68           39 :     return getChargingStation(stopID)->getBeginLanePosition();
      69              : }
      70              : 
      71              : 
      72              : double
      73           39 : ChargingStation::getEndPos(const std::string& stopID) {
      74           39 :     return getChargingStation(stopID)->getEndLanePosition();
      75              : }
      76              : 
      77              : 
      78              : std::string
      79           39 : ChargingStation::getName(const std::string& stopID) {
      80           39 :     return getChargingStation(stopID)->getMyName();
      81              : }
      82              : 
      83              : 
      84              : int
      85          109 : ChargingStation::getVehicleCount(const std::string& stopID) {
      86          109 :     return (int)getChargingStation(stopID)->getStoppedVehicles().size();
      87              : }
      88              : 
      89              : 
      90              : std::vector<std::string>
      91          109 : ChargingStation::getVehicleIDs(const std::string& stopID) {
      92              :     std::vector<std::string> result;
      93          169 :     for (const SUMOVehicle* veh : getChargingStation(stopID)->getStoppedVehicles()) {
      94           60 :         result.push_back(veh->getID());
      95          109 :     }
      96          109 :     return result;
      97            0 : }
      98              : 
      99              : 
     100              : double
     101          529 : ChargingStation::getChargingPower(const std::string& stopID) {
     102          529 :     return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargingPower(true);
     103              : }
     104              : 
     105              : 
     106              : double
     107          533 : ChargingStation::getEfficiency(const std::string& stopID) {
     108          533 :     return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getEfficency();
     109              : }
     110              : 
     111              : 
     112              : double
     113           28 : ChargingStation::getChargeDelay(const std::string& stopID) {
     114           28 :     return STEPS2TIME(dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargeDelay());
     115              : }
     116              : 
     117              : 
     118              : int
     119           28 : ChargingStation::getChargeInTransit(const std::string& stopID) {
     120           28 :     return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargeInTransit();
     121              : }
     122              : 
     123              : 
     124              : void
     125          512 : ChargingStation::setChargingPower(const std::string& stopID, double power) {
     126          512 :     dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargingPower(power);
     127          511 : }
     128              : 
     129              : 
     130              : void
     131          512 : ChargingStation::setEfficiency(const std::string& stopID, double efficiency) {
     132          512 :     dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setEfficiency(efficiency);
     133          511 : }
     134              : 
     135              : 
     136              : void
     137           10 : ChargingStation::setChargeDelay(const std::string& stopID, double delay) {
     138           10 :     dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargeDelay(TIME2STEPS(delay));
     139           10 : }
     140              : 
     141              : 
     142              : void
     143           10 : ChargingStation::setChargeInTransit(const std::string& stopID, bool inTransit) {
     144           10 :     dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargeInTransit(inTransit);
     145           10 : }
     146              : 
     147              : 
     148              : std::string
     149           18 : ChargingStation::getParameter(const std::string& stopID, const std::string& param) {
     150           36 :     return getChargingStation(stopID)->getParameter(param, "");
     151              : }
     152              : 
     153              : 
     154           16 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(ChargingStation)
     155              : 
     156              : void
     157            1 : ChargingStation::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
     158            1 :     getChargingStation(stopID)->setParameter(key, value);
     159            1 : }
     160              : 
     161              : 
     162          268 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(ChargingStation, CHARGINGSTATION)
     163              : 
     164              : 
     165              : MSStoppingPlace*
     166         2556 : ChargingStation::getChargingStation(const std::string& id) {
     167         2556 :     return Helper::getStoppingPlace(id, SUMO_TAG_CHARGING_STATION);
     168              : }
     169              : 
     170              : 
     171              : std::shared_ptr<VariableWrapper>
     172          266 : ChargingStation::makeWrapper() {
     173          266 :     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
     174              : }
     175              : 
     176              : 
     177              : bool
     178         1461 : ChargingStation::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
     179         1461 :     switch (variable) {
     180          491 :         case TRACI_ID_LIST:
     181          491 :             return wrapper->wrapStringList(objID, variable, getIDList());
     182           14 :         case ID_COUNT:
     183           14 :             return wrapper->wrapInt(objID, variable, getIDCount());
     184           28 :         case VAR_LANE_ID:
     185           55 :             return wrapper->wrapString(objID, variable, getLaneID(objID));
     186           27 :         case VAR_POSITION:
     187           27 :             return wrapper->wrapDouble(objID, variable, getStartPos(objID));
     188           27 :         case VAR_LANEPOSITION:
     189           27 :             return wrapper->wrapDouble(objID, variable, getEndPos(objID));
     190           27 :         case VAR_NAME:
     191           54 :             return wrapper->wrapString(objID, variable, getName(objID));
     192           69 :         case VAR_STOP_STARTING_VEHICLES_NUMBER:
     193           69 :             return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
     194           69 :         case VAR_STOP_STARTING_VEHICLES_IDS:
     195           69 :             return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
     196          321 :         case VAR_CS_POWER:
     197          321 :             return wrapper->wrapDouble(objID, variable, getChargingPower(objID));
     198          325 :         case VAR_CS_EFFICIENCY:
     199          325 :             return wrapper->wrapDouble(objID, variable, getEfficiency(objID));
     200           20 :         case VAR_CS_CHARGE_DELAY:
     201           20 :             return wrapper->wrapDouble(objID, variable, STEPS2TIME(getChargeDelay(objID)));
     202           20 :         case VAR_CS_CHARGE_IN_TRANSIT:
     203           20 :             return wrapper->wrapInt(objID, variable, getChargeInTransit(objID));
     204           10 :         case libsumo::VAR_PARAMETER:
     205           10 :             paramData->readUnsignedByte();
     206           20 :             return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
     207            8 :         case libsumo::VAR_PARAMETER_WITH_KEY:
     208            8 :             paramData->readUnsignedByte();
     209           16 :             return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
     210              :         default:
     211              :             return false;
     212              :     }
     213              : }
     214              : 
     215              : }
     216              : 
     217              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1