Eclipse SUMO - Simulation of Urban MObility
Domain.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2012-2024 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 /****************************************************************************/
21 // C++ TraCI client API implementation
22 /****************************************************************************/
23 #pragma once
24 #include <config.h>
25 
26 #include <vector>
27 #include <limits>
28 #include <map>
29 #include <string>
30 #include <stdexcept>
31 #include <sstream>
32 #include <memory>
33 #include <foreign/tcpip/storage.h>
34 #include <libtraci/Connection.h>
35 #include <libsumo/StorageHelper.h>
36 
37 
38 #define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN) \
39 const int CLASS::DOMAIN_ID(libsumo::CMD_GET_##DOMAIN##_VARIABLE); \
40 void CLASS::subscribe(const std::string& objectID, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
41  libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_VARIABLE, objectID, begin, end, -1, -1, varIDs, params); \
42 } \
43 \
44 void CLASS::unsubscribe(const std::string& objectID) { \
45  subscribe(objectID, std::vector<int>()); \
46 } \
47 \
48 void CLASS::subscribeContext(const std::string& objectID, int domain, double dist, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
49  libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_CONTEXT, objectID, begin, end, domain, dist, varIDs, params); \
50 } \
51 \
52 void CLASS::unsubscribeContext(const std::string& objectID, int domain, double dist) { \
53  subscribeContext(objectID, domain, dist, std::vector<int>()); \
54 } \
55 \
56 const libsumo::SubscriptionResults CLASS::getAllSubscriptionResults() { \
57  return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE); \
58 } \
59 \
60 const libsumo::TraCIResults CLASS::getSubscriptionResults(const std::string& objectID) { \
61  return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE)[objectID]; \
62 } \
63 \
64 const libsumo::ContextSubscriptionResults CLASS::getAllContextSubscriptionResults() { \
65  return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT); \
66 } \
67 \
68 const libsumo::SubscriptionResults CLASS::getContextSubscriptionResults(const std::string& objectID) { \
69  return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT)[objectID]; \
70 } \
71 \
72 void CLASS::subscribeParameterWithKey(const std::string& objectID, const std::string& key, double beginTime, double endTime) { \
73  subscribe(objectID, std::vector<int>({libsumo::VAR_PARAMETER_WITH_KEY}), beginTime, endTime, libsumo::TraCIResults {{libsumo::VAR_PARAMETER_WITH_KEY, std::make_shared<libsumo::TraCIString>(key)}}); \
74 }
75 
76 
77 #define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN) \
78 std::string \
79 CLASS::getParameter(const std::string& objectID, const std::string& param) { \
80  tcpip::Storage content; \
81  content.writeByte(libsumo::TYPE_STRING); \
82  content.writeString(param); \
83  return Dom::getString(libsumo::VAR_PARAMETER, objectID, &content); \
84 } \
85 \
86 void \
87 CLASS::setParameter(const std::string& objectID, const std::string& key, const std::string& value) { \
88  tcpip::Storage content; \
89  content.writeUnsignedByte(libsumo::TYPE_COMPOUND); \
90  content.writeInt(2); \
91  content.writeUnsignedByte(libsumo::TYPE_STRING); \
92  content.writeString(key); \
93  content.writeUnsignedByte(libsumo::TYPE_STRING); \
94  content.writeString(value); \
95  Connection::getActive().doCommand(libsumo::CMD_SET_##DOMAIN##_VARIABLE, libsumo::VAR_PARAMETER, objectID, &content); \
96 } \
97 \
98 const std::pair<std::string, std::string> \
99 CLASS::getParameterWithKey(const std::string& objectID, const std::string& key) { \
100  return std::make_pair(key, getParameter(objectID, key)); \
101 }
102 
103 
104 // ===========================================================================
105 // class and type definitions
106 // ===========================================================================
107 namespace libtraci {
108 template<int GET, int SET>
109 class Domain {
110 public:
111  static inline tcpip::Storage& get(int var, const std::string& id, tcpip::Storage* add = nullptr, int expectedType = libsumo::TYPE_COMPOUND) {
112  return libtraci::Connection::getActive().doCommand(GET, var, id, add, expectedType);
113  }
114 
115  static int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
116  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
117  return get(var, id, add, libsumo::TYPE_UBYTE).readUnsignedByte();
118  }
119 
120  static int getByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
121  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
122  return get(var, id, add, libsumo::TYPE_BYTE).readByte();
123  }
124 
125  static int getInt(int var, const std::string& id, tcpip::Storage* add = nullptr) {
126  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
127  return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
128  }
129 
130  static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
131  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
132  return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
133  }
134 
135  static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
136  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
137  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
139  int size = result.readUnsignedByte();
140  if (size == 0) {
141  size = result.readInt();
142  }
143  for (int i = 0; i < size; ++i) {
145  p.x = result.readDouble();
146  p.y = result.readDouble();
147  p.z = 0.;
148  ret.value.push_back(p);
149  }
150  return ret;
151  }
152 
153  static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
154  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
155  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
157  p.x = result.readDouble();
158  p.y = result.readDouble();
159  return p;
160  }
161 
162  static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
163  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
164  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
166  p.x = result.readDouble();
167  p.y = result.readDouble();
168  p.z = result.readDouble();
169  return p;
170  }
171 
172  static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
173  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
174  return get(var, id, add, libsumo::TYPE_STRING).readString();
175  }
176 
177  static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
178  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
179  return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
180  }
181 
182  static std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
183  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
184  return get(var, id, add, libsumo::TYPE_DOUBLELIST).readDoubleList();
185  }
186 
187  static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
188  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
189  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
191  c.r = (unsigned char)result.readUnsignedByte();
192  c.g = (unsigned char)result.readUnsignedByte();
193  c.b = (unsigned char)result.readUnsignedByte();
194  c.a = (unsigned char)result.readUnsignedByte();
195  return c;
196  }
197 
198  static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
199  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
200  tcpip::Storage& result = get(var, id, add);
202  result.readInt(); // components
203  s.type = StoHelp::readTypedInt(result);
204  s.vType = StoHelp::readTypedString(result);
205  s.line = StoHelp::readTypedString(result);
209  s.cost = StoHelp::readTypedDouble(result);
210  s.length = StoHelp::readTypedDouble(result);
212  s.depart = StoHelp::readTypedDouble(result);
216  return s;
217  }
218 
219  static void set(int var, const std::string& id, tcpip::Storage* add) {
220  std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
221  libtraci::Connection::getActive().doCommand(SET, var, id, add);
222  }
223 
224  static void setInt(int var, const std::string& id, int value) {
225  tcpip::Storage content;
227  content.writeInt(value);
228  set(var, id, &content);
229  }
230 
231  static void setDouble(int var, const std::string& id, double value) {
232  tcpip::Storage content;
234  content.writeDouble(value);
235  set(var, id, &content);
236  }
237 
238  static void setString(int var, const std::string& id, const std::string& value) {
239  tcpip::Storage content;
241  content.writeString(value);
242  set(var, id, &content);
243  }
244 
245  static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
246  tcpip::Storage content;
248  content.writeStringList(value);
249  set(var, id, &content);
250  }
251 
252  static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
253  tcpip::Storage content;
255  content.writeUnsignedByte(value.r);
256  content.writeUnsignedByte(value.g);
257  content.writeUnsignedByte(value.b);
258  content.writeUnsignedByte(value.a);
259  set(var, id, &content);
260  }
261 
262 };
263 
264 }
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:50
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
std::string intended
id of the intended vehicle for public transport ride
Definition: TraCIDefs.h:582
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:566
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:572
double length
length in m
Definition: TraCIDefs.h:580
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:576
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:586
std::string description
arbitrary description string
Definition: TraCIDefs.h:590
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:570
double cost
effort needed
Definition: TraCIDefs.h:578
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:584
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:574
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:588
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:568
std::mutex & getMutex() const
Definition: Connection.h:76
static Connection & getActive()
Definition: Connection.h:57
tcpip::Storage & doCommand(int command, int var=-1, const std::string &id="", tcpip::Storage *add=nullptr, int expectedType=-1)
Definition: Connection.cpp:329
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:231
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:153
static void setCol(int var, const std::string &id, const libsumo::TraCIColor value)
Definition: Domain.h:252
static int getUnsignedByte(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:115
static void setStringVector(int var, const std::string &id, const std::vector< std::string > &value)
Definition: Domain.h:245
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:177
static libsumo::TraCIColor getCol(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:187
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:172
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:125
static libsumo::TraCIStage getTraCIStage(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:198
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:135
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition: Domain.h:219
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:162
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:130
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:111
static void setInt(int var, const std::string &id, int value)
Definition: Domain.h:224
static std::vector< double > getDoubleVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:182
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:238
static int getByte(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:120
virtual std::string readString()
Definition: storage.cpp:180
virtual void writeString(const std::string &s)
Definition: storage.cpp:197
virtual void writeInt(int)
Definition: storage.cpp:321
virtual void writeDouble(double)
Definition: storage.cpp:354
virtual int readUnsignedByte()
Definition: storage.cpp:155
virtual void writeStringList(const std::vector< std::string > &s)
Definition: storage.cpp:247
virtual void writeUnsignedByte(int)
Definition: storage.cpp:165
virtual int readByte()
Definition: storage.cpp:128
virtual std::vector< std::string > readStringList()
Definition: storage.cpp:211
virtual double readDouble()
Definition: storage.cpp:362
virtual int readInt()
Definition: storage.cpp:311
virtual std::vector< double > readDoubleList()
Definition: storage.cpp:229
TRACI_CONST int TYPE_COLOR
TRACI_CONST int POSITION_3D
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int TYPE_UBYTE
TRACI_CONST int POSITION_2D
TRACI_CONST int TYPE_POLYGON
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int POSITION_LON_LAT
TRACI_CONST int TYPE_DOUBLELIST
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int TYPE_BYTE
TRACI_CONST int POSITION_LON_LAT_ALT
TRACI_CONST int TYPE_STRING
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition: TraCIDefs.h:178
A list of positions.
Definition: TraCIDefs.h:234
std::vector< TraCIPosition > value
Definition: TraCIDefs.h:244