SUMO - Simulation of Urban MObility
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // APIs for getting/setting lane values via TraCI
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2009-2017 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
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 <microsim/MSEdge.h>
39 #include <microsim/MSEdgeControl.h>
40 #include <microsim/MSLane.h>
41 #include <microsim/MSNet.h>
42 #include <microsim/MSVehicle.h>
43 #include "TraCIConstants.h"
44 #include "TraCIServer.h"
45 #include "TraCIServerAPI_Lane.h"
46 #include "lib/TraCI_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_WIDTH && variable != ID_COUNT
68  && 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  MSLane* lane = MSLane::dictionary(id);
86  if (lane == 0) {
87  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
88  }
89  switch (variable) {
90  case LANE_LINK_NUMBER:
93  break;
94  case LANE_EDGE_ID:
97  break;
98  case VAR_LENGTH:
100  tempMsg.writeDouble(TraCI_Lane::getLength(id));
101  break;
102  case VAR_MAXSPEED:
105  break;
106  case LANE_LINKS: {
108  const std::vector<TraCIConnection> links = TraCI_Lane::getLinks(id);
109  tcpip::Storage tempContent;
110  int cnt = 0;
111  tempContent.writeUnsignedByte(TYPE_INTEGER);
112  tempContent.writeInt((int) links.size());
113  ++cnt;
114  for (std::vector<TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
115  // approached non-internal lane (if any)
116  tempContent.writeUnsignedByte(TYPE_STRING);
117  tempContent.writeString(i->approachedLane);
118  ++cnt;
119  // approached "via", internal lane (if any)
120  tempContent.writeUnsignedByte(TYPE_STRING);
121  tempContent.writeString(i->approachedInternal);
122  ++cnt;
123  // priority
124  tempContent.writeUnsignedByte(TYPE_UBYTE);
125  tempContent.writeUnsignedByte(i->hasPrio);
126  ++cnt;
127  // opened
128  tempContent.writeUnsignedByte(TYPE_UBYTE);
129  tempContent.writeUnsignedByte(i->isOpen);
130  ++cnt;
131  // approaching foe
132  tempContent.writeUnsignedByte(TYPE_UBYTE);
133  tempContent.writeUnsignedByte(i->hasFoe);
134  ++cnt;
135  // state (not implemented, yet)
136  tempContent.writeUnsignedByte(TYPE_STRING);
137  tempContent.writeString(i->state);
138  ++cnt;
139  // direction
140  tempContent.writeUnsignedByte(TYPE_STRING);
141  tempContent.writeString(i->direction);
142  ++cnt;
143  // length
144  tempContent.writeUnsignedByte(TYPE_DOUBLE);
145  tempContent.writeDouble(i->length);
146  ++cnt;
147  }
148  tempMsg.writeInt(cnt);
149  tempMsg.writeStorage(tempContent);
150  }
151  break;
152  case LANE_ALLOWED: {
155  }
156  break;
157  case LANE_DISALLOWED: {
160  }
161  break;
162  case VAR_SHAPE: {
165  tempMsg.writeUnsignedByte(MIN2(255, (int) shp.size()));
166  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
167  tempMsg.writeDouble(shp[iPoint].x);
168  tempMsg.writeDouble(shp[iPoint].y);
169  }
170  }
171  break;
172  case VAR_CO2EMISSION:
175  break;
176  case VAR_COEMISSION:
179  break;
180  case VAR_HCEMISSION:
183  break;
184  case VAR_PMXEMISSION:
187  break;
188  case VAR_NOXEMISSION:
191  break;
192  case VAR_FUELCONSUMPTION:
195  break;
196  case VAR_NOISEEMISSION:
199  break;
203  break;
207  break;
211  break;
215  }
216  break;
217  case LAST_STEP_OCCUPANCY:
220  break;
224  }
225  break;
226  case LAST_STEP_LENGTH: {
229  }
230  break;
231  case VAR_WAITING_TIME: {
234  }
235  break;
236  case VAR_CURRENT_TRAVELTIME: {
239  }
240  break;
241  case VAR_WIDTH:
243  tempMsg.writeDouble(TraCI_Lane::getWidth(id));
244  break;
245  case VAR_PARAMETER: {
246  std::string paramName = "";
247  if (!server.readTypeCheckingString(inputStorage, paramName)) {
248  return server.writeErrorStatusCmd(RESPONSE_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
249  }
251  tempMsg.writeString(TraCI_Lane::getParameter(id, paramName));
252  }
253  break;
254  default:
255  break;
256  }
257  }
258  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
259  server.writeResponseWithLength(outputStorage, tempMsg);
260  return true;
261 }
262 
263 
264 bool
266  tcpip::Storage& outputStorage) {
267  std::string warning = ""; // additional description for response
268  // variable
269  int variable = inputStorage.readUnsignedByte();
270  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
271  && variable != VAR_PARAMETER) {
272  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
273  }
274  // id
275  std::string id = inputStorage.readString();
276  MSLane* l = MSLane::dictionary(id);
277  if (l == 0) {
278  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
279  }
280  // process
281  switch (variable) {
282  case VAR_MAXSPEED: {
283  double value = 0;
284  if (!server.readTypeCheckingDouble(inputStorage, value)) {
285  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
286  }
287  TraCI_Lane::setMaxSpeed(id, value);
288  }
289  break;
290  case VAR_LENGTH: {
291  double value = 0;
292  if (!server.readTypeCheckingDouble(inputStorage, value)) {
293  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
294  }
295  TraCI_Lane::setLength(id, value);
296  }
297  break;
298  case LANE_ALLOWED: {
299  std::vector<std::string> classes;
300  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
301  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
302  }
303  TraCI_Lane::setAllowed(id, classes);
304  }
305  break;
306  case LANE_DISALLOWED: {
307  std::vector<std::string> classes;
308  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
309  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
310  }
311  TraCI_Lane::setDisallowed(id, classes);
312  }
313  break;
314  case VAR_PARAMETER: {
315  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
316  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
317  }
318  //readt itemNo
319  inputStorage.readInt();
320  std::string name;
321  if (!server.readTypeCheckingString(inputStorage, name)) {
322  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
323  }
324  std::string value;
325  if (!server.readTypeCheckingString(inputStorage, value)) {
326  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
327  }
328  TraCI_Lane::setParameter(id, name, value);
329  }
330  break;
331  default:
332  break;
333  }
334  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
335  return true;
336 }
337 
338 
339 bool
340 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
341  const MSLane* const l = MSLane::dictionary(id);
342  if (l == 0) {
343  return false;
344  }
345  shape = l->getShape();
346  return true;
347 }
348 
349 
350 void
352  switch (myDomain) {
354  const MSLane::VehCont& vehs = l->getVehiclesSecure();
355  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
356  if (myShape.distance2D((*j)->getPosition()) <= myRange) {
357  myIDs.insert((*j)->getID());
358  }
359  }
360  l->releaseVehicles();
361  }
362  break;
363  case CMD_GET_EDGE_VARIABLE: {
364  if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
365  myIDs.insert(l->getEdge().getID());
366  }
367  }
368  break;
369  case CMD_GET_LANE_VARIABLE: {
370  if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
371  myIDs.insert(l->getID());
372  }
373  }
374  break;
375  default:
376  break;
377 
378  }
379 }
380 
381 
382 #endif
383 
384 
385 /****************************************************************************/
386 
static double getCO2Emission(std::string laneID)
Definition: TraCI_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 int getLastStepHaltingNumber(std::string laneID)
Definition: TraCI_Lane.cpp:244
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:582
#define VAR_CO2EMISSION
#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 CMD_GET_VEHICLE_VARIABLE
#define TYPE_COMPOUND
double distance2D(const Position &p, bool perpendicular=false) const
closest 2D-distance to point p (or -1 if perpendicular is true and the point is beyond this vector) ...
#define VAR_CURRENT_TRAVELTIME
static double getHCEmission(std::string laneID)
Definition: TraCI_Lane.cpp:158
#define LANE_LINKS
static double getLength(std::string laneID)
Definition: TraCI_Lane.cpp:67
static std::vector< std::string > getLastStepVehicleIDs(std::string laneID)
Definition: TraCI_Lane.cpp:259
static double getWaitingTime(std::string laneID)
Definition: TraCI_Lane.cpp:221
#define LANE_EDGE_ID
#define LANE_LINK_NUMBER
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: TraCI_Lane.cpp:274
#define TYPE_UBYTE
#define RTYPE_OK
#define VAR_WAITING_TIME
static double getElectricityConsumption(std::string laneID)
Definition: TraCI_Lane.cpp:187
#define TYPE_POLYGON
static std::string getEdgeID(std::string laneID)
Definition: TraCI_Lane.cpp:61
static std::vector< std::string > getIDList()
Definition: TraCI_Lane.cpp:47
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:379
#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:426
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: TraCI_Lane.cpp:85
static double getNoiseEmission(std::string laneID)
Definition: TraCI_Lane.cpp:181
const std::string & getID() const
Returns the id.
Definition: Named.h:66
virtual void writeUnsignedByte(int)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: TraCI_Lane.cpp:282
#define VAR_SHAPE
#define VAR_NOISEEMISSION
#define VAR_FUELCONSUMPTION
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
static double getWidth(std::string laneID)
Definition: TraCI_Lane.cpp:140
#define VAR_NOXEMISSION
static double getLastStepMeanSpeed(std::string laneID)
Definition: TraCI_Lane.cpp:193
#define RESPONSE_GET_LANE_VARIABLE
static double getNOxEmission(std::string laneID)
Definition: TraCI_Lane.cpp:170
#define LANE_ALLOWED
virtual int readInt()
static double getLastStepLength(std::string laneID)
Definition: TraCI_Lane.cpp:205
A list of positions.
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
T MIN2(T a, T b)
Definition: StdDefs.h:64
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
static double getTraveltime(std::string laneID)
Definition: TraCI_Lane.cpp:227
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
virtual void writeStorage(tcpip::Storage &store)
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:91
static int getIDCount()
Definition: TraCI_Lane.cpp:55
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define LAST_STEP_VEHICLE_NUMBER
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: TraCI_Lane.cpp:304
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1371
#define VAR_COEMISSION
static std::vector< std::string > getAllowed(std::string laneID)
Definition: TraCI_Lane.cpp:109
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define LANE_DISALLOWED
static TraCIPositionVector getShape(std::string laneID)
Definition: TraCI_Lane.cpp:125
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static double getLastStepOccupancy(std::string laneID)
Definition: TraCI_Lane.cpp:199
#define CMD_SET_LANE_VARIABLE
#define VAR_ELECTRICITYCONSUMPTION
static double getFuelConsumption(std::string laneID)
Definition: TraCI_Lane.cpp:175
static int getLinkNumber(std::string laneID)
Definition: TraCI_Lane.cpp:79
virtual void writeDouble(double)
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: TraCI_Lane.cpp:310
static void setLength(std::string laneID, double length)
Definition: TraCI_Lane.cpp:297
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 double getCOEmission(std::string laneID)
Definition: TraCI_Lane.cpp:152
#define VAR_MAXSPEED
#define VAR_PARAMETER
#define ID_COUNT
static int getLastStepVehicleNumber(std::string laneID)
Definition: TraCI_Lane.cpp:239
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:406
void add(const MSLane *const l) const
Adds the given object to the container.
#define TYPE_INTEGER
#define ID_LIST
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
A list of positions.
static double getPMxEmission(std::string laneID)
Definition: TraCI_Lane.cpp:164
static std::vector< std::string > getDisallowed(std::string laneID)
Definition: TraCI_Lane.cpp:119
#define LAST_STEP_VEHICLE_HALTING_NUMBER
static void setMaxSpeed(std::string laneID, double speed)
Definition: TraCI_Lane.cpp:290
#define VAR_HCEMISSION
static double getMaxSpeed(std::string laneID)
Definition: TraCI_Lane.cpp:73
#define VAR_WIDTH
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named lane&#39;s shape.