Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
libsumo/OverheadWire.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2024 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>
27#include "Helper.h"
28#include "OverheadWire.h"
29
30
31namespace libsumo {
32// ===========================================================================
33// static member initializations
34// ===========================================================================
35SubscriptionResults OverheadWire::mySubscriptionResults;
36ContextSubscriptionResults OverheadWire::myContextSubscriptionResults;
37
38
39// ===========================================================================
40// static member definitions
41// ===========================================================================
42std::vector<std::string>
43OverheadWire::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
52int
53OverheadWire::getIDCount() {
54 return (int)getIDList().size();
55}
56
57
58std::string
59OverheadWire::getLaneID(const std::string& stopID) {
60 return getOverheadWire(stopID)->getLane().getID();
61}
62
63
64double
65OverheadWire::getStartPos(const std::string& stopID) {
66 return getOverheadWire(stopID)->getBeginLanePosition();
67}
68
69double
70OverheadWire::getEndPos(const std::string& stopID) {
71 return getOverheadWire(stopID)->getEndLanePosition();
72}
73
74std::string
75OverheadWire::getName(const std::string& stopID) {
76 return getOverheadWire(stopID)->getMyName();
77}
78
79int
80OverheadWire::getVehicleCount(const std::string& stopID) {
81 MSOverheadWire* wire = dynamic_cast<MSOverheadWire*>(getOverheadWire(stopID));
82 return (int)wire->getChargingVehicles().size();
83}
84
85std::vector<std::string>
86OverheadWire::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
97std::string
98OverheadWire::getParameter(const std::string& stopID, const std::string& param) {
99 const MSStoppingPlace* s = getOverheadWire(stopID);
100 return s->getParameter(param, "");
101}
102
104
105void
106OverheadWire::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
113
114
116OverheadWire::getOverheadWire(const std::string& id) {
118 if (s == nullptr) {
119 throw TraCIException("OverheadWire '" + id + "' is not known");
120 }
121 return s;
122}
123
124
125std::shared_ptr<VariableWrapper>
126OverheadWire::makeWrapper() {
127 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
128}
129
130
131bool
132OverheadWire::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));
147 return wrapper->wrapInt(objID, variable, getVehicleCount(objID));
149 return wrapper->wrapStringList(objID, variable, getVehicleIDs(objID));
151 paramData->readUnsignedByte();
152 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
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/****************************************************************************/
@ OVERHEADWIRE
@ SUMO_TAG_OVERHEAD_WIRE_SEGMENT
An overhead wire segment.
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition TraCIDefs.h:123
The simulated network and simulation perfomer.
Definition MSNet.h:89
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
MSStoppingPlace * getStoppingPlace(const std::string &id, const SumoXMLTag category) const
Returns the named stopping place of the given category.
Definition MSNet.cpp:1381
Definition of overhead wire segment.
const std::vector< SUMOVehicle * > & getChargingVehicles() const
A lane area vehicles can halt at.
C++ TraCI client API implementation.
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:62
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:338
TRACI_CONST int VAR_POSITION
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition TraCIDefs.h:337
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