SUMO - Simulation of Urban MObility
TraCIServerAPI_VehicleType.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00010 // APIs for getting/setting vehicle type values via TraCI
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #ifndef NO_TRACI
00035 
00036 #include <limits>
00037 #include <microsim/MSNet.h>
00038 #include <microsim/MSVehicleType.h>
00039 #include "TraCIConstants.h"
00040 #include "TraCIServerAPI_VehicleType.h"
00041 
00042 #ifdef CHECK_MEMORY_LEAKS
00043 #include <foreign/nvwa/debug_new.h>
00044 #endif // CHECK_MEMORY_LEAKS
00045 
00046 
00047 // ===========================================================================
00048 // used namespaces
00049 // ===========================================================================
00050 using namespace traci;
00051 
00052 
00053 // ===========================================================================
00054 // method definitions
00055 // ===========================================================================
00056 bool
00057 TraCIServerAPI_VehicleType::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
00058                                        tcpip::Storage& outputStorage) {
00059     std::string warning = ""; // additional description for response
00060     // variable & id
00061     int variable = inputStorage.readUnsignedByte();
00062     std::string id = inputStorage.readString();
00063     // check variable
00064     if (variable != ID_LIST && variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_ACCEL && variable != VAR_DECEL
00065             && variable != VAR_TAU && variable != VAR_VEHICLECLASS && variable != VAR_EMISSIONCLASS && variable != VAR_SHAPECLASS
00066             && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_IMPERFECTION
00067             && variable != VAR_MINGAP && variable != VAR_WIDTH && variable != VAR_COLOR && variable != ID_COUNT) {
00068         server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Get Vehicle Type Variable: unsupported variable specified", outputStorage);
00069         return false;
00070     }
00071     // begin response building
00072     tcpip::Storage tempMsg;
00073     //  response-code, variableID, objectID
00074     tempMsg.writeUnsignedByte(RESPONSE_GET_VEHICLETYPE_VARIABLE);
00075     tempMsg.writeUnsignedByte(variable);
00076     tempMsg.writeString(id);
00077     // process request
00078     if (variable == ID_LIST) {
00079         std::vector<std::string> ids;
00080         MSNet::getInstance()->getVehicleControl().insertVTypeIDs(ids);
00081         tempMsg.writeUnsignedByte(TYPE_STRINGLIST);
00082         tempMsg.writeStringList(ids);
00083     } else if (variable == ID_COUNT) {
00084         std::vector<std::string> ids;
00085         MSNet::getInstance()->getVehicleControl().insertVTypeIDs(ids);
00086         tempMsg.writeUnsignedByte(TYPE_INTEGER);
00087         tempMsg.writeInt((int) ids.size());
00088     } else {
00089         MSVehicleType* v = MSNet::getInstance()->getVehicleControl().getVType(id);
00090         if (v == 0) {
00091             server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
00092             return false;
00093         }
00094         getVariable(variable, *v, tempMsg);
00095     }
00096     server.writeStatusCmd(CMD_GET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
00097     server.writeResponseWithLength(outputStorage, tempMsg);
00098     return true;
00099 }
00100 
00101 bool
00102 TraCIServerAPI_VehicleType::getVariable(const int variable, const MSVehicleType& v, tcpip::Storage& tempMsg) {
00103     switch (variable) {
00104         case VAR_LENGTH:
00105             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00106             tempMsg.writeDouble(v.getLength());
00107             break;
00108         case VAR_MINGAP:
00109             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00110             tempMsg.writeDouble(v.getMinGap());
00111             break;
00112         case VAR_MAXSPEED:
00113             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00114             tempMsg.writeDouble(v.getMaxSpeed());
00115             break;
00116         case VAR_ACCEL:
00117             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00118             tempMsg.writeDouble(v.getCarFollowModel().getMaxAccel());
00119             break;
00120         case VAR_DECEL:
00121             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00122             tempMsg.writeDouble(v.getCarFollowModel().getMaxDecel());
00123             break;
00124         case VAR_IMPERFECTION:
00125             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00126             tempMsg.writeDouble(v.getCarFollowModel().getImperfection());
00127             break;
00128         case VAR_TAU:
00129             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00130             tempMsg.writeDouble(v.getCarFollowModel().getHeadwayTime());
00131             break;
00132         case VAR_SPEED_FACTOR:
00133             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00134             tempMsg.writeDouble(v.getSpeedFactor());
00135             break;
00136         case VAR_SPEED_DEVIATION:
00137             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00138             tempMsg.writeDouble(v.getSpeedDeviation());
00139             break;
00140         case VAR_VEHICLECLASS:
00141             tempMsg.writeUnsignedByte(TYPE_STRING);
00142             tempMsg.writeString(toString(v.getVehicleClass()));
00143             break;
00144         case VAR_EMISSIONCLASS:
00145             tempMsg.writeUnsignedByte(TYPE_STRING);
00146             tempMsg.writeString(getVehicleEmissionTypeName(v.getEmissionClass()));
00147             break;
00148         case VAR_SHAPECLASS:
00149             tempMsg.writeUnsignedByte(TYPE_STRING);
00150             tempMsg.writeString(getVehicleShapeName(v.getGuiShape()));
00151             break;
00152         case VAR_WIDTH:
00153             tempMsg.writeUnsignedByte(TYPE_DOUBLE);
00154             tempMsg.writeDouble(v.getWidth());
00155             break;
00156         case VAR_COLOR:
00157             tempMsg.writeUnsignedByte(TYPE_COLOR);
00158             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().red() * 255. + 0.5));
00159             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().green() * 255. + 0.5));
00160             tempMsg.writeUnsignedByte(static_cast<int>(v.getColor().blue() * 255. + 0.5));
00161             tempMsg.writeUnsignedByte(255);
00162             break;
00163         default:
00164             break;
00165     }
00166     return true;
00167 }
00168 
00169 bool
00170 TraCIServerAPI_VehicleType::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
00171                                        tcpip::Storage& outputStorage) {
00172     std::string warning = ""; // additional description for response
00173     // variable
00174     int variable = inputStorage.readUnsignedByte();
00175     if (variable != VAR_LENGTH && variable != VAR_MAXSPEED && variable != VAR_VEHICLECLASS
00176             && variable != VAR_SPEED_FACTOR && variable != VAR_SPEED_DEVIATION && variable != VAR_EMISSIONCLASS
00177             && variable != VAR_WIDTH && variable != VAR_MINGAP && variable != VAR_SHAPECLASS
00178             && variable != VAR_ACCEL && variable != VAR_DECEL && variable != VAR_IMPERFECTION
00179             && variable != VAR_TAU && variable != VAR_COLOR
00180        ) {
00181         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Change Vehicle Type State: unsupported variable specified", outputStorage);
00182         return false;
00183     }
00184     // id
00185     std::string id = inputStorage.readString();
00186     MSVehicleType* v = MSNet::getInstance()->getVehicleControl().getVType(id);
00187     if (v == 0) {
00188         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, "Vehicle type '" + id + "' is not known", outputStorage);
00189         return false;
00190     }
00191     // process
00192     try {
00193         if (setVariable(CMD_SET_VEHICLETYPE_VARIABLE, variable, inputStorage.readUnsignedByte(),
00194                         *v, server, inputStorage, outputStorage)) {
00195             server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_OK, warning, outputStorage);
00196             return true;
00197         }
00198     } catch (ProcessError& e) {
00199         server.writeStatusCmd(CMD_SET_VEHICLETYPE_VARIABLE, RTYPE_ERR, e.what(), outputStorage);
00200     }
00201     return false;
00202 }
00203 
00204 
00205 bool
00206 TraCIServerAPI_VehicleType::setVariable(const int cmd, const int variable, const int valueDataType,
00207                                         MSVehicleType& v, traci::TraCIServer& server,
00208                                         tcpip::Storage& inputStorage, tcpip::Storage& outputStorage) {
00209     switch (variable) {
00210         case VAR_LENGTH: {
00211             if (valueDataType != TYPE_DOUBLE) {
00212                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting length requires a double.", outputStorage);
00213                 return false;
00214             }
00215             double val = inputStorage.readDouble();
00216             if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
00217                 server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid length.", outputStorage);
00218                 return false;
00219             }
00220             v.setLength(val);
00221         }
00222         break;
00223         case VAR_MAXSPEED: {
00224             if (valueDataType != TYPE_DOUBLE) {
00225                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting maximum speed requires a double.", outputStorage);
00226                 return false;
00227             }
00228             double val = inputStorage.readDouble();
00229             if (val == 0.0 || fabs(val) == std::numeric_limits<double>::infinity()) {
00230                 server.writeStatusCmd(cmd, RTYPE_ERR, "Invalid maximum speed.", outputStorage);
00231                 return false;
00232             }
00233             v.setMaxSpeed(val);
00234         }
00235         break;
00236         case VAR_VEHICLECLASS: {
00237             if (valueDataType != TYPE_STRING) {
00238                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle class requires a string.", outputStorage);
00239                 return false;
00240             }
00241             v.setVClass(getVehicleClassID(inputStorage.readString()));
00242         }
00243         break;
00244         case VAR_SPEED_FACTOR: {
00245             if (valueDataType != TYPE_DOUBLE) {
00246                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed factor requires a double.", outputStorage);
00247                 return false;
00248             }
00249             v.setSpeedFactor(inputStorage.readDouble());
00250         }
00251         break;
00252         case VAR_SPEED_DEVIATION: {
00253             if (valueDataType != TYPE_DOUBLE) {
00254                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting speed deviation requires a double.", outputStorage);
00255                 return false;
00256             }
00257             v.setSpeedDeviation(inputStorage.readDouble());
00258         }
00259         break;
00260         case VAR_EMISSIONCLASS: {
00261             if (valueDataType != TYPE_STRING) {
00262                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting emission class requires a string.", outputStorage);
00263                 return false;
00264             }
00265             v.setEmissionClass(getVehicleEmissionTypeID(inputStorage.readString()));
00266         }
00267         break;
00268         case VAR_WIDTH: {
00269             if (valueDataType != TYPE_DOUBLE) {
00270                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting width requires a double.", outputStorage);
00271                 return false;
00272             }
00273             v.setWidth(inputStorage.readDouble());
00274         }
00275         break;
00276         case VAR_MINGAP: {
00277             if (valueDataType != TYPE_DOUBLE) {
00278                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting minimum gap requires a double.", outputStorage);
00279                 return false;
00280             }
00281             v.setMinGap(inputStorage.readDouble());
00282         }
00283         break;
00284         case VAR_SHAPECLASS: {
00285             if (valueDataType != TYPE_STRING) {
00286                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting vehicle shape requires a string.", outputStorage);
00287                 return false;
00288             }
00289             v.setShape(getVehicleShapeID(inputStorage.readString()));
00290         }
00291         break;
00292         case VAR_ACCEL: {
00293             if (valueDataType != TYPE_DOUBLE) {
00294                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting acceleration requires a double.", outputStorage);
00295                 return false;
00296             }
00297             v.getCarFollowModel().setMaxAccel(inputStorage.readDouble());
00298         }
00299         break;
00300         case VAR_DECEL: {
00301             if (valueDataType != TYPE_DOUBLE) {
00302                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting deceleration requires a double.", outputStorage);
00303                 return false;
00304             }
00305             v.getCarFollowModel().setMaxDecel(inputStorage.readDouble());
00306         }
00307         break;
00308         case VAR_IMPERFECTION: {
00309             if (valueDataType != TYPE_DOUBLE) {
00310                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting driver imperfection requires a double.", outputStorage);
00311                 return false;
00312             }
00313             v.getCarFollowModel().setImperfection(inputStorage.readDouble());
00314         }
00315         break;
00316         case VAR_TAU: {
00317             if (valueDataType != TYPE_DOUBLE) {
00318                 server.writeStatusCmd(cmd, RTYPE_ERR, "Setting headway time requires a double.", outputStorage);
00319                 return false;
00320             }
00321             v.getCarFollowModel().setHeadwayTime(inputStorage.readDouble());
00322         }
00323         break;
00324         case VAR_COLOR: {
00325             if (valueDataType != TYPE_COLOR) {
00326                 server.writeStatusCmd(cmd, RTYPE_ERR, "The color must be given using the according type.", outputStorage);
00327                 return false;
00328             }
00329             SUMOReal r = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00330             SUMOReal g = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00331             SUMOReal b = (SUMOReal) inputStorage.readUnsignedByte() / 255.;
00332             inputStorage.readUnsignedByte(); // skip alpha level
00333             RGBColor col(r, g, b);
00334             v.setColor(col);
00335         }
00336         break;
00337         default:
00338             break;
00339     }
00340     return true;
00341 }
00342 
00343 #endif
00344 
00345 
00346 /****************************************************************************/
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines