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-sim.org/
12 // Copyright (C) 2001-2013 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"
36 #include <microsim/MSNet.h>
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
51  tcpip::Storage& outputStorage) {
52  // variable & id
53  int variable = inputStorage.readUnsignedByte();
54  std::string id = inputStorage.readString();
55  // check variable
56  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
57  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
58  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
59  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
60  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
61  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Get Induction Loop Variable: unsupported variable specified", outputStorage);
62  }
63  // begin response building
64  tcpip::Storage tempMsg;
65  // response-code, variableID, objectID
67  tempMsg.writeUnsignedByte(variable);
68  tempMsg.writeString(id);
69  // process request
70  if (variable == ID_LIST) {
71  std::vector<std::string> ids;
74  tempMsg.writeStringList(ids);
75  } else if (variable == ID_COUNT) {
76  std::vector<std::string> ids;
79  tempMsg.writeInt((int) ids.size());
80  } else {
82  if (il == 0) {
83  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Induction loop '" + id + "' is not known", outputStorage);
84  }
85  switch (variable) {
86  case ID_LIST:
87  break;
90  tempMsg.writeInt((int)(il->getCurrentPassedNumber()));
91  break;
94  tempMsg.writeDouble(il->getCurrentSpeed());
95  break;
98  std::vector<std::string> ids = il->getCurrentVehicleIDs();
99  tempMsg.writeStringList(ids);
100  }
101  break;
102  case LAST_STEP_OCCUPANCY:
104  tempMsg.writeDouble(il->getCurrentOccupancy());
105  break;
106  case LAST_STEP_LENGTH:
108  tempMsg.writeDouble(il->getCurrentLength());
109  break;
113  break;
114  case LAST_STEP_VEHICLE_DATA: {
115  std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T);
117  tcpip::Storage tempContent;
118  unsigned int cnt = 0;
119  tempContent.writeUnsignedByte(TYPE_INTEGER);
120  tempContent.writeInt((int) vd.size());
121  ++cnt;
122  for (unsigned int i = 0; i < vd.size(); ++i) {
123  MSInductLoop::VehicleData& svd = vd[i];
124  tempContent.writeUnsignedByte(TYPE_STRING);
125  tempContent.writeString(svd.idM);
126  ++cnt;
127  tempContent.writeUnsignedByte(TYPE_DOUBLE);
128  tempContent.writeDouble(svd.lengthM);
129  ++cnt;
130  tempContent.writeUnsignedByte(TYPE_DOUBLE);
131  tempContent.writeDouble(svd.entryTimeM);
132  ++cnt;
133  tempContent.writeUnsignedByte(TYPE_DOUBLE);
134  tempContent.writeDouble(svd.leaveTimeM);
135  ++cnt;
136  tempContent.writeUnsignedByte(TYPE_STRING);
137  tempContent.writeString(svd.typeIDM);
138  ++cnt;
139  }
140 
141  tempMsg.writeInt((int) cnt);
142  tempMsg.writeStorage(tempContent);
143  break;
144  }
145  case VAR_POSITION:
147  tempMsg.writeDouble(il->getPosition());
148  break;
149  case VAR_LANE_ID:
151  tempMsg.writeString(il->getLane()->getID());
152  break;
153  default:
154  break;
155  }
156  }
157  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, "", outputStorage);
158  server.writeResponseWithLength(outputStorage, tempMsg);
159  return true;
160 }
161 
162 
163 bool
166  if (il == 0) {
167  return false;
168  }
169  p = il->getLane()->getShape().positionAtOffset(il->getPosition());
170  return true;
171 }
172 
173 
174 NamedRTree*
176  NamedRTree* t = new NamedRTree();
177  const std::map<std::string, MSDetectorFileOutput*>& dets = MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).getMyMap();
178  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
179  MSInductLoop* il = static_cast<MSInductLoop*>((*i).second);
181  const float cmin[2] = {(float) p.x(), (float) p.y()};
182  const float cmax[2] = {(float) p.x(), (float) p.y()};
183  t->Insert(cmin, cmax, il);
184  }
185  return t;
186 }
187 
188 #endif
189 
190 
191 /****************************************************************************/
192