Eclipse SUMO - Simulation of Urban MObility
BusStop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2020 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 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <microsim/MSNet.h>
23 #include <microsim/MSLane.h>
26 #include <libsumo/TraCIConstants.h>
27 #include "Helper.h"
28 #include "BusStop.h"
29 
30 
31 namespace libsumo {
32 // ===========================================================================
33 // static member initializations
34 // ===========================================================================
37 
38 
39 // ===========================================================================
40 // static member definitions
41 // ===========================================================================
42 std::vector<std::string>
44  std::vector<std::string> ids;
45  for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_BUS_STOP)) {
46  ids.push_back(item.first);
47  }
48  std::sort(ids.begin(), ids.end());
49  return ids;
50 }
51 
52 int
54  return (int)getIDList().size();
55 }
56 
57 
58 std::string
59 BusStop::getLaneID(const std::string& stopID) {
60  return getBusStop(stopID)->getLane().getID();
61 }
62 
63 double
64 BusStop::getStartPos(const std::string& stopID) {
65  return getBusStop(stopID)->getBeginLanePosition();
66 }
67 
68 double
69 BusStop::getEndPos(const std::string& stopID) {
70  return getBusStop(stopID)->getEndLanePosition();
71 }
72 
73 std::string
74 BusStop::getName(const std::string& stopID) {
75  return getBusStop(stopID)->getMyName();
76 }
77 
78 int
79 BusStop::getVehicleCount(const std::string& stopID) {
80  return (int)getBusStop(stopID)->getStoppedVehicles().size();
81 }
82 
83 std::vector<std::string>
84 BusStop::getVehicleIDs(const std::string& stopID) {
85  std::vector<std::string> result;
86  for (const SUMOVehicle* veh : getBusStop(stopID)->getStoppedVehicles()) {
87  result.push_back(veh->getID());
88  }
89  return result;
90 }
91 
92 int
93 BusStop::getPersonCount(const std::string& stopID) {
94  return (int)getBusStop(stopID)->getNumWaitingPersons();
95 }
96 
97 std::vector<std::string>
98 BusStop::getPersonIDs(const std::string& stopID) {
99  std::vector<std::string> result;
100  getBusStop(stopID)->getWaitingPersonIDs(result);
101  return result;
102 }
103 
104 
105 std::string
106 BusStop::getParameter(const std::string& stopID, const std::string& param) {
107  const MSStoppingPlace* s = getBusStop(stopID);
108  return s->getParameter(param, "");
109 }
110 
112 
113 void
114 BusStop::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
115  MSStoppingPlace* s = getBusStop(stopID);
116  s->setParameter(key, value);
117 }
118 
119 
121 
122 
124 BusStop::getBusStop(const std::string& id) {
126  if (s == nullptr) {
127  throw TraCIException("BusStop '" + id + "' is not known");
128  }
129  return s;
130 }
131 
132 
133 std::shared_ptr<VariableWrapper>
135  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
136 }
137 
138 
139 bool
140 BusStop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
141  switch (variable) {
142  case TRACI_ID_LIST:
143  return wrapper->wrapStringList(objID, variable, getIDList());
144  case ID_COUNT:
145  return wrapper->wrapInt(objID, variable, getIDCount());
146  case VAR_LANE_ID:
147  return wrapper->wrapString(objID, variable, getLaneID(objID));
148  case VAR_POSITION:
149  return wrapper->wrapDouble(objID, variable, getStartPos(objID));
150  case VAR_LANEPOSITION:
151  return wrapper->wrapDouble(objID, variable, getEndPos(objID));
152  case VAR_NAME:
153  return wrapper->wrapString(objID, variable, getName(objID));
155  return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
157  return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
159  return wrapper->wrapInt(objID, variable, getPersonCount(objID));
161  return wrapper->wrapStringList(objID, variable, getPersonIDs(objID));
162  default:
163  return false;
164  }
165 }
166 
167 
168 }
169 
170 
171 /****************************************************************************/
static double getEndPos(const std::string &stopID)
Definition: BusStop.cpp:69
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:241
TRACI_CONST int VAR_LANE_ID
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
double getBeginLanePosition() const
Returns the begin position of this stop.
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:98
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:1073
static std::string getLaneID(const std::string &stopID)
Definition: BusStop.cpp:59
A lane area vehicles can halt at.
static ContextSubscriptionResults myContextSubscriptionResults
Definition: BusStop.h:75
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
TRACI_CONST int VAR_POSITION
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
static std::string getName(const std::string &stopID)
Definition: BusStop.cpp:74
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:242
static MSStoppingPlace * getBusStop(const std::string &id)
Definition: BusStop.cpp:124
const std::string & getID() const
Returns the id.
Definition: Named.h:73
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
const std::string & getMyName() const
TRACI_CONST int VAR_BUS_STOP_WAITING
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: BusStop.cpp:140
TRACI_CONST int VAR_BUS_STOP_WAITING_IDS
static std::string getParameter(const std::string &stopID, const std::string &param)
Definition: BusStop.cpp:106
TRACI_CONST int TRACI_ID_LIST
double getEndLanePosition() const
Returns the end position of this stop.
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_NUMBER
static int getVehicleCount(const std::string &stopID)
Definition: BusStop.cpp:79
Representation of a vehicle.
Definition: SUMOVehicle.h:58
static LIBSUMO_SUBSCRIPTION_API std::shared_ptr< VariableWrapper > makeWrapper()
Definition: BusStop.cpp:134
static int getPersonCount(const std::string &stopID)
Definition: BusStop.cpp:93
void getWaitingPersonIDs(std::vector< std::string > &into) const
get IDs of persons waiting at this stop
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:55
int getNumWaitingPersons() const
get number of persons waiting at this stop
static std::vector< std::string > getVehicleIDs(const std::string &stopID)
Definition: BusStop.cpp:84
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
static double getStartPos(const std::string &stopID)
Definition: BusStop.cpp:64
TRACI_CONST int VAR_LANEPOSITION
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_IDS
static std::vector< std::string > getPersonIDs(const std::string &stopID)
Definition: BusStop.cpp:98
std::vector< const SUMOVehicle * > getStoppedVehicles() const
get list of vehicles waiting at this stop
static LIBSUMO_GET_PARAMETER_WITH_KEY_API void setParameter(const std::string &stopID, const std::string &key, const std::string &value)
Definition: BusStop.cpp:114
const MSLane & getLane() const
Returns the lane this stop is located at.
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
static int getIDCount()
Definition: BusStop.cpp:53
static SubscriptionResults mySubscriptionResults
Definition: BusStop.h:74
TRACI_CONST int VAR_NAME
static std::vector< std::string > getIDList()
Definition: BusStop.cpp:43