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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
20 // APIs for getting/setting vehicle type values via TraCI
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <limits>
31 #include <microsim/MSNet.h>
32 #include <microsim/MSVehicleType.h>
33 #include <libsumo/TraCIConstants.h>
34 #include <libsumo/VehicleType.h>
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
41 bool
43  tcpip::Storage& outputStorage) {
44  const int variable = inputStorage.readUnsignedByte();
45  const std::string id = inputStorage.readString();
47  try {
48  if (!libsumo::VehicleType::handleVariable(id, variable, &server)) {
49  switch (variable) {
51  std::string paramName = "";
52  if (!server.readTypeCheckingString(inputStorage, paramName)) {
54  "Retrieval of a parameter requires its name.", outputStorage);
55  }
58  break;
59  }
60  default:
62  "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2)
63  + " specified", outputStorage);
64  }
65  }
66  } catch (libsumo::TraCIException& e) {
67  return server.writeErrorStatusCmd(libsumo::CMD_GET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
68  }
70  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
71  return true;
72 }
73 
74 
75 bool
77  tcpip::Storage& outputStorage) {
78  std::string warning = ""; // additional description for response
79  // variable
80  int variable = inputStorage.readUnsignedByte();
81  if (variable != libsumo::VAR_LENGTH && variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_VEHICLECLASS
82  && variable != libsumo::VAR_SPEED_FACTOR && variable != libsumo::VAR_SPEED_DEVIATION && variable != libsumo::VAR_EMISSIONCLASS
83  && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MINGAP && variable != libsumo::VAR_SHAPECLASS
84  && variable != libsumo::VAR_ACCEL && variable != libsumo::VAR_IMPERFECTION
85  && variable != libsumo::VAR_DECEL && variable != libsumo::VAR_EMERGENCY_DECEL && variable != libsumo::VAR_APPARENT_DECEL
86  && variable != libsumo::VAR_TAU && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_ACTIONSTEPLENGTH
87  && variable != libsumo::VAR_HEIGHT
88  && variable != libsumo::VAR_MINGAP_LAT
89  && variable != libsumo::VAR_MAXSPEED_LAT
90  && variable != libsumo::VAR_LATALIGNMENT
91  && variable != libsumo::VAR_PARAMETER
92  && variable != libsumo::COPY
93  ) {
95  "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
96  + " specified", outputStorage);
97  }
98  // id
99  std::string id = inputStorage.readString();
100 // MSVehicleType* v = libsumo::VehicleType::getVType(id);
101 // if (v == 0) {
102 // return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
103 // outputStorage);
104 // }
105  // process
106  try {
107  if (setVariable(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
109  return true;
110  }
111  } catch (ProcessError& e) {
112  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
113  } catch (libsumo::TraCIException& e) {
114  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
115  }
116  return false;
117 }
118 
119 
120 bool
121 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
122  const std::string& id, TraCIServer& server,
123  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
124  switch (variable) {
125  case libsumo::VAR_LENGTH: {
126  double value = 0;
127  if (!server.readTypeCheckingDouble(inputStorage, value)) {
128  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
129  }
130  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
131  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
132  }
133  libsumo::VehicleType::setLength(id, value);
134  }
135  break;
136  case libsumo::VAR_HEIGHT: {
137  double value = 0;
138  if (!server.readTypeCheckingDouble(inputStorage, value)) {
139  return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
140  }
141  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
142  return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
143  }
144  libsumo::VehicleType::setHeight(id, value);
145  }
146  break;
147  case libsumo::VAR_MAXSPEED: {
148  double value = 0;
149  if (!server.readTypeCheckingDouble(inputStorage, value)) {
150  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
151  }
152  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
153  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
154  }
155  libsumo::VehicleType::setMaxSpeed(id, value);
156  }
157  break;
159  std::string vclass;
160  if (!server.readTypeCheckingString(inputStorage, vclass)) {
161  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
162  }
163  try {
164  libsumo::VehicleType::setVehicleClass(id, vclass);
165  } catch (InvalidArgument&) {
166  return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
167  }
168  }
169  break;
171  double value = 0;
172  if (!server.readTypeCheckingDouble(inputStorage, value)) {
173  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
174  }
175  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
176  return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
177  }
178  libsumo::VehicleType::setSpeedFactor(id, value);
179  }
180  break;
182  double value = 0;
183  if (!server.readTypeCheckingDouble(inputStorage, value)) {
184  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
185  }
186  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
187  return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
188  }
190  }
191  break;
193  std::string eclass;
194  if (!server.readTypeCheckingString(inputStorage, eclass)) {
195  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
196  }
197  try {
198  libsumo::VehicleType::setEmissionClass(id, eclass);
199  } catch (InvalidArgument& e) {
200  return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
201  }
202  }
203  break;
204  case libsumo::VAR_WIDTH: {
205  double value = 0;
206  if (!server.readTypeCheckingDouble(inputStorage, value)) {
207  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
208  }
209  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
210  return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
211  }
212  libsumo::VehicleType::setWidth(id, value);
213  }
214  break;
215  case libsumo::VAR_MINGAP: {
216  double value = 0;
217  if (!server.readTypeCheckingDouble(inputStorage, value)) {
218  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
219  }
220  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
221  return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
222  }
223  libsumo::VehicleType::setMinGap(id, value);
224  }
225  break;
227  double value = 0;
228  if (!server.readTypeCheckingDouble(inputStorage, value)) {
229  return server.writeErrorStatusCmd(cmd, "Setting minimum lateral 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 lateral gap.", outputStorage);
233  }
234  libsumo::VehicleType::setMinGapLat(id, value);
235  }
236  break;
238  double value = 0;
239  if (!server.readTypeCheckingDouble(inputStorage, value)) {
240  return server.writeErrorStatusCmd(cmd, "Setting maximum lateral speed requires a double.", outputStorage);
241  }
242  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
243  return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
244  }
245  libsumo::VehicleType::setMaxSpeedLat(id, value);
246  }
247  break;
249  std::string latAlign;
250  if (!server.readTypeCheckingString(inputStorage, latAlign)) {
251  return server.writeErrorStatusCmd(cmd, "Setting preferred lateral alignment requires a string.",
252  outputStorage);
253  }
254  if (SUMOXMLDefinitions::LateralAlignments.hasString(latAlign)) {
255  libsumo::VehicleType::setLateralAlignment(id, latAlign);
256  } else {
257  return server.writeErrorStatusCmd(cmd, "Unknown lateral alignment " + latAlign + "'.", outputStorage);
258  }
259  }
260  break;
262  std::string sclass;
263  if (!server.readTypeCheckingString(inputStorage, sclass)) {
264  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
265  }
266  try {
267  libsumo::VehicleType::setShapeClass(id, sclass);
268  } catch (InvalidArgument& e) {
269  return server.writeErrorStatusCmd(cmd, e.what(), outputStorage);
270  }
271  }
272  break;
273  case libsumo::VAR_ACCEL: {
274  double value = 0;
275  if (!server.readTypeCheckingDouble(inputStorage, value)) {
276  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
277  }
278  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
279  return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
280  }
281  libsumo::VehicleType::setAccel(id, value);
282  }
283  break;
284  case libsumo::VAR_DECEL: {
285  double value = 0;
286  if (!server.readTypeCheckingDouble(inputStorage, value)) {
287  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
288  }
289  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
290  return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
291  }
292  libsumo::VehicleType::setDecel(id, value);
293  }
294  break;
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::setEmergencyDecel(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::setApparentDecel(id, value);
315  }
316  break;
318  double value = 0;
319  if (!server.readTypeCheckingDouble(inputStorage, value)) {
320  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Setting action step length requires a double.", outputStorage);
321  }
322  if (fabs(value) == std::numeric_limits<double>::infinity()) {
323  return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Invalid action step length.", outputStorage);
324  }
325  bool resetActionOffset = value >= 0.0;
326  libsumo::VehicleType::setActionStepLength(id, fabs(value), resetActionOffset);
327  }
328  break;
330  double value = 0;
331  if (!server.readTypeCheckingDouble(inputStorage, value)) {
332  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
333  }
334  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
335  return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
336  }
337  libsumo::VehicleType::setImperfection(id, value);
338  }
339  break;
340  case libsumo::VAR_TAU: {
341  double value = 0;
342  if (!server.readTypeCheckingDouble(inputStorage, value)) {
343  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
344  }
345  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
346  return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
347  }
348  libsumo::VehicleType::setTau(id, value);
349  }
350  break;
351  case libsumo::VAR_COLOR: {
353  if (!server.readTypeCheckingColor(inputStorage, col)) {
354  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
355  }
356  libsumo::VehicleType::setColor(id, col);
357  }
358  break;
359  case libsumo::COPY: {
360  std::string newTypeID;
361  if (!server.readTypeCheckingString(inputStorage, newTypeID)) {
362  return server.writeErrorStatusCmd(cmd, "copying a vehicle type requires a string.",
363  outputStorage);
364  }
365  libsumo::VehicleType::copy(id, newTypeID);
366  }
367  break;
368  case libsumo::VAR_PARAMETER: {
369  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
370  return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.",
371  outputStorage);
372  }
373  //readt itemNo
374  inputStorage.readInt();
375  std::string name;
376  if (!server.readTypeCheckingString(inputStorage, name)) {
377  return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.",
378  outputStorage);
379  }
380  std::string value;
381  if (!server.readTypeCheckingString(inputStorage, value)) {
382  return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.",
383  outputStorage);
384  }
385  libsumo::VehicleType::setParameter(id, name, value);
386  }
387  break;
388  default:
389  break;
390  }
391  return true;
392 }
393 
394 
395 /****************************************************************************/
TRACI_CONST int VAR_APPARENT_DECEL
TRACI_CONST int VAR_EMISSIONCLASS
TRACI_CONST int VAR_MINGAP
TRACI_CONST int VAR_MAXSPEED_LAT
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
TRACI_CONST int VAR_ACTIONSTEPLENGTH
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int RTYPE_OK
TRACI_CONST int CMD_GET_VEHICLETYPE_VARIABLE
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_MINGAP_LAT
TRACI_CONST int VAR_VEHICLECLASS
static void setSpeedDeviation(const std::string &typeID, double deviation)
TRACI_CONST int VAR_PARAMETER
static LIBSUMO_VEHICLE_TYPE_GETTER std::string getParameter(const std::string &typeID, const std::string &key)
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
virtual void writeUnsignedByte(int)
TRACI_CONST int VAR_MAXSPEED
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
TRACI_CONST int VAR_TAU
virtual int readUnsignedByte()
TRACI_CONST int VAR_LATALIGNMENT
TRACI_CONST int VAR_ACCEL
virtual int readInt()
TRACI_CONST int CMD_SET_VEHICLETYPE_VARIABLE
static LIBSUMO_VEHICLE_TYPE_SETTER void copy(const std::string &origTypeID, const std::string &newTypeID)
TRACI_CONST int VAR_LENGTH
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
TRACI_CONST int TYPE_STRING
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
TRACI_CONST int VAR_SPEED_DEVIATION
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
TRACI_CONST int VAR_EMERGENCY_DECEL
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State)
static StringBijection< LateralAlignment > LateralAlignments
lateral alignments
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.
virtual void writeString(const std::string &s)
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
TRACI_CONST int VAR_SPEED_FACTOR
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:58
TRACI_CONST int VAR_SHAPECLASS
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
TRACI_CONST int RESPONSE_GET_VEHICLETYPE_VARIABLE
TRACI_CONST int VAR_HEIGHT
void initWrapper(const int domainID, const int variable, const std::string &objID)
TRACI_CONST int VAR_IMPERFECTION
TRACI_CONST int VAR_DECEL
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int COPY