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