Eclipse SUMO - Simulation of Urban MObility
libsumo/ParkingArea.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>
25 #include <libsumo/TraCIConstants.h>
26 #include "Helper.h"
27 #include "ParkingArea.h"
28 
29 
30 namespace libsumo {
31 // ===========================================================================
32 // static member initializations
33 // ===========================================================================
34 SubscriptionResults ParkingArea::mySubscriptionResults;
35 ContextSubscriptionResults ParkingArea::myContextSubscriptionResults;
36 
37 
38 // ===========================================================================
39 // static member definitions
40 // ===========================================================================
41 std::vector<std::string>
42 ParkingArea::getIDList() {
43  std::vector<std::string> ids;
44  for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_PARKING_AREA)) {
45  ids.push_back(item.first);
46  }
47  std::sort(ids.begin(), ids.end());
48  return ids;
49 }
50 
51 int
52 ParkingArea::getIDCount() {
53  return (int)getIDList().size();
54 }
55 
56 
57 std::string
58 ParkingArea::getLaneID(const std::string& stopID) {
59  return getParkingArea(stopID)->getLane().getID();
60 }
61 
62 double
63 ParkingArea::getStartPos(const std::string& stopID) {
64  return getParkingArea(stopID)->getBeginLanePosition();
65 }
66 
67 double
68 ParkingArea::getEndPos(const std::string& stopID) {
69  return getParkingArea(stopID)->getEndLanePosition();
70 }
71 
72 std::string
73 ParkingArea::getName(const std::string& stopID) {
74  return getParkingArea(stopID)->getMyName();
75 }
76 
77 int
78 ParkingArea::getVehicleCount(const std::string& stopID) {
79  return (int)getParkingArea(stopID)->getStoppedVehicles().size();
80 }
81 
82 std::vector<std::string>
83 ParkingArea::getVehicleIDs(const std::string& stopID) {
84  std::vector<std::string> result;
85  for (const SUMOVehicle* veh : getParkingArea(stopID)->getStoppedVehicles()) {
86  result.push_back(veh->getID());
87  }
88  return result;
89 }
90 
91 
92 std::string
93 ParkingArea::getParameter(const std::string& stopID, const std::string& param) {
94  const MSStoppingPlace* s = getParkingArea(stopID);
95  return s->getParameter(param, "");
96 }
97 
99 
100 void
101 ParkingArea::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
102  MSStoppingPlace* s = getParkingArea(stopID);
103  s->setParameter(key, value);
104 }
105 
106 
108 
109 
111 ParkingArea::getParkingArea(const std::string& id) {
113  if (s == nullptr) {
114  throw TraCIException("ParkingArea '" + id + "' is not known");
115  }
116  return s;
117 }
118 
119 
120 std::shared_ptr<VariableWrapper>
121 ParkingArea::makeWrapper() {
122  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
123 }
124 
125 
126 bool
127 ParkingArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
128  switch (variable) {
129  case TRACI_ID_LIST:
130  return wrapper->wrapStringList(objID, variable, getIDList());
131  case ID_COUNT:
132  return wrapper->wrapInt(objID, variable, getIDCount());
133  case VAR_LANE_ID:
134  return wrapper->wrapString(objID, variable, getLaneID(objID));
135  case VAR_POSITION:
136  return wrapper->wrapDouble(objID, variable, getStartPos(objID));
137  case VAR_LANEPOSITION:
138  return wrapper->wrapDouble(objID, variable, getEndPos(objID));
139  case VAR_NAME:
140  return wrapper->wrapString(objID, variable, getName(objID));
142  return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
144  return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
145  default:
146  return false;
147  }
148 }
149 
150 }
151 
152 
153 /****************************************************************************/
@ SUMO_TAG_PARKING_AREA
A parking area.
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition: MSNet.cpp:1098
A lane area vehicles can halt at.
const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
C++ TraCI client API implementation.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
TRACI_CONST int VAR_NAME
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_NUMBER
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_POSITION
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_LANEPOSITION
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int VAR_STOP_STARTING_VEHICLES_IDS
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251