Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/ChargingStation.cpp
169666 views
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
ChargingStation::getIDList() {
46
std::vector<std::string> ids;
47
for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_CHARGING_STATION)) {
48
ids.push_back(item.first);
49
}
50
std::sort(ids.begin(), ids.end());
51
return ids;
52
}
53
54
55
int
56
ChargingStation::getIDCount() {
57
return (int)getIDList().size();
58
}
59
60
61
std::string
62
ChargingStation::getLaneID(const std::string& stopID) {
63
return getChargingStation(stopID)->getLane().getID();
64
}
65
66
double
67
ChargingStation::getStartPos(const std::string& stopID) {
68
return getChargingStation(stopID)->getBeginLanePosition();
69
}
70
71
72
double
73
ChargingStation::getEndPos(const std::string& stopID) {
74
return getChargingStation(stopID)->getEndLanePosition();
75
}
76
77
78
std::string
79
ChargingStation::getName(const std::string& stopID) {
80
return getChargingStation(stopID)->getMyName();
81
}
82
83
84
int
85
ChargingStation::getVehicleCount(const std::string& stopID) {
86
return (int)getChargingStation(stopID)->getStoppedVehicles().size();
87
}
88
89
90
std::vector<std::string>
91
ChargingStation::getVehicleIDs(const std::string& stopID) {
92
std::vector<std::string> result;
93
for (const SUMOVehicle* veh : getChargingStation(stopID)->getStoppedVehicles()) {
94
result.push_back(veh->getID());
95
}
96
return result;
97
}
98
99
100
double
101
ChargingStation::getChargingPower(const std::string& stopID) {
102
return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargingPower(true);
103
}
104
105
106
double
107
ChargingStation::getEfficiency(const std::string& stopID) {
108
return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getEfficency();
109
}
110
111
112
double
113
ChargingStation::getChargeDelay(const std::string& stopID) {
114
return STEPS2TIME(dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargeDelay());
115
}
116
117
118
int
119
ChargingStation::getChargeInTransit(const std::string& stopID) {
120
return dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->getChargeInTransit();
121
}
122
123
124
void
125
ChargingStation::setChargingPower(const std::string& stopID, double power) {
126
dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargingPower(power);
127
}
128
129
130
void
131
ChargingStation::setEfficiency(const std::string& stopID, double efficiency) {
132
dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setEfficiency(efficiency);
133
}
134
135
136
void
137
ChargingStation::setChargeDelay(const std::string& stopID, double delay) {
138
dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargeDelay(TIME2STEPS(delay));
139
}
140
141
142
void
143
ChargingStation::setChargeInTransit(const std::string& stopID, bool inTransit) {
144
dynamic_cast<MSChargingStation*>(getChargingStation(stopID))->setChargeInTransit(inTransit);
145
}
146
147
148
std::string
149
ChargingStation::getParameter(const std::string& stopID, const std::string& param) {
150
return getChargingStation(stopID)->getParameter(param, "");
151
}
152
153
154
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(ChargingStation)
155
156
void
157
ChargingStation::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
158
getChargingStation(stopID)->setParameter(key, value);
159
}
160
161
162
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(ChargingStation, CHARGINGSTATION)
163
164
165
MSStoppingPlace*
166
ChargingStation::getChargingStation(const std::string& id) {
167
return Helper::getStoppingPlace(id, SUMO_TAG_CHARGING_STATION);
168
}
169
170
171
std::shared_ptr<VariableWrapper>
172
ChargingStation::makeWrapper() {
173
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
174
}
175
176
177
bool
178
ChargingStation::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
179
switch (variable) {
180
case TRACI_ID_LIST:
181
return wrapper->wrapStringList(objID, variable, getIDList());
182
case ID_COUNT:
183
return wrapper->wrapInt(objID, variable, getIDCount());
184
case VAR_LANE_ID:
185
return wrapper->wrapString(objID, variable, getLaneID(objID));
186
case VAR_POSITION:
187
return wrapper->wrapDouble(objID, variable, getStartPos(objID));
188
case VAR_LANEPOSITION:
189
return wrapper->wrapDouble(objID, variable, getEndPos(objID));
190
case VAR_NAME:
191
return wrapper->wrapString(objID, variable, getName(objID));
192
case VAR_STOP_STARTING_VEHICLES_NUMBER:
193
return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
194
case VAR_STOP_STARTING_VEHICLES_IDS:
195
return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
196
case VAR_CS_POWER:
197
return wrapper->wrapDouble(objID, variable, getChargingPower(objID));
198
case VAR_CS_EFFICIENCY:
199
return wrapper->wrapDouble(objID, variable, getEfficiency(objID));
200
case VAR_CS_CHARGE_DELAY:
201
return wrapper->wrapDouble(objID, variable, STEPS2TIME(getChargeDelay(objID)));
202
case VAR_CS_CHARGE_IN_TRANSIT:
203
return wrapper->wrapInt(objID, variable, getChargeInTransit(objID));
204
case libsumo::VAR_PARAMETER:
205
paramData->readUnsignedByte();
206
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
207
case libsumo::VAR_PARAMETER_WITH_KEY:
208
paramData->readUnsignedByte();
209
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
210
default:
211
return false;
212
}
213
}
214
215
}
216
217
/****************************************************************************/
218
219