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  // variable & id
60  int variable = inputStorage.readUnsignedByte();
61  std::string id = inputStorage.readString();
62  // check variable
63  if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
64  && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
65  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
66  && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
67  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
68  return false;
69  }
70  // begin response building
71  tcpip::Storage tempMsg;
72  // response-code, variableID, objectID
74  tempMsg.writeUnsignedByte(variable);
75  tempMsg.writeString(id);
76  // process request
77  if (variable == ID_LIST) {
78  std::vector<std::string> ids;
81  tempMsg.writeStringList(ids);
82  } else if (variable == ID_COUNT) {
83  std::vector<std::string> ids;
86  tempMsg.writeInt((int) ids.size());
87  } else {
89  if (v == 0) {
90  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
91  return false;
92  }
93  getVariable(variable, *v, tempMsg);
94  }
95  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
96  server.writeResponseWithLength(outputStorage, tempMsg);
97  return true;
98 }
99 
100 bool
102  switch (variable) {
103  case VAR_LENGTH:
105  tempMsg.writeDouble(v.getLength());
106  break;
107  case VAR_MINGAP:
109  tempMsg.writeDouble(v.getMinGap());
110  break;
111  case VAR_MAXSPEED:
113  tempMsg.writeDouble(v.getMaxSpeed());
114  break;
115  case VAR_ACCEL:
118  break;
119  case VAR_DECEL:
122  break;
123  case VAR_IMPERFECTION:
126  break;
127  case VAR_TAU:
130  break;
131  case VAR_SPEED_FACTOR:
133  tempMsg.writeDouble(v.getSpeedFactor());
134  break;
135  case VAR_SPEED_DEVIATION:
137  tempMsg.writeDouble(v.getSpeedDeviation());
138  break;
139  case VAR_VEHICLECLASS:
141  tempMsg.writeString(toString(v.getVehicleClass()));
142  break;
143  case VAR_EMISSIONCLASS:
146  break;
147  case VAR_SHAPECLASS:
150  break;
151  case VAR_WIDTH:
153  tempMsg.writeDouble(v.getWidth());
154  break;
155  case VAR_COLOR:
156  tempMsg.writeUnsignedByte(TYPE_COLOR);
157  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().red() * 255. + 0.5));
158  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().green() * 255. + 0.5));
159  tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().blue() * 255. + 0.5));
160  tempMsg.writeUnsignedByte(255);
161  break;
162  default:
163  break;
164  }
165  return true;
166 }
167 
168 bool
170  tcpip::Storage& outputStorage) {
171  std::string warning = ""; // additional description for response
172  // variable
173  int variable = inputStorage.readUnsignedByte();
174  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
175  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
176  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
177  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
178  && variable != VAR_TAU && variable != VAR_COLOR
179  ) {
180  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Change Vehicle Type State: unsupported variable specified", outputStorage);
181  return false;
182  }
183  // id
184  std::string id = inputStorage.readString();
186  if (v == 0) {
187  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
188  return false;
189  }
190  // process
191  try {
192  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, inputStorage.readUnsignedByte(),
193  *v, server, inputStorage, outputStorage)) {
194  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
195  return true;
196  }
197  } catch (ProcessError& e) {
198  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, e.what(), outputStorage);
199  }
200  return false;
201 }
202 
203 
204 bool
205 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable, const int valueDataType,
206  MSVehicleType& v, traci::TraCIServer& server,
207  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
208  switch (variable) {
209  case VAR_LENGTH: {
210  if (valueDataType != TYPE_DOUBLE) {
211  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting length requires a double.", outputStorage);
212  return false;
213  }
214  double val = inputStorage.readDouble();
215  if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
216  server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid length.", outputStorage);
217  return false;
218  }
219  v.setLength(val);
220  }
221  break;
222  case VAR_MAXSPEED: {
223  if (valueDataType != TYPE_DOUBLE) {
224  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting maximum speed requires a double.", outputStorage);
225  return false;
226  }
227  double val = inputStorage.readDouble();
228  if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
229  server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid maximum speed.", outputStorage);
230  return false;
231  }
232  v.setMaxSpeed(val);
233  }
234  break;
235  case VAR_VEHICLECLASS: {
236  if (valueDataType != TYPE_STRING) {
237  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle class requires a string.", outputStorage);
238  return false;
239  }
240  v.setVClass(getVehicleClassID(inputStorage.readString()));
241  }
242  break;
243  case VAR_SPEED_FACTOR: {
244  if (valueDataType != TYPE_DOUBLE) {
245  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed factor requires a double.", outputStorage);
246  return false;
247  }
248  v.setSpeedFactor(inputStorage.readDouble());
249  }
250  break;
251  case VAR_SPEED_DEVIATION: {
252  if (valueDataType != TYPE_DOUBLE) {
253  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed deviation requires a double.", outputStorage);
254  return false;
255  }
256  v.setSpeedDeviation(inputStorage.readDouble());
257  }
258  break;
259  case VAR_EMISSIONCLASS: {
260  if (valueDataType != TYPE_STRING) {
261  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting emission class requires a string.", outputStorage);
262  return false;
263  }
265  }
266  break;
267  case VAR_WIDTH: {
268  if (valueDataType != TYPE_DOUBLE) {
269  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting width requires a double.", outputStorage);
270  return false;
271  }
272  v.setWidth(inputStorage.readDouble());
273  }
274  break;
275  case VAR_MINGAP: {
276  if (valueDataType != TYPE_DOUBLE) {
277  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting minimum gap requires a double.", outputStorage);
278  return false;
279  }
280  v.setMinGap(inputStorage.readDouble());
281  }
282  break;
283  case VAR_SHAPECLASS: {
284  if (valueDataType != TYPE_STRING) {
285  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle shape requires a string.", outputStorage);
286  return false;
287  }
288  v.setShape(getVehicleShapeID(inputStorage.readString()));
289  }
290  break;
291  case VAR_ACCEL: {
292  if (valueDataType != TYPE_DOUBLE) {
293  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting acceleration requires a double.", outputStorage);
294  return false;
295  }
296  v.getCarFollowModel().setMaxAccel(inputStorage.readDouble());
297  }
298  break;
299  case VAR_DECEL: {
300  if (valueDataType != TYPE_DOUBLE) {
301  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting deceleration requires a double.", outputStorage);
302  return false;
303  }
304  v.getCarFollowModel().setMaxDecel(inputStorage.readDouble());
305  }
306  break;
307  case VAR_IMPERFECTION: {
308  if (valueDataType != TYPE_DOUBLE) {
309  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting driver imperfection requires a double.", outputStorage);
310  return false;
311  }
312  v.getCarFollowModel().setImperfection(inputStorage.readDouble());
313  }
314  break;
315  case VAR_TAU: {
316  if (valueDataType != TYPE_DOUBLE) {
317  server.writeStatusCmd(cmd, RTYPE_ERR, "Setting headway time requires a double.", outputStorage);
318  return false;
319  }
320  v.getCarFollowModel().setHeadwayTime(inputStorage.readDouble());
321  }
322  break;
323  case VAR_COLOR: {
324  if (valueDataType != TYPE_COLOR) {
325  server.writeStatusCmd(cmd, RTYPE_ERR, "The color must be given using the according type.", outputStorage);
326  return false;
327  }
328  SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
329  SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
330  SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
331  inputStorage.readUnsignedByte(); // skip alpha level
332  RGBColor col(r, g, b);
333  v.setColor(col);
334  }
335  break;
336  default:
337  break;
338  }
339  return true;
340 }
341 
342 #endif
343 
344 
345 /****************************************************************************/