SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00009 // APIs for getting/setting induction loop values via TraCI 00010 /****************************************************************************/ 00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00013 /****************************************************************************/ 00014 // 00015 // This file is part of SUMO. 00016 // SUMO is free software: you can redistribute it and/or modify 00017 // it under the terms of the GNU General Public License as published by 00018 // the Free Software Foundation, either version 3 of the License, or 00019 // (at your option) any later version. 00020 // 00021 /****************************************************************************/ 00022 00023 00024 // =========================================================================== 00025 // included modules 00026 // =========================================================================== 00027 #ifdef _MSC_VER 00028 #include <windows_config.h> 00029 #else 00030 #include <config.h> 00031 #endif 00032 00033 #ifndef NO_TRACI 00034 00035 #include "TraCIConstants.h" 00036 #include <microsim/output/MSDetectorControl.h> 00037 #include <microsim/output/MSInductLoop.h> 00038 #include "TraCIServerAPI_InductionLoop.h" 00039 00040 #ifdef CHECK_MEMORY_LEAKS 00041 #include <foreign/nvwa/debug_new.h> 00042 #endif // CHECK_MEMORY_LEAKS 00043 00044 00045 // =========================================================================== 00046 // used namespaces 00047 // =========================================================================== 00048 using namespace traci; 00049 00050 00051 // =========================================================================== 00052 // method definitions 00053 // =========================================================================== 00054 bool 00055 TraCIServerAPI_InductionLoop::processGet(TraCIServer& server, tcpip::Storage& inputStorage, 00056 tcpip::Storage& outputStorage) { 00057 std::string warning = ""; // additional description for response 00058 // variable & id 00059 int variable = inputStorage.readUnsignedByte(); 00060 std::string id = inputStorage.readString(); 00061 // check variable 00062 if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED 00063 && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY 00064 && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION 00065 && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT 00066 && variable != VAR_POSITION && variable != VAR_LANE_ID) { 00067 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Get Induction Loop Variable: unsupported variable specified", outputStorage); 00068 return false; 00069 } 00070 // begin response building 00071 tcpip::Storage tempMsg; 00072 // response-code, variableID, objectID 00073 tempMsg.writeUnsignedByte(RESPONSE_GET_INDUCTIONLOOP_VARIABLE); 00074 tempMsg.writeUnsignedByte(variable); 00075 tempMsg.writeString(id); 00076 // process request 00077 if (variable == ID_LIST) { 00078 std::vector<std::string> ids; 00079 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids); 00080 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00081 tempMsg.writeStringList(ids); 00082 } else if (variable == ID_COUNT) { 00083 std::vector<std::string> ids; 00084 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids); 00085 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00086 tempMsg.writeInt((int) ids.size()); 00087 } else { 00088 MSInductLoop* il = static_cast<MSInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id)); 00089 if (il == 0) { 00090 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Induction loop '" + id + "' is not known", outputStorage); 00091 return false; 00092 } 00093 switch (variable) { 00094 case ID_LIST: 00095 break; 00096 case LAST_STEP_VEHICLE_NUMBER: 00097 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00098 tempMsg.writeInt((int)(il->getCurrentPassedNumber())); 00099 break; 00100 case LAST_STEP_MEAN_SPEED: 00101 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00102 tempMsg.writeDouble(il->getCurrentSpeed()); 00103 break; 00104 case LAST_STEP_VEHICLE_ID_LIST: { 00105 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00106 std::vector<std::string> ids = il->getCurrentVehicleIDs(); 00107 tempMsg.writeStringList(ids); 00108 } 00109 break; 00110 case LAST_STEP_OCCUPANCY: 00111 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00112 tempMsg.writeDouble(il->getCurrentOccupancy()); 00113 break; 00114 case LAST_STEP_LENGTH: 00115 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00116 tempMsg.writeDouble(il->getCurrentLength()); 00117 break; 00118 case LAST_STEP_TIME_SINCE_DETECTION: 00119 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00120 tempMsg.writeDouble(il->getTimestepsSinceLastDetection()); 00121 break; 00122 case LAST_STEP_VEHICLE_DATA: { 00123 std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T); 00124 tempMsg.writeUnsignedByte(TYPE_COMPOUND); 00125 tcpip::Storage tempContent; 00126 unsigned int cnt = 0; 00127 tempContent.writeUnsignedByte(TYPE_INTEGER); 00128 tempContent.writeInt((int) vd.size()); 00129 ++cnt; 00130 for (unsigned int i = 0; i < vd.size(); ++i) { 00131 MSInductLoop::VehicleData& svd = vd[i]; 00132 tempContent.writeUnsignedByte(TYPE_STRING); 00133 tempContent.writeString(svd.idM); 00134 ++cnt; 00135 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00136 tempContent.writeDouble(svd.lengthM); 00137 ++cnt; 00138 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00139 tempContent.writeDouble(svd.entryTimeM); 00140 ++cnt; 00141 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00142 tempContent.writeDouble(svd.leaveTimeM); 00143 ++cnt; 00144 tempContent.writeUnsignedByte(TYPE_STRING); 00145 tempContent.writeString(svd.typeIDM); 00146 ++cnt; 00147 } 00148 00149 tempMsg.writeInt((int) cnt); 00150 tempMsg.writeStorage(tempContent); 00151 break; 00152 } 00153 case VAR_POSITION: 00154 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00155 tempMsg.writeDouble(il->getPosition()); 00156 break; 00157 case VAR_LANE_ID: 00158 tempMsg.writeUnsignedByte(TYPE_STRING); 00159 tempMsg.writeString(il->getLane()->getID()); 00160 break; 00161 default: 00162 break; 00163 } 00164 } 00165 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, warning, outputStorage); 00166 server.writeResponseWithLength(outputStorage, tempMsg); 00167 return true; 00168 } 00169 00170 #endif 00171 00172 00173 /****************************************************************************/ 00174