SUMO - Simulation of Urban MObility
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // APIs for getting/setting vehicle type values via TraCI
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #ifndef NO_TRACI
37 
38 #include <limits>
40 #include <microsim/MSNet.h>
41 #include <microsim/MSVehicleType.h>
43 #include "TraCIConstants.h"
45 #include "lib/TraCI.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 bool
53  tcpip::Storage& outputStorage) {
54  // variable & id
55  int variable = inputStorage.readUnsignedByte();
56  std::string id = inputStorage.readString();
57  // check variable
58  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL
59  && variable != VAR_DECEL
60  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS
61  && variable != VAR_SHAPECLASS
62  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
63  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT
64  && variable != VAR_HEIGHT
65  && variable != VAR_MINGAP_LAT
66  && variable != VAR_MAXSPEED_LAT
67  && variable != VAR_LATALIGNMENT
68  && variable != VAR_PARAMETER) {
70  "Get Vehicle Type Variable: unsupported variable " + toHex(variable, 2)
71  + " specified", outputStorage);
72  }
73  // begin response building
74  tcpip::Storage tempMsg;
75  // response-code, variableID, objectID
77  tempMsg.writeUnsignedByte(variable);
78  tempMsg.writeString(id);
79  // process request
80  if (variable == ID_LIST) {
81  std::vector<std::string> ids = TraCI_VehicleType::getIDList();
83  tempMsg.writeStringList(ids);
84  } else if (variable == ID_COUNT) {
85  std::vector<std::string> ids = TraCI_VehicleType::getIDList();
87  tempMsg.writeInt((int) ids.size());
88  } else {
89  try {
90  switch (variable) {
91  case VAR_PARAMETER: {
92  std::string paramName = "";
93  if (!server.readTypeCheckingString(inputStorage, paramName)) {
95  "Retrieval of a parameter requires its name.", outputStorage);
96  }
98  tempMsg.writeString(TraCI_VehicleType::getParameter(id, paramName));
99  }
100  break;
101  default:
102  getVariable(variable, id, tempMsg);
103  break;
104  }
105  } catch (TraCIException& e) {
106  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
107  }
108  }
109  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
110  server.writeResponseWithLength(outputStorage, tempMsg);
111  return true;
112 }
113 
114 bool
115 TraCIServerAPI_VehicleType::getVariable(const int variable, const std::string& id, tcpip::Storage& tempMsg) {
116  switch (variable) {
117  case VAR_LENGTH: {
120  }
121  break;
122  case VAR_HEIGHT: {
125  }
126  break;
127  case VAR_MINGAP: {
130  }
131  break;
132  case VAR_MAXSPEED: {
135  }
136  break;
137  case VAR_ACCEL: {
140  }
141  break;
142  case VAR_DECEL: {
145  }
146  break;
147  case VAR_IMPERFECTION: {
150  }
151  break;
152  case VAR_TAU: {
155  }
156  break;
157  case VAR_SPEED_FACTOR: {
160  }
161  break;
162  case VAR_SPEED_DEVIATION: {
165  }
166  break;
167  case VAR_VEHICLECLASS: {
170  }
171  break;
172  case VAR_EMISSIONCLASS: {
175  }
176  break;
177  case VAR_SHAPECLASS: {
180  }
181  break;
182  case VAR_WIDTH: {
185  }
186  break;
187  case VAR_COLOR: {
188  tempMsg.writeUnsignedByte(TYPE_COLOR);
189  const TraCIColor& col = TraCI_VehicleType::getColor(id);
190  tempMsg.writeUnsignedByte(col.r);
191  tempMsg.writeUnsignedByte(col.g);
192  tempMsg.writeUnsignedByte(col.b);
193  tempMsg.writeUnsignedByte(col.a);
194  }
195  break;
196  case VAR_MINGAP_LAT: {
199  }
200  break;
201  case VAR_MAXSPEED_LAT: {
204  }
205  break;
206  case VAR_LATALIGNMENT: {
209  }
210  break;
211  default:
212  break;
213  }
214  return true;
215 }
216 
217 bool
219  tcpip::Storage& outputStorage) {
220  std::string warning = ""; // additional description for response
221  // variable
222  int variable = inputStorage.readUnsignedByte();
223  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
224  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
225  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
226  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
227  && variable != VAR_TAU && variable != VAR_COLOR
228  && variable != VAR_HEIGHT
229  && variable != VAR_MINGAP_LAT
230  && variable != VAR_MAXSPEED_LAT
231  && variable != VAR_LATALIGNMENT
232  && variable != VAR_PARAMETER
233  ) {
235  "Change Vehicle Type State: unsupported variable " + toHex(variable, 2)
236  + " specified", outputStorage);
237  }
238  // id
239  std::string id = inputStorage.readString();
240 // MSVehicleType* v = TraCI_VehicleType::getVType(id);
241 // if (v == 0) {
242 // return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known",
243 // outputStorage);
244 // }
245  // process
246  try {
247  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, id, server, inputStorage, outputStorage)) {
248  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
249  return true;
250  }
251  } catch (ProcessError& e) {
252  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
253  } catch (TraCIException& e) {
254  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
255  }
256  return false;
257 }
258 
259 
260 bool
261 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
262  const std::string& id, TraCIServer& server,
263  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
264  switch (variable) {
265  case VAR_LENGTH: {
266  double value = 0;
267  if (!server.readTypeCheckingDouble(inputStorage, value)) {
268  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
269  }
270  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
271  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
272  }
273  TraCI_VehicleType::setLength(id, value);
274  }
275  break;
276  case VAR_HEIGHT: {
277  double value = 0;
278  if (!server.readTypeCheckingDouble(inputStorage, value)) {
279  return server.writeErrorStatusCmd(cmd, "Setting height requires a double.", outputStorage);
280  }
281  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
282  return server.writeErrorStatusCmd(cmd, "Invalid height.", outputStorage);
283  }
284  TraCI_VehicleType::setHeight(id, value);
285  }
286  break;
287  case VAR_MAXSPEED: {
288  double value = 0;
289  if (!server.readTypeCheckingDouble(inputStorage, value)) {
290  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
291  }
292  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
293  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
294  }
296  }
297  break;
298  case VAR_VEHICLECLASS: {
299  std::string vclass;
300  if (!server.readTypeCheckingString(inputStorage, vclass)) {
301  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
302  }
303  try {
305  } catch (InvalidArgument e) {
306  return server.writeErrorStatusCmd(cmd, "Unknown vehicle class '" + vclass + "'.", outputStorage);
307  }
308  }
309  break;
310  case VAR_SPEED_FACTOR: {
311  double value = 0;
312  if (!server.readTypeCheckingDouble(inputStorage, value)) {
313  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
314  }
315  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
316  return server.writeErrorStatusCmd(cmd, "Invalid speed factor.", outputStorage);
317  }
319  }
320  break;
321  case VAR_SPEED_DEVIATION: {
322  double value = 0;
323  if (!server.readTypeCheckingDouble(inputStorage, value)) {
324  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
325  }
326  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
327  return server.writeErrorStatusCmd(cmd, "Invalid speed deviation.", outputStorage);
328  }
330  }
331  break;
332  case VAR_EMISSIONCLASS: {
333  std::string eclass;
334  if (!server.readTypeCheckingString(inputStorage, eclass)) {
335  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
336  }
337  try {
339  } catch (InvalidArgument e) {
340  return server.writeErrorStatusCmd(cmd, "Unknown emission class '" + eclass + "'.", outputStorage);
341  }
342  }
343  break;
344  case VAR_WIDTH: {
345  double value = 0;
346  if (!server.readTypeCheckingDouble(inputStorage, value)) {
347  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
348  }
349  if (value <= 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
350  return server.writeErrorStatusCmd(cmd, "Invalid width.", outputStorage);
351  }
352  TraCI_VehicleType::setWidth(id, value);
353  }
354  break;
355  case VAR_MINGAP: {
356  double value = 0;
357  if (!server.readTypeCheckingDouble(inputStorage, value)) {
358  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
359  }
360  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
361  return server.writeErrorStatusCmd(cmd, "Invalid minimum gap.", outputStorage);
362  }
363  TraCI_VehicleType::setMinGap(id, value);
364  }
365  break;
366  case VAR_MINGAP_LAT: {
367  double value = 0;
368  if (!server.readTypeCheckingDouble(inputStorage, value)) {
369  return server.writeErrorStatusCmd(cmd, "Setting minimum lateral gap requires a double.", outputStorage);
370  }
371  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
372  return server.writeErrorStatusCmd(cmd, "Invalid minimum lateral gap.", outputStorage);
373  }
375  }
376  break;
377  case VAR_MAXSPEED_LAT: {
378  double value = 0;
379  if (!server.readTypeCheckingDouble(inputStorage, value)) {
380  return server.writeErrorStatusCmd(cmd, "Setting maximum lateral speed requires a double.", outputStorage);
381  }
382  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
383  return server.writeErrorStatusCmd(cmd, "Invalid maximum lateral speed.", outputStorage);
384  }
386  }
387  break;
388  case VAR_LATALIGNMENT: {
389  std::string latAlign;
390  if (!server.readTypeCheckingString(inputStorage, latAlign)) {
391  return server.writeErrorStatusCmd(cmd, "Setting preferred lateral alignment requires a string.",
392  outputStorage);
393  }
394  if (SUMOXMLDefinitions::LateralAlignments.hasString(latAlign)) {
396  } else {
397  return server.writeErrorStatusCmd(cmd, "Unknown lateral alignment " + latAlign + "'.", outputStorage);
398  }
399  }
400  break;
401  case VAR_SHAPECLASS: {
402  std::string sclass;
403  if (!server.readTypeCheckingString(inputStorage, sclass)) {
404  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
405  }
406  try {
408  } catch (InvalidArgument e) {
409  return server.writeErrorStatusCmd(cmd, "Unknown vehicle shape " + sclass + "'.", outputStorage);
410  }
411  }
412  break;
413  case VAR_ACCEL: {
414  double value = 0;
415  if (!server.readTypeCheckingDouble(inputStorage, value)) {
416  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
417  }
418  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
419  return server.writeErrorStatusCmd(cmd, "Invalid acceleration.", outputStorage);
420  }
421  TraCI_VehicleType::setAccel(id, value);
422  }
423  break;
424  case VAR_DECEL: {
425  double value = 0;
426  if (!server.readTypeCheckingDouble(inputStorage, value)) {
427  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
428  }
429  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
430  return server.writeErrorStatusCmd(cmd, "Invalid deceleration.", outputStorage);
431  }
432  TraCI_VehicleType::setDecel(id, value);
433  }
434  break;
435  case VAR_IMPERFECTION: {
436  double value = 0;
437  if (!server.readTypeCheckingDouble(inputStorage, value)) {
438  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
439  }
440  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
441  return server.writeErrorStatusCmd(cmd, "Invalid driver imperfection.", outputStorage);
442  }
444  }
445  break;
446  case VAR_TAU: {
447  double value = 0;
448  if (!server.readTypeCheckingDouble(inputStorage, value)) {
449  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
450  }
451  if (value < 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
452  return server.writeErrorStatusCmd(cmd, "Invalid headway time.", outputStorage);
453  }
454  TraCI_VehicleType::setTau(id, value);
455  }
456  break;
457  case VAR_COLOR: {
458  TraCIColor col;
459  if (!server.readTypeCheckingColor(inputStorage, col)) {
460  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
461  }
463  }
464  break;
465  case VAR_PARAMETER: {
466  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
467  return server.writeErrorStatusCmd(cmd, "A compound object is needed for setting a parameter.",
468  outputStorage);
469  }
470  //readt itemNo
471  inputStorage.readInt();
472  std::string name;
473  if (!server.readTypeCheckingString(inputStorage, name)) {
474  return server.writeErrorStatusCmd(cmd, "The name of the parameter must be given as a string.",
475  outputStorage);
476  }
477  std::string value;
478  if (!server.readTypeCheckingString(inputStorage, value)) {
479  return server.writeErrorStatusCmd(cmd, "The value of the parameter must be given as a string.",
480  outputStorage);
481  }
482  TraCI_VehicleType::addParameter(id, name, value);
483  }
484  break;
485  default:
486  break;
487  }
488  return true;
489 }
490 
491 #endif
492 
493 
494 /****************************************************************************/
static void setAccel(const std::string &typeID, double accel)
static void setSpeedDeviation(const std::string &typeID, double deviation)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, TraCIColor &into)
Reads the value type and a color, verifying the type.
#define VAR_EMISSIONCLASS
static void setShapeClass(const std::string &typeID, const std::string &shapeClass)
#define VAR_LENGTH
static std::string getParameter(const std::string &typeID, const std::string &key)
#define VAR_LATALIGNMENT
#define TYPE_COMPOUND
static double getSpeedFactor(const std::string &typeID)
static std::string getShapeClass(const std::string &typeID)
#define RESPONSE_GET_VEHICLETYPE_VARIABLE
static TraCIColor getColor(const std::string &typeID)
#define VAR_TAU
static void setVehicleClass(const std::string &typeID, const std::string &clazz)
#define RTYPE_OK
#define VAR_HEIGHT
static std::string getEmissionClass(const std::string &typeID)
unsigned char g
Definition: TraCIDefs.h:79
#define VAR_VEHICLECLASS
#define VAR_SPEED_FACTOR
#define VAR_COLOR
static std::vector< std::string > getIDList()
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
unsigned char r
Definition: TraCIDefs.h:79
virtual void writeUnsignedByte(int)
static double getMaxSpeedLat(const std::string &typeID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static void setMinGapLat(const std::string &typeID, double minGapLat)
unsigned char a
Definition: TraCIDefs.h:79
static bool getVariable(const int variable, const std::string &v, tcpip::Storage &tempMsg)
Processes a value request for the given type.
#define VAR_SPEED_DEVIATION
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
static void setEmissionClass(const std::string &typeID, const std::string &clazz)
#define CMD_GET_VEHICLETYPE_VARIABLE
static std::string getLateralAlignment(const std::string &typeID)
static double getMinGap(const std::string &typeID)
#define VAR_SHAPECLASS
static double getMinGapLat(const std::string &typeID)
static std::string getVehicleClass(const std::string &typeID)
#define VAR_ACCEL
virtual int readInt()
static void setMaxSpeedLat(const std::string &typeID, double speed)
static double getAccel(const std::string &typeID)
static void setHeight(const std::string &typeID, double height)
static void setImperfection(const std::string &typeID, double imperfection)
static void setSpeedFactor(const std::string &typeID, double factor)
#define CMD_SET_VEHICLETYPE_VARIABLE
static void setLength(const std::string &typeID, double length)
virtual void writeStringList(const std::vector< std::string > &s)
static double getTau(const std::string &typeID)
static void setMinGap(const std::string &typeID, double minGap)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
#define VAR_IMPERFECTION
virtual std::string readString()
static void setMaxSpeed(const std::string &typeID, double speed)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
static void setWidth(const std::string &typeID, double width)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static double getImperfection(const std::string &typeID)
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.
static double getDecel(const std::string &typeID)
virtual void writeString(const std::string &s)
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static void setTau(const std::string &typeID, double tau)
static double getHeight(const std::string &typeID)
static void setDecel(const std::string &typeID, double decel)
virtual void writeDouble(double)
static void setColor(const std::string &typeID, const TraCIColor &c)
unsigned char b
Definition: TraCIDefs.h:79
static void setLateralAlignment(const std::string &typeID, const std::string &latAlignment)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define VAR_MAXSPEED
#define VAR_MINGAP_LAT
#define VAR_DECEL
static double getLength(const std::string &typeID)
#define VAR_PARAMETER
#define ID_COUNT
#define TYPE_INTEGER
#define VAR_MINGAP
#define ID_LIST
static double getSpeedDeviation(const std::string &typeID)
static double getWidth(const std::string &typeID)
#define VAR_MAXSPEED_LAT
static void addParameter(const std::string &id, const std::string &name, const std::string &value)
#define VAR_WIDTH
static double getMaxSpeed(const std::string &typeID)