SUMO - Simulation of Urban MObility
TraCIServerAPI_ArealDetector.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting areal detector values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2014 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 
37 #include "TraCIConstants.h"
38 #include "TraCIServer.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
57  && variable != ID_COUNT
58  && variable != JAM_LENGTH_VEHICLE
59  && variable != JAM_LENGTH_METERS
60  && variable != LAST_STEP_VEHICLE_NUMBER
61  && variable != LAST_STEP_MEAN_SPEED
62  && variable != LAST_STEP_VEHICLE_ID_LIST
64  && variable != ID_COUNT
65  && variable != LAST_STEP_OCCUPANCY
66  && variable != VAR_POSITION
67  && variable != VAR_LANE_ID
68  && variable != VAR_LENGTH) {
69  return server.writeErrorStatusCmd(CMD_GET_AREAL_DETECTOR_VARIABLE, "Get Areal Detector Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
70  }
71 
72  // begin response building
73  tcpip::Storage tempMsg;
74  // response-code, variableID, objectID
76  tempMsg.writeUnsignedByte(variable);
77  tempMsg.writeString(id);
78  if (variable == ID_LIST) {
79  std::vector<std::string> ids;
82  tempMsg.writeStringList(ids);
83  } else if (variable == ID_COUNT) {
84  std::vector<std::string> ids;
87  tempMsg.writeInt((int) ids.size());
88  } else {
90  if (e2 == 0) {
91  return server.writeErrorStatusCmd(CMD_GET_AREAL_DETECTOR_VARIABLE, "Areal detector '" + id + "' is not known", outputStorage);
92  }
93  std::vector<std::string> ids;
94  switch (variable) {
95  case ID_LIST:
96  break;
99  tempMsg.writeInt((int) e2->getCurrentVehicleNumber());
100  break;
103  tempMsg.writeDouble(e2->getCurrentMeanSpeed());
104  break;
107  ids = e2->getCurrentVehicleIDs();
108  tempMsg.writeStringList(ids);
109  break;
112  tempMsg.writeInt(e2->getCurrentHaltingNumber());
113  break;
114  case JAM_LENGTH_VEHICLE:
116  tempMsg.writeInt((int) e2->getCurrentJamLengthInVehicles());
117  break;
118  case JAM_LENGTH_METERS:
121  break;
122  case LAST_STEP_OCCUPANCY:
124  tempMsg.writeDouble(e2->getCurrentOccupancy());
125  break;
126  case VAR_POSITION:
128  tempMsg.writeDouble(e2->getStartPos());
129  break;
130  case VAR_LANE_ID:
132  tempMsg.writeString(e2->getLane()->getID());
133  break;
134  case VAR_LENGTH:
136  tempMsg.writeDouble(e2->getEndPos() - e2->getStartPos());
137  break;
138  default:
139  break;
140  }
141  }
142  server.writeStatusCmd(CMD_GET_AREAL_DETECTOR_VARIABLE, RTYPE_OK, "", outputStorage);
143  server.writeResponseWithLength(outputStorage, tempMsg);
144  return true;
145 }
146 
147 #endif
148 
149 
150 /****************************************************************************/
151 
#define LAST_STEP_MEAN_SPEED
std::vector< std::string > getCurrentVehicleIDs() const
Returns the IDs of the vehicles within the area.
#define VAR_LENGTH
#define VAR_POSITION
An areal (along a single lane) detector.
Definition: MSE2Collector.h:83
#define RTYPE_OK
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
#define TYPE_STRINGLIST
#define JAM_LENGTH_METERS
SUMOReal getCurrentJamLengthInMeters() const
Returns the length of all jams in meters.
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.
const MSLane * getLane() const
Returns the lane the reminder works on.
virtual void writeInt(int)
#define TYPE_STRING
virtual int readUnsignedByte()
T get(const std::string &id) const
Retrieves an item.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
#define CMD_GET_AREAL_DETECTOR_VARIABLE
#define JAM_LENGTH_VEHICLE
void insertIDs(std::vector< std::string > &into) const
SUMOReal getStartPos() const
Returns the begin position of the detector.
virtual void writeStringList(const std::vector< std::string > &s)
SUMOReal getCurrentMeanSpeed() const
Returns the mean vehicle speed of vehicles currently on the detector.
#define RESPONSE_GET_AREAL_DETECTOR_VARIABLE
virtual std::string readString()
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:359
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define LAST_STEP_VEHICLE_NUMBER
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:64
SUMOReal getCurrentOccupancy() const
Returns the curent detector occupancy.
unsigned getCurrentVehicleNumber() const
Returns the number of vehicles currently on the detector.
virtual void writeDouble(double)
unsigned getCurrentJamLengthInVehicles() const
Returns the length of all jams in vehicles.
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
#define ID_COUNT
#define VAR_LANE_ID
int getCurrentHaltingNumber() const
Returns the number of current haltings within the area.
#define TYPE_INTEGER
#define ID_LIST
#define LAST_STEP_VEHICLE_HALTING_NUMBER
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa1: Get AreaDetector Variable)
SUMOReal getEndPos() const
Returns the end position of the detector.