SUMO - Simulation of Urban MObility
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2009-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 /****************************************************************************/
22 // APIs for getting/setting lane values via TraCI
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifndef NO_TRACI
36 
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSEdgeControl.h>
39 #include <microsim/MSLane.h>
40 #include <microsim/MSNet.h>
41 #include <microsim/MSVehicle.h>
43 #include "TraCIConstants.h"
44 #include "TraCIServer.h"
45 #include "TraCIServerAPI_Lane.h"
46 #include <libsumo/Lane.h>
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 bool
54  tcpip::Storage& outputStorage) {
55  // variable
56  int variable = inputStorage.readUnsignedByte();
57  std::string id = inputStorage.readString();
58  // check variable
59  if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH
60  && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE
61  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION
62  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION
63  && variable != VAR_ELECTRICITYCONSUMPTION && variable != VAR_WAITING_TIME
64  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
65  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
66  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
67  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_FOES
68  && variable != VAR_WIDTH && variable != ID_COUNT && variable != VAR_PARAMETER
69  ) {
70  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
71  }
72  // begin response building
73  tcpip::Storage tempMsg;
74  // response-code, variableID, objectID
76  tempMsg.writeUnsignedByte(variable);
77  tempMsg.writeString(id);
78  if (variable == ID_LIST) {
81  } else if (variable == ID_COUNT) {
84  } else {
85  try {
86  switch (variable) {
87  case LANE_LINK_NUMBER:
90  break;
91  case LANE_EDGE_ID:
94  break;
95  case VAR_LENGTH:
98  break;
99  case VAR_MAXSPEED:
102  break;
103  case LANE_LINKS: {
105  const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
106  tcpip::Storage tempContent;
107  int cnt = 0;
108  tempContent.writeUnsignedByte(TYPE_INTEGER);
109  tempContent.writeInt((int) links.size());
110  ++cnt;
111  for (std::vector<libsumo::TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
112  // approached non-internal lane (if any)
113  tempContent.writeUnsignedByte(TYPE_STRING);
114  tempContent.writeString(i->approachedLane);
115  ++cnt;
116  // approached "via", internal lane (if any)
117  tempContent.writeUnsignedByte(TYPE_STRING);
118  tempContent.writeString(i->approachedInternal);
119  ++cnt;
120  // priority
121  tempContent.writeUnsignedByte(TYPE_UBYTE);
122  tempContent.writeUnsignedByte(i->hasPrio);
123  ++cnt;
124  // opened
125  tempContent.writeUnsignedByte(TYPE_UBYTE);
126  tempContent.writeUnsignedByte(i->isOpen);
127  ++cnt;
128  // approaching foe
129  tempContent.writeUnsignedByte(TYPE_UBYTE);
130  tempContent.writeUnsignedByte(i->hasFoe);
131  ++cnt;
132  // state (not implemented, yet)
133  tempContent.writeUnsignedByte(TYPE_STRING);
134  tempContent.writeString(i->state);
135  ++cnt;
136  // direction
137  tempContent.writeUnsignedByte(TYPE_STRING);
138  tempContent.writeString(i->direction);
139  ++cnt;
140  // length
141  tempContent.writeUnsignedByte(TYPE_DOUBLE);
142  tempContent.writeDouble(i->length);
143  ++cnt;
144  }
145  tempMsg.writeInt(cnt);
146  tempMsg.writeStorage(tempContent);
147  }
148  break;
149  case LANE_ALLOWED: {
152  }
153  break;
154  case LANE_DISALLOWED: {
157  }
158  break;
159  case VAR_FOES: {
160  std::string toLane;
161  if (!server.readTypeCheckingString(inputStorage, toLane)) {
162  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "foe retrieval requires a string.", outputStorage);
163  }
165  if (toLane == "") {
167  } else {
168  tempMsg.writeStringList(libsumo::Lane::getFoes(id, toLane));
169  }
170  }
171  break;
172  case VAR_SHAPE: {
175  tempMsg.writeUnsignedByte(MIN2(255, (int) shp.size()));
176  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
177  tempMsg.writeDouble(shp[iPoint].x);
178  tempMsg.writeDouble(shp[iPoint].y);
179  }
180  }
181  break;
182  case VAR_CO2EMISSION:
185  break;
186  case VAR_COEMISSION:
189  break;
190  case VAR_HCEMISSION:
193  break;
194  case VAR_PMXEMISSION:
197  break;
198  case VAR_NOXEMISSION:
201  break;
202  case VAR_FUELCONSUMPTION:
205  break;
206  case VAR_NOISEEMISSION:
209  break;
213  break;
217  break;
221  break;
225  }
226  break;
227  case LAST_STEP_OCCUPANCY:
230  break;
234  }
235  break;
236  case LAST_STEP_LENGTH: {
239  }
240  break;
241  case VAR_WAITING_TIME: {
244  }
245  break;
246  case VAR_CURRENT_TRAVELTIME: {
249  }
250  break;
251  case VAR_WIDTH:
254  break;
255  case VAR_PARAMETER: {
256  std::string paramName = "";
257  if (!server.readTypeCheckingString(inputStorage, paramName)) {
258  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
259  }
261  tempMsg.writeString(libsumo::Lane::getParameter(id, paramName));
262  }
263  break;
264  default:
265  break;
266  }
267  } catch (libsumo::TraCIException& e) {
268  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
269  }
270  }
271  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
272  server.writeResponseWithLength(outputStorage, tempMsg);
273  return true;
274 }
275 
276 
277 bool
279  tcpip::Storage& outputStorage) {
280  std::string warning = ""; // additional description for response
281  // variable
282  int variable = inputStorage.readUnsignedByte();
283  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
284  && variable != VAR_PARAMETER) {
285  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
286  }
287  // id
288  std::string id = inputStorage.readString();
289  MSLane* l = MSLane::dictionary(id);
290  if (l == 0) {
291  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
292  }
293  // process
294  switch (variable) {
295  case VAR_MAXSPEED: {
296  double value = 0;
297  if (!server.readTypeCheckingDouble(inputStorage, value)) {
298  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
299  }
300  libsumo::Lane::setMaxSpeed(id, value);
301  }
302  break;
303  case VAR_LENGTH: {
304  double value = 0;
305  if (!server.readTypeCheckingDouble(inputStorage, value)) {
306  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
307  }
308  libsumo::Lane::setLength(id, value);
309  }
310  break;
311  case LANE_ALLOWED: {
312  std::vector<std::string> classes;
313  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
314  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
315  }
316  libsumo::Lane::setAllowed(id, classes);
317  }
318  break;
319  case LANE_DISALLOWED: {
320  std::vector<std::string> classes;
321  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
322  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
323  }
324  libsumo::Lane::setDisallowed(id, classes);
325  }
326  break;
327  case VAR_PARAMETER: {
328  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
329  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
330  }
331  //readt itemNo
332  inputStorage.readInt();
333  std::string name;
334  if (!server.readTypeCheckingString(inputStorage, name)) {
335  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
336  }
337  std::string value;
338  if (!server.readTypeCheckingString(inputStorage, value)) {
339  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
340  }
341  libsumo::Lane::setParameter(id, name, value);
342  }
343  break;
344  default:
345  break;
346  }
347  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
348  return true;
349 }
350 
351 
352 bool
353 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
354  const MSLane* const l = MSLane::dictionary(id);
355  if (l == 0) {
356  return false;
357  }
358  shape = l->getShape();
359  return true;
360 }
361 
362 
363 
364 
365 #endif
366 
367 
368 /****************************************************************************/
369 
static double getCO2Emission(std::string laneID)
Definition: Lane.cpp:146
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
#define LAST_STEP_MEAN_SPEED
static double getLength(std::string laneID)
Definition: Lane.cpp:67
static double getNOxEmission(std::string laneID)
Definition: Lane.cpp:170
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: Lane.cpp:306
static std::vector< std::string > getFoes(const std::string &laneID, const std::string &toLaneID)
Definition: Lane.cpp:271
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: Lane.cpp:85
#define VAR_CO2EMISSION
static std::string getEdgeID(std::string laneID)
Definition: Lane.cpp:61
#define VAR_LENGTH
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
#define TYPE_COMPOUND
#define VAR_CURRENT_TRAVELTIME
#define LANE_LINKS
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: Lane.cpp:342
#define LANE_EDGE_ID
#define LANE_LINK_NUMBER
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_WAITING_TIME
#define TYPE_POLYGON
static std::vector< std::string > getAllowed(std::string laneID)
Definition: Lane.cpp:109
static std::vector< std::string > getInternalFoes(const std::string &laneID)
Definition: Lane.cpp:287
static double getElectricityConsumption(std::string laneID)
Definition: Lane.cpp:187
static double getFuelConsumption(std::string laneID)
Definition: Lane.cpp:175
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.
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:439
static int getIDCount()
Definition: Lane.cpp:55
static int getLastStepHaltingNumber(std::string laneID)
Definition: Lane.cpp:244
static double getWidth(std::string laneID)
Definition: Lane.cpp:140
static int getLastStepVehicleNumber(std::string laneID)
Definition: Lane.cpp:239
virtual void writeUnsignedByte(int)
static double getMaxSpeed(std::string laneID)
Definition: Lane.cpp:73
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_SHAPE
#define VAR_NOISEEMISSION
#define VAR_FUELCONSUMPTION
static double getPMxEmission(std::string laneID)
Definition: Lane.cpp:164
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
#define VAR_NOXEMISSION
static double getLastStepLength(std::string laneID)
Definition: Lane.cpp:205
#define RESPONSE_GET_LANE_VARIABLE
#define LANE_ALLOWED
static double getHCEmission(std::string laneID)
Definition: Lane.cpp:158
virtual int readInt()
A list of positions.
static double getLastStepMeanSpeed(std::string laneID)
Definition: Lane.cpp:193
static TraCIPositionVector getShape(std::string laneID)
Definition: Lane.cpp:125
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
virtual void writeStringList(const std::vector< std::string > &s)
#define VAR_PMXEMISSION
#define CMD_GET_LANE_VARIABLE
static void setMaxSpeed(std::string laneID, double speed)
Definition: Lane.cpp:322
T MIN2(T a, T b)
Definition: StdDefs.h:67
virtual std::string readString()
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: Lane.cpp:314
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
virtual void writeStorage(tcpip::Storage &store)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: Lane.cpp:336
#define LAST_STEP_VEHICLE_NUMBER
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1639
#define VAR_COEMISSION
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
std::vector< TraCIPosition > TraCIPositionVector
Definition: TraCIDefs.h:85
static std::vector< std::string > getLastStepVehicleIDs(std::string laneID)
Definition: Lane.cpp:259
#define CMD_SET_LANE_VARIABLE
#define VAR_ELECTRICITYCONSUMPTION
static double getWaitingTime(std::string laneID)
Definition: Lane.cpp:221
virtual void writeDouble(double)
static double getTraveltime(std::string laneID)
Definition: Lane.cpp:227
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define LAST_STEP_OCCUPANCY
static void setLength(std::string laneID, double length)
Definition: Lane.cpp:329
#define VAR_MAXSPEED
#define VAR_PARAMETER
#define ID_COUNT
#define TYPE_INTEGER
#define ID_LIST
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
static double getNoiseEmission(std::string laneID)
Definition: Lane.cpp:181
#define LAST_STEP_VEHICLE_HALTING_NUMBER
#define VAR_HCEMISSION
static double getLastStepOccupancy(std::string laneID)
Definition: Lane.cpp:199
#define VAR_FOES
#define VAR_WIDTH
static std::vector< std::string > getDisallowed(std::string laneID)
Definition: Lane.cpp:119
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named lane&#39;s shape.
static int getLinkNumber(std::string laneID)
Definition: Lane.cpp:79
static double getCOEmission(std::string laneID)
Definition: Lane.cpp:152
static std::vector< std::string > getIDList()
Definition: Lane.cpp:47