SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_InductionLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting induction loop values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include "TraCIConstants.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // used namespaces
47 // ===========================================================================
48 using namespace traci;
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 bool
56  tcpip::Storage& outputStorage) {
57  std::string warning = ""; // additional description for response
58  // variable & id
59  int variable = inputStorage.readUnsignedByte();
60  std::string id = inputStorage.readString();
61  // check variable
62  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
63  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
64  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
65  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
66  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
67  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Get Induction Loop Variable: unsupported variable specified", outputStorage);
68  return false;
69  }
70  // begin response building
71  tcpip::Storage tempMsg;
72  // response-code, variableID, objectID
74  tempMsg.writeUnsignedByte(variable);
75  tempMsg.writeString(id);
76  // process request
77  if (variable == ID_LIST) {
78  std::vector<std::string> ids;
81  tempMsg.writeStringList(ids);
82  } else if (variable == ID_COUNT) {
83  std::vector<std::string> ids;
86  tempMsg.writeInt((int) ids.size());
87  } else {
89  if (il == 0) {
90  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Induction loop '" + id + "' is not known", outputStorage);
91  return false;
92  }
93  switch (variable) {
94  case ID_LIST:
95  break;
98  tempMsg.writeInt((int)(il->getCurrentPassedNumber()));
99  break;
102  tempMsg.writeDouble(il->getCurrentSpeed());
103  break;
106  std::vector<std::string> ids = il->getCurrentVehicleIDs();
107  tempMsg.writeStringList(ids);
108  }
109  break;
110  case LAST_STEP_OCCUPANCY:
112  tempMsg.writeDouble(il->getCurrentOccupancy());
113  break;
114  case LAST_STEP_LENGTH:
116  tempMsg.writeDouble(il->getCurrentLength());
117  break;
121  break;
122  case LAST_STEP_VEHICLE_DATA: {
123  std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T);
125  tcpip::Storage tempContent;
126  unsigned int cnt = 0;
127  tempContent.writeUnsignedByte(TYPE_INTEGER);
128  tempContent.writeInt((int) vd.size());
129  ++cnt;
130  for (unsigned int i = 0; i < vd.size(); ++i) {
131  MSInductLoop::VehicleData& svd = vd[i];
132  tempContent.writeUnsignedByte(TYPE_STRING);
133  tempContent.writeString(svd.idM);
134  ++cnt;
135  tempContent.writeUnsignedByte(TYPE_DOUBLE);
136  tempContent.writeDouble(svd.lengthM);
137  ++cnt;
138  tempContent.writeUnsignedByte(TYPE_DOUBLE);
139  tempContent.writeDouble(svd.entryTimeM);
140  ++cnt;
141  tempContent.writeUnsignedByte(TYPE_DOUBLE);
142  tempContent.writeDouble(svd.leaveTimeM);
143  ++cnt;
144  tempContent.writeUnsignedByte(TYPE_STRING);
145  tempContent.writeString(svd.typeIDM);
146  ++cnt;
147  }
148 
149  tempMsg.writeInt((int) cnt);
150  tempMsg.writeStorage(tempContent);
151  break;
152  }
153  case VAR_POSITION:
155  tempMsg.writeDouble(il->getPosition());
156  break;
157  case VAR_LANE_ID:
159  tempMsg.writeString(il->getLane()->getID());
160  break;
161  default:
162  break;
163  }
164  }
165  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, warning, outputStorage);
166  server.writeResponseWithLength(outputStorage, tempMsg);
167  return true;
168 }
169 
170 #endif
171 
172 
173 /****************************************************************************/
174