LCOV - code coverage report
Current view: top level - src/libsumo - OverheadWire.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 48 65 73.8 %
Date: 2024-04-27 15:34:54 Functions: 11 23 47.8 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2017-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    OverheadWire.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/MSOverheadWire.h>
      26             : #include <libsumo/TraCIConstants.h>
      27             : #include "Helper.h"
      28             : #include "OverheadWire.h"
      29             : 
      30             : 
      31             : namespace libsumo {
      32             : // ===========================================================================
      33             : // static member initializations
      34             : // ===========================================================================
      35             : SubscriptionResults OverheadWire::mySubscriptionResults;
      36             : ContextSubscriptionResults OverheadWire::myContextSubscriptionResults;
      37             : 
      38             : 
      39             : // ===========================================================================
      40             : // static member definitions
      41             : // ===========================================================================
      42             : std::vector<std::string>
      43         206 : OverheadWire::getIDList() {
      44             :     std::vector<std::string> ids;
      45         362 :     for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_OVERHEAD_WIRE_SEGMENT)) {
      46         156 :         ids.push_back(item.first);
      47             :     }
      48         204 :     std::sort(ids.begin(), ids.end());
      49         204 :     return ids;
      50           2 : }
      51             : 
      52             : int
      53           6 : OverheadWire::getIDCount() {
      54           6 :     return (int)getIDList().size();
      55             : }
      56             : 
      57             : 
      58             : std::string
      59          12 : OverheadWire::getLaneID(const std::string& stopID) {
      60          12 :     return getOverheadWire(stopID)->getLane().getID();
      61             : }
      62             : 
      63             : 
      64             : double
      65          12 : OverheadWire::getStartPos(const std::string& stopID) {
      66          12 :     return getOverheadWire(stopID)->getBeginLanePosition();
      67             : }
      68             : 
      69             : double
      70          12 : OverheadWire::getEndPos(const std::string& stopID) {
      71          12 :     return getOverheadWire(stopID)->getEndLanePosition();
      72             : }
      73             : 
      74             : std::string
      75          12 : OverheadWire::getName(const std::string& stopID) {
      76          12 :     return getOverheadWire(stopID)->getMyName();
      77             : }
      78             : 
      79             : int
      80         120 : OverheadWire::getVehicleCount(const std::string& stopID) {
      81         120 :     MSOverheadWire* wire = dynamic_cast<MSOverheadWire*>(getOverheadWire(stopID));
      82         120 :     return (int)wire->getChargingVehicles().size();
      83             : }
      84             : 
      85             : std::vector<std::string>
      86         120 : OverheadWire::getVehicleIDs(const std::string& stopID) {
      87         120 :     MSOverheadWire* wire = dynamic_cast<MSOverheadWire*>(getOverheadWire(stopID));
      88             :     std::vector<std::string> result;
      89         234 :     for (const SUMOVehicle* veh : wire->getChargingVehicles()) {
      90         114 :         result.push_back(veh->getID());
      91             :     }
      92         120 :     return result;
      93           0 : }
      94             : 
      95             : 
      96             : 
      97             : std::string
      98           0 : OverheadWire::getParameter(const std::string& stopID, const std::string& param) {
      99           0 :     const MSStoppingPlace* s = getOverheadWire(stopID);
     100           0 :     return s->getParameter(param, "");
     101             : }
     102             : 
     103           0 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(OverheadWire)
     104             : 
     105             : void
     106           0 : OverheadWire::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
     107           0 :     MSStoppingPlace* s = getOverheadWire(stopID);
     108           0 :     s->setParameter(key, value);
     109           0 : }
     110             : 
     111             : 
     112           0 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(OverheadWire, OVERHEADWIRE)
     113             : 
     114             : 
     115             : MSStoppingPlace*
     116         288 : OverheadWire::getOverheadWire(const std::string& id) {
     117         288 :     MSStoppingPlace* s = MSNet::getInstance()->getStoppingPlace(id, SUMO_TAG_OVERHEAD_WIRE_SEGMENT);
     118         288 :     if (s == nullptr) {
     119           0 :         throw TraCIException("OverheadWire '" + id + "' is not known");
     120             :     }
     121         288 :     return s;
     122             : }
     123             : 
     124             : 
     125             : std::shared_ptr<VariableWrapper>
     126         267 : OverheadWire::makeWrapper() {
     127         267 :     return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
     128             : }
     129             : 
     130             : 
     131             : bool
     132         320 : OverheadWire::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
     133         320 :     switch (variable) {
     134         124 :         case TRACI_ID_LIST:
     135         124 :             return wrapper->wrapStringList(objID, variable, getIDList());
     136           4 :         case ID_COUNT:
     137           4 :             return wrapper->wrapInt(objID, variable, getIDCount());
     138           8 :         case VAR_LANE_ID:
     139          16 :             return wrapper->wrapString(objID, variable, getLaneID(objID));
     140           8 :         case VAR_POSITION:
     141           8 :             return wrapper->wrapDouble(objID, variable, getStartPos(objID));
     142           8 :         case VAR_LANEPOSITION:
     143           8 :             return wrapper->wrapDouble(objID, variable, getEndPos(objID));
     144           8 :         case VAR_NAME:
     145          16 :             return wrapper->wrapString(objID, variable, getName(objID));
     146          80 :         case VAR_STOP_STARTING_VEHICLES_NUMBER:
     147          80 :             return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
     148          80 :         case VAR_STOP_STARTING_VEHICLES_IDS:
     149          80 :             return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
     150           0 :         case libsumo::VAR_PARAMETER:
     151           0 :             paramData->readUnsignedByte();
     152           0 :             return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
     153           0 :         case libsumo::VAR_PARAMETER_WITH_KEY:
     154           0 :             paramData->readUnsignedByte();
     155           0 :             return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
     156             :         default:
     157             :             return false;
     158             :     }
     159             : }
     160             : 
     161             : }
     162             : 
     163             : 
     164             : /****************************************************************************/

Generated by: LCOV version 1.14