Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/OverheadWire.cpp
169665 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 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
OverheadWire::getIDList() {
44
std::vector<std::string> ids;
45
for (auto& item : MSNet::getInstance()->getStoppingPlaces(SUMO_TAG_OVERHEAD_WIRE_SEGMENT)) {
46
ids.push_back(item.first);
47
}
48
std::sort(ids.begin(), ids.end());
49
return ids;
50
}
51
52
int
53
OverheadWire::getIDCount() {
54
return (int)getIDList().size();
55
}
56
57
58
std::string
59
OverheadWire::getLaneID(const std::string& stopID) {
60
return getOverheadWire(stopID)->getLane().getID();
61
}
62
63
64
double
65
OverheadWire::getStartPos(const std::string& stopID) {
66
return getOverheadWire(stopID)->getBeginLanePosition();
67
}
68
69
double
70
OverheadWire::getEndPos(const std::string& stopID) {
71
return getOverheadWire(stopID)->getEndLanePosition();
72
}
73
74
std::string
75
OverheadWire::getName(const std::string& stopID) {
76
return getOverheadWire(stopID)->getMyName();
77
}
78
79
int
80
OverheadWire::getVehicleCount(const std::string& stopID) {
81
MSOverheadWire* wire = dynamic_cast<MSOverheadWire*>(getOverheadWire(stopID));
82
return (int)wire->getChargingVehicles().size();
83
}
84
85
std::vector<std::string>
86
OverheadWire::getVehicleIDs(const std::string& stopID) {
87
MSOverheadWire* wire = dynamic_cast<MSOverheadWire*>(getOverheadWire(stopID));
88
std::vector<std::string> result;
89
for (const SUMOVehicle* veh : wire->getChargingVehicles()) {
90
result.push_back(veh->getID());
91
}
92
return result;
93
}
94
95
96
97
std::string
98
OverheadWire::getParameter(const std::string& stopID, const std::string& param) {
99
const MSStoppingPlace* s = getOverheadWire(stopID);
100
return s->getParameter(param, "");
101
}
102
103
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(OverheadWire)
104
105
void
106
OverheadWire::setParameter(const std::string& stopID, const std::string& key, const std::string& value) {
107
MSStoppingPlace* s = getOverheadWire(stopID);
108
s->setParameter(key, value);
109
}
110
111
112
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(OverheadWire, OVERHEADWIRE)
113
114
115
MSStoppingPlace*
116
OverheadWire::getOverheadWire(const std::string& id) {
117
MSStoppingPlace* s = MSNet::getInstance()->getStoppingPlace(id, SUMO_TAG_OVERHEAD_WIRE_SEGMENT);
118
if (s == nullptr) {
119
throw TraCIException("OverheadWire '" + id + "' is not known");
120
}
121
return s;
122
}
123
124
125
std::shared_ptr<VariableWrapper>
126
OverheadWire::makeWrapper() {
127
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
128
}
129
130
131
bool
132
OverheadWire::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
133
switch (variable) {
134
case TRACI_ID_LIST:
135
return wrapper->wrapStringList(objID, variable, getIDList());
136
case ID_COUNT:
137
return wrapper->wrapInt(objID, variable, getIDCount());
138
case VAR_LANE_ID:
139
return wrapper->wrapString(objID, variable, getLaneID(objID));
140
case VAR_POSITION:
141
return wrapper->wrapDouble(objID, variable, getStartPos(objID));
142
case VAR_LANEPOSITION:
143
return wrapper->wrapDouble(objID, variable, getEndPos(objID));
144
case VAR_NAME:
145
return wrapper->wrapString(objID, variable, getName(objID));
146
case VAR_STOP_STARTING_VEHICLES_NUMBER:
147
return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
148
case VAR_STOP_STARTING_VEHICLES_IDS:
149
return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
150
case libsumo::VAR_PARAMETER:
151
paramData->readUnsignedByte();
152
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
153
case libsumo::VAR_PARAMETER_WITH_KEY:
154
paramData->readUnsignedByte();
155
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
156
default:
157
return false;
158
}
159
}
160
161
}
162
163
164
/****************************************************************************/
165
166