SUMO - Simulation of Urban MObility
TraCIServerAPI_Edge.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
23 // APIs for getting/setting edge values via TraCI
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 <utils/common/StdDefs.h>
39 #include <microsim/MSNet.h>
40 #include <microsim/MSEdgeControl.h>
41 #include <microsim/MSEdge.h>
42 #include <microsim/MSLane.h>
43 #include <microsim/MSVehicle.h>
45 #include "TraCIConstants.h"
46 #include "TraCIServerAPI_Edge.h"
49 #include <libsumo/Edge.h>
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 bool
57  tcpip::Storage& outputStorage) {
58  // variable & id
59  int variable = inputStorage.readUnsignedByte();
60  std::string id = inputStorage.readString();
61  // check variable
62  if (variable != ID_LIST && variable != VAR_EDGE_TRAVELTIME && variable != VAR_EDGE_EFFORT
63  && variable != VAR_CURRENT_TRAVELTIME
64  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION
65  && variable != VAR_PMXEMISSION
66  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION
67  && variable != VAR_ELECTRICITYCONSUMPTION && variable != VAR_WAITING_TIME
68  && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
69  && variable != LAST_STEP_OCCUPANCY
70  && variable != LAST_STEP_VEHICLE_HALTING_NUMBER && variable != LAST_STEP_LENGTH
71  && variable != LAST_STEP_PERSON_ID_LIST
72  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != ID_COUNT && variable != VAR_PARAMETER) {
74  "Get Edge Variable: unsupported variable " + toHex(variable, 2)
75  + " specified", outputStorage);
76  }
77  // begin response building
78  tcpip::Storage tempMsg;
79  // response-code, variableID, objectID
81  tempMsg.writeUnsignedByte(variable);
82  tempMsg.writeString(id);
83  // process request
84 
85  try {
86  if (variable == ID_LIST) {
89  } else if (variable == ID_COUNT) {
90  ;
93  } else {
94  switch (variable) {
95  case VAR_EDGE_TRAVELTIME: {
96  // time
97  int time = 0;
98  if (!server.readTypeCheckingInt(inputStorage, time)) {
100  "The message must contain the time definition.", outputStorage);
101  }
104  }
105  break;
106  case VAR_EDGE_EFFORT: {
107  // time
108  int time = 0;
109  if (!server.readTypeCheckingInt(inputStorage, time)) {
111  "The message must contain the time definition.", outputStorage);
112  }
115  }
116  break;
120  break;
121  case VAR_WAITING_TIME: {
124  }
125  break;
129  }
130  break;
134  }
135  break;
136  case VAR_CO2EMISSION: {
139  }
140  break;
141  case VAR_COEMISSION: {
144  }
145  break;
146  case VAR_HCEMISSION: {
149  }
150  break;
151  case VAR_PMXEMISSION: {
154  }
155  break;
156  case VAR_NOXEMISSION: {
159  }
160  break;
161  case VAR_FUELCONSUMPTION: {
164  }
165  break;
166  case VAR_NOISEEMISSION: {
169  }
170  break;
174  }
175  break;
179  }
180  break;
181  case LAST_STEP_MEAN_SPEED: {
184  }
185  break;
186  case LAST_STEP_OCCUPANCY: {
189 
190  }
191  break;
195  }
196  break;
197  case LAST_STEP_LENGTH: {
200  }
201  break;
202  case VAR_PARAMETER: {
203  std::string paramName;
204  if (!server.readTypeCheckingString(inputStorage, paramName)) {
206  "Retrieval of a parameter requires its name.",
207  outputStorage);
208  }
210  tempMsg.writeString(libsumo::Edge::getParameter(id, paramName));
211  }
212  break;
213  default:
214  break;
215  }
216  }
217  } catch (libsumo::TraCIException& e) {
218  return server.writeErrorStatusCmd(CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
219  }
220  server.writeStatusCmd(CMD_GET_EDGE_VARIABLE, RTYPE_OK, "", outputStorage);
221  server.writeResponseWithLength(outputStorage, tempMsg);
222  return true;
223 }
224 
225 
226 bool
228  tcpip::Storage& outputStorage) {
229  std::string warning; // additional description for response
230  // variable
231  int variable = inputStorage.readUnsignedByte();
232  if (variable != VAR_EDGE_TRAVELTIME && variable != VAR_EDGE_EFFORT && variable != VAR_MAXSPEED
233  && variable != VAR_PARAMETER) {
235  "Change Edge State: unsupported variable " + toHex(variable, 2)
236  + " specified", outputStorage);
237  }
238  // id
239  std::string id = inputStorage.readString();
240  try {
241  // process
242  switch (variable) {
243 
244  case LANE_ALLOWED: {
245  // read and set allowed vehicle classes
246  std::vector<std::string> classes;
247  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
249  "Allowed vehicle classes must be given as a list of strings.",
250  outputStorage);
251  }
253  }
254  break;
255  case LANE_DISALLOWED: {
256  // read and set disallowed vehicle classes
257  std::vector<std::string> classes;
258  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
260  "Not allowed vehicle classes must be given as a list of strings.",
261  outputStorage);
262  }
264  }
265  break;
266  case VAR_EDGE_TRAVELTIME: {
267  // read and set travel time
268  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
270  "Setting travel time requires a compound object.", outputStorage);
271  }
272  int parameterCount = inputStorage.readInt();
273  if (parameterCount == 3) {
274  // bound by time
275  int begTime = 0, endTime = 0;
276  double value = 0;
277  if (!server.readTypeCheckingInt(inputStorage, begTime)) {
279  "The first variable must be the begin time given as int.",
280  outputStorage);
281  }
282  if (!server.readTypeCheckingInt(inputStorage, endTime)) {
284  "The second variable must be the end time given as int.",
285  outputStorage);
286  }
287  if (!server.readTypeCheckingDouble(inputStorage, value)) {
289  "The third variable must be the value given as double",
290  outputStorage);
291  }
292  libsumo::Edge::addTravelTime(id, begTime, endTime, value);
293  } else if (parameterCount == 1) {
294  // unbound
295  double value = 0;
296  if (!server.readTypeCheckingDouble(inputStorage, value)) {
298  "The variable must be the value given as double", outputStorage);
299  }
300  libsumo::Edge::addTravelTime(id, 0, double(SUMOTime_MAX), value);
301  } else {
303  "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
304  outputStorage);
305  }
306  }
307  break;
308  case VAR_EDGE_EFFORT: {
309  // read and set effort
310  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
312  "Setting effort requires a compound object.",
313  outputStorage);
314  }
315  int parameterCount = inputStorage.readInt();
316  if (parameterCount == 3) {
317  // bound by time
318  int begTime = 0, endTime = 0;
319  double value = 0;
320  if (!server.readTypeCheckingInt(inputStorage, begTime)) {
322  "The first variable must be the begin time given as int.",
323  outputStorage);
324  }
325  if (!server.readTypeCheckingInt(inputStorage, endTime)) {
327  "The second variable must be the end time given as int.",
328  outputStorage);
329  }
330  if (!server.readTypeCheckingDouble(inputStorage, value)) {
332  "The third variable must be the value given as double",
333  outputStorage);
334  }
335  libsumo::Edge::addEffort(id, begTime, endTime, value);
336  } else if (parameterCount == 1) {
337  // unbound
338  double value = 0;
339  if (!server.readTypeCheckingDouble(inputStorage, value)) {
341  "The variable must be the value given as double", outputStorage);
342  }
343  libsumo::Edge::addEffort(id, 0., double(SUMOTime_MAX), value);
344  } else {
346  "Setting effort requires either begin time, end time, and value, or only value as parameter.",
347  outputStorage);
348  }
349  }
350  break;
351  case VAR_MAXSPEED: {
352  // read and set max. speed
353  double value = 0;
354  if (!server.readTypeCheckingDouble(inputStorage, value)) {
355  return server.writeErrorStatusCmd(CMD_SET_EDGE_VARIABLE, "The speed must be given as a double.",
356  outputStorage);
357  }
358  libsumo::Edge::setMaxSpeed(id, value);
359  }
360  break;
361  case VAR_PARAMETER: {
362  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
364  "A compound object is needed for setting a parameter.",
365  outputStorage);
366  }
367  //readt itemNo
368  inputStorage.readInt();
369  std::string name;
370  if (!server.readTypeCheckingString(inputStorage, name)) {
372  "The name of the parameter must be given as a string.",
373  outputStorage);
374  }
375  std::string value;
376  if (!server.readTypeCheckingString(inputStorage, value)) {
378  "The value of the parameter must be given as a string.",
379  outputStorage);
380  }
381  libsumo::Edge::setParameter(id, name, value);
382 
383  }
384  break;
385  default:
386  break;
387  }
388  } catch (libsumo::TraCIException& e) {
389  return server.writeErrorStatusCmd(CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
390  }
391  server.writeStatusCmd(CMD_SET_EDGE_VARIABLE, RTYPE_OK, warning, outputStorage);
392  return true;
393 }
394 
395 bool
396 TraCIServerAPI_Edge::getShape(const std::string& id, PositionVector& shape) {
397  try {
398  libsumo::Edge::getShape(id, shape);
399  } catch (libsumo::TraCIException& e) {
400  WRITE_WARNING(e.what());
401  return false;
402  }
403  return true;
404 
405 }
406 
407 #endif
408 
409 
410 /****************************************************************************/
411 
#define LAST_STEP_MEAN_SPEED
static void setAllowedVehicleClasses(const std::string &id, std::vector< std::string > vector)
Definition: Edge.cpp:262
static std::vector< std::string > getIDList()
Definition: Edge.cpp:33
#define VAR_CO2EMISSION
#define TYPE_COMPOUND
#define VAR_CURRENT_TRAVELTIME
static double retrieveExistingEffort(const std::string &id, double time)
Definition: Edge.cpp:54
static double getOccupancy(const std::string &id)
Definition: Edge.cpp:212
static double getVehicleAverageLength(const std::string &id)
Definition: Edge.cpp:238
static void addTravelTime(const std::string &id, double begTime, double endTime, double value)
Definition: Edge.cpp:284
#define RTYPE_OK
static double getFuelConsumption(const std::string &id)
Definition: Edge.cpp:164
#define VAR_WAITING_TIME
static void addEffort(const std::string &id, double begTime, double endTime, double value)
Definition: Edge.cpp:289
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
static double getWaitingSeconds(const std::string &id)
Definition: Edge.cpp:78
static double getCurrentTravelTime(const std::string &id)
Definition: Edge.cpp:64
static double retrieveExistingTravelTime(const std::string &id, double time)
Definition: Edge.cpp:44
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
static double getNOxEmissions(const std::string &id)
Definition: Edge.cpp:154
virtual void writeUnsignedByte(int)
#define CMD_SET_EDGE_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define VAR_NOISEEMISSION
#define VAR_FUELCONSUMPTION
virtual void writeInt(int)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
#define VAR_NOXEMISSION
static int getIDCount()
Definition: Edge.cpp:40
static const std::vector< std::string > getPersonIDs(const std::string &id)
Definition: Edge.cpp:88
static int getVehicleNumber(const std::string &id)
Definition: Edge.cpp:197
#define LANE_ALLOWED
virtual int readInt()
#define SUMOTime_MAX
Definition: TraCIDefs.h:52
A list of positions.
static double getNoiseEmissions(const std::string &id)
Definition: Edge.cpp:174
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
static double getCOEmissions(const std::string &id)
Definition: Edge.cpp:124
virtual void writeStringList(const std::vector< std::string > &s)
#define VAR_PMXEMISSION
static double getPMxEmissions(const std::string &id)
Definition: Edge.cpp:144
static void setMaxSpeed(const std::string &id, double value)
Definition: Edge.cpp:294
#define CMD_SET_VEHICLE_VARIABLE
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
static double getElectricityConsumption(const std::string &id)
Definition: Edge.cpp:187
#define VAR_EDGE_EFFORT
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
Definition: Edge.cpp:302
static std::string getParameter(const std::string &id, const std::string &paramName)
Definition: Edge.cpp:257
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static void getShape(const std::string &id, PositionVector &shape)
Definition: Edge.cpp:307
#define LAST_STEP_VEHICLE_NUMBER
static double getHCEmissions(const std::string &id)
Definition: Edge.cpp:134
#define VAR_EDGE_TRAVELTIME
#define VAR_COEMISSION
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named edge&#39;s shape.
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define LANE_DISALLOWED
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
#define VAR_ELECTRICITYCONSUMPTION
static int getVehicleHaltingNumber(const std::string &id)
Definition: Edge.cpp:222
virtual void writeDouble(double)
#define LAST_STEP_PERSON_ID_LIST
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static double getCO2Emissions(const std::string &id)
Definition: Edge.cpp:114
#define LAST_STEP_OCCUPANCY
#define VAR_MAXSPEED
#define VAR_PARAMETER
#define ID_COUNT
static void setDisallowedVehicleClasses(const std::string &id, std::vector< std::string > classes)
Definition: Edge.cpp:268
static double getMeanSpeed(const std::string &id)
Definition: Edge.cpp:207
#define TYPE_INTEGER
#define ID_LIST
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
static const std::vector< std::string > getVehicleIDs(const std::string &id)
Definition: Edge.cpp:99
#define LAST_STEP_VEHICLE_HALTING_NUMBER
#define VAR_HCEMISSION
#define RESPONSE_GET_EDGE_VARIABLE