Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
23// APIs for getting/setting vehicle type values via TraCI
24/****************************************************************************/
25#include <config.h>
26
27#include <limits>
29#include <microsim/MSNet.h>
32#include <libsumo/VehicleType.h>
34
35
36// ===========================================================================
37// method definitions
38// ===========================================================================
39bool
41 tcpip::Storage& outputStorage) {
42 const int variable = inputStorage.readUnsignedByte();
43 const std::string id = inputStorage.readString();
45 try {
46 if (!libsumo::VehicleType::handleVariable(id, variable, &server, &inputStorage)) {
48 "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2)
49 + " specified", outputStorage);
50 }
51 } catch (libsumo::TraCIException& e) {
52 return server.writeErrorStatusCmd(libsumo::CMD_GET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
53 }
55 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
56 return true;
57}
58
59
60bool
62 tcpip::Storage& outputStorage) {
63 std::string warning = ""; // additional description for response
64 // variable
65 int variable = inputStorage.readUnsignedByte();
66 if (variable != libsumo::VAR_LENGTH && variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_VEHICLECLASS
68 && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MINGAP && variable != libsumo::VAR_SHAPECLASS
69 && variable != libsumo::VAR_ACCEL && variable != libsumo::VAR_IMPERFECTION
70 && variable != libsumo::VAR_DECEL && variable != libsumo::VAR_EMERGENCY_DECEL && variable != libsumo::VAR_APPARENT_DECEL
71 && variable != libsumo::VAR_TAU && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_ACTIONSTEPLENGTH
72 && variable != libsumo::VAR_SCALE
73 && variable != libsumo::VAR_HEIGHT
74 && variable != libsumo::VAR_MASS
75 && variable != libsumo::VAR_MINGAP_LAT
76 && variable != libsumo::VAR_MAXSPEED_LAT
77 && variable != libsumo::VAR_LATALIGNMENT
79 && variable != libsumo::VAR_IMPATIENCE
80 && variable != libsumo::VAR_PARAMETER
81 && variable != libsumo::COPY
82 ) {
84 "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
85 + " specified", outputStorage);
86 }
87 // id
88 std::string id = inputStorage.readString();
89// MSVehicleType* v = libsumo::VehicleType::getVType(id);
90// if (v == 0) {
91// return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
92// outputStorage);
93// }
94 // process
95 try {
96 if (setVariable(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
98 return true;
99 }
100 } catch (ProcessError& e) {
101 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
102 } catch (libsumo::TraCIException& e) {
103 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
104 }
105 return false;
106}
107
108
109bool
110TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
111 const std::string& id, TraCIServer& server,
112 tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
113 switch (variable) {
114 case libsumo::VAR_LENGTH: {
115 double value = 0;
116 if (!server.readTypeCheckingDouble(inputStorage, value)) {
117 return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
118 }
119 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
120 return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
121 }
122 libsumo::VehicleType::setLength(id, value);
123 }
124 break;
125 case libsumo::VAR_HEIGHT: {
126 double value = 0;
127 if (!server.readTypeCheckingDouble(inputStorage, value)) {
128 return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
129 }
130 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
131 return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
132 }
133 libsumo::VehicleType::setHeight(id, value);
134 }
135 break;
136 case libsumo::VAR_MASS: {
137 double value = 0;
138 if (!server.readTypeCheckingDouble(inputStorage, value)) {
139 return server.writeErrorStatusCmd(cmd, "Setting mass requires a double.", outputStorage);
140 }
141 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
142 return server.writeErrorStatusCmd(cmd, "Invalid mass.", outputStorage);
143 }
144 libsumo::VehicleType::setMass(id, value);
145 }
146 break;
148 double value = 0;
149 if (!server.readTypeCheckingDouble(inputStorage, value)) {
150 return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
151 }
152 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
153 return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
154 }
155 libsumo::VehicleType::setMaxSpeed(id, value);
156 }
157 break;
159 std::string vclass;
160 if (!server.readTypeCheckingString(inputStorage, vclass)) {
161 return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
162 }
163 try {
164 libsumo::VehicleType::setVehicleClass(id, vclass);
165 } catch (InvalidArgument&) {
166 return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
167 }
168 }
169 break;
171 double value = 0;
172 if (!server.readTypeCheckingDouble(inputStorage, value)) {
173 return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
174 }
175 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
176 return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
177 }
178 libsumo::VehicleType::setSpeedFactor(id, value);
179 }
180 break;
182 double value = 0;
183 if (!server.readTypeCheckingDouble(inputStorage, value)) {
184 return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
185 }
186 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
187 return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
188 }
189 libsumo::VehicleType::setSpeedDeviation(id, value);
190 }
191 break;
193 std::string eclass;
194 if (!server.readTypeCheckingString(inputStorage, eclass)) {
195 return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
196 }
197 try {
198 libsumo::VehicleType::setEmissionClass(id, eclass);
199 } catch (InvalidArgument& e) {
200 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
201 }
202 }
203 break;
204 case libsumo::VAR_WIDTH: {
205 double value = 0;
206 if (!server.readTypeCheckingDouble(inputStorage, value)) {
207 return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
208 }
209 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
210 return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
211 }
212 libsumo::VehicleType::setWidth(id, value);
213 }
214 break;
215 case libsumo::VAR_MINGAP: {
216 double value = 0;
217 if (!server.readTypeCheckingDouble(inputStorage, value)) {
218 return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
219 }
220 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
221 return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
222 }
223 libsumo::VehicleType::setMinGap(id, value);
224 }
225 break;
227 double value = 0;
228 if (!server.readTypeCheckingDouble(inputStorage, value)) {
229 return server.writeErrorStatusCmd(cmd, "Setting minimum lateral gap requires a double.", outputStorage);
230 }
231 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
232 return server.writeErrorStatusCmd(cmd, "Invalid minimum lateral gap.", outputStorage);
233 }
234 libsumo::VehicleType::setMinGapLat(id, value);
235 }
236 break;
238 double value = 0;
239 if (!server.readTypeCheckingDouble(inputStorage, value)) {
240 return server.writeErrorStatusCmd(cmd, "Setting maximum lateral speed requires a double.", outputStorage);
241 }
242 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
243 return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
244 }
245 libsumo::VehicleType::setMaxSpeedLat(id, value);
246 }
247 break;
249 std::string latAlign;
250 if (!server.readTypeCheckingString(inputStorage, latAlign)) {
251 return server.writeErrorStatusCmd(cmd, "Setting preferred lateral alignment requires a string.",
252 outputStorage);
253 }
254 try {
255 libsumo::VehicleType::setLateralAlignment(id, latAlign);
256 } catch (const libsumo::TraCIException& e) {
257 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
258 }
259 }
260 break;
262 std::string sclass;
263 if (!server.readTypeCheckingString(inputStorage, sclass)) {
264 return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
265 }
266 try {
267 libsumo::VehicleType::setShapeClass(id, sclass);
268 } catch (InvalidArgument& e) {
269 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
270 }
271 }
272 break;
273 case libsumo::VAR_ACCEL: {
274 double value = 0;
275 if (!server.readTypeCheckingDouble(inputStorage, value)) {
276 return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
277 }
278 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
279 return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
280 }
281 libsumo::VehicleType::setAccel(id, value);
282 }
283 break;
284 case libsumo::VAR_DECEL: {
285 double value = 0;
286 if (!server.readTypeCheckingDouble(inputStorage, value)) {
287 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
288 }
289 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
290 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
291 }
292 libsumo::VehicleType::setDecel(id, value);
293 }
294 break;
296 double value = 0;
297 if (!server.readTypeCheckingDouble(inputStorage, value)) {
298 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
299 }
300 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
301 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
302 }
303 libsumo::VehicleType::setEmergencyDecel(id, value);
304 }
305 break;
307 double value = 0;
308 if (!server.readTypeCheckingDouble(inputStorage, value)) {
309 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
310 }
311 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
312 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
313 }
314 libsumo::VehicleType::setApparentDecel(id, value);
315 }
316 break;
317 case libsumo::VAR_SCALE: {
318 double value = 0;
319 if (!server.readTypeCheckingDouble(inputStorage, value)) {
320 return server.writeErrorStatusCmd(cmd, "Setting traffic scale requires a double.", outputStorage);
321 }
322 if (value < 0.0) {
323 return server.writeErrorStatusCmd(cmd, "Traffic scale may not be negative.", outputStorage);
324 }
325 libsumo::VehicleType::setScale(id, value);
326 }
327 break;
329 double value = 0;
330 if (!server.readTypeCheckingDouble(inputStorage, value)) {
331 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Setting action step length requires a double.", outputStorage);
332 }
333 if (fabs(value) == std::numeric_limits<double>::infinity()) {
334 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Invalid action step length.", outputStorage);
335 }
336 bool resetActionOffset = value >= 0.0;
337 libsumo::VehicleType::setActionStepLength(id, fabs(value), resetActionOffset);
338 }
339 break;
341 double value = 0;
342 if (!server.readTypeCheckingDouble(inputStorage, value)) {
343 return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
344 }
345 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
346 return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
347 }
348 libsumo::VehicleType::setImperfection(id, value);
349 }
350 break;
351 case libsumo::VAR_TAU: {
352 double value = 0;
353 if (!server.readTypeCheckingDouble(inputStorage, value)) {
354 return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
355 }
356 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
357 return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
358 }
359 libsumo::VehicleType::setTau(id, value);
360 }
361 break;
363 double value = 0;
364 if (!server.readTypeCheckingDouble(inputStorage, value)) {
365 return server.writeErrorStatusCmd(cmd, "Setting impatience requires a double.", outputStorage);
366 }
367 libsumo::VehicleType::setImpatience(id, value);
368 }
369 break;
371 double value = 0;
372 if (!server.readTypeCheckingDouble(inputStorage, value)) {
373 return server.writeErrorStatusCmd(cmd, "Setting boardingDuration requires a double.", outputStorage);
374 }
375 libsumo::VehicleType::setBoardingDuration(id, value);
376 }
377 break;
378 case libsumo::VAR_COLOR: {
380 if (!server.readTypeCheckingColor(inputStorage, col)) {
381 return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
382 }
383 libsumo::VehicleType::setColor(id, col);
384 }
385 break;
386 case libsumo::COPY: {
387 std::string newTypeID;
388 if (!server.readTypeCheckingString(inputStorage, newTypeID)) {
389 return server.writeErrorStatusCmd(cmd, "copying a vehicle type requires a string.",
390 outputStorage);
391 }
392 libsumo::VehicleType::copy(id, newTypeID);
393 }
394 break;
396 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
397 return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.",
398 outputStorage);
399 }
400 //readt itemNo
401 inputStorage.readInt();
402 std::string name;
403 if (!server.readTypeCheckingString(inputStorage, name)) {
404 return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.",
405 outputStorage);
406 }
407 std::string value;
408 if (!server.readTypeCheckingString(inputStorage, value)) {
409 return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.",
410 outputStorage);
411 }
412 libsumo::VehicleType::setParameter(id, name, value);
413 }
414 break;
415 default:
416 break;
417 }
418 return true;
419}
420
421
422/****************************************************************************/
const std::string invalid_return< std::string >::value
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:56
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State).
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable).
static bool setVariable(const int cmd, const int variable, const std::string &id, TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value for the given type.
TraCI server used to control sumo by a remote TraCI client.
Definition TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
tcpip::Storage & getWrapperStorage()
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
An error which allows to continue.
Definition TraCIDefs.h:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual int readInt()
Definition storage.cpp:311
TRACI_CONST int VAR_VEHICLECLASS
TRACI_CONST int VAR_IMPATIENCE
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_SCALE
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_SHAPECLASS
TRACI_CONST int VAR_ACTIONSTEPLENGTH
TRACI_CONST int VAR_SPEED_FACTOR
TRACI_CONST int VAR_TAU
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_BOARDING_DURATION
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int COPY
TRACI_CONST int VAR_MASS
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_MAXSPEED_LAT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int CMD_SET_VEHICLETYPE_VARIABLE
TRACI_CONST int CMD_GET_VEHICLETYPE_VARIABLE
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int VAR_APPARENT_DECEL
TRACI_CONST int VAR_DECEL
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_EMERGENCY_DECEL
TRACI_CONST int RTYPE_OK
TRACI_CONST int RESPONSE_GET_VEHICLETYPE_VARIABLE
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_ACCEL
TRACI_CONST int VAR_SPEED_DEVIATION