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-2013 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  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
68  }
69  // begin response building
70  tcpip::Storage tempMsg;
71  // response-code, variableID, objectID
73  tempMsg.writeUnsignedByte(variable);
74  tempMsg.writeString(id);
75  // process request
76  if (variable == ID_LIST) {
77  std::vector<std::string> ids;
80  tempMsg.writeStringList(ids);
81  } else if (variable == ID_COUNT) {
82  std::vector<std::string> ids;
85  tempMsg.writeInt((int) ids.size());
86  } else {
88  if (v == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
90  }
91  getVariable(variable, *v, tempMsg);
92  }
93  server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, "", outputStorage);
94  server.writeResponseWithLength(outputStorage, tempMsg);
95  return true;
96 }
97 
98 bool
99 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
100  switch (variable) {
101  case VAR_LENGTH:
103  tempMsg.writeDouble(v.getLength());
104  break;
105  case VAR_MINGAP:
107  tempMsg.writeDouble(v.getMinGap());
108  break;
109  case VAR_MAXSPEED:
111  tempMsg.writeDouble(v.getMaxSpeed());
112  break;
113  case VAR_ACCEL:
116  break;
117  case VAR_DECEL:
120  break;
121  case VAR_IMPERFECTION:
124  break;
125  case VAR_TAU:
128  break;
129  case VAR_SPEED_FACTOR:
131  tempMsg.writeDouble(v.getSpeedFactor());
132  break;
133  case VAR_SPEED_DEVIATION:
135  tempMsg.writeDouble(v.getSpeedDeviation());
136  break;
137  case VAR_VEHICLECLASS:
139  tempMsg.writeString(toString(v.getVehicleClass()));
140  break;
141  case VAR_EMISSIONCLASS:
144  break;
145  case VAR_SHAPECLASS:
148  break;
149  case VAR_WIDTH:
151  tempMsg.writeDouble(v.getWidth());
152  break;
153  case VAR_COLOR:
154  tempMsg.writeUnsignedByte(TYPE_COLOR);
155  tempMsg.writeUnsignedByte(v.getColor().red());
156  tempMsg.writeUnsignedByte(v.getColor().green());
157  tempMsg.writeUnsignedByte(v.getColor().blue());
158  tempMsg.writeUnsignedByte(v.getColor().alpha());
159  break;
160  default:
161  break;
162  }
163  return true;
164 }
165 
166 bool
168  tcpip::Storage& outputStorage) {
169  std::string warning = ""; // additional description for response
170  // variable
171  int variable = inputStorage.readUnsignedByte();
172  if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
173  && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
174  && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
175  && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
176  && variable != VAR_TAU && variable != VAR_COLOR
177  ) {
178  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Change Vehicle Type State: unsupported variable specified", outputStorage);
179  }
180  // id
181  std::string id = inputStorage.readString();
183  if (v == 0) {
184  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, "Vehicle type '" + id + "' is not known", outputStorage);
185  }
186  // process
187  try {
188  if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, *v, server, inputStorage, outputStorage)) {
189  server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
190  return true;
191  }
192  } catch (ProcessError& e) {
193  return server.writeErrorStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, e.what(), outputStorage);
194  }
195  return false;
196 }
197 
198 
199 bool
200 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable,
201  MSVehicleType& v, traci::TraCIServer& server,
202  tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
203  switch (variable) {
204  case VAR_LENGTH: {
205  double value = 0;
206  if (!server.readTypeCheckingDouble(inputStorage, value)) {
207  return server.writeErrorStatusCmd(cmd, "Setting length requires a double.", outputStorage);
208  }
209  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
210  return server.writeErrorStatusCmd(cmd, "Invalid length.", outputStorage);
211  }
212  v.setLength(value);
213  }
214  break;
215  case VAR_MAXSPEED: {
216  double value = 0;
217  if (!server.readTypeCheckingDouble(inputStorage, value)) {
218  return server.writeErrorStatusCmd(cmd, "Setting maximum speed requires a double.", outputStorage);
219  }
220  if (value == 0.0 || fabs(value) == std::numeric_limits<double>::infinity()) {
221  return server.writeErrorStatusCmd(cmd, "Invalid maximum speed.", outputStorage);
222  }
223  v.setMaxSpeed(value);
224  }
225  break;
226  case VAR_VEHICLECLASS: {
227  std::string vclass;
228  if (!server.readTypeCheckingString(inputStorage, vclass)) {
229  return server.writeErrorStatusCmd(cmd, "Setting vehicle class requires a string.", outputStorage);
230  }
231  v.setVClass(getVehicleClassID(vclass));
232  }
233  break;
234  case VAR_SPEED_FACTOR: {
235  double value = 0;
236  if (!server.readTypeCheckingDouble(inputStorage, value)) {
237  return server.writeErrorStatusCmd(cmd, "Setting speed factor requires a double.", outputStorage);
238  }
239  v.setSpeedFactor(value);
240  }
241  break;
242  case VAR_SPEED_DEVIATION: {
243  double value = 0;
244  if (!server.readTypeCheckingDouble(inputStorage, value)) {
245  return server.writeErrorStatusCmd(cmd, "Setting speed deviation requires a double.", outputStorage);
246  }
247  v.setSpeedDeviation(value);
248  }
249  break;
250  case VAR_EMISSIONCLASS: {
251  std::string eclass;
252  if (!server.readTypeCheckingString(inputStorage, eclass)) {
253  return server.writeErrorStatusCmd(cmd, "Setting emission class requires a string.", outputStorage);
254  }
256  }
257  break;
258  case VAR_WIDTH: {
259  double value = 0;
260  if (!server.readTypeCheckingDouble(inputStorage, value)) {
261  return server.writeErrorStatusCmd(cmd, "Setting width requires a double.", outputStorage);
262  }
263  v.setWidth(value);
264  }
265  break;
266  case VAR_MINGAP: {
267  double value = 0;
268  if (!server.readTypeCheckingDouble(inputStorage, value)) {
269  return server.writeErrorStatusCmd(cmd, "Setting minimum gap requires a double.", outputStorage);
270  }
271  v.setMinGap(value);
272  }
273  break;
274  case VAR_SHAPECLASS: {
275  std::string sclass;
276  if (!server.readTypeCheckingString(inputStorage, sclass)) {
277  return server.writeErrorStatusCmd(cmd, "Setting vehicle shape requires a string.", outputStorage);
278  }
279  v.setShape(getVehicleShapeID(sclass));
280  }
281  break;
282  case VAR_ACCEL: {
283  double value = 0;
284  if (!server.readTypeCheckingDouble(inputStorage, value)) {
285  return server.writeErrorStatusCmd(cmd, "Setting acceleration requires a double.", outputStorage);
286  }
287  v.getCarFollowModel().setMaxAccel(value);
288  }
289  break;
290  case VAR_DECEL: {
291  double value = 0;
292  if (!server.readTypeCheckingDouble(inputStorage, value)) {
293  return server.writeErrorStatusCmd(cmd, "Setting deceleration requires a double.", outputStorage);
294  }
295  v.getCarFollowModel().setMaxDecel(value);
296  }
297  break;
298  case VAR_IMPERFECTION: {
299  double value = 0;
300  if (!server.readTypeCheckingDouble(inputStorage, value)) {
301  return server.writeErrorStatusCmd(cmd, "Setting driver imperfection requires a double.", outputStorage);
302  }
304  }
305  break;
306  case VAR_TAU: {
307  double value = 0;
308  if (!server.readTypeCheckingDouble(inputStorage, value)) {
309  return server.writeErrorStatusCmd(cmd, "Setting headway time requires a double.", outputStorage);
310  }
312  }
313  break;
314  case VAR_COLOR: {
315  RGBColor col;
316  if (!server.readTypeCheckingColor(inputStorage, col)) {
317  return server.writeErrorStatusCmd(cmd, "The color must be given using the according type.", outputStorage);
318  }
319  v.setColor(col);
320  }
321  break;
322  default:
323  break;
324  }
325  return true;
326 }
327 
328 #endif
329 
330 
331 /****************************************************************************/