TraCIServerAPI_InductionLoop.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include "TraCIConstants.h"
00031 #include <microsim/output/MSDetectorControl.h>
00032 #include <microsim/output/MSInductLoop.h>
00033 #include "TraCIServerAPIHelper.h"
00034 #include "TraCIServerAPI_InductionLoop.h"
00035
00036 #ifdef CHECK_MEMORY_LEAKS
00037 #include <foreign/nvwa/debug_new.h>
00038 #endif // CHECK_MEMORY_LEAKS
00039
00040
00041
00042
00043
00044 using namespace std;
00045 using namespace traci;
00046 using namespace tcpip;
00047
00048
00049
00050
00051
00052 bool
00053 TraCIServerAPI_InductionLoop::processGet(tcpip::Storage &inputStorage,
00054 tcpip::Storage &outputStorage,
00055 bool withStatus) throw(TraCIException) {
00056 std::string warning = "";
00057
00058 int variable = inputStorage.readUnsignedByte();
00059 std::string id = inputStorage.readString();
00060
00061 if (variable!=ID_LIST&&variable!=LAST_STEP_VEHICLE_NUMBER&&variable!=LAST_STEP_MEAN_SPEED
00062 &&variable!=LAST_STEP_VEHICLE_ID_LIST&&variable!=LAST_STEP_OCCUPANCY
00063 &&variable!=LAST_STEP_LENGTH&&variable!=LAST_STEP_TIME_SINCE_DETECTION) {
00064 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Get Induction Loop Variable: unsupported variable specified", outputStorage);
00065 return false;
00066 }
00067
00068 Storage tempMsg;
00069
00070 tempMsg.writeUnsignedByte(RESPONSE_GET_INDUCTIONLOOP_VARIABLE);
00071 tempMsg.writeUnsignedByte(variable);
00072 tempMsg.writeString(id);
00073
00074 if (variable==ID_LIST) {
00075 std::vector<std::string> ids;
00076 MSNet::getInstance()->getDetectorControl().getInductLoops().insertIDs(ids);
00077 tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00078 tempMsg.writeStringList(ids);
00079 } else {
00080 MSInductLoop *il = MSNet::getInstance()->getDetectorControl().getInductLoops().get(id);
00081 if (il==0) {
00082 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Induction loop '" + id + "' is not known", outputStorage);
00083 return false;
00084 }
00085 switch (variable) {
00086 case ID_LIST:
00087 break;
00088 case LAST_STEP_VEHICLE_NUMBER:
00089 tempMsg.writeUnsignedByte(TYPE_INTEGER);
00090 tempMsg.writeInt(il->getCurrentPassedNumber());
00091 break;
00092 case LAST_STEP_MEAN_SPEED:
00093 tempMsg.writeUnsignedByte(TYPE_FLOAT);
00094 tempMsg.writeFloat((float) il->getCurrentSpeed());
00095 break;
00096 case LAST_STEP_VEHICLE_ID_LIST: {
00097 tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00098 std::vector<std::string> ids = il->getCurrentVehicleIDs();
00099 tempMsg.writeStringList(ids);
00100 }
00101 break;
00102 case LAST_STEP_OCCUPANCY:
00103 tempMsg.writeUnsignedByte(TYPE_FLOAT);
00104 tempMsg.writeFloat((float) il->getCurrentOccupancy());
00105 break;
00106 case LAST_STEP_LENGTH:
00107 tempMsg.writeUnsignedByte(TYPE_FLOAT);
00108 tempMsg.writeFloat((float) il->getCurrentLength());
00109 break;
00110 case LAST_STEP_TIME_SINCE_DETECTION:
00111 tempMsg.writeUnsignedByte(TYPE_FLOAT);
00112 tempMsg.writeFloat((float) il->getTimestepsSinceLastDetection());
00113 break;
00114 default:
00115 break;
00116 }
00117 }
00118 if (withStatus) {
00119 TraCIServerAPIHelper::writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, warning, outputStorage);
00120 }
00121
00122 outputStorage.writeUnsignedByte(0);
00123 outputStorage.writeInt(1 + 4 + tempMsg.size());
00124 outputStorage.writeStorage(tempMsg);
00125 return true;
00126 }
00127
00128
00129
00130