1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file MultiEntryExit.cpp
11 /// @author Daniel Krajzewicz
12 /// @author Mario Krumnow
13 /// @author Jakob Erdmann
14 /// @author Michael Behrisch
15 /// @date 30.05.2012
16 /// @version $Id$
17 ///
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20
21
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26
27 #include <microsim/output/MSDetectorControl.h>
28 #include <microsim/output/MSE2Collector.h>
29 #include <microsim/MSNet.h>
30 #include <libsumo/TraCIConstants.h>
31 #include "MultiEntryExit.h"
32
33
34 namespace libsumo {
35 // ===========================================================================
36 // static member initializations
37 // ===========================================================================
38 SubscriptionResults MultiEntryExit::mySubscriptionResults;
39 ContextSubscriptionResults MultiEntryExit::myContextSubscriptionResults;
40
41
42 // ===========================================================================
43 // static member definitions
44 // ===========================================================================
45 std::vector<std::string>
getIDList()46 MultiEntryExit::getIDList() {
47 std::vector<std::string> ids;
48 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).insertIDs(ids);
49 return ids;
50 }
51
52
53 int
getIDCount()54 MultiEntryExit::getIDCount() {
55 std::vector<std::string> ids;
56 return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).size();
57 }
58
59
60 int
getLastStepVehicleNumber(const std::string & detID)61 MultiEntryExit::getLastStepVehicleNumber(const std::string& detID) {
62 return getDetector(detID)->getVehiclesWithin();
63 }
64
65
66 double
getLastStepMeanSpeed(const std::string & detID)67 MultiEntryExit::getLastStepMeanSpeed(const std::string& detID) {
68 return getDetector(detID)->getCurrentMeanSpeed();
69 }
70
71
72 std::vector<std::string>
getLastStepVehicleIDs(const std::string & detID)73 MultiEntryExit::getLastStepVehicleIDs(const std::string& detID) {
74 return getDetector(detID)->getCurrentVehicleIDs();
75 }
76
77
78 int
getLastStepHaltingNumber(const std::string & detID)79 MultiEntryExit::getLastStepHaltingNumber(const std::string& detID) {
80 return getDetector(detID)->getCurrentHaltingNumber();
81 }
82
83
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(MultiEntryExit,MULTIENTRYEXIT)84 LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(MultiEntryExit, MULTIENTRYEXIT)
85
86
87 MSE3Collector*
88 MultiEntryExit::getDetector(const std::string& id) {
89 MSE3Collector* e3 = dynamic_cast<MSE3Collector*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_ENTRY_EXIT_DETECTOR).get(id));
90 if (e3 == nullptr) {
91 throw TraCIException("Multi entry exit detector '" + id + "' is not known");
92 }
93 return e3;
94 }
95
96
97 std::shared_ptr<VariableWrapper>
makeWrapper()98 MultiEntryExit::makeWrapper() {
99 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
100 }
101
102
103 bool
handleVariable(const std::string & objID,const int variable,VariableWrapper * wrapper)104 MultiEntryExit::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
105 switch (variable) {
106 case TRACI_ID_LIST:
107 return wrapper->wrapStringList(objID, variable, getIDList());
108 case ID_COUNT:
109 return wrapper->wrapInt(objID, variable, getIDCount());
110 case LAST_STEP_VEHICLE_NUMBER:
111 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
112 case LAST_STEP_MEAN_SPEED:
113 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
114 case LAST_STEP_VEHICLE_ID_LIST:
115 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
116 case LAST_STEP_VEHICLE_HALTING_NUMBER:
117 return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
118 default:
119 return false;
120 }
121 }
122
123
124 }
125
126
127 /****************************************************************************/
128