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