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 void CLASS::subscribe(const std::string& objectID, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
40  libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_VARIABLE, objectID, begin, end, -1, -1, varIDs, params); \
41 } \
42 \
43 void CLASS::unsubscribe(const std::string& objectID) { \
44  subscribe(objectID, std::vector<int>()); \
45 } \
46 \
47 void CLASS::subscribeContext(const std::string& objectID, int domain, double dist, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
48  libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_CONTEXT, objectID, begin, end, domain, dist, varIDs, params); \
49 } \
50 \
51 void CLASS::unsubscribeContext(const std::string& objectID, int domain, double dist) { \
52  subscribeContext(objectID, domain, dist, std::vector<int>()); \
53 } \
54 \
55 const libsumo::SubscriptionResults CLASS::getAllSubscriptionResults() { \
56  return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE); \
57 } \
58 \
59 const libsumo::TraCIResults CLASS::getSubscriptionResults(const std::string& objectID) { \
60  return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE)[objectID]; \
61 } \
62 \
63 const libsumo::ContextSubscriptionResults CLASS::getAllContextSubscriptionResults() { \
64  return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT); \
65 } \
66 \
67 const libsumo::SubscriptionResults CLASS::getContextSubscriptionResults(const std::string& objectID) { \
68  return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT)[objectID]; \
69 } \
70 \
71 void CLASS::subscribeParameterWithKey(const std::string& objectID, const std::string& key, double beginTime, double endTime) { \
72  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)}}); \
73 }
74 
75 
76 #define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN) \
77 std::string \
78 CLASS::getParameter(const std::string& objectID, const std::string& param) { \
79  tcpip::Storage content; \
80  content.writeByte(libsumo::TYPE_STRING); \
81  content.writeString(param); \
82  return Dom::getString(libsumo::VAR_PARAMETER, objectID, &content); \
83 } \
84 \
85 void \
86 CLASS::setParameter(const std::string& objectID, const std::string& key, const std::string& value) { \
87  tcpip::Storage content; \
88  content.writeUnsignedByte(libsumo::TYPE_COMPOUND); \
89  content.writeInt(2); \
90  content.writeUnsignedByte(libsumo::TYPE_STRING); \
91  content.writeString(key); \
92  content.writeUnsignedByte(libsumo::TYPE_STRING); \
93  content.writeString(value); \
94  Connection::getActive().doCommand(libsumo::CMD_SET_##DOMAIN##_VARIABLE, libsumo::VAR_PARAMETER, objectID, &content); \
95 } \
96 \
97 const std::pair<std::string, std::string> \
98 CLASS::getParameterWithKey(const std::string& objectID, const std::string& key) { \
99  return std::make_pair(key, getParameter(objectID, key)); \
100 }
101 
102 
103 // ===========================================================================
104 // class and type definitions
105 // ===========================================================================
106 namespace libtraci {
107 template<int GET, int SET>
108 class Domain {
109 public:
110  static tcpip::Storage& get(int var, const std::string& id, tcpip::Storage* add = nullptr, int expectedType = libsumo::TYPE_COMPOUND) {
111  tcpip::Storage& result = libtraci::Connection::getActive().doCommand(GET, var, id, add);
112  libtraci::Connection::getActive().check_commandGetResult(result, GET, expectedType);
113  return result;
114  }
115 
116  static int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
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  return get(var, id, add, libsumo::TYPE_BYTE).readByte();
122  }
123 
124  static int getInt(int var, const std::string& id, tcpip::Storage* add = nullptr) {
125  return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
126  }
127 
128  static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
129  return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
130  }
131 
132  static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
133  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
135  int size = result.readUnsignedByte();
136  if (size == 0) {
137  size = result.readInt();
138  }
139  for (int i = 0; i < size; ++i) {
141  p.x = result.readDouble();
142  p.y = result.readDouble();
143  p.z = 0.;
144  ret.value.push_back(p);
145  }
146  return ret;
147  }
148 
149  static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
150  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
152  p.x = result.readDouble();
153  p.y = result.readDouble();
154  return p;
155  }
156 
157  static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
158  tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
160  p.x = result.readDouble();
161  p.y = result.readDouble();
162  p.z = result.readDouble();
163  return p;
164  }
165 
166  static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
167  return get(var, id, add, libsumo::TYPE_STRING).readString();
168  }
169 
170  static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
171  return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
172  }
173 
174  static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
175  tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
177  c.r = (unsigned char)result.readUnsignedByte();
178  c.g = (unsigned char)result.readUnsignedByte();
179  c.b = (unsigned char)result.readUnsignedByte();
180  c.a = (unsigned char)result.readUnsignedByte();
181  return c;
182  }
183 
184  static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
185  tcpip::Storage& result = get(var, id, add);
187  result.readInt(); // components
188  s.type = StoHelp::readTypedInt(result);
189  s.vType = StoHelp::readTypedString(result);
190  s.line = StoHelp::readTypedString(result);
194  s.cost = StoHelp::readTypedDouble(result);
195  s.length = StoHelp::readTypedDouble(result);
197  s.depart = StoHelp::readTypedDouble(result);
201  return s;
202  }
203 
204  static void set(int var, const std::string& id, tcpip::Storage* add) {
205  libtraci::Connection::getActive().doCommand(SET, var, id, add);
206  }
207 
208  static void setInt(int var, const std::string& id, int value) {
209  tcpip::Storage content;
211  content.writeInt(value);
212  set(var, id, &content);
213  }
214 
215  static void setDouble(int var, const std::string& id, double value) {
216  tcpip::Storage content;
218  content.writeDouble(value);
219  set(var, id, &content);
220  }
221 
222  static void setString(int var, const std::string& id, const std::string& value) {
223  tcpip::Storage content;
225  content.writeString(value);
226  set(var, id, &content);
227  }
228 
229  static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
230  tcpip::Storage content;
232  content.writeStringList(value);
233  set(var, id, &content);
234  }
235 
236  static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
237  tcpip::Storage content;
239  content.writeUnsignedByte(value.r);
240  content.writeUnsignedByte(value.g);
241  content.writeUnsignedByte(value.b);
242  content.writeUnsignedByte(value.a);
243  set(var, id, &content);
244  }
245 
246 };
247 
248 }
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:523
int type
The type of stage (walking, driving, ...)
Definition: TraCIDefs.h:507
std::string destStop
The id of the destination stop.
Definition: TraCIDefs.h:513
double length
length in m
Definition: TraCIDefs.h:521
double travelTime
duration of the stage in seconds
Definition: TraCIDefs.h:517
double departPos
position on the lane when starting the stage
Definition: TraCIDefs.h:527
std::string description
arbitrary description string
Definition: TraCIDefs.h:531
std::string line
The line or the id of the vehicle type.
Definition: TraCIDefs.h:511
double cost
effort needed
Definition: TraCIDefs.h:519
double depart
intended depart time for public transport ride or INVALID_DOUBLE_VALUE
Definition: TraCIDefs.h:525
std::vector< std::string > edges
The sequence of edges to travel.
Definition: TraCIDefs.h:515
double arrivalPos
position on the lane when ending the stage
Definition: TraCIDefs.h:529
std::string vType
The vehicle type when using a private car or bike.
Definition: TraCIDefs.h:509
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:303
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:326
static void setDouble(int var, const std::string &id, double value)
Definition: Domain.h:215
static libsumo::TraCIPosition getPos(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:149
static void setCol(int var, const std::string &id, const libsumo::TraCIColor value)
Definition: Domain.h:236
static int getUnsignedByte(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:116
static void setStringVector(int var, const std::string &id, const std::vector< std::string > &value)
Definition: Domain.h:229
static std::vector< std::string > getStringVector(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:170
static libsumo::TraCIColor getCol(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:174
static std::string getString(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:166
static int getInt(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:124
static libsumo::TraCIStage getTraCIStage(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:184
static libsumo::TraCIPositionVector getPolygon(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:132
static void set(int var, const std::string &id, tcpip::Storage *add)
Definition: Domain.h:204
static libsumo::TraCIPosition getPos3D(int var, const std::string &id, tcpip::Storage *add=nullptr, const bool isGeo=false)
Definition: Domain.h:157
static double getDouble(int var, const std::string &id, tcpip::Storage *add=nullptr)
Definition: Domain.h:128
static tcpip::Storage & get(int var, const std::string &id, tcpip::Storage *add=nullptr, int expectedType=libsumo::TYPE_COMPOUND)
Definition: Domain.h:110
static void setInt(int var, const std::string &id, int value)
Definition: Domain.h:208
static void setString(int var, const std::string &id, const std::string &value)
Definition: Domain.h:222
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
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_DOUBLE
TRACI_CONST int TYPE_BYTE
TRACI_CONST int POSITION_LON_LAT_ALT
TRACI_CONST int TYPE_STRING
A 3D-position.
Definition: TraCIDefs.h:164
A list of positions.
Definition: TraCIDefs.h:207
std::vector< TraCIPosition > value
Definition: TraCIDefs.h:217