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>
44 #include "TraCIConstants.h"
45 #include "TraCIServer.h"
46 #include "TraCIServerAPI_Lane.h"
47 #include "lib/TraCI_Lane.h"
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 bool
55  tcpip::Storage& outputStorage) {
56  // variable
57  int variable = inputStorage.readUnsignedByte();
58  std::string id = inputStorage.readString();
59  // check variable
60  if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH
61  && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE
62  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION
63  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION
64  && variable != VAR_ELECTRICITYCONSUMPTION && variable != VAR_WAITING_TIME
65  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
66  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
67  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
68  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT
69  && variable != VAR_PARAMETER
70  ) {
71  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
72  }
73  // begin response building
74  tcpip::Storage tempMsg;
75  // response-code, variableID, objectID
77  tempMsg.writeUnsignedByte(variable);
78  tempMsg.writeString(id);
79  if (variable == ID_LIST) {
82  } else if (variable == ID_COUNT) {
85  } else {
86  MSLane* lane = MSLane::dictionary(id);
87  if (lane == 0) {
88  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
89  }
90  switch (variable) {
91  case LANE_LINK_NUMBER:
94  break;
95  case LANE_EDGE_ID:
98  break;
99  case VAR_LENGTH:
101  tempMsg.writeDouble(TraCI_Lane::getLength(id));
102  break;
103  case VAR_MAXSPEED:
106  break;
107  case LANE_LINKS: {
109  const std::vector<TraCIConnection> links = TraCI_Lane::getLinks(id);
110  tcpip::Storage tempContent;
111  int cnt = 0;
112  tempContent.writeUnsignedByte(TYPE_INTEGER);
113  tempContent.writeInt((int) links.size());
114  ++cnt;
115  for (std::vector<TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
116  // approached non-internal lane (if any)
117  tempContent.writeUnsignedByte(TYPE_STRING);
118  tempContent.writeString(i->approachedLane);
119  ++cnt;
120  // approached "via", internal lane (if any)
121  tempContent.writeUnsignedByte(TYPE_STRING);
122  tempContent.writeString(i->approachedInternal);
123  ++cnt;
124  // priority
125  tempContent.writeUnsignedByte(TYPE_UBYTE);
126  tempContent.writeUnsignedByte(i->hasPrio);
127  ++cnt;
128  // opened
129  tempContent.writeUnsignedByte(TYPE_UBYTE);
130  tempContent.writeUnsignedByte(i->isOpen);
131  ++cnt;
132  // approaching foe
133  tempContent.writeUnsignedByte(TYPE_UBYTE);
134  tempContent.writeUnsignedByte(i->hasFoe);
135  ++cnt;
136  // state (not implemented, yet)
137  tempContent.writeUnsignedByte(TYPE_STRING);
138  tempContent.writeString(i->state);
139  ++cnt;
140  // direction
141  tempContent.writeUnsignedByte(TYPE_STRING);
142  tempContent.writeString(i->direction);
143  ++cnt;
144  // length
145  tempContent.writeUnsignedByte(TYPE_DOUBLE);
146  tempContent.writeDouble(i->length);
147  ++cnt;
148  }
149  tempMsg.writeInt(cnt);
150  tempMsg.writeStorage(tempContent);
151  }
152  break;
153  case LANE_ALLOWED: {
156  }
157  break;
158  case LANE_DISALLOWED: {
161  }
162  break;
163  case VAR_SHAPE: {
166  tempMsg.writeUnsignedByte(MIN2(255, (int) shp.size()));
167  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
168  tempMsg.writeDouble(shp[iPoint].x);
169  tempMsg.writeDouble(shp[iPoint].y);
170  }
171  }
172  break;
173  case VAR_CO2EMISSION:
176  break;
177  case VAR_COEMISSION:
180  break;
181  case VAR_HCEMISSION:
184  break;
185  case VAR_PMXEMISSION:
188  break;
189  case VAR_NOXEMISSION:
192  break;
193  case VAR_FUELCONSUMPTION:
196  break;
197  case VAR_NOISEEMISSION:
200  break;
204  break;
208  break;
212  break;
216  }
217  break;
218  case LAST_STEP_OCCUPANCY:
221  break;
225  }
226  break;
227  case LAST_STEP_LENGTH: {
230  }
231  break;
232  case VAR_WAITING_TIME: {
235  }
236  break;
237  case VAR_CURRENT_TRAVELTIME: {
240  }
241  break;
242  case VAR_WIDTH:
244  tempMsg.writeDouble(TraCI_Lane::getWidth(id));
245  break;
246  case VAR_PARAMETER: {
247  std::string paramName = "";
248  if (!server.readTypeCheckingString(inputStorage, paramName)) {
249  return server.writeErrorStatusCmd(RESPONSE_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
250  }
252  tempMsg.writeString(TraCI_Lane::getParameter(id, paramName));
253  }
254  break;
255  default:
256  break;
257  }
258  }
259  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
260  server.writeResponseWithLength(outputStorage, tempMsg);
261  return true;
262 }
263 
264 
265 bool
267  tcpip::Storage& outputStorage) {
268  std::string warning = ""; // additional description for response
269  // variable
270  int variable = inputStorage.readUnsignedByte();
271  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
272  && variable != VAR_PARAMETER) {
273  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
274  }
275  // id
276  std::string id = inputStorage.readString();
277  MSLane* l = MSLane::dictionary(id);
278  if (l == 0) {
279  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
280  }
281  // process
282  switch (variable) {
283  case VAR_MAXSPEED: {
284  double value = 0;
285  if (!server.readTypeCheckingDouble(inputStorage, value)) {
286  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
287  }
288  TraCI_Lane::setMaxSpeed(id, value);
289  }
290  break;
291  case VAR_LENGTH: {
292  double value = 0;
293  if (!server.readTypeCheckingDouble(inputStorage, value)) {
294  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
295  }
296  TraCI_Lane::setLength(id, value);
297  }
298  break;
299  case LANE_ALLOWED: {
300  std::vector<std::string> classes;
301  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
302  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
303  }
304  TraCI_Lane::setAllowed(id, classes);
305  }
306  break;
307  case LANE_DISALLOWED: {
308  std::vector<std::string> classes;
309  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
310  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
311  }
312  TraCI_Lane::setDisallowed(id, classes);
313  }
314  break;
315  case VAR_PARAMETER: {
316  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
317  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
318  }
319  //readt itemNo
320  inputStorage.readInt();
321  std::string name;
322  if (!server.readTypeCheckingString(inputStorage, name)) {
323  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
324  }
325  std::string value;
326  if (!server.readTypeCheckingString(inputStorage, value)) {
327  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
328  }
329  TraCI_Lane::setParameter(id, name, value);
330  }
331  break;
332  default:
333  break;
334  }
335  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
336  return true;
337 }
338 
339 
340 bool
341 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
342  const MSLane* const l = MSLane::dictionary(id);
343  if (l == 0) {
344  return false;
345  }
346  shape = l->getShape();
347  return true;
348 }
349 
350 
351 void
353  switch (myDomain) {
355  const MSLane::VehCont& vehs = l->getVehiclesSecure();
356  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
357  if (myShape.distance2D((*j)->getPosition()) <= myRange) {
358  myIDs.insert((*j)->getID());
359  }
360  }
361  l->releaseVehicles();
362  }
363  break;
365  l->getVehiclesSecure();
366  std::vector<MSTransportable*> persons = l->getEdge().getSortedPersons(MSNet::getInstance()->getCurrentTimeStep(), true);
367  for (auto p : persons) {
368  if (myShape.distance2D(p->getPosition()) <= myRange) {
369  myIDs.insert(p->getID());
370  }
371  }
372  l->releaseVehicles();
373  }
374  break;
375  case CMD_GET_EDGE_VARIABLE: {
376  if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
377  myIDs.insert(l->getEdge().getID());
378  }
379  }
380  break;
381  case CMD_GET_LANE_VARIABLE: {
382  if (myShape.size() != 1 || l->getShape().distance2D(myShape[0]) <= myRange) {
383  myIDs.insert(l->getID());
384  }
385  }
386  break;
387  default:
388  break;
389 
390  }
391 }
392 
393 
394 #endif
395 
396 
397 /****************************************************************************/
398 
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
#define VAR_CURRENT_TRAVELTIME
static double getHCEmission(std::string laneID)
Definition: TraCI_Lane.cpp:158
#define LANE_LINKS
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:406
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
#define CMD_GET_PERSON_VARIABLE
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
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) ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:169
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.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: TraCI_Lane.cpp:85
std::vector< MSTransportable * > getSortedPersons(SUMOTime timestep, bool includeRiding=false) const
Returns this edge&#39;s persons sorted by pos.
Definition: MSEdge.cpp:877
static double getNoiseEmission(std::string laneID)
Definition: TraCI_Lane.cpp:181
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()
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:379
#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
const std::string & getID() const
Returns the id.
Definition: Named.h:66
#define RESPONSE_GET_LANE_VARIABLE
static double getNOxEmission(std::string laneID)
Definition: TraCI_Lane.cpp:170
#define LANE_ALLOWED
virtual int readInt()
void add(const MSLane *const l) const
Adds the given object to the container.
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:68
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:1553
#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)
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:426
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
#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.