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-2023 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_MINGAP_LAT
75 && variable != libsumo::VAR_MAXSPEED_LAT
76 && variable != libsumo::VAR_LATALIGNMENT
78 && variable != libsumo::VAR_IMPATIENCE
79 && variable != libsumo::VAR_PARAMETER
80 && variable != libsumo::COPY
81 ) {
83 "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
84 + " specified", outputStorage);
85 }
86 // id
87 std::string id = inputStorage.readString();
88// MSVehicleType* v = libsumo::VehicleType::getVType(id);
89// if (v == 0) {
90// return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
91// outputStorage);
92// }
93 // process
94 try {
95 if (setVariable(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
97 return true;
98 }
99 } catch (ProcessError& e) {
100 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
101 } catch (libsumo::TraCIException& e) {
102 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
103 }
104 return false;
105}
106
107
108bool
109TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
110 const std::string& id, TraCIServer& server,
111 tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
112 switch (variable) {
113 case libsumo::VAR_LENGTH: {
114 double value = 0;
115 if (!server.readTypeCheckingDouble(inputStorage, value)) {
116 return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
117 }
118 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
119 return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
120 }
121 libsumo::VehicleType::setLength(id, value);
122 }
123 break;
124 case libsumo::VAR_HEIGHT: {
125 double value = 0;
126 if (!server.readTypeCheckingDouble(inputStorage, value)) {
127 return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
128 }
129 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
130 return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
131 }
132 libsumo::VehicleType::setHeight(id, value);
133 }
134 break;
136 double value = 0;
137 if (!server.readTypeCheckingDouble(inputStorage, value)) {
138 return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
139 }
140 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
141 return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
142 }
143 libsumo::VehicleType::setMaxSpeed(id, value);
144 }
145 break;
147 std::string vclass;
148 if (!server.readTypeCheckingString(inputStorage, vclass)) {
149 return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
150 }
151 try {
152 libsumo::VehicleType::setVehicleClass(id, vclass);
153 } catch (InvalidArgument&) {
154 return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
155 }
156 }
157 break;
159 double value = 0;
160 if (!server.readTypeCheckingDouble(inputStorage, value)) {
161 return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
162 }
163 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
164 return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
165 }
166 libsumo::VehicleType::setSpeedFactor(id, value);
167 }
168 break;
170 double value = 0;
171 if (!server.readTypeCheckingDouble(inputStorage, value)) {
172 return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
173 }
174 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
175 return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
176 }
177 libsumo::VehicleType::setSpeedDeviation(id, value);
178 }
179 break;
181 std::string eclass;
182 if (!server.readTypeCheckingString(inputStorage, eclass)) {
183 return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
184 }
185 try {
186 libsumo::VehicleType::setEmissionClass(id, eclass);
187 } catch (InvalidArgument& e) {
188 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
189 }
190 }
191 break;
192 case libsumo::VAR_WIDTH: {
193 double value = 0;
194 if (!server.readTypeCheckingDouble(inputStorage, value)) {
195 return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
196 }
197 if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
198 return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
199 }
200 libsumo::VehicleType::setWidth(id, value);
201 }
202 break;
203 case libsumo::VAR_MINGAP: {
204 double value = 0;
205 if (!server.readTypeCheckingDouble(inputStorage, value)) {
206 return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
207 }
208 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
209 return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
210 }
211 libsumo::VehicleType::setMinGap(id, value);
212 }
213 break;
215 double value = 0;
216 if (!server.readTypeCheckingDouble(inputStorage, value)) {
217 return server.writeErrorStatusCmd(cmd, "Setting minimum lateral gap requires a double.", outputStorage);
218 }
219 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
220 return server.writeErrorStatusCmd(cmd, "Invalid minimum lateral gap.", outputStorage);
221 }
222 libsumo::VehicleType::setMinGapLat(id, value);
223 }
224 break;
226 double value = 0;
227 if (!server.readTypeCheckingDouble(inputStorage, value)) {
228 return server.writeErrorStatusCmd(cmd, "Setting maximum lateral speed requires a double.", outputStorage);
229 }
230 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
231 return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
232 }
233 libsumo::VehicleType::setMaxSpeedLat(id, value);
234 }
235 break;
237 std::string latAlign;
238 if (!server.readTypeCheckingString(inputStorage, latAlign)) {
239 return server.writeErrorStatusCmd(cmd, "Setting preferred lateral alignment requires a string.",
240 outputStorage);
241 }
242 try {
243 libsumo::VehicleType::setLateralAlignment(id, latAlign);
244 } catch (const libsumo::TraCIException& e) {
245 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
246 }
247 }
248 break;
250 std::string sclass;
251 if (!server.readTypeCheckingString(inputStorage, sclass)) {
252 return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
253 }
254 try {
255 libsumo::VehicleType::setShapeClass(id, sclass);
256 } catch (InvalidArgument& e) {
257 return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
258 }
259 }
260 break;
261 case libsumo::VAR_ACCEL: {
262 double value = 0;
263 if (!server.readTypeCheckingDouble(inputStorage, value)) {
264 return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
265 }
266 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
267 return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
268 }
269 libsumo::VehicleType::setAccel(id, value);
270 }
271 break;
272 case libsumo::VAR_DECEL: {
273 double value = 0;
274 if (!server.readTypeCheckingDouble(inputStorage, value)) {
275 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
276 }
277 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
278 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
279 }
280 libsumo::VehicleType::setDecel(id, value);
281 }
282 break;
284 double value = 0;
285 if (!server.readTypeCheckingDouble(inputStorage, value)) {
286 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
287 }
288 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
289 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
290 }
291 libsumo::VehicleType::setEmergencyDecel(id, value);
292 }
293 break;
295 double value = 0;
296 if (!server.readTypeCheckingDouble(inputStorage, value)) {
297 return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
298 }
299 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
300 return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
301 }
302 libsumo::VehicleType::setApparentDecel(id, value);
303 }
304 break;
305 case libsumo::VAR_SCALE: {
306 double value = 0;
307 if (!server.readTypeCheckingDouble(inputStorage, value)) {
308 return server.writeErrorStatusCmd(cmd, "Setting traffic scale requires a double.", outputStorage);
309 }
310 if (value < 0.0) {
311 return server.writeErrorStatusCmd(cmd, "Traffic scale may not be negative.", outputStorage);
312 }
313 libsumo::VehicleType::setScale(id, value);
314 }
315 break;
317 double value = 0;
318 if (!server.readTypeCheckingDouble(inputStorage, value)) {
319 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Setting action step length requires a double.", outputStorage);
320 }
321 if (fabs(value) == std::numeric_limits<double>::infinity()) {
322 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Invalid action step length.", outputStorage);
323 }
324 bool resetActionOffset = value >= 0.0;
325 libsumo::VehicleType::setActionStepLength(id, fabs(value), resetActionOffset);
326 }
327 break;
329 double value = 0;
330 if (!server.readTypeCheckingDouble(inputStorage, value)) {
331 return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
332 }
333 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
334 return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
335 }
336 libsumo::VehicleType::setImperfection(id, value);
337 }
338 break;
339 case libsumo::VAR_TAU: {
340 double value = 0;
341 if (!server.readTypeCheckingDouble(inputStorage, value)) {
342 return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
343 }
344 if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
345 return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
346 }
347 libsumo::VehicleType::setTau(id, value);
348 }
349 break;
351 double value = 0;
352 if (!server.readTypeCheckingDouble(inputStorage, value)) {
353 return server.writeErrorStatusCmd(cmd, "Setting impatience requires a double.", outputStorage);
354 }
355 libsumo::VehicleType::setImpatience(id, value);
356 }
357 break;
359 double value = 0;
360 if (!server.readTypeCheckingDouble(inputStorage, value)) {
361 return server.writeErrorStatusCmd(cmd, "Setting boardingDuration requires a double.", outputStorage);
362 }
363 libsumo::VehicleType::setBoardingDuration(id, value);
364 }
365 break;
366 case libsumo::VAR_COLOR: {
368 if (!server.readTypeCheckingColor(inputStorage, col)) {
369 return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
370 }
371 libsumo::VehicleType::setColor(id, col);
372 }
373 break;
374 case libsumo::COPY: {
375 std::string newTypeID;
376 if (!server.readTypeCheckingString(inputStorage, newTypeID)) {
377 return server.writeErrorStatusCmd(cmd, "copying a vehicle type requires a string.",
378 outputStorage);
379 }
380 libsumo::VehicleType::copy(id, newTypeID);
381 }
382 break;
384 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
385 return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.",
386 outputStorage);
387 }
388 //readt itemNo
389 inputStorage.readInt();
390 std::string name;
391 if (!server.readTypeCheckingString(inputStorage, name)) {
392 return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.",
393 outputStorage);
394 }
395 std::string value;
396 if (!server.readTypeCheckingString(inputStorage, value)) {
397 return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.",
398 outputStorage);
399 }
400 libsumo::VehicleType::setParameter(id, name, value);
401 }
402 break;
403 default:
404 break;
405 }
406 return true;
407}
408
409
410/****************************************************************************/
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:144
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 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