SUMO - Simulation of Urban MObility
TraCIServerAPI_InductionLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2009-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // APIs for getting/setting induction loop values via TraCI
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 <microsim/MSNet.h>
37 #include <libsumo/InductionLoop.h>
38 #include "TraCIConstants.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 bool
47  tcpip::Storage& outputStorage) {
48  // variable & id
49  int variable = inputStorage.readUnsignedByte();
50  std::string id = inputStorage.readString();
51  // check variable
52  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
53  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
54  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
55  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
56  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
57  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Get Induction Loop Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58  }
59  // begin response building
60  tcpip::Storage tempMsg;
61  // response-code, variableID, objectID
63  tempMsg.writeUnsignedByte(variable);
64  tempMsg.writeString(id);
65  // process request
66  try {
67  switch (variable) {
68  case ID_LIST:
71  break;
72  case ID_COUNT:
75  break;
76  case VAR_POSITION:
79  break;
80  case VAR_LANE_ID:
83  break;
87  break;
91  break;
95  break;
99  break;
100  case LAST_STEP_LENGTH:
103  break;
107  break;
108  case LAST_STEP_VEHICLE_DATA: {
109  std::vector<libsumo::TraCIVehicleData> vd = libsumo::InductionLoop::getVehicleData(id);
111  tcpip::Storage tempContent;
112  int cnt = 0;
113  tempContent.writeUnsignedByte(TYPE_INTEGER);
114  tempContent.writeInt((int) vd.size());
115  ++cnt;
116  for (int i = 0; i < (int)vd.size(); ++i) {
117  libsumo::TraCIVehicleData& svd = vd[i];
118  tempContent.writeUnsignedByte(TYPE_STRING);
119  tempContent.writeString(svd.id);
120  ++cnt;
121  tempContent.writeUnsignedByte(TYPE_DOUBLE);
122  tempContent.writeDouble(svd.length);
123  ++cnt;
124  tempContent.writeUnsignedByte(TYPE_DOUBLE);
125  tempContent.writeDouble(svd.entryTime);
126  ++cnt;
127  tempContent.writeUnsignedByte(TYPE_DOUBLE);
128  tempContent.writeDouble(svd.leaveTime);
129  ++cnt;
130  tempContent.writeUnsignedByte(TYPE_STRING);
131  tempContent.writeString(svd.typeID);
132  ++cnt;
133  }
134 
135  tempMsg.writeInt((int) cnt);
136  tempMsg.writeStorage(tempContent);
137  break;
138  }
139  default:
140  break;
141  }
142  } catch (libsumo::TraCIException& e) {
143  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, e.what(), outputStorage);
144  }
145  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, "", outputStorage);
146  server.writeResponseWithLength(outputStorage, tempMsg);
147  return true;
148 }
149 
150 
151 bool
154  if (il == 0) {
155  return false;
156  }
157  p = il->getLane()->getShape().positionAtOffset(il->getPosition());
158  return true;
159 }
160 
161 
162 #endif
163 
164 
165 /****************************************************************************/
166 
#define LAST_STEP_MEAN_SPEED
const MSLane * getLane() const
Returns the lane the reminder works on.
double leaveTime
Leave-time of the vehicle in [s].
Definition: TraCIDefs.h:173
alternative tag for e1 detector
std::string typeID
Type of the vehicle in.
Definition: TraCIDefs.h:175
#define TYPE_COMPOUND
#define VAR_POSITION
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:165
#define CMD_GET_INDUCTIONLOOP_VARIABLE
static std::string getLaneID(const std::string &detID)
#define LAST_STEP_VEHICLE_DATA
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
#define TYPE_STRINGLIST
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:439
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 std::vector< std::string > getIDList()
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
static double getLastStepOccupancy(const std::string &detID)
static bool getPosition(const std::string &id, Position &p)
Returns the named inductive loop&#39;s position.
#define LAST_STEP_TIME_SINCE_DETECTION
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
static double getTimeSinceDetection(const std::string &detID)
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:100
static std::vector< std::string > getLastStepVehicleIDs(const std::string &detID)
virtual void writeStringList(const std::vector< std::string > &s)
double length
Length of the vehicle.
Definition: TraCIDefs.h:169
std::string id
The id of the vehicle.
Definition: TraCIDefs.h:167
virtual std::string readString()
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa0: Get Induction Loop Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:69
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:369
#define RESPONSE_GET_INDUCTIONLOOP_VARIABLE
virtual void writeStorage(tcpip::Storage &store)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define LAST_STEP_VEHICLE_NUMBER
static int getLastStepVehicleNumber(const std::string &detID)
static std::vector< libsumo::TraCIVehicleData > getVehicleData(const std::string &detID)
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:65
virtual void writeDouble(double)
static double getLastStepMeanSpeed(const std::string &detID)
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 getPosition(const std::string &detID)
double entryTime
Entry-time of the vehicle in [s].
Definition: TraCIDefs.h:171
#define ID_COUNT
#define VAR_LANE_ID
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
#define TYPE_INTEGER
#define ID_LIST
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:70
static double getLastStepMeanLength(const std::string &detID)