SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting vehicle type values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
13 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifndef NO_TRACI
35 
36 #include <limits>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSVehicleType.h>
39 #include "TraCIConstants.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // used namespaces
49 // ===========================================================================
50 using namespace traci;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 bool
58  tcpip::Storage& outputStorage) {
59  std::string warning = ""; // additional description for response
60  // variable & id
61  int variable = inputStorage.readUnsignedByte();
62  std::string id = inputStorage.readString();
63  // check variable
64  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
65  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
66  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
67  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
68  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
69  return false;
70  }
71  // begin response building
72  tcpip::Storage tempMsg;
73  // response-code, variableID, objectID
75  tempMsg.writeUnsignedByte(variable);
76  tempMsg.writeString(id);
77  // process request
78  if (variable == ID_LIST) {
79  std::vector<std::string> ids;
82  tempMsg.writeStringList(ids);
83  } else if (variable == ID_COUNT) {
84  std::vector<std::string> ids;
87  tempMsg.writeInt((int) ids.size());
88  } else {
90  if (v == 0) {
91  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
92  return false;
93  }
94  getVariable(variable, *v, tempMsg);
95  }
96  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
97  server.writeResponseWithLength(outputStorage, tempMsg);
98  return true;
99 }
100 
101 bool
103  switch (variable) {
104  case VAR_LENGTH:
106  tempMsg.writeDouble(v.getLength());
107  break;
108  case VAR_MINGAP:
110  tempMsg.writeDouble(v.getMinGap());
111  break;
112  case VAR_MAXSPEED:
114  tempMsg.writeDouble(v.getMaxSpeed());
115  break;
116  case VAR_ACCEL:
119  break;
120  case VAR_DECEL:
123  break;
124  case VAR_IMPERFECTION:
127  break;
128  case VAR_TAU:
131  break;
132  case VAR_SPEED_FACTOR:
134  tempMsg.writeDouble(v.getSpeedFactor());
135  break;
136  case VAR_SPEED_DEVIATION:
138  tempMsg.writeDouble(v.getSpeedDeviation());
139  break;
140  case VAR_VEHICLECLASS:
142  tempMsg.writeString(toString(v.getVehicleClass()));
143  break;
144  case VAR_EMISSIONCLASS:
147  break;
148  case VAR_SHAPECLASS:
151  break;
152  case VAR_WIDTH:
154  tempMsg.writeDouble(v.getWidth());
155  break;
156  case VAR_COLOR:
157  tempMsg.writeUnsignedByte(TYPE_COLOR);
158  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().red() * 255. + 0.5));
159  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().green() * 255. + 0.5));
160  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().blue() * 255. + 0.5));
161  tempMsg.writeUnsignedByte(255);
162  break;
163  default:
164  break;
165  }
166  return true;
167 }
168 
169 bool
171  tcpip::Storage& outputStorage) {
172  std::string warning = ""; // additional description for response
173  // variable
174  int variable = inputStorage.readUnsignedByte();
175  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
176  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
177  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
178  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
179  && variable != VAR_TAU && variable != VAR_COLOR
180  ) {
181  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Change Vehicle Type State: unsupported variable specified", outputStorage);
182  return false;
183  }
184  // id
185  std::string id = inputStorage.readString();
187  if (v == 0) {
188  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
189  return false;
190  }
191  // process
192  try {
193  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, inputStorage.readUnsignedByte(),
194  *v, server, inputStorage, outputStorage)) {
195  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
196  return true;
197  }
198  } catch (ProcessError& e) {
199  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, e.what(), outputStorage);
200  }
201  return false;
202 }
203 
204 
205 bool
206 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable, const int valueDataType,
207  MSVehicleType& v, traci::TraCIServer& server,
208  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
209  switch (variable) {
210  case VAR_LENGTH: {
211  if (valueDataType != TYPE_DOUBLE) {
212  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting length requires a double.", outputStorage);
213  return false;
214  }
215  double val = inputStorage.readDouble();
216  if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
217  server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid length.", outputStorage);
218  return false;
219  }
220  v.setLength(val);
221  }
222  break;
223  case VAR_MAXSPEED: {
224  if (valueDataType != TYPE_DOUBLE) {
225  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting maximum speed requires a double.", outputStorage);
226  return false;
227  }
228  double val = inputStorage.readDouble();
229  if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
230  server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid maximum speed.", outputStorage);
231  return false;
232  }
233  v.setMaxSpeed(val);
234  }
235  break;
236  case VAR_VEHICLECLASS: {
237  if (valueDataType != TYPE_STRING) {
238  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle class requires a string.", outputStorage);
239  return false;
240  }
241  v.setVClass(getVehicleClassID(inputStorage.readString()));
242  }
243  break;
244  case VAR_SPEED_FACTOR: {
245  if (valueDataType != TYPE_DOUBLE) {
246  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed factor requires a double.", outputStorage);
247  return false;
248  }
249  v.setSpeedFactor(inputStorage.readDouble());
250  }
251  break;
252  case VAR_SPEED_DEVIATION: {
253  if (valueDataType != TYPE_DOUBLE) {
254  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed deviation requires a double.", outputStorage);
255  return false;
256  }
257  v.setSpeedDeviation(inputStorage.readDouble());
258  }
259  break;
260  case VAR_EMISSIONCLASS: {
261  if (valueDataType != TYPE_STRING) {
262  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting emission class requires a string.", outputStorage);
263  return false;
264  }
266  }
267  break;
268  case VAR_WIDTH: {
269  if (valueDataType != TYPE_DOUBLE) {
270  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting width requires a double.", outputStorage);
271  return false;
272  }
273  v.setWidth(inputStorage.readDouble());
274  }
275  break;
276  case VAR_MINGAP: {
277  if (valueDataType != TYPE_DOUBLE) {
278  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting minimum gap requires a double.", outputStorage);
279  return false;
280  }
281  v.setMinGap(inputStorage.readDouble());
282  }
283  break;
284  case VAR_SHAPECLASS: {
285  if (valueDataType != TYPE_STRING) {
286  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle shape requires a string.", outputStorage);
287  return false;
288  }
289  v.setShape(getVehicleShapeID(inputStorage.readString()));
290  }
291  break;
292  case VAR_ACCEL: {
293  if (valueDataType != TYPE_DOUBLE) {
294  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting acceleration requires a double.", outputStorage);
295  return false;
296  }
297  v.getCarFollowModel().setMaxAccel(inputStorage.readDouble());
298  }
299  break;
300  case VAR_DECEL: {
301  if (valueDataType != TYPE_DOUBLE) {
302  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting deceleration requires a double.", outputStorage);
303  return false;
304  }
305  v.getCarFollowModel().setMaxDecel(inputStorage.readDouble());
306  }
307  break;
308  case VAR_IMPERFECTION: {
309  if (valueDataType != TYPE_DOUBLE) {
310  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting driver imperfection requires a double.", outputStorage);
311  return false;
312  }
313  v.getCarFollowModel().setImperfection(inputStorage.readDouble());
314  }
315  break;
316  case VAR_TAU: {
317  if (valueDataType != TYPE_DOUBLE) {
318  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting headway time requires a double.", outputStorage);
319  return false;
320  }
321  v.getCarFollowModel().setHeadwayTime(inputStorage.readDouble());
322  }
323  break;
324  case VAR_COLOR: {
325  if (valueDataType != TYPE_COLOR) {
326  server.writeStatusCmd(cmd, RTYPE_ERR, "The color must be given using the according type.", outputStorage);
327  return false;
328  }
329  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
330  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
331  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
332  inputStorage.readUnsignedByte(); // skip alpha level
333  RGBColor col(r, g, b);
334  v.setColor(col);
335  }
336  break;
337  default:
338  break;
339  }
340  return true;
341 }
342 
343 #endif
344 
345 
346 /****************************************************************************/