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 LaneArea.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 "LaneArea.h"
32
33
34 namespace libsumo {
35 // ===========================================================================
36 // static member initializations
37 // ===========================================================================
38 SubscriptionResults LaneArea::mySubscriptionResults;
39 ContextSubscriptionResults LaneArea::myContextSubscriptionResults;
40
41
42 // ===========================================================================
43 // static member definitions
44 // ===========================================================================
45 std::vector<std::string>
getIDList()46 LaneArea::getIDList() {
47 std::vector<std::string> ids;
48 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).insertIDs(ids);
49 return ids;
50 }
51
52
53 int
getIDCount()54 LaneArea::getIDCount() {
55 std::vector<std::string> ids;
56 return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).size();
57 }
58
59
60 int
getJamLengthVehicle(const std::string & detID)61 LaneArea::getJamLengthVehicle(const std::string& detID) {
62 return getDetector(detID)->getCurrentJamLengthInVehicles();
63 }
64
65
66 double
getJamLengthMeters(const std::string & detID)67 LaneArea::getJamLengthMeters(const std::string& detID) {
68 return getDetector(detID)->getCurrentJamLengthInMeters();
69 }
70
71
72 double
getLastStepMeanSpeed(const std::string & detID)73 LaneArea::getLastStepMeanSpeed(const std::string& detID) {
74 return getDetector(detID)->getCurrentMeanSpeed();
75 }
76
77
78 std::vector<std::string>
getLastStepVehicleIDs(const std::string & detID)79 LaneArea::getLastStepVehicleIDs(const std::string& detID) {
80 return getDetector(detID)->getCurrentVehicleIDs();
81 }
82
83
84 double
getLastStepOccupancy(const std::string & detID)85 LaneArea::getLastStepOccupancy(const std::string& detID) {
86 return getDetector(detID)->getCurrentOccupancy();
87 }
88
89
90 double
getPosition(const std::string & detID)91 LaneArea::getPosition(const std::string& detID) {
92 return getDetector(detID)->getStartPos();
93 }
94
95
96 std::string
getLaneID(const std::string & detID)97 LaneArea::getLaneID(const std::string& detID) {
98 return getDetector(detID)->getLane()->getID();
99 }
100
101
102 double
getLength(const std::string & detID)103 LaneArea::getLength(const std::string& detID) {
104 const MSE2Collector* const e2 = getDetector(detID);
105 return e2->getLength();
106 }
107
108
109 int
getLastStepVehicleNumber(const std::string & detID)110 LaneArea::getLastStepVehicleNumber(const std::string& detID) {
111 return getDetector(detID)->getCurrentVehicleNumber();
112 }
113
114
115 int
getLastStepHaltingNumber(const std::string & detID)116 LaneArea::getLastStepHaltingNumber(const std::string& detID) {
117 return getDetector(detID)->getCurrentHaltingNumber();
118 }
119
120
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(LaneArea,LANEAREA)121 LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(LaneArea, LANEAREA)
122
123
124 MSE2Collector*
125 LaneArea::getDetector(const std::string& id) {
126 MSE2Collector* e2 = dynamic_cast<MSE2Collector*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_LANE_AREA_DETECTOR).get(id));
127 if (e2 == nullptr) {
128 throw TraCIException("Lane area detector '" + id + "' is not known");
129 }
130 return e2;
131 }
132
133
134 std::shared_ptr<VariableWrapper>
makeWrapper()135 LaneArea::makeWrapper() {
136 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
137 }
138
139
140 bool
handleVariable(const std::string & objID,const int variable,VariableWrapper * wrapper)141 LaneArea::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
142 switch (variable) {
143 case TRACI_ID_LIST:
144 return wrapper->wrapStringList(objID, variable, getIDList());
145 case ID_COUNT:
146 return wrapper->wrapInt(objID, variable, getIDCount());
147 case LAST_STEP_VEHICLE_NUMBER:
148 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
149 case LAST_STEP_MEAN_SPEED:
150 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
151 case LAST_STEP_VEHICLE_ID_LIST:
152 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
153 case LAST_STEP_VEHICLE_HALTING_NUMBER:
154 return wrapper->wrapInt(objID, variable, getLastStepHaltingNumber(objID));
155 case JAM_LENGTH_VEHICLE:
156 return wrapper->wrapInt(objID, variable, getJamLengthVehicle(objID));
157 case JAM_LENGTH_METERS:
158 return wrapper->wrapDouble(objID, variable, getJamLengthMeters(objID));
159 case LAST_STEP_OCCUPANCY:
160 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
161 case VAR_POSITION:
162 return wrapper->wrapDouble(objID, variable, getPosition(objID));
163 case VAR_LANE_ID:
164 return wrapper->wrapString(objID, variable, getLaneID(objID));
165 case VAR_LENGTH:
166 return wrapper->wrapDouble(objID, variable, getLength(objID));
167 default:
168 return false;
169 }
170 }
171
172
173 }
174
175
176 /****************************************************************************/
177