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