SUMO - Simulation of Urban MObility

TraCIServerAPI_InductionLoop.cpp

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