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 2001-2010 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 "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 // used namespaces
00043 // ===========================================================================
00044 using namespace std;
00045 using namespace traci;
00046 using namespace tcpip;
00047 
00048 
00049 // ===========================================================================
00050 // method definitions
00051 // ===========================================================================
00052 bool
00053 TraCIServerAPI_InductionLoop::processGet(tcpip::Storage &inputStorage,
00054         tcpip::Storage &outputStorage,
00055         bool withStatus) throw(TraCIException) {
00056     std::string warning = ""; // additional description for response
00057     // variable & id
00058     int variable = inputStorage.readUnsignedByte();
00059     std::string id = inputStorage.readString();
00060     // check variable
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     // begin response building
00068     Storage tempMsg;
00069     //  response-code, variableID, objectID
00070     tempMsg.writeUnsignedByte(RESPONSE_GET_INDUCTIONLOOP_VARIABLE);
00071     tempMsg.writeUnsignedByte(variable);
00072     tempMsg.writeString(id);
00073     // process request
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     // send response
00122     outputStorage.writeUnsignedByte(0); // command length -> extended
00123     outputStorage.writeInt(1 + 4 + tempMsg.size());
00124     outputStorage.writeStorage(tempMsg);
00125     return true;
00126 }
00127 
00128 
00129 /****************************************************************************/
00130 

Generated on Wed May 5 00:06:37 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6