Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2025 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 Domain.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Mario Krumnow
17 : /// @author Michael Behrisch
18 : /// @author Robert Hilbrich
19 : /// @date 30.05.2012
20 : ///
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 683065 : 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 93365 : static int getInt(int var, const std::string& id, tcpip::Storage* add = nullptr) {
126 93365 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
127 186726 : return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
128 : }
129 :
130 91415 : static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
131 91415 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
132 182802 : return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
133 : }
134 :
135 6036 : static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
136 6036 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
137 : tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
138 : libsumo::TraCIPositionVector poly;
139 6036 : StoHelp::readPolygon(result, poly);
140 6036 : return poly;
141 : }
142 :
143 359966 : static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
144 359966 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
145 359966 : tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
146 359965 : libsumo::TraCIPosition p;
147 359965 : p.x = result.readDouble();
148 359965 : p.y = result.readDouble();
149 359965 : return p;
150 : }
151 :
152 404 : static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
153 404 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
154 404 : tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
155 404 : libsumo::TraCIPosition p;
156 404 : p.x = result.readDouble();
157 404 : p.y = result.readDouble();
158 404 : p.z = result.readDouble();
159 404 : return p;
160 : }
161 :
162 22999 : static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
163 22999 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
164 45720 : return get(var, id, add, libsumo::TYPE_STRING).readString();
165 : }
166 :
167 78232 : static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
168 78232 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
169 156322 : return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
170 : }
171 :
172 2 : static std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
173 2 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
174 4 : return get(var, id, add, libsumo::TYPE_DOUBLELIST).readDoubleList();
175 : }
176 :
177 72 : static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
178 72 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
179 : tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
180 : libsumo::TraCIColor c;
181 72 : c.r = (unsigned char)result.readUnsignedByte();
182 72 : c.g = (unsigned char)result.readUnsignedByte();
183 72 : c.b = (unsigned char)result.readUnsignedByte();
184 72 : c.a = (unsigned char)result.readUnsignedByte();
185 72 : return c;
186 : }
187 :
188 193 : static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
189 193 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
190 : tcpip::Storage& result = get(var, id, add);
191 369 : libsumo::TraCIStage s;
192 176 : result.readInt(); // components
193 176 : s.type = StoHelp::readTypedInt(result);
194 176 : s.vType = StoHelp::readTypedString(result);
195 176 : s.line = StoHelp::readTypedString(result);
196 176 : s.destStop = StoHelp::readTypedString(result);
197 176 : s.edges = StoHelp::readTypedStringList(result);
198 176 : s.travelTime = StoHelp::readTypedDouble(result);
199 176 : s.cost = StoHelp::readTypedDouble(result);
200 176 : s.length = StoHelp::readTypedDouble(result);
201 176 : s.intended = StoHelp::readTypedString(result);
202 176 : s.depart = StoHelp::readTypedDouble(result);
203 176 : s.departPos = StoHelp::readTypedDouble(result);
204 176 : s.arrivalPos = StoHelp::readTypedDouble(result);
205 352 : s.description = StoHelp::readTypedString(result);
206 176 : return s;
207 0 : }
208 :
209 17134 : static void set(int var, const std::string& id, tcpip::Storage* add) {
210 17134 : std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
211 17134 : libtraci::Connection::getActive().doCommand(SET, var, id, add);
212 17091 : }
213 :
214 938 : static void setInt(int var, const std::string& id, int value) {
215 938 : tcpip::Storage content;
216 938 : content.writeUnsignedByte(libsumo::TYPE_INTEGER);
217 938 : content.writeInt(value);
218 938 : set(var, id, &content);
219 938 : }
220 :
221 3469 : static void setDouble(int var, const std::string& id, double value) {
222 3469 : tcpip::Storage content;
223 3469 : content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
224 3469 : content.writeDouble(value);
225 3469 : set(var, id, &content);
226 3469 : }
227 :
228 192 : static void setString(int var, const std::string& id, const std::string& value) {
229 192 : tcpip::Storage content;
230 192 : content.writeUnsignedByte(libsumo::TYPE_STRING);
231 192 : content.writeString(value);
232 192 : set(var, id, &content);
233 192 : }
234 :
235 105 : static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
236 105 : tcpip::Storage content;
237 105 : content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
238 105 : content.writeStringList(value);
239 105 : set(var, id, &content);
240 105 : }
241 :
242 49 : static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
243 49 : tcpip::Storage content;
244 49 : content.writeUnsignedByte(libsumo::TYPE_COLOR);
245 49 : content.writeUnsignedByte(value.r);
246 49 : content.writeUnsignedByte(value.g);
247 49 : content.writeUnsignedByte(value.b);
248 49 : content.writeUnsignedByte(value.a);
249 49 : set(var, id, &content);
250 49 : }
251 :
252 : };
253 :
254 : }
|