Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2026 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 TraCIAPI.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Mario Krumnow
17 : /// @author Michael Behrisch
18 : /// @date 30.05.2012
19 : ///
20 : // C++ TraCI client API implementation
21 : /****************************************************************************/
22 : #pragma once
23 : #include <config.h>
24 : #include <vector>
25 : #include <limits>
26 : #include <string>
27 : #include <sstream>
28 : #include <iomanip>
29 : #include <foreign/tcpip/socket.h>
30 : #include <libsumo/TraCIConstants.h>
31 : #include <libsumo/TraCIDefs.h>
32 :
33 : // ===========================================================================
34 : // global definitions
35 : // ===========================================================================
36 : #define DEFAULT_VIEW "View #0"
37 : #define PRECISION 2
38 :
39 : // ===========================================================================
40 : // class definitions
41 : // ===========================================================================
42 : /**
43 : * @class TraCIAPI
44 : * @brief C++ TraCI client API implementation
45 : */
46 :
47 :
48 : class TraCIAPI {
49 : public:
50 : /** @brief Constructor
51 : */
52 : TraCIAPI();
53 :
54 : /// @brief Destructor
55 : ~TraCIAPI();
56 :
57 : /// @name Connection handling
58 : /// @{
59 :
60 : /** @brief Connects to the specified SUMO server
61 : * @param[in] host The name of the host to connect to
62 : * @param[in] port The port to connect to
63 : * @exception tcpip::SocketException if the connection fails
64 : */
65 : void connect(const std::string& host, int port);
66 :
67 : /// @brief set priority (execution order) for the client
68 : void setOrder(int order);
69 :
70 : /// @brief ends the simulation and closes the connection
71 : void close();
72 : /// @}
73 :
74 : /// @brief Advances by one step (or up to the given time)
75 : void simulationStep(double time = 0);
76 :
77 : /// @brief Let sumo load a simulation using the given command line like options.
78 : void load(const std::vector<std::string>& args);
79 :
80 : /// @brief return TraCI API and SUMO version
81 : std::pair<int, std::string> getVersion();
82 :
83 : /** @class TraCIScopeWrapper
84 : * @brief An abstract interface for accessing type-dependent values
85 : *
86 : * Must be derived by interfaces which implement access methods to certain object types
87 : */
88 : class TraCIScopeWrapper {
89 : public:
90 : /** @brief Constructor
91 : * @param[in] parent The parent TraCI client which offers the connection
92 : */
93 1218 : TraCIScopeWrapper(TraCIAPI& parent, int cmdGetID, int cmdSetID, int subscribeID, int contextSubscribeID) :
94 1218 : myParent(parent),
95 1218 : myCmdGetID(cmdGetID),
96 1218 : myCmdSetID(cmdSetID),
97 1218 : mySubscribeID(subscribeID),
98 1218 : myContextSubscribeID(contextSubscribeID) {
99 : }
100 :
101 : /// @brief Destructor
102 20706 : virtual ~TraCIScopeWrapper() {}
103 :
104 : std::vector<std::string> getIDList() const;
105 : int getIDCount() const;
106 :
107 : /// @brief retrieve generic parameter
108 : std::string getParameter(const std::string& objectID, const std::string& key) const;
109 :
110 : /// @brief retrieve generic parameter and return (key, value) tuple
111 : std::pair<std::string, std::string> getParameterWithKey(const std::string& objectID, const std::string& key) const;
112 :
113 : /// @brief set generic paramter
114 : void setParameter(const std::string& objectID, const std::string& key, const std::string& value) const;
115 :
116 : void subscribe(const std::string& objID, const std::vector<int>& vars, double beginTime, double endTime) const;
117 : void subscribeContext(const std::string& objID, int domain, double range, const std::vector<int>& vars, double beginTime, double endTime) const;
118 :
119 : const libsumo::SubscriptionResults getAllSubscriptionResults() const;
120 : const libsumo::TraCIResults getSubscriptionResults(const std::string& objID) const;
121 :
122 : const libsumo::ContextSubscriptionResults getAllContextSubscriptionResults() const;
123 : const libsumo::SubscriptionResults getContextSubscriptionResults(const std::string& objID) const;
124 :
125 : // the following are only for internal use
126 : void clearSubscriptionResults();
127 : libsumo::SubscriptionResults& getModifiableSubscriptionResults();
128 : libsumo::SubscriptionResults& getModifiableContextSubscriptionResults(const std::string& objID);
129 :
130 : protected:
131 : int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = 0) const;
132 : int getByte(int var, const std::string& id, tcpip::Storage* add = 0) const;
133 : int getInt(int var, const std::string& id, tcpip::Storage* add = 0) const;
134 : double getDouble(int var, const std::string& id, tcpip::Storage* add = 0) const;
135 : libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = 0) const;
136 : libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = 0) const;
137 : libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = 0) const;
138 : std::string getString(int var, const std::string& id, tcpip::Storage* add = 0) const;
139 : std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = 0) const;
140 : std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = 0) const;
141 : libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = 0) const;
142 : libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = 0) const;
143 :
144 : void setInt(int var, const std::string& id, int value) const;
145 : void setDouble(int var, const std::string& id, double value) const;
146 : void setString(int var, const std::string& id, const std::string& value) const;
147 : void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) const;
148 :
149 : protected:
150 : /// @brief The parent TraCI client which offers the connection
151 : TraCIAPI& myParent;
152 :
153 : private:
154 : int myCmdGetID;
155 : int myCmdSetID;
156 : int mySubscribeID;
157 : int myContextSubscribeID;
158 : libsumo::SubscriptionResults mySubscriptionResults;
159 : libsumo::ContextSubscriptionResults myContextSubscriptionResults;
160 :
161 :
162 : private:
163 : /// @brief invalidated assignment operator
164 : TraCIScopeWrapper& operator=(const TraCIScopeWrapper& src) = delete;
165 : };
166 :
167 :
168 : /** @class EdgeScope
169 : * @brief Scope for interaction with edges
170 : */
171 : class EdgeScope : public TraCIScopeWrapper {
172 : public:
173 1218 : EdgeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_EDGE_VARIABLE, libsumo::CMD_SET_EDGE_VARIABLE, libsumo::CMD_SUBSCRIBE_EDGE_VARIABLE, libsumo::CMD_SUBSCRIBE_EDGE_CONTEXT) {}
174 0 : virtual ~EdgeScope() {}
175 :
176 : double getAdaptedTraveltime(const std::string& edgeID, double time) const;
177 : double getEffort(const std::string& edgeID, double time) const;
178 : double getCO2Emission(const std::string& edgeID) const;
179 : double getCOEmission(const std::string& edgeID) const;
180 : double getHCEmission(const std::string& edgeID) const;
181 : double getPMxEmission(const std::string& edgeID) const;
182 : double getNOxEmission(const std::string& edgeID) const;
183 : double getFuelConsumption(const std::string& edgeID) const;
184 : double getNoiseEmission(const std::string& edgeID) const;
185 : double getElectricityConsumption(const std::string& edgeID) const;
186 : double getLastStepMeanSpeed(const std::string& edgeID) const;
187 : double getLastStepOccupancy(const std::string& edgeID) const;
188 : double getLastStepLength(const std::string& edgeID) const;
189 : double getTraveltime(const std::string& edgeID) const;
190 : int getLastStepVehicleNumber(const std::string& edgeID) const;
191 : double getLastStepHaltingNumber(const std::string& edgeID) const;
192 : std::vector<std::string> getLastStepVehicleIDs(const std::string& edgeID) const;
193 : int getLaneNumber(const std::string& edgeID) const;
194 : std::string getStreetName(const std::string& id) const;
195 :
196 : void adaptTraveltime(const std::string& edgeID, double time, double beginSeconds = 0., double endSeconds = std::numeric_limits<double>::max()) const;
197 : void setEffort(const std::string& edgeID, double effort, double beginSeconds = 0., double endSeconds = std::numeric_limits<double>::max()) const;
198 : void setMaxSpeed(const std::string& edgeID, double speed) const;
199 : };
200 :
201 :
202 : /** @class GUIScope
203 : * @brief Scope for interaction with the gui
204 : */
205 : class GUIScope : public TraCIScopeWrapper {
206 : public:
207 1218 : GUIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_GUI_VARIABLE, libsumo::CMD_SET_GUI_VARIABLE, libsumo::CMD_SUBSCRIBE_GUI_VARIABLE, libsumo::CMD_SUBSCRIBE_GUI_CONTEXT) {}
208 1218 : virtual ~GUIScope() {}
209 :
210 : double getZoom(const std::string& viewID = DEFAULT_VIEW) const;
211 : libsumo::TraCIPosition getOffset(const std::string& viewID = DEFAULT_VIEW) const;
212 : std::string getSchema(const std::string& viewID = DEFAULT_VIEW) const;
213 : libsumo::TraCIPositionVector getBoundary(const std::string& viewID = DEFAULT_VIEW) const;
214 : void setZoom(const std::string& viewID, double zoom) const;
215 : void setOffset(const std::string& viewID, double x, double y) const;
216 : void setSchema(const std::string& viewID, const std::string& schemeName) const;
217 : void setBoundary(const std::string& viewID, double xmin, double ymin, double xmax, double ymax) const;
218 : void screenshot(const std::string& viewID, const std::string& filename, const int width = -1, const int height = -1) const;
219 : void trackVehicle(const std::string& viewID, const std::string& vehID) const;
220 : };
221 :
222 :
223 : /** @class InductionLoopScope
224 : * @brief Scope for interaction with inductive loops
225 : */
226 : class InductionLoopScope : public TraCIScopeWrapper {
227 : public:
228 1218 : InductionLoopScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_INDUCTIONLOOP_VARIABLE, -1, libsumo::CMD_SUBSCRIBE_INDUCTIONLOOP_VARIABLE, libsumo::CMD_SUBSCRIBE_INDUCTIONLOOP_CONTEXT) {}
229 1218 : virtual ~InductionLoopScope() {}
230 :
231 : /**
232 : * @param loopID The ID of the Induction Loop
233 : * @return the number of vehicles that passed the detector during the current interval
234 : */
235 : int getIntervalVehicleNumber(const std::string& loopID) const;
236 : double getPosition(const std::string& loopID) const;
237 : std::string getLaneID(const std::string& loopID) const;
238 : int getLastStepVehicleNumber(const std::string& loopID) const;
239 : double getLastStepMeanSpeed(const std::string& loopID) const;
240 : std::vector<std::string> getLastStepVehicleIDs(const std::string& loopID) const;
241 : double getLastStepOccupancy(const std::string& loopID) const;
242 : double getLastStepMeanLength(const std::string& loopID) const;
243 : double getTimeSinceDetection(const std::string& loopID) const;
244 : std::vector<libsumo::TraCIVehicleData> getVehicleData(const std::string& loopID) const;
245 : };
246 :
247 :
248 : /** @class JunctionScope
249 : * @brief Scope for interaction with junctions
250 : */
251 : class JunctionScope : public TraCIScopeWrapper {
252 : public:
253 1218 : JunctionScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_JUNCTION_VARIABLE, libsumo::CMD_SET_JUNCTION_VARIABLE, libsumo::CMD_SUBSCRIBE_JUNCTION_VARIABLE, libsumo::CMD_SUBSCRIBE_JUNCTION_CONTEXT) {}
254 1218 : virtual ~JunctionScope() {}
255 :
256 : libsumo::TraCIPosition getPosition(const std::string& junctionID) const;
257 : libsumo::TraCIPositionVector getShape(const std::string& junctionID) const;
258 : };
259 :
260 :
261 : /** @class LaneScope
262 : * @brief Scope for interaction with lanes
263 : */
264 : class LaneScope : public TraCIScopeWrapper {
265 : public:
266 1218 : LaneScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_LANE_VARIABLE, libsumo::CMD_SET_LANE_VARIABLE, libsumo::CMD_SUBSCRIBE_LANE_VARIABLE, libsumo::CMD_SUBSCRIBE_LANE_CONTEXT) {}
267 1218 : virtual ~LaneScope() {}
268 :
269 : double getLength(const std::string& laneID) const;
270 : double getMaxSpeed(const std::string& laneID) const;
271 : double getWidth(const std::string& laneID) const;
272 : std::vector<std::string> getAllowed(const std::string& laneID) const;
273 : std::vector<std::string> getDisallowed(const std::string& laneID) const;
274 : int getLinkNumber(const std::string& laneID) const;
275 : std::vector<libsumo::TraCIConnection> getLinks(const std::string& laneID) const;
276 : libsumo::TraCIPositionVector getShape(const std::string& laneID) const;
277 : std::string getEdgeID(const std::string& laneID) const;
278 : double getCO2Emission(const std::string& laneID) const;
279 : double getCOEmission(const std::string& laneID) const;
280 : double getHCEmission(const std::string& laneID) const;
281 : double getPMxEmission(const std::string& laneID) const;
282 : double getNOxEmission(const std::string& laneID) const;
283 : double getFuelConsumption(const std::string& laneID) const;
284 : double getNoiseEmission(const std::string& laneID) const;
285 : double getElectricityConsumption(const std::string& laneID) const;
286 : double getLastStepMeanSpeed(const std::string& laneID) const;
287 : double getLastStepOccupancy(const std::string& laneID) const;
288 : double getLastStepLength(const std::string& laneID) const;
289 : double getTraveltime(const std::string& laneID) const;
290 : int getLastStepVehicleNumber(const std::string& laneID) const;
291 : int getLastStepHaltingNumber(const std::string& laneID) const;
292 : std::vector<std::string> getLastStepVehicleIDs(const std::string& laneID) const;
293 : std::vector<std::string> getFoes(const std::string& laneID, const std::string& toLaneID) const;
294 : std::vector<std::string> getInternalFoes(const std::string& laneID) const;
295 :
296 : void setAllowed(const std::string& laneID, const std::vector<std::string>& allowedClasses) const;
297 : void setDisallowed(const std::string& laneID, const std::vector<std::string>& disallowedClasses) const;
298 : void setMaxSpeed(const std::string& laneID, double speed) const;
299 : void setLength(const std::string& laneID, double length) const;
300 : };
301 :
302 :
303 : /** @class LaneAreaScope
304 : * @brief Scope for interaction with lane area detectors
305 : */
306 : class LaneAreaScope : public TraCIScopeWrapper {
307 : public:
308 1218 : LaneAreaScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_LANEAREA_VARIABLE, -1, libsumo::CMD_SUBSCRIBE_LANEAREA_VARIABLE, libsumo::CMD_SUBSCRIBE_LANEAREA_CONTEXT) {}
309 1218 : virtual ~LaneAreaScope() {}
310 : };
311 :
312 :
313 : /** @class MeMeScope
314 : * @brief Scope for interaction with multi entry/-exit detectors
315 : */
316 : class MeMeScope : public TraCIScopeWrapper {
317 : public:
318 1218 : MeMeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_MULTIENTRYEXIT_VARIABLE, -1, libsumo::CMD_SUBSCRIBE_MULTIENTRYEXIT_VARIABLE, libsumo::CMD_SUBSCRIBE_MULTIENTRYEXIT_CONTEXT) {}
319 1218 : virtual ~MeMeScope() {}
320 :
321 : int getLastStepVehicleNumber(const std::string& detID) const;
322 : double getLastStepMeanSpeed(const std::string& detID) const;
323 : std::vector<std::string> getLastStepVehicleIDs(const std::string& detID) const;
324 : int getLastStepHaltingNumber(const std::string& detID) const;
325 : std::vector<std::string> getEntryLanes(const std::string& detID) const;
326 : std::vector<std::string> getExitLanes(const std::string& detID) const;
327 : std::vector<double> getEntryPositions(const std::string& detID) const;
328 : std::vector<double> getExitPositions(const std::string& detID) const;
329 : };
330 :
331 :
332 : /** @class POIScope
333 : * @brief Scope for interaction with POIs
334 : */
335 : class POIScope : public TraCIScopeWrapper {
336 : public:
337 1218 : POIScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_POI_VARIABLE, libsumo::CMD_SET_POI_VARIABLE, libsumo::CMD_SUBSCRIBE_POI_VARIABLE, libsumo::CMD_SUBSCRIBE_POI_CONTEXT) {}
338 0 : virtual ~POIScope() {}
339 :
340 : std::string getType(const std::string& poiID) const;
341 : libsumo::TraCIPosition getPosition(const std::string& poiID) const;
342 : libsumo::TraCIColor getColor(const std::string& poiID) const;
343 : double getWidth(const std::string& poiID) const;
344 : double getHeight(const std::string& poiID) const;
345 : double getAngle(const std::string& poiID) const;
346 : std::string getImageFile(const std::string& poiID) const;
347 :
348 : void setType(const std::string& poiID, const std::string& setType) const;
349 : void setPosition(const std::string& poiID, double x, double y) const;
350 : void setColor(const std::string& poiID, const libsumo::TraCIColor& c) const;
351 : void setWidth(const std::string& poiID, double width) const;
352 : void setHeight(const std::string& poiID, double height) const;
353 : void setAngle(const std::string& poiID, double angle) const;
354 : void setImageFile(const std::string& poiID, const std::string& imageFile) const;
355 : void add(const std::string& poiID, double x, double y, const libsumo::TraCIColor& c, const std::string& type, int layer, const std::string& imgFile, double width, double height, double angle) const;
356 : void remove(const std::string& poiID, int layer = 0) const;
357 : };
358 :
359 :
360 : /** @class PolygonScope
361 : * @brief Scope for interaction with polygons
362 : */
363 : class PolygonScope : public TraCIScopeWrapper {
364 : public:
365 1218 : PolygonScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_POLYGON_VARIABLE, libsumo::CMD_SET_POLYGON_VARIABLE, libsumo::CMD_SUBSCRIBE_POLYGON_VARIABLE, libsumo::CMD_SUBSCRIBE_POLYGON_CONTEXT) {}
366 1218 : virtual ~PolygonScope() {}
367 :
368 : double getLineWidth(const std::string& polygonID) const;
369 : double getHeight(const std::string& polygonID) const;
370 : bool getFilled(const std::string& polygonID) const;
371 : std::string getType(const std::string& polygonID) const;
372 : libsumo::TraCIPositionVector getShape(const std::string& polygonID) const;
373 : libsumo::TraCIColor getColor(const std::string& polygonID) const;
374 : void setType(const std::string& polygonID, const std::string& setType) const;
375 : void setShape(const std::string& polygonID, const libsumo::TraCIPositionVector& shape) const;
376 : void setColor(const std::string& polygonID, const libsumo::TraCIColor& c) const;
377 : void setLineWidth(const std::string& polygonID, const double lineWidth) const;
378 : void setHeight(const std::string& polygonID, const double height) const;
379 : void add(const std::string& polygonID, const libsumo::TraCIPositionVector& shape, const libsumo::TraCIColor& c, bool fill, const std::string& type, int layer) const;
380 : void remove(const std::string& polygonID, int layer = 0) const;
381 : };
382 :
383 :
384 : /** @class RerouterScope
385 : * @brief Scope for interaction with rerouters
386 : */
387 : class RerouterScope : public TraCIScopeWrapper {
388 : public:
389 1218 : RerouterScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_REROUTER_VARIABLE, libsumo::CMD_SET_REROUTER_VARIABLE, libsumo::CMD_SUBSCRIBE_REROUTER_VARIABLE, libsumo::CMD_SUBSCRIBE_REROUTER_CONTEXT) {}
390 1218 : virtual ~RerouterScope() {}
391 : };
392 :
393 :
394 : /** @class RouteScope
395 : * @brief Scope for interaction with routes
396 : */
397 : class RouteScope : public TraCIScopeWrapper {
398 : public:
399 1218 : RouteScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_ROUTE_VARIABLE, libsumo::CMD_SET_ROUTE_VARIABLE, libsumo::CMD_SUBSCRIBE_ROUTE_VARIABLE, libsumo::CMD_SUBSCRIBE_ROUTE_CONTEXT) {}
400 1218 : virtual ~RouteScope() {}
401 :
402 : std::vector<std::string> getEdges(const std::string& routeID) const;
403 :
404 : void add(const std::string& routeID, const std::vector<std::string>& edges) const;
405 : };
406 :
407 :
408 : /** @class RouteProbeScope
409 : * @brief Scope for interaction with route probes
410 : */
411 : class RouteProbeScope : public TraCIScopeWrapper {
412 : public:
413 1218 : RouteProbeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_ROUTEPROBE_VARIABLE, libsumo::CMD_SET_ROUTEPROBE_VARIABLE, libsumo::CMD_SUBSCRIBE_ROUTEPROBE_VARIABLE, libsumo::CMD_SUBSCRIBE_ROUTEPROBE_CONTEXT) {}
414 1218 : virtual ~RouteProbeScope() {}
415 : };
416 :
417 :
418 : /** @class SimulationScope
419 : * @brief Scope for interaction with the simulation
420 : */
421 : class SimulationScope : public TraCIScopeWrapper {
422 : public:
423 1218 : SimulationScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_SIM_VARIABLE, libsumo::CMD_SET_SIM_VARIABLE, libsumo::CMD_SUBSCRIBE_SIM_VARIABLE, libsumo::CMD_SUBSCRIBE_SIM_CONTEXT) {}
424 1218 : virtual ~SimulationScope() {}
425 :
426 : int getCurrentTime() const;
427 : double getTime() const;
428 : int getLoadedNumber() const;
429 : std::vector<std::string> getLoadedIDList() const;
430 : int getDepartedNumber() const;
431 : std::vector<std::string> getDepartedIDList() const;
432 : int getArrivedNumber() const;
433 : std::vector<std::string> getArrivedIDList() const;
434 : int getStartingTeleportNumber() const;
435 : std::vector<std::string> getStartingTeleportIDList() const;
436 : int getEndingTeleportNumber() const;
437 : std::vector<std::string> getEndingTeleportIDList() const;
438 : double getDeltaT() const;
439 : libsumo::TraCIPositionVector getNetBoundary() const;
440 : int getMinExpectedNumber() const;
441 : std::string getOption(const std::string& option) const;
442 :
443 : int getDepartedPersonNumber() const;
444 : std::vector<std::string> getDepartedPersonIDList() const;
445 : int getArrivedPersonNumber() const;
446 : std::vector<std::string> getArrivedPersonIDList() const;
447 :
448 : int getBusStopWaiting(const std::string& stopID) const;
449 : std::vector<std::string> getBusStopWaitingIDList(const std::string& stopID) const;
450 :
451 : libsumo::TraCIPosition convert2D(const std::string& edgeID, double pos, int laneIndex = 0, bool toGeo = false) const;
452 : libsumo::TraCIPosition convert3D(const std::string& edgeID, double pos, int laneIndex = 0, bool toGeo = false) const;
453 : libsumo::TraCIRoadPosition convertRoad(double x, double y, bool isGeo = false, const std::string& vClass = "ignoring") const;
454 : libsumo::TraCIPosition convertGeo(double x, double y, bool fromGeo = false) const;
455 :
456 : double getDistance2D(double x1, double y1, double x2, double y2, bool isGeo = false, bool isDriving = false);
457 : double getDistanceRoad(const std::string& edgeID1, double pos1, const std::string& edgeID2, double pos2, bool isDriving = false);
458 : libsumo::TraCIStage findRoute(const std::string& fromEdge, const std::string& toEdge, const std::string& vType = "", double pos = -1., int routingMode = 0) const;
459 : void loadState(const std::string& path) const;
460 : void saveState(const std::string& destination) const;
461 : void writeMessage(const std::string msg);
462 : };
463 :
464 :
465 : /** @class TrafficLightScope
466 : * @brief Scope for interaction with traffic lights
467 : */
468 : class TrafficLightScope : public TraCIScopeWrapper {
469 : public:
470 1218 : TrafficLightScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_TL_VARIABLE, libsumo::CMD_SET_TL_VARIABLE, libsumo::CMD_SUBSCRIBE_TL_VARIABLE, libsumo::CMD_SUBSCRIBE_TL_CONTEXT) {}
471 1218 : virtual ~TrafficLightScope() {}
472 :
473 : std::string getRedYellowGreenState(const std::string& tlsID) const;
474 : std::vector<libsumo::TraCILogic> getAllProgramLogics(const std::string& tlsID) const;
475 : std::vector<std::string> getControlledLanes(const std::string& tlsID) const;
476 : std::vector<std::vector<libsumo::TraCILink> > getControlledLinks(const std::string& tlsID) const;
477 : std::string getProgram(const std::string& tlsID) const;
478 : int getPhase(const std::string& tlsID) const;
479 : double getPhaseDuration(const std::string& tlsID) const;
480 : double getNextSwitch(const std::string& tlsID) const;
481 : int getServedPersonCount(const std::string& tlsID, int index) const;
482 : std::string getPhaseName(const std::string& tlsID) const;
483 :
484 : void setRedYellowGreenState(const std::string& tlsID, const std::string& state) const;
485 : void setPhase(const std::string& tlsID, int index) const;
486 : void setPhaseName(const std::string& tlsID, const std::string& name) const;
487 : void setProgram(const std::string& tlsID, const std::string& programID) const;
488 : void setPhaseDuration(const std::string& tlsID, double phaseDuration) const;
489 : void setProgramLogic(const std::string& tlsID, const libsumo::TraCILogic& logic) const;
490 :
491 : // aliases for backward compatibility
492 : inline std::vector<libsumo::TraCILogic> getCompleteRedYellowGreenDefinition(const std::string& tlsID) const {
493 : return getAllProgramLogics(tlsID);
494 : }
495 : void setCompleteRedYellowGreenDefinition(const std::string& tlsID, const libsumo::TraCILogic& logic) const {
496 : setProgramLogic(tlsID, logic);
497 : }
498 : };
499 :
500 :
501 : /** @class VehicleTypeScope
502 : * @brief Scope for interaction with vehicle types
503 : */
504 : class VehicleTypeScope : public TraCIScopeWrapper {
505 : public:
506 1218 : VehicleTypeScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_VEHICLETYPE_VARIABLE, libsumo::CMD_SET_VEHICLETYPE_VARIABLE, libsumo::CMD_SUBSCRIBE_VEHICLETYPE_VARIABLE, libsumo::CMD_SUBSCRIBE_VEHICLETYPE_CONTEXT) {}
507 1218 : virtual ~VehicleTypeScope() {}
508 :
509 : double getLength(const std::string& typeID) const;
510 : double getMaxSpeed(const std::string& typeID) const;
511 : double getSpeedFactor(const std::string& typeID) const;
512 : double getSpeedDeviation(const std::string& typeID) const;
513 : double getAccel(const std::string& typeID) const;
514 : double getDecel(const std::string& typeID) const;
515 : double getEmergencyDecel(const std::string& typeID) const;
516 : double getApparentDecel(const std::string& typeID) const;
517 : double getImperfection(const std::string& typeID) const;
518 : double getTau(const std::string& typeID) const;
519 : std::string getVehicleClass(const std::string& typeID) const;
520 : std::string getEmissionClass(const std::string& typeID) const;
521 : std::string getShapeClass(const std::string& typeID) const;
522 : double getMinGap(const std::string& typeID) const;
523 : double getWidth(const std::string& typeID) const;
524 : double getHeight(const std::string& typeID) const;
525 : libsumo::TraCIColor getColor(const std::string& typeID) const;
526 : double getMinGapLat(const std::string& typeID) const;
527 : double getMaxSpeedLat(const std::string& typeID) const;
528 : std::string getLateralAlignment(const std::string& typeID) const;
529 : int getPersonCapacity(const std::string& typeID) const;
530 :
531 : void setLength(const std::string& typeID, double length) const;
532 : void setMaxSpeed(const std::string& typeID, double speed) const;
533 : void setVehicleClass(const std::string& typeID, const std::string& clazz) const;
534 : void setSpeedFactor(const std::string& typeID, double factor) const;
535 : void setSpeedDeviation(const std::string& typeID, double deviation) const;
536 : void setEmissionClass(const std::string& typeID, const std::string& clazz) const;
537 : void setShapeClass(const std::string& typeID, const std::string& shapeClass) const;
538 : void setWidth(const std::string& typeID, double width) const;
539 : void setHeight(const std::string& typeID, double height) const;
540 : void setMinGap(const std::string& typeID, double minGap) const;
541 : void setAccel(const std::string& typeID, double accel) const;
542 : void setDecel(const std::string& typeID, double decel) const;
543 : void setEmergencyDecel(const std::string& typeID, double decel) const;
544 : void setApparentDecel(const std::string& typeID, double decel) const;
545 : void setImperfection(const std::string& typeID, double imperfection) const;
546 : void setTau(const std::string& typeID, double tau) const;
547 : void setColor(const std::string& typeID, const libsumo::TraCIColor& c) const;
548 : void setMinGapLat(const std::string& typeID, double minGapLat) const;
549 : void setMaxSpeedLat(const std::string& typeID, double speed) const;
550 : void setLateralAlignment(const std::string& typeID, const std::string& latAlignment) const;
551 : void copy(const std::string& origTypeID, const std::string& newTypeID) const;
552 : };
553 :
554 :
555 : /** @class VehicleScope
556 : * @brief Scope for interaction with vehicles
557 : */
558 : class VehicleScope : public TraCIScopeWrapper {
559 : public:
560 1218 : VehicleScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_VEHICLE_VARIABLE, libsumo::CMD_SET_VEHICLE_VARIABLE, libsumo::CMD_SUBSCRIBE_VEHICLE_VARIABLE, libsumo::CMD_SUBSCRIBE_VEHICLE_CONTEXT) {}
561 1218 : virtual ~VehicleScope() {}
562 :
563 : enum VehicleSignal {
564 : SIGNAL_BLINKER_RIGHT = 1,
565 : SIGNAL_BLINKER_LEFT = 2,
566 : SIGNAL_BLINKER_EMERGENCY = 4,
567 : SIGNAL_BRAKELIGHT = 8,
568 : SIGNAL_FRONTLIGHT = 16,
569 : SIGNAL_FOGLIGHT = 32,
570 : SIGNAL_HIGHBEAM = 64,
571 : SIGNAL_BACKDRIVE = 128,
572 : SIGNAL_WIPER = 256,
573 : SIGNAL_DOOR_OPEN_LEFT = 512,
574 : SIGNAL_DOOR_OPEN_RIGHT = 1024,
575 : SIGNAL_EMERGENCY_BLUE = 2048,
576 : SIGNAL_EMERGENCY_RED = 4096,
577 : SIGNAL_EMERGENCY_YELLOW = 8192,
578 : SIGNAL_RESET = -1, /*< sending a negative signal resets all signals to their computed values immediately */
579 : };
580 :
581 : /// @name vehicle value retrieval
582 : /// @{
583 : double getSpeed(const std::string& vehicleID) const;
584 : double getLateralSpeed(const std::string& vehicleID) const;
585 : double getAcceleration(const std::string& vehicleID) const;
586 : double getFollowSpeed(const std::string& vehicleID, double speed, double gap, double leaderSpeed, double leaderMaxDecel, const std::string& leaderID = "") const;
587 : double getSecureGap(const std::string& vehicleID, double speed, double leaderSpeed, double leaderMaxDecel, const std::string& leaderID = "") const;
588 : double getStopSpeed(const std::string& vehicleID, double speed, double gap) const;
589 : libsumo::TraCIPosition getPosition(const std::string& vehicleID) const;
590 : libsumo::TraCIPosition getPosition3D(const std::string& vehicleID) const;
591 : double getAngle(const std::string& vehicleID) const;
592 : std::string getRoadID(const std::string& vehicleID) const;
593 : std::string getLaneID(const std::string& vehicleID) const;
594 : int getLaneIndex(const std::string& vehicleID) const;
595 : std::string getTypeID(const std::string& vehicleID) const;
596 : std::string getRouteID(const std::string& vehicleID) const;
597 : int getRouteIndex(const std::string& vehicleID) const;
598 : std::vector<std::string> getRoute(const std::string& vehicleID) const;
599 : libsumo::TraCIColor getColor(const std::string& vehicleID) const;
600 : double getLanePosition(const std::string& vehicleID) const;
601 : double getDistance(const std::string& vehicleID) const;
602 : int getSignals(const std::string& vehicleID) const;
603 : double getCO2Emission(const std::string& vehicleID) const;
604 : double getCOEmission(const std::string& vehicleID) const;
605 : double getHCEmission(const std::string& vehicleID) const;
606 : double getPMxEmission(const std::string& vehicleID) const;
607 : double getNOxEmission(const std::string& vehicleID) const;
608 : double getFuelConsumption(const std::string& vehicleID) const;
609 : double getNoiseEmission(const std::string& vehicleID) const;
610 : double getElectricityConsumption(const std::string& vehicleID) const;
611 : int getStopState(const std::string& vehicleID) const;
612 : double getWaitingTime(const std::string& vehicleID) const;
613 : double getAccumulatedWaitingTime(const std::string& vehicleID) const;
614 : int getLaneChangeMode(const std::string& vehicleID) const;
615 : int getSpeedMode(const std::string& vehicleID) const;
616 : double getSlope(const std::string& vehicleID) const;
617 : double getAllowedSpeed(const std::string& vehicleID) const;
618 : int getPersonNumber(const std::string& vehicleID) const;
619 : std::vector<std::string> getPersonIDList(const std::string& vehicleID) const;
620 : double getSpeedWithoutTraCI(const std::string& vehicleID) const;
621 : bool isRouteValid(const std::string& vehicleID) const;
622 : double getLateralLanePosition(const std::string& vehicleID) const;
623 : double getSpeedFactor(const std::string& vehicleID) const;
624 : std::string getLine(const std::string& vehicleID) const;
625 : std::vector<std::string> getVia(const std::string& vehicleID) const;
626 : std::vector<libsumo::TraCINextTLSData> getNextTLS(const std::string& vehID) const;
627 : std::vector<libsumo::TraCIBestLanesData> getBestLanes(const std::string& vehicleID) const;
628 : std::pair<std::string, double> getLeader(const std::string& vehicleID, double dist) const;
629 : std::pair<std::string, double> getFollower(const std::string& vehicleID, double dist) const;
630 : int getRoutingMode(const std::string& vehicleID) const;
631 : double getStopDelay(const std::string& vehicleID) const;
632 : double getStopArrivalDelay(const std::string& vehicleID) const;
633 : std::pair<int, int> getLaneChangeState(const std::string& vehicleID, int direction) const;
634 : /// @}
635 :
636 : /// @name vehicle type value retrieval shortcuts
637 : /// @{
638 : double getLength(const std::string& vehicleID) const;
639 : double getMaxSpeed(const std::string& vehicleID) const;
640 : double getAccel(const std::string& vehicleID) const;
641 : double getDecel(const std::string& vehicleID) const;
642 : double getEmergencyDecel(const std::string& vehicleID) const;
643 : double getApparentDecel(const std::string& vehicleID) const;
644 : double getTau(const std::string& vehicleID) const;
645 : double getImperfection(const std::string& vehicleID) const;
646 : double getSpeedDeviation(const std::string& vehicleID) const;
647 : double getMinGap(const std::string& vehicleID) const;
648 : double getWidth(const std::string& vehicleID) const;
649 : double getHeight(const std::string& veihcleID) const;
650 : double getMaxSpeedLat(const std::string& vehicleID) const;
651 : double getMinGapLat(const std::string& vehicleID) const;
652 : int getPersonCapacity(const std::string& vehicleID) const;
653 : std::string getVehicleClass(const std::string& vehicleID) const;
654 : std::string getEmissionClass(const std::string& vehicleID) const;
655 : std::string getShapeClass(const std::string& vehicleID) const;
656 : std::string getLateralAlignment(const std::string& vehicleID) const;
657 : /// @}
658 :
659 : /// @name vehicle state changing
660 : /// @{
661 : void add(const std::string& vehicleID,
662 : const std::string& routeID,
663 : const std::string& typeID = "DEFAULT_VEHTYPE",
664 : std::string depart = "-1",
665 : const std::string& departLane = "first",
666 : const std::string& departPos = "base",
667 : const std::string& departSpeed = "0",
668 : const std::string& arrivalLane = "current",
669 : const std::string& arrivalPos = "max",
670 : const std::string& arrivalSpeed = "current",
671 : const std::string& fromTaz = "",
672 : const std::string& toTaz = "",
673 : const std::string& line = "",
674 : int personCapacity = 0,
675 : int personNumber = 0) const;
676 :
677 : void changeTarget(const std::string& vehicleID, const std::string& edgeID) const;
678 : void changeLane(const std::string& vehicleID, int laneIndex, double duration) const;
679 : void changeLaneRelative(const std::string& vehicleID, int laneChange, double duration) const;
680 : void changeSublane(const std::string& vehicleID, double latDist) const;
681 : void setRouteID(const std::string& vehicleID, const std::string& routeID) const;
682 : void setRoute(const std::string& vehicleID, const std::vector<std::string>& edge) const;
683 : void rerouteTraveltime(const std::string& vehicleID, bool currentTravelTimes = true) const;
684 : void moveTo(const std::string& vehicleID, const std::string& laneID, double position, int reason = libsumo::MOVE_TELEPORT) const;
685 : void moveToXY(const std::string& vehicleID, const std::string& edgeID, const int lane, const double x, const double y, const double angle, const int keepRoute) const;
686 : void slowDown(const std::string& vehicleID, double speed, double duration) const;
687 : void openGap(const std::string& vehicleID, double newTau, double duration, double changeRate, double maxDecel) const;
688 : void setSpeed(const std::string& vehicleID, double speed) const;
689 : void setAcceleration(const std::string& vehicleID, double accel, double duration) const;
690 : void setPreviousSpeed(const std::string& vehicleID, double prevSpeed, double prevAcceleration = std::numeric_limits<int>::min()) const;
691 : void setLaneChangeMode(const std::string& vehicleID, int mode) const;
692 : void setSpeedMode(const std::string& vehicleID, int mode) const;
693 : void setStop(const std::string vehicleID, const std::string edgeID, const double endPos = 1.,
694 : const int laneIndex = 0, const double duration = std::numeric_limits<double>::max(),
695 : const int flags = 0, const double startPos = std::numeric_limits<int>::min(),
696 : const double until = -1) const;
697 : void setType(const std::string& vehicleID, const std::string& typeID) const;
698 : void remove(const std::string& vehicleID, char reason = libsumo::REMOVE_VAPORIZED) const;
699 : void setColor(const std::string& vehicleID, const libsumo::TraCIColor& c) const;
700 : void setLine(const std::string& vehicleID, const std::string& line) const;
701 : void setVia(const std::string& vehicleID, const std::vector<std::string>& via) const;
702 : void setSignals(const std::string& vehicleID, int signals) const;
703 : void setRoutingMode(const std::string& vehicleID, int routingMode) const;
704 : /// @}
705 :
706 : /// @name vehicle type attribute changing shortcuts
707 : /// @{
708 : void setShapeClass(const std::string& vehicleID, const std::string& clazz) const;
709 : void setEmissionClass(const std::string& vehicleID, const std::string& clazz) const;
710 : void setSpeedFactor(const std::string& vehicleID, double factor) const;
711 : void setMinGap(const std::string& vehicleID, double minGap) const;
712 : void setMaxSpeed(const std::string& vehicleID, double speed) const;
713 : /// @}
714 :
715 : /// @name subscription filtering
716 : /* @brief Filters are added to the last modified vehicle context
717 : * subscription (call these fucntions right after subscribing) */
718 : /// @{
719 :
720 : /* @brief Adds a lane-filter, lanes is a list of relative lane indices (-1 -> right neighboring lane of the ego, 0 -> ego lane, etc.)
721 : * noOpposite specifies whether vehicles on opposite direction lanes shall be returned
722 : * downstreamDist and upstreamDist specify the range of the search for surrounding vehicles along the road net. */
723 : void addSubscriptionFilterLanes(const std::vector<int>& lanes,
724 : bool noOpposite = false, double downstreamDist = -1, double upstreamDist = -1) const;
725 :
726 : /* @brief Omits vehicles on other edges than the ego's */
727 : void addSubscriptionFilterNoOpposite() const;
728 :
729 : /* @brief Limits the downstream distance for resulting vehicles */
730 : void addSubscriptionFilterDownstreamDistance(double dist) const;
731 :
732 : /* @brief Limits the updstream distance for resulting vehicles */
733 : void addSubscriptionFilterUpstreamDistance(double dist) const;
734 :
735 : /* @brief Restricts vehicles returned by the last modified vehicle context subscription to leader and follower of the ego.
736 : * downstreamDist and upstreamDist specify the range of the search for leader and follower along the road net. */
737 : void addSubscriptionFilterCFManeuver(double downstreamDist = -1, double upstreamDist = -1) const;
738 :
739 : /* @brief Restricts returned vehicles to neighbor and ego-lane leader
740 : * and follower of the ego in the given direction
741 : * noOpposite specifies whether vehicles on opposite direction lanes shall be returned
742 : * downstreamDist and upstreamDist specify the range of the search for leader and follower along the road net.
743 : * Combine with: distance filters; vClass/vType filter. */
744 : void addSubscriptionFilterLCManeuver(int direction, bool noOpposite = false, double downstreamDist = -1, double upstreamDist = -1) const;
745 :
746 : /* @brief Restricts returned vehicles to neighbor and ego-lane leader and follower of the ego.
747 : * Combine with: lanes-filter to restrict to one direction; distance filters; vClass/vType filter. */
748 : void addSubscriptionFilterLeadFollow(const std::vector<int>& lanes) const;
749 :
750 : /* @brief Restricts returned vehicles to foes on an upcoming junction */
751 : void addSubscriptionFilterTurn(double downstreamDist = -1, double upstreamDist = -1) const;
752 :
753 : /* @brief Restricts returned vehicles to the given classes */
754 : void addSubscriptionFilterVClass(const std::vector<std::string>& vClasses) const;
755 :
756 : /* @brief Restricts returned vehicles to the given types */
757 : void addSubscriptionFilterVType(const std::vector<std::string>& vTypes) const;
758 :
759 : /* @brief Restricts returned vehicles to the given FOV-angle */
760 : void addSubscriptionFilterFieldOfVision(double angle) const;
761 :
762 : /* @brief Restricts returned vehicles to the given lateral distance */
763 : void addSubscriptionFilterLateralDistance(double lateralDist, double downstreamDist = -1, double foeDistToJunction = -1) const;
764 :
765 : /// @}
766 :
767 : private:
768 : void addSubscriptionFilterEmpty(int filterType) const;
769 : void addSubscriptionFilterFloat(int filterType, double val) const;
770 : void addSubscriptionFilterStringList(int filterType, const std::vector<std::string>& vals) const;
771 : void addSubscriptionFilterByteList(int filterType, const std::vector<int>& vals) const;
772 : };
773 :
774 :
775 : /** @class PersonScope
776 : * * @brief Scope for interaction with vehicles
777 : * */
778 : class PersonScope : public TraCIScopeWrapper {
779 : public:
780 1218 : PersonScope(TraCIAPI& parent) : TraCIScopeWrapper(parent, libsumo::CMD_GET_PERSON_VARIABLE, libsumo::CMD_SET_PERSON_VARIABLE, libsumo::CMD_SUBSCRIBE_PERSON_VARIABLE, libsumo::CMD_SUBSCRIBE_PERSON_CONTEXT) {}
781 1218 : virtual ~PersonScope() {}
782 :
783 : double getSpeed(const std::string& personID) const;
784 : libsumo::TraCIPosition getPosition(const std::string& personID) const;
785 : libsumo::TraCIPosition getPosition3D(const std::string& personID) const;
786 : std::string getRoadID(const std::string& personID) const;
787 : std::string getLaneID(const std::string& personID) const;
788 : std::string getTypeID(const std::string& personID) const;
789 : double getSpeedFactor(const std::string& personID) const;
790 : double getWaitingTime(const std::string& personID) const;
791 : std::string getNextEdge(const std::string& personID) const;
792 : std::string getVehicle(const std::string& personID) const;
793 : int getRemainingStages(const std::string& personID) const;
794 : libsumo::TraCIStage getStage(const std::string& personID, int nextStageIndex = 0) const;
795 : std::vector<std::string> getEdges(const std::string& personID, int nextStageIndex = 0) const;
796 : double getAngle(const std::string& personID) const;
797 : double getSlope(const std::string& personID) const;
798 : double getLanePosition(const std::string& personID) const;
799 : libsumo::TraCIColor getColor(const std::string& personID) const;
800 :
801 : /// @name vehicle type value retrieval shortcuts
802 : /// @{
803 : double getLength(const std::string& personID) const;
804 : /// @}
805 :
806 :
807 : void removeStages(const std::string& personID) const;
808 : void add(const std::string& personID, const std::string& edgeID, double pos, double depart = libsumo::DEPARTFLAG_NOW, const std::string typeID = "DEFAULT_PEDTYPE");
809 : void appendStage(const std::string& personID, const libsumo::TraCIStage& stage);
810 : void appendWaitingStage(const std::string& personID, double duration, const std::string& description = "waiting", const std::string& stopID = "");
811 : void appendWalkingStage(const std::string& personID, const std::vector<std::string>& edges, double arrivalPos, double duration = -1, double speed = -1, const std::string& stopID = "");
812 : void appendDrivingStage(const std::string& personID, const std::string& toEdge, const std::string& lines, const std::string& stopID = "");
813 : void removeStage(const std::string& personID, int nextStageIndex) const;
814 : void rerouteTraveltime(const std::string& personID) const;
815 : void moveTo(const std::string& personID, const std::string& edgeID, double position) const;
816 : void moveToXY(const std::string& personID, const std::string& edgeID, const double x, const double y, double angle, const int keepRoute) const;
817 : void setSpeed(const std::string& personID, double speed) const;
818 : void setType(const std::string& personID, const std::string& typeID) const;
819 : void setSpeedFactor(const std::string& personID, double factor) const;
820 : void setLength(const std::string& personID, double length) const;
821 : void setWidth(const std::string& personID, double width) const;
822 : void setHeight(const std::string& personID, double height) const;
823 : void setMinGap(const std::string& personID, double minGap) const;
824 : void setColor(const std::string& personID, const libsumo::TraCIColor& c) const;
825 : };
826 :
827 :
828 :
829 : public:
830 : /// @brief Scope for interaction with edges
831 : EdgeScope edge;
832 : /// @brief Scope for interaction with the gui
833 : GUIScope gui;
834 : /// @brief Scope for interaction with inductive loops
835 : InductionLoopScope inductionloop;
836 : /// @brief Scope for interaction with junctions
837 : JunctionScope junction;
838 : /// @brief Scope for interaction with lanes
839 : LaneScope lane;
840 : /// @brief Scope for interaction with lanes
841 : LaneAreaScope lanearea;
842 : /// @brief Scope for interaction with multi-entry/-exit detectors
843 : MeMeScope multientryexit;
844 : /// @brief Scope for interaction with persons
845 : PersonScope person;
846 : /// @brief Scope for interaction with POIs
847 : POIScope poi;
848 : /// @brief Scope for interaction with polygons
849 : PolygonScope polygon;
850 : /// @brief Scope for interaction with rerouters
851 : RerouterScope rerouter;
852 : /// @brief Scope for interaction with routes
853 : RouteScope route;
854 : /// @brief Scope for interaction with route probes
855 : RouteProbeScope routeprobe;
856 : /// @brief Scope for interaction with the simulation
857 : SimulationScope simulation;
858 : /// @brief Scope for interaction with traffic lights
859 : TrafficLightScope trafficlights;
860 : /// @brief Scope for interaction with vehicles
861 : VehicleScope vehicle;
862 : /// @brief Scope for interaction with vehicle types
863 : VehicleTypeScope vehicletype;
864 :
865 :
866 : protected:
867 : /// @name Command sending methods
868 : /// @{
869 :
870 : /** @brief Sends a SimulationStep command
871 : */
872 : void send_commandSimulationStep(double time) const;
873 :
874 :
875 : /** @brief Sends a Close command
876 : */
877 : void send_commandClose() const;
878 :
879 :
880 : /** @brief Sends a SetOrder command
881 : */
882 : void send_commandSetOrder(int order) const;
883 :
884 : /** @brief Sends a GetVariable / SetVariable request if mySocket is connected.
885 : * Otherwise writes to myOutput only.
886 : * @param[in] cmdID The command and domain of the variable
887 : * @param[in] varID The variable to retrieve
888 : * @param[in] objID The object to retrieve the variable from
889 : * @param[in] add Optional additional parameter
890 : */
891 : void createCommand(int cmdID, int varID, const std::string& objID, tcpip::Storage* add = nullptr) const;
892 : void createFilterCommand(int cmdID, int varID, tcpip::Storage* add = nullptr) const;
893 :
894 :
895 : /** @brief Sends a SubscribeVariable request
896 : * @param[in] domID The domain of the variable
897 : * @param[in] objID The object to subscribe the variables from
898 : * @param[in] beginTime The begin time step of subscriptions
899 : * @param[in] endTime The end time step of subscriptions
900 : * @param[in] vars The variables to subscribe
901 : */
902 : void send_commandSubscribeObjectVariable(int domID, const std::string& objID, double beginTime, double endTime, const std::vector<int>& vars) const;
903 :
904 :
905 : /** @brief Sends a SubscribeContext request
906 : * @param[in] domID The domain of the variable
907 : * @param[in] objID The object to subscribe the variables from
908 : * @param[in] beginTime The begin time step of subscriptions
909 : * @param[in] endTime The end time step of subscriptions
910 : * @param[in] domain The domain of the objects which values shall be returned
911 : * @param[in] range The range around the obj to investigate
912 : * @param[in] vars The variables to subscribe
913 : */
914 : void send_commandSubscribeObjectContext(int domID, const std::string& objID, double beginTime, double endTime,
915 : int domain, double range, const std::vector<int>& vars) const;
916 : /// @}
917 :
918 :
919 : /// @name Command sending methods
920 : /// @{
921 :
922 : /** @brief Validates the result state of a command
923 : * @param[in] inMsg The buffer to read the message from
924 : * @param[in] command The original command id
925 : * @param[in] ignoreCommandId Whether the returning command id shall be validated
926 : * @param[in] acknowledgement Pointer to an existing string into which the acknowledgement message shall be inserted
927 : */
928 : void check_resultState(tcpip::Storage& inMsg, int command, bool ignoreCommandId = false, std::string* acknowledgement = 0) const;
929 :
930 : /** @brief Validates the result state of a command
931 : * @return The command Id
932 : */
933 : int check_commandGetResult(tcpip::Storage& inMsg, int command, int expectedType = -1, bool ignoreCommandId = false) const;
934 :
935 : bool processGet(int command, int expectedType, bool ignoreCommandId = false);
936 : bool processSet(int command);
937 : /// @}
938 :
939 : void readVariableSubscription(int cmdId, tcpip::Storage& inMsg);
940 : void readContextSubscription(int cmdId, tcpip::Storage& inMsg);
941 : void readVariables(tcpip::Storage& inMsg, const std::string& objectID, int variableCount, libsumo::SubscriptionResults& into);
942 :
943 : template <class T>
944 7295 : static inline std::string toString(const T& t, std::streamsize accuracy = PRECISION) {
945 7295 : std::ostringstream oss;
946 : oss.setf(std::ios::fixed, std::ios::floatfield);
947 7295 : oss << std::setprecision(accuracy);
948 7295 : oss << t;
949 7295 : return oss.str();
950 7295 : }
951 :
952 : /// @brief Closes the connection
953 : void closeSocket();
954 :
955 : protected:
956 : std::map<int, TraCIScopeWrapper*> myDomains;
957 : /// @brief The socket
958 : tcpip::Socket* mySocket;
959 : /// @brief The reusable output storage
960 : mutable tcpip::Storage myOutput;
961 : /// @brief The reusable input storage
962 : mutable tcpip::Storage myInput;
963 : };
|