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