Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-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 MultiEntryExit.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Mario Krumnow
17 : /// @author Jakob Erdmann
18 : /// @author Michael Behrisch
19 : /// @date 30.05.2012
20 : ///
21 : // C++ TraCI client API implementation
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/output/MSDetectorControl.h>
26 : #include <microsim/output/MSE2Collector.h>
27 : #include <microsim/MSNet.h>
28 : #include <libsumo/Helper.h>
29 : #include <libsumo/TraCIConstants.h>
30 : #include "MultiEntryExit.h"
31 :
32 :
33 : namespace libsumo {
34 : // ===========================================================================
35 : // static member initializations
36 : // ===========================================================================
37 : SubscriptionResults MultiEntryExit::mySubscriptionResults;
38 : ContextSubscriptionResults MultiEntryExit::myContextSubscriptionResults;
39 :
40 :
41 : // ===========================================================================
42 : // static member definitions
43 : // ===========================================================================
44 : std::vector<std::string>
45 439 : MultiEntryExit::getIDList() {
46 : std::vector<std::string> ids;
47 439 : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).insertIDs(ids);
48 437 : return ids;
49 2 : }
50 :
51 :
52 : int
53 16 : MultiEntryExit::getIDCount() {
54 16 : return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).size();
55 : }
56 :
57 :
58 : std::vector<std::string>
59 13 : MultiEntryExit::getEntryLanes(const std::string& detID) {
60 : std::vector<std::string> ids;
61 26 : for (const MSCrossSection& cs : getDetector(detID)->getEntries()) {
62 13 : ids.push_back(cs.myLane->getID());
63 : }
64 13 : return ids;
65 0 : }
66 :
67 :
68 : std::vector<std::string>
69 13 : MultiEntryExit::getExitLanes(const std::string& detID) {
70 : std::vector<std::string> ids;
71 26 : for (const MSCrossSection& cs : getDetector(detID)->getExits()) {
72 13 : ids.push_back(cs.myLane->getID());
73 : }
74 13 : return ids;
75 0 : }
76 :
77 :
78 : std::vector<double>
79 13 : MultiEntryExit::getEntryPositions(const std::string& detID) {
80 : std::vector<double> pos;
81 26 : for (const MSCrossSection& cs : getDetector(detID)->getEntries()) {
82 13 : pos.push_back(cs.myPosition);
83 : }
84 13 : return pos;
85 0 : }
86 :
87 :
88 : std::vector<double>
89 13 : MultiEntryExit::getExitPositions(const std::string& detID) {
90 : std::vector<double> pos;
91 26 : for (const MSCrossSection& cs : getDetector(detID)->getExits()) {
92 13 : pos.push_back(cs.myPosition);
93 : }
94 13 : return pos;
95 0 : }
96 :
97 :
98 : int
99 587 : MultiEntryExit::getLastStepVehicleNumber(const std::string& detID) {
100 587 : return getDetector(detID)->getVehiclesWithin();
101 : }
102 :
103 :
104 : double
105 55 : MultiEntryExit::getLastStepMeanSpeed(const std::string& detID) {
106 55 : return getDetector(detID)->getCurrentMeanSpeed();
107 : }
108 :
109 :
110 : std::vector<std::string>
111 255 : MultiEntryExit::getLastStepVehicleIDs(const std::string& detID) {
112 255 : return getDetector(detID)->getCurrentVehicleIDs();
113 : }
114 :
115 :
116 : int
117 13 : MultiEntryExit::getLastStepHaltingNumber(const std::string& detID) {
118 13 : return getDetector(detID)->getCurrentHaltingNumber();
119 : }
120 :
121 :
122 : double
123 18 : MultiEntryExit::getLastIntervalMeanTravelTime(const std::string& detID) {
124 18 : return getDetector(detID)->getLastIntervalMeanTravelTime();
125 : }
126 :
127 :
128 : double
129 18 : MultiEntryExit::getLastIntervalMeanHaltsPerVehicle(const std::string& detID) {
130 18 : return getDetector(detID)->getLastIntervalMeanHaltsPerVehicle();
131 : }
132 :
133 :
134 : double
135 18 : MultiEntryExit::getLastIntervalMeanTimeLoss(const std::string& detID) {
136 18 : return getDetector(detID)->getLastIntervalMeanTimeLoss();
137 : }
138 :
139 :
140 : int
141 18 : MultiEntryExit::getLastIntervalVehicleSum(const std::string& detID) {
142 18 : return getDetector(detID)->getLastIntervalVehicleSum();
143 : }
144 :
145 :
146 : std::string
147 26 : MultiEntryExit::getParameter(const std::string& detID, const std::string& param) {
148 52 : return getDetector(detID)->getParameter(param, "");
149 : }
150 :
151 :
152 16 : LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(MultiEntryExit)
153 :
154 :
155 : void
156 5 : MultiEntryExit::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
157 5 : getDetector(detID)->setParameter(name, value);
158 5 : }
159 :
160 :
161 280 : LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(MultiEntryExit, MULTIENTRYEXIT)
162 :
163 :
164 : MSE3Collector*
165 1398 : MultiEntryExit::getDetector(const std::string& id) {
166 2795 : MSE3Collector* e3 = dynamic_cast<MSE3Collector*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).get(id));
167 1398 : if (e3 == nullptr) {
168 2 : throw TraCIException("Multi entry exit detector '" + id + "' is not known");
169 : }
170 1397 : return e3;
171 : }
172 :
173 :
174 : std::shared_ptr<VariableWrapper>
175 269 : MultiEntryExit::makeWrapper() {
176 269 : return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
177 : }
178 :
179 :
180 : bool
181 1140 : MultiEntryExit::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
182 1140 : switch (variable) {
183 257 : case TRACI_ID_LIST:
184 257 : return wrapper->wrapStringList(objID, variable, getIDList());
185 14 : case ID_COUNT:
186 14 : return wrapper->wrapInt(objID, variable, getIDCount());
187 505 : case LAST_STEP_VEHICLE_NUMBER:
188 505 : return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
189 53 : case LAST_STEP_MEAN_SPEED:
190 53 : return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
191 173 : case LAST_STEP_VEHICLE_ID_LIST:
192 173 : return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
193 11 : case LAST_STEP_VEHICLE_HALTING_NUMBER:
194 11 : return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
195 14 : case VAR_LAST_INTERVAL_TRAVELTIME:
196 14 : return wrapper->wrapDouble(objID, variable, getLastIntervalMeanTravelTime(objID));
197 14 : case VAR_LAST_INTERVAL_MEAN_HALTING_NUMBER:
198 14 : return wrapper->wrapDouble(objID, variable, getLastIntervalMeanHaltsPerVehicle(objID));
199 14 : case VAR_TIMELOSS:
200 14 : return wrapper->wrapDouble(objID, variable, getLastIntervalMeanTimeLoss(objID));
201 14 : case VAR_LAST_INTERVAL_VEHICLE_NUMBER:
202 14 : return wrapper->wrapInt(objID, variable, getLastIntervalVehicleSum(objID));
203 11 : case VAR_LANES:
204 11 : return wrapper->wrapStringList(objID, variable, getEntryLanes(objID));
205 11 : case VAR_EXIT_LANES:
206 11 : return wrapper->wrapStringList(objID, variable, getExitLanes(objID));
207 11 : case VAR_POSITION:
208 11 : return wrapper->wrapDoubleList(objID, variable, getEntryPositions(objID));
209 11 : case VAR_EXIT_POSITIONS:
210 11 : return wrapper->wrapDoubleList(objID, variable, getExitPositions(objID));
211 14 : case libsumo::VAR_PARAMETER:
212 14 : paramData->readUnsignedByte();
213 28 : return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
214 8 : case libsumo::VAR_PARAMETER_WITH_KEY:
215 8 : paramData->readUnsignedByte();
216 16 : return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
217 : default:
218 : return false;
219 : }
220 : }
221 :
222 :
223 : }
224 :
225 :
226 : /****************************************************************************/
|