Eclipse SUMO - Simulation of Urban MObility
libsumo/RouteProbe.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/MSEdge.h>
24 #include <microsim/MSRoute.h>
27 #include <libsumo/TraCIConstants.h>
28 #include "Helper.h"
29 #include "RouteProbe.h"
30 
31 
32 namespace libsumo {
33 // ===========================================================================
34 // static member initializations
35 // ===========================================================================
36 SubscriptionResults RouteProbe::mySubscriptionResults;
37 ContextSubscriptionResults RouteProbe::myContextSubscriptionResults;
38 
39 
40 // ===========================================================================
41 // static member definitions
42 // ===========================================================================
43 std::vector<std::string>
44 RouteProbe::getIDList() {
45  std::vector<std::string> ids;
47  return ids;
48 }
49 
50 
51 int
52 RouteProbe::getIDCount() {
53  return (int)getIDList().size();
54 }
55 
56 std::string
57 RouteProbe::getEdgeID(const std::string& probeID) {
58  MSRouteProbe* rp = getRouteProbe(probeID);
59  return rp->getEdge()->getID();
60 }
61 
62 std::string
63 RouteProbe::sampleLastRouteID(const std::string& probeID) {
64  MSRouteProbe* rp = getRouteProbe(probeID);
65  ConstMSRoutePtr route = rp->sampleRoute(true);
66  if (route == nullptr) {
67  throw TraCIException("RouteProbe '" + probeID + "' did not collect any routes yet");
68  }
69  return route->getID();
70 }
71 
72 std::string
73 RouteProbe::sampleCurrentRouteID(const std::string& probeID) {
74  MSRouteProbe* rp = getRouteProbe(probeID);
75  ConstMSRoutePtr route = rp->sampleRoute(false);
76  if (route == nullptr) {
77  throw TraCIException("RouteProbe '" + probeID + "' did not collect any routes yet");
78  }
79  return route->getID();
80 }
81 
82 std::string
83 RouteProbe::getParameter(const std::string& /* probeID */, const std::string& /* param */) {
84  return "";
85 }
86 
88 
89 void
90 RouteProbe::setParameter(const std::string& /* probeID */, const std::string& /* key */, const std::string& /* value */) {
91  //MSRouteProbe* rp = getRouteProbe(probeID);
92  //r->setParameter(key, value);
93 }
94 
95 
97 
98 
100 RouteProbe::getRouteProbe(const std::string& id) {
102  if (rp == nullptr) {
103  throw TraCIException("Lane area detector '" + id + "' is not known");
104  }
105  return rp;
106 }
107 
108 
109 std::shared_ptr<VariableWrapper>
110 RouteProbe::makeWrapper() {
111  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
112 }
113 
114 
115 bool
116 RouteProbe::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
117  switch (variable) {
118  case TRACI_ID_LIST:
119  return wrapper->wrapStringList(objID, variable, getIDList());
120  case ID_COUNT:
121  return wrapper->wrapInt(objID, variable, getIDCount());
122  case VAR_ROAD_ID:
123  return wrapper->wrapString(objID, variable, getEdgeID(objID));
124  case VAR_SAMPLE_LAST:
125  return wrapper->wrapString(objID, variable, sampleLastRouteID(objID));
126  case VAR_SAMPLE_CURRENT:
127  return wrapper->wrapString(objID, variable, sampleCurrentRouteID(objID));
129  paramData->readUnsignedByte();
130  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
132  paramData->readUnsignedByte();
133  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
134  default:
135  return false;
136  }
137 }
138 
139 }
140 
141 
142 /****************************************************************************/
std::shared_ptr< const MSRoute > ConstMSRoutePtr
Definition: Route.h:31
@ SUMO_TAG_ROUTEPROBE
a routeprobe detector
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:441
Writes routes of vehicles passing a certain edge.
Definition: MSRouteProbe.h:58
const MSEdge * getEdge()
Definition: MSRouteProbe.h:128
ConstMSRoutePtr sampleRoute(bool last=true) const
const std::string & getID() const
Returns the id.
Definition: Named.h:74
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
C++ TraCI client API implementation.
Definition: RouteProbe.h:29
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
TRACI_CONST int VAR_SAMPLE_LAST
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:338
TRACI_CONST int VAR_SAMPLE_CURRENT
TRACI_CONST int VAR_ROAD_ID
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_PARAMETER_WITH_KEY