SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting lane values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
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 <microsim/MSEdge.h>
37 #include <microsim/MSEdgeControl.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSNet.h>
40 #include "TraCIConstants.h"
41 #include "TraCIServer.h"
42 #include "TraCIServerAPI_Lane.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
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 && variable != VAR_WAITING_TIME
63  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
64  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
65  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
66  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT
67  ) {
68  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable specified", outputStorage);
69  }
70  // begin response building
71  tcpip::Storage tempMsg;
72  // response-code, variableID, objectID
74  tempMsg.writeUnsignedByte(variable);
75  tempMsg.writeString(id);
76  if (variable == ID_LIST) {
77  std::vector<std::string> ids;
78  MSLane::insertIDs(ids);
80  tempMsg.writeStringList(ids);
81  } else if (variable == ID_COUNT) {
82  std::vector<std::string> ids;
83  MSLane::insertIDs(ids);
85  tempMsg.writeInt((int) ids.size());
86  } else {
87  MSLane* lane = MSLane::dictionary(id);
88  if (lane == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
90  }
91  switch (variable) {
92  case LANE_LINK_NUMBER:
94  tempMsg.writeUnsignedByte((int) lane->getLinkCont().size());
95  break;
96  case LANE_EDGE_ID:
98  tempMsg.writeString(lane->getEdge().getID());
99  break;
100  case VAR_LENGTH:
102  tempMsg.writeDouble(lane->getLength());
103  break;
104  case VAR_MAXSPEED:
106  tempMsg.writeDouble(lane->getSpeedLimit());
107  break;
108  case LANE_LINKS: {
110  tcpip::Storage tempContent;
111  unsigned int cnt = 0;
112  tempContent.writeUnsignedByte(TYPE_INTEGER);
113  const MSLinkCont& links = lane->getLinkCont();
114  tempContent.writeInt((int) links.size());
115  ++cnt;
116  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
117  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
118  MSLink* link = (*i);
119  // approached non-internal lane (if any)
120  tempContent.writeUnsignedByte(TYPE_STRING);
121  tempContent.writeString(link->getLane() != 0 ? link->getLane()->getID() : "");
122  ++cnt;
123  // approached "via", internal lane (if any)
124  tempContent.writeUnsignedByte(TYPE_STRING);
125 #ifdef HAVE_INTERNAL_LANES
126  tempContent.writeString(link->getViaLane() != 0 ? link->getViaLane()->getID() : "");
127 #else
128  tempContent.writeString("");
129 #endif
130  ++cnt;
131  // priority
132  tempContent.writeUnsignedByte(TYPE_UBYTE);
133  tempContent.writeUnsignedByte(link->havePriority() ? 1 : 0);
134  ++cnt;
135  // opened
136  tempContent.writeUnsignedByte(TYPE_UBYTE);
137  const SUMOReal speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
138  tempContent.writeUnsignedByte(link->opened(currTime, speed, speed, DEFAULT_VEH_LENGTH, 0.0, DEFAULT_VEH_DECEL, 0) ? 1 : 0);
139  ++cnt;
140  // approaching foe
141  tempContent.writeUnsignedByte(TYPE_UBYTE);
142  tempContent.writeUnsignedByte(link->hasApproachingFoe(currTime, currTime, 0) ? 1 : 0);
143  ++cnt;
144  // state (not implemented, yet)
145  tempContent.writeUnsignedByte(TYPE_STRING);
146  tempContent.writeString(SUMOXMLDefinitions::LinkStates.getString(link->getState()));
147  ++cnt;
148  // direction
149  tempContent.writeUnsignedByte(TYPE_STRING);
150  tempContent.writeString(SUMOXMLDefinitions::LinkDirections.getString(link->getDirection()));
151  ++cnt;
152  // length
153  tempContent.writeUnsignedByte(TYPE_DOUBLE);
154  tempContent.writeDouble(link->getLength());
155  ++cnt;
156  }
157  tempMsg.writeInt((int) cnt);
158  tempMsg.writeStorage(tempContent);
159  }
160  break;
161  case LANE_ALLOWED: {
163  SVCPermissions permissions = lane->getPermissions();
164  if (permissions == SVCFreeForAll) { // special case: write nothing
165  permissions = 0;
166  }
167  tempMsg.writeStringList(getAllowedVehicleClassNamesList(permissions));
168  }
169  case LANE_DISALLOWED: {
171  tempMsg.writeStringList(getAllowedVehicleClassNamesList(~(lane->getPermissions()))); // negation yields disallowed
172  }
173  break;
174  case VAR_SHAPE:
176  tempMsg.writeUnsignedByte((int)MIN2(static_cast<size_t>(255), lane->getShape().size()));
177  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), lane->getShape().size()); ++iPoint) {
178  tempMsg.writeDouble(lane->getShape()[iPoint].x());
179  tempMsg.writeDouble(lane->getShape()[iPoint].y());
180  }
181  break;
182  case VAR_CO2EMISSION:
184  tempMsg.writeDouble(lane->getHBEFA_CO2Emissions());
185  break;
186  case VAR_COEMISSION:
188  tempMsg.writeDouble(lane->getHBEFA_COEmissions());
189  break;
190  case VAR_HCEMISSION:
192  tempMsg.writeDouble(lane->getHBEFA_HCEmissions());
193  break;
194  case VAR_PMXEMISSION:
196  tempMsg.writeDouble(lane->getHBEFA_PMxEmissions());
197  break;
198  case VAR_NOXEMISSION:
200  tempMsg.writeDouble(lane->getHBEFA_NOxEmissions());
201  break;
202  case VAR_FUELCONSUMPTION:
204  tempMsg.writeDouble(lane->getHBEFA_FuelConsumption());
205  break;
206  case VAR_NOISEEMISSION:
208  tempMsg.writeDouble(lane->getHarmonoise_NoiseEmissions());
209  break;
212  tempMsg.writeInt((int) lane->getVehicleNumber());
213  break;
216  tempMsg.writeDouble(lane->getMeanSpeed());
217  break;
219  std::vector<std::string> vehIDs;
220  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
221  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
222  vehIDs.push_back((*j)->getID());
223  }
224  lane->releaseVehicles();
226  tempMsg.writeStringList(vehIDs);
227  }
228  break;
229  case LAST_STEP_OCCUPANCY:
231  tempMsg.writeDouble(lane->getNettoOccupancy());
232  break;
234  int halting = 0;
235  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
236  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
237  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
238  ++halting;
239  }
240  }
241  lane->releaseVehicles();
243  tempMsg.writeInt(halting);
244  }
245  break;
246  case LAST_STEP_LENGTH: {
247  SUMOReal lengthSum = 0;
248  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
249  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
250  lengthSum += (*j)->getVehicleType().getLength();
251  }
253  if (vehs.size() == 0) {
254  tempMsg.writeDouble(0);
255  } else {
256  tempMsg.writeDouble(lengthSum / (SUMOReal) vehs.size());
257  }
258  lane->releaseVehicles();
259  }
260  break;
261  case VAR_WAITING_TIME: {
263  tempMsg.writeDouble(lane->getWaitingSeconds());
264  }
265  break;
266  case VAR_CURRENT_TRAVELTIME: {
267  SUMOReal meanSpeed = lane->getMeanSpeed();
269  if (meanSpeed != 0) {
270  tempMsg.writeDouble(lane->getLength() / meanSpeed);
271  } else {
272  tempMsg.writeDouble(1000000.);
273  }
274  }
275  break;
276  case VAR_WIDTH:
278  tempMsg.writeDouble(lane->getWidth());
279  break;
280  default:
281  break;
282  }
283  }
284  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
285  server.writeResponseWithLength(outputStorage, tempMsg);
286  return true;
287 }
288 
289 
290 bool
292  tcpip::Storage& outputStorage) {
293  std::string warning = ""; // additional description for response
294  // variable
295  int variable = inputStorage.readUnsignedByte();
296  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED) {
297  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable specified", outputStorage);
298  }
299  // id
300  std::string id = inputStorage.readString();
301  MSLane* l = MSLane::dictionary(id);
302  if (l == 0) {
303  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
304  }
305  // process
306  switch (variable) {
307  case VAR_MAXSPEED: {
308  double value = 0;
309  if (!server.readTypeCheckingDouble(inputStorage, value)) {
310  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
311  }
312  l->setMaxSpeed(value);
313  }
314  break;
315  case VAR_LENGTH: {
316  double value = 0;
317  if (!server.readTypeCheckingDouble(inputStorage, value)) {
318  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
319  }
320  l->setLength(value);
321  }
322  break;
323  case LANE_ALLOWED: {
324  std::vector<std::string> classes;
325  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
326  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
327  }
328  l->setPermissions(parseVehicleClasses(classes));
330  }
331  break;
332  case LANE_DISALLOWED: {
333  std::vector<std::string> classes;
334  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
335  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
336  }
337  l->setPermissions(~parseVehicleClasses(classes)); // negation yields allowed
339  }
340  break;
341  default:
342  break;
343  }
344  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
345  return true;
346 }
347 
348 
349 bool
350 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
351  const MSLane* const l = MSLane::dictionary(id);
352  if (l == 0) {
353  return false;
354  }
355  shape.push_back(l->getShape());
356  return true;
357 }
358 
359 
360 void
362  switch (myDomain) {
364  const MSLane::VehCont& vehs = l->getVehiclesSecure();
365  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
366  if (myShape.distance((*j)->getPosition()) <= myRange) {
367  myIDs.insert((*j)->getID());
368  }
369  }
370  l->releaseVehicles();
371  }
372  break;
373  case CMD_GET_EDGE_VARIABLE: {
374  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
375  myIDs.insert(l->getEdge().getID());
376  }
377  }
378  break;
379  case CMD_GET_LANE_VARIABLE: {
380  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
381  myIDs.insert(l->getID());
382  }
383  }
384  break;
385  default:
386  break;
387 
388  }
389 }
390 
391 
392 #endif
393 
394 
395 /****************************************************************************/
396