|
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 #include "TraCIConstants.h" 00034 #include <microsim/output/MSDetectorControl.h> 00035 #include <microsim/output/MSInductLoop.h> 00036 #include "TraCIServerAPI_InductionLoop.h" 00037 00038 #ifdef CHECK_MEMORY_LEAKS 00039 #include <foreign/nvwa/debug_new.h> 00040 #endif // CHECK_MEMORY_LEAKS 00041 00042 00043 // =========================================================================== 00044 // used namespaces 00045 // =========================================================================== 00046 using namespace traci; 00047 00048 00049 // =========================================================================== 00050 // method definitions 00051 // =========================================================================== 00052 bool 00053 TraCIServerAPI_InductionLoop::processGet(TraCIServer& server, tcpip::Storage& inputStorage, 00054 tcpip::Storage& outputStorage) { 00055 std::string warning = ""; // additional description for response 00056 // variable & id 00057 int variable = inputStorage.readUnsignedByte(); 00058 std::string id = inputStorage.readString(); 00059 // check variable 00060 if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED 00061 && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY 00062 && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION 00063 && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT 00064 && variable != VAR_POSITION && variable != VAR_LANE_ID) { 00065 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Get Induction Loop Variable: unsupported variable specified", outputStorage); 00066 return false; 00067 } 00068 // begin response building 00069 tcpip::Storage tempMsg; 00070 // response-code, variableID, objectID 00071 tempMsg.writeUnsignedByte(RESPONSE_GET_INDUCTIONLOOP_VARIABLE); 00072 tempMsg.writeUnsignedByte(variable); 00073 tempMsg.writeString(id); 00074 // process request 00075 if (variable == ID_LIST) { 00076 std::vector<std::string> ids; 00077 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids); 00078 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00079 tempMsg.writeStringList(ids); 00080 } else if (variable == ID_COUNT) { 00081 std::vector<std::string> ids; 00082 MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids); 00083 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00084 tempMsg.writeInt((int) ids.size()); 00085 } else { 00086 MSInductLoop* il = static_cast<MSInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id)); 00087 if (il == 0) { 00088 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_ERR, "Induction loop '" + id + "' is not known", outputStorage); 00089 return false; 00090 } 00091 switch (variable) { 00092 case ID_LIST: 00093 break; 00094 case LAST_STEP_VEHICLE_NUMBER: 00095 tempMsg.writeUnsignedByte(TYPE_INTEGER); 00096 tempMsg.writeInt((int)(il->getCurrentPassedNumber())); 00097 break; 00098 case LAST_STEP_MEAN_SPEED: 00099 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00100 tempMsg.writeDouble(il->getCurrentSpeed()); 00101 break; 00102 case LAST_STEP_VEHICLE_ID_LIST: { 00103 tempMsg.writeUnsignedByte(TYPE_STRINGLIST); 00104 std::vector<std::string> ids = il->getCurrentVehicleIDs(); 00105 tempMsg.writeStringList(ids); 00106 } 00107 break; 00108 case LAST_STEP_OCCUPANCY: 00109 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00110 tempMsg.writeDouble(il->getCurrentOccupancy()); 00111 break; 00112 case LAST_STEP_LENGTH: 00113 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00114 tempMsg.writeDouble(il->getCurrentLength()); 00115 break; 00116 case LAST_STEP_TIME_SINCE_DETECTION: 00117 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00118 tempMsg.writeDouble(il->getTimestepsSinceLastDetection()); 00119 break; 00120 case LAST_STEP_VEHICLE_DATA: { 00121 std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T); 00122 tempMsg.writeUnsignedByte(TYPE_COMPOUND); 00123 tcpip::Storage tempContent; 00124 unsigned int cnt = 0; 00125 tempContent.writeUnsignedByte(TYPE_INTEGER); 00126 tempContent.writeInt((int) vd.size()); 00127 ++cnt; 00128 for (unsigned int i = 0; i < vd.size(); ++i) { 00129 MSInductLoop::VehicleData& svd = vd[i]; 00130 tempContent.writeUnsignedByte(TYPE_STRING); 00131 tempContent.writeString(svd.idM); 00132 ++cnt; 00133 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00134 tempContent.writeDouble(svd.lengthM); 00135 ++cnt; 00136 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00137 tempContent.writeDouble(svd.entryTimeM); 00138 ++cnt; 00139 tempContent.writeUnsignedByte(TYPE_DOUBLE); 00140 tempContent.writeDouble(svd.leaveTimeM); 00141 ++cnt; 00142 tempContent.writeUnsignedByte(TYPE_STRING); 00143 tempContent.writeString(svd.typeIDM); 00144 ++cnt; 00145 } 00146 00147 tempMsg.writeInt((int) cnt); 00148 tempMsg.writeStorage(tempContent); 00149 break; 00150 } 00151 case VAR_POSITION: 00152 tempMsg.writeUnsignedByte(TYPE_DOUBLE); 00153 tempMsg.writeDouble(il->getPosition()); 00154 break; 00155 case VAR_LANE_ID: 00156 tempMsg.writeUnsignedByte(TYPE_STRING); 00157 tempMsg.writeString(il->getLane()->getID()); 00158 break; 00159 default: 00160 break; 00161 } 00162 } 00163 server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, warning, outputStorage); 00164 server.writeResponseWithLength(outputStorage, tempMsg); 00165 return true; 00166 } 00167 00168 00169 /****************************************************************************/ 00170