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.org/sumo
3 // Copyright (C) 2012-2022 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 tcpip::Storage& get(int var, const std::string& id, tcpip::Storage* add = nullptr, int expectedType = libsumo::TYPE_COMPOUND) {
112  tcpip::Storage& result = libtraci::Connection::getActive().doCommand(GET, var, id, add);
113  libtraci::Connection::getActive().check_commandGetResult(result, GET, expectedType);
114  return result;
115  }
116 
117  static int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
118  return get(var, id, add, libsumo::TYPE_UBYTE).readUnsignedByte();
119  }
120 
121  static int getByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
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  return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
127  }
128 
129  static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
130  return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
131  }
132 
133  static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
134  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
136  int size = result.readUnsignedByte();
137  if (size == 0) {
138  size = result.readInt();
139  }
140  for (int i = 0; i < size; ++i) {
142  p.x = result.readDouble();
143  p.y = result.readDouble();
144  p.z = 0.;
145  ret.value.push_back(p);
146  }
147  return ret;
148  }
149 
150  static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
151  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
153  p.x = result.readDouble();
154  p.y = result.readDouble();
155  return p;
156  }
157 
158  static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
159  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
161  p.x = result.readDouble();
162  p.y = result.readDouble();
163  p.z = result.readDouble();
164  return p;
165  }
166 
167  static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
168  return get(var, id, add, libsumo::TYPE_STRING).readString();
169  }
170 
171  static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
172  return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
173  }
174 
175  static std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
176  return get(var, id, add, libsumo::TYPE_DOUBLELIST).readDoubleList();
177  }
178 
179  static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
180  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
182  c.r = (unsigned char)result.readUnsignedByte();
183  c.g = (unsigned char)result.readUnsignedByte();
184  c.b = (unsigned char)result.readUnsignedByte();
185  c.a = (unsigned char)result.readUnsignedByte();
186  return c;
187  }
188 
189  static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
190  tcpip::Storage& result = get(var, id, add);
192  result.readInt(); // components
193  s.type = StoHelp::readTypedInt(result);
194  s.vType = StoHelp::readTypedString(result);
195  s.line = StoHelp::readTypedString(result);
199  s.cost = StoHelp::readTypedDouble(result);
200  s.length = StoHelp::readTypedDouble(result);
202  s.depart = StoHelp::readTypedDouble(result);
206  return s;
207  }
208 
209  static void set(int var, const std::string& id, tcpip::Storage* add) {
210  libtraci::Connection::getActive().doCommand(SET, var, id, add);
211  }
212 
213  static void setInt(int var, const std::string& id, int value) {
214  tcpip::Storage content;
216  content.writeInt(value);
217  set(var, id, &content);
218  }
219 
220  static void setDouble(int var, const std::string& id, double value) {
221  tcpip::Storage content;
223  content.writeDouble(value);
224  set(var, id, &content);
225  }
226 
227  static void setString(int var, const std::string& id, const std::string& value) {
228  tcpip::Storage content;
230  content.writeString(value);
231  set(var, id, &content);
232  }
233 
234  static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
235  tcpip::Storage content;
237  content.writeStringList(value);
238  set(var, id, &content);
239  }
240 
241  static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
242  tcpip::Storage content;
244  content.writeUnsignedByte(value.r);
245  content.writeUnsignedByte(value.g);
246  content.writeUnsignedByte(value.b);
247  content.writeUnsignedByte(value.a);
248  set(var, id, &content);
249  }
250 
251 };
252 
253 }
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:545
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:529
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:535
double length
length in m
Definition: TraCIDefs.h:543
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:539
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:549
std::string description
arbitrary description string
Definition: TraCIDefs.h:553
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:533
double cost
effort needed
Definition: TraCIDefs.h:541
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:547
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:537
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:551
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:531
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
Definition: Connection.cpp:305
static Connection & getActive()
Definition: Connection.h:55
tcpip::Storage & doCommand(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Connection.cpp:328
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:220
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:150
static void setCol(int var, const std::string &id, const libsumo::TraCIColor value)
Definition: Domain.h:241
static int getUnsignedByte(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:117
static void setStringVector(int var, const std::string &id, const std::vector< std::string > &value)
Definition: Domain.h:234
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:171
static libsumo::TraCIColor getCol(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:179
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:167
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:189
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:133
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition: Domain.h:209
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:158
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:129
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:213
static std::vector< double > getDoubleVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:175
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:227
static int getByte(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:121
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 3D-position.
Definition: TraCIDefs.h:172
A list of positions.
Definition: TraCIDefs.h:215
std::vector< TraCIPosition > value
Definition: TraCIDefs.h:225