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-2020 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)) {
47  switch (variable) {
49  std::string paramName = "";
50  if (!server.readTypeCheckingString(inputStorage, paramName)) {
52  "Retrieval of a parameter requires its name.", outputStorage);
53  }
55  server.getWrapperStorage().writeString(libsumo::VehicleType::getParameter(id, paramName));
56  break;
57  }
59  std::string paramName = "";
60  if (!server.readTypeCheckingString(inputStorage, paramName)) {
61  return server.writeErrorStatusCmd(libsumo::CMD_GET_VEHICLETYPE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
62  }
64  server.getWrapperStorage().writeInt(2);
66  server.getWrapperStorage().writeString(paramName);
68  server.getWrapperStorage().writeString(libsumo::VehicleType::getParameter(id, paramName));
69  break;
70  }
71  default:
73  "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2)
74  + " specified", outputStorage);
75  }
76  }
77  } catch (libsumo::TraCIException& e) {
78  return server.writeErrorStatusCmd(libsumo::CMD_GET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
79  }
81  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
82  return true;
83 }
84 
85 
86 bool
88  tcpip::Storage& outputStorage) {
89  std::string warning = ""; // additional description for response
90  // variable
91  int variable = inputStorage.readUnsignedByte();
92  if (variable != libsumo::VAR_LENGTH && variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_VEHICLECLASS
93  && variable != libsumo::VAR_SPEED_FACTOR && variable != libsumo::VAR_SPEED_DEVIATION && variable != libsumo::VAR_EMISSIONCLASS
94  && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MINGAP && variable != libsumo::VAR_SHAPECLASS
95  && variable != libsumo::VAR_ACCEL && variable != libsumo::VAR_IMPERFECTION
96  && variable != libsumo::VAR_DECEL && variable != libsumo::VAR_EMERGENCY_DECEL && variable != libsumo::VAR_APPARENT_DECEL
97  && variable != libsumo::VAR_TAU && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_ACTIONSTEPLENGTH
98  && variable != libsumo::VAR_HEIGHT
99  && variable != libsumo::VAR_MINGAP_LAT
100  && variable != libsumo::VAR_MAXSPEED_LAT
101  && variable != libsumo::VAR_LATALIGNMENT
102  && variable != libsumo::VAR_PARAMETER
103  && variable != libsumo::COPY
104  ) {
106  "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
107  + " specified", outputStorage);
108  }
109  // id
110  std::string id = inputStorage.readString();
111 // MSVehicleType* v = libsumo::VehicleType::getVType(id);
112 // if (v == 0) {
113 // return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
114 // outputStorage);
115 // }
116  // process
117  try {
118  if (setVariable(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
120  return true;
121  }
122  } catch (ProcessError& e) {
123  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
124  } catch (libsumo::TraCIException& e) {
125  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
126  }
127  return false;
128 }
129 
130 
131 bool
132 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
133  const std::string& id, TraCIServer& server,
134  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
135  switch (variable) {
136  case libsumo::VAR_LENGTH: {
137  double value = 0;
138  if (!server.readTypeCheckingDouble(inputStorage, value)) {
139  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
140  }
141  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
142  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
143  }
144  libsumo::VehicleType::setLength(id, value);
145  }
146  break;
147  case libsumo::VAR_HEIGHT: {
148  double value = 0;
149  if (!server.readTypeCheckingDouble(inputStorage, value)) {
150  return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
151  }
152  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
153  return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
154  }
155  libsumo::VehicleType::setHeight(id, value);
156  }
157  break;
158  case libsumo::VAR_MAXSPEED: {
159  double value = 0;
160  if (!server.readTypeCheckingDouble(inputStorage, value)) {
161  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
162  }
163  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
164  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
165  }
166  libsumo::VehicleType::setMaxSpeed(id, value);
167  }
168  break;
170  std::string vclass;
171  if (!server.readTypeCheckingString(inputStorage, vclass)) {
172  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
173  }
174  try {
175  libsumo::VehicleType::setVehicleClass(id, vclass);
176  } catch (InvalidArgument&) {
177  return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
178  }
179  }
180  break;
182  double value = 0;
183  if (!server.readTypeCheckingDouble(inputStorage, value)) {
184  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
185  }
186  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
187  return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
188  }
189  libsumo::VehicleType::setSpeedFactor(id, value);
190  }
191  break;
193  double value = 0;
194  if (!server.readTypeCheckingDouble(inputStorage, value)) {
195  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
196  }
197  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
198  return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
199  }
200  libsumo::VehicleType::setSpeedDeviation(id, value);
201  }
202  break;
204  std::string eclass;
205  if (!server.readTypeCheckingString(inputStorage, eclass)) {
206  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
207  }
208  try {
209  libsumo::VehicleType::setEmissionClass(id, eclass);
210  } catch (InvalidArgument& e) {
211  return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
212  }
213  }
214  break;
215  case libsumo::VAR_WIDTH: {
216  double value = 0;
217  if (!server.readTypeCheckingDouble(inputStorage, value)) {
218  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
219  }
220  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
221  return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
222  }
223  libsumo::VehicleType::setWidth(id, value);
224  }
225  break;
226  case libsumo::VAR_MINGAP: {
227  double value = 0;
228  if (!server.readTypeCheckingDouble(inputStorage, value)) {
229  return server.writeErrorStatusCmd(cmd, "Setting minimum 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 gap.", outputStorage);
233  }
234  libsumo::VehicleType::setMinGap(id, value);
235  }
236  break;
238  double value = 0;
239  if (!server.readTypeCheckingDouble(inputStorage, value)) {
240  return server.writeErrorStatusCmd(cmd, "Setting minimum lateral gap requires a double.", outputStorage);
241  }
242  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
243  return server.writeErrorStatusCmd(cmd, "Invalid minimum lateral gap.", outputStorage);
244  }
245  libsumo::VehicleType::setMinGapLat(id, value);
246  }
247  break;
249  double value = 0;
250  if (!server.readTypeCheckingDouble(inputStorage, value)) {
251  return server.writeErrorStatusCmd(cmd, "Setting maximum lateral speed requires a double.", outputStorage);
252  }
253  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
254  return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
255  }
256  libsumo::VehicleType::setMaxSpeedLat(id, value);
257  }
258  break;
260  std::string latAlign;
261  if (!server.readTypeCheckingString(inputStorage, latAlign)) {
262  return server.writeErrorStatusCmd(cmd, "Setting preferred lateral alignment requires a string.",
263  outputStorage);
264  }
265  if (SUMOXMLDefinitions::LateralAlignments.hasString(latAlign)) {
266  libsumo::VehicleType::setLateralAlignment(id, latAlign);
267  } else {
268  return server.writeErrorStatusCmd(cmd, "Unknown lateral alignment " + latAlign + "'.", outputStorage);
269  }
270  }
271  break;
273  std::string sclass;
274  if (!server.readTypeCheckingString(inputStorage, sclass)) {
275  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
276  }
277  try {
278  libsumo::VehicleType::setShapeClass(id, sclass);
279  } catch (InvalidArgument& e) {
280  return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
281  }
282  }
283  break;
284  case libsumo::VAR_ACCEL: {
285  double value = 0;
286  if (!server.readTypeCheckingDouble(inputStorage, value)) {
287  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
288  }
289  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
290  return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
291  }
292  libsumo::VehicleType::setAccel(id, value);
293  }
294  break;
295  case libsumo::VAR_DECEL: {
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::setDecel(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::setEmergencyDecel(id, value);
315  }
316  break;
318  double value = 0;
319  if (!server.readTypeCheckingDouble(inputStorage, value)) {
320  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
321  }
322  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
323  return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
324  }
325  libsumo::VehicleType::setApparentDecel(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;
362  case libsumo::VAR_COLOR: {
364  if (!server.readTypeCheckingColor(inputStorage, col)) {
365  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
366  }
367  libsumo::VehicleType::setColor(id, col);
368  }
369  break;
370  case libsumo::COPY: {
371  std::string newTypeID;
372  if (!server.readTypeCheckingString(inputStorage, newTypeID)) {
373  return server.writeErrorStatusCmd(cmd, "copying a vehicle type requires a string.",
374  outputStorage);
375  }
376  libsumo::VehicleType::copy(id, newTypeID);
377  }
378  break;
379  case libsumo::VAR_PARAMETER: {
380  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
381  return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.",
382  outputStorage);
383  }
384  //readt itemNo
385  inputStorage.readInt();
386  std::string name;
387  if (!server.readTypeCheckingString(inputStorage, name)) {
388  return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.",
389  outputStorage);
390  }
391  std::string value;
392  if (!server.readTypeCheckingString(inputStorage, value)) {
393  return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.",
394  outputStorage);
395  }
396  libsumo::VehicleType::setParameter(id, name, value);
397  }
398  break;
399  default:
400  break;
401  }
402  return true;
403 }
404 
405 
406 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:54
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
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.
virtual std::string readString()
Definition: storage.cpp:175
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
virtual void writeInt(int)
Definition: storage.cpp:316
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
virtual int readInt()
Definition: storage.cpp:306
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_PARAMETER_WITH_KEY
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
TRACI_CONST int TYPE_STRING