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