Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/libsumo/RouteProbe.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 RouteProbe.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/MSEdge.h>
24
#include <microsim/MSRoute.h>
25
#include <microsim/output/MSDetectorControl.h>
26
#include <microsim/output/MSRouteProbe.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;
46
MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ROUTEPROBE).insertIDs(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
87
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(RouteProbe)
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
96
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(RouteProbe, ROUTEPROBE)
97
98
99
MSRouteProbe*
100
RouteProbe::getRouteProbe(const std::string& id) {
101
MSRouteProbe* rp = dynamic_cast<MSRouteProbe*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ROUTEPROBE).get(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));
128
case libsumo::VAR_PARAMETER:
129
paramData->readUnsignedByte();
130
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
131
case libsumo::VAR_PARAMETER_WITH_KEY:
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
/****************************************************************************/
143
144