LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_POI.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 75.9 % 133 101
Test Date: 2024-11-20 15:55:46 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2017-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    TraCIServerAPI_POI.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Laura Bieker
      17              : /// @author  Michael Behrisch
      18              : /// @author  Jakob Erdmann
      19              : /// @author  Robert Hilbrich
      20              : /// @date    07.05.2009
      21              : ///
      22              : // APIs for getting/setting POI values via TraCI
      23              : /****************************************************************************/
      24              : #include <config.h>
      25              : 
      26              : #include <microsim/MSNet.h>
      27              : #include <utils/shapes/PointOfInterest.h>
      28              : #include <utils/shapes/ShapeContainer.h>
      29              : #include <libsumo/POI.h>
      30              : #include <libsumo/TraCIConstants.h>
      31              : #include "TraCIServerAPI_POI.h"
      32              : 
      33              : 
      34              : // ===========================================================================
      35              : // method definitions
      36              : // ===========================================================================
      37              : bool
      38         4304 : TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      39              :                                tcpip::Storage& outputStorage) {
      40         4304 :     const int variable = inputStorage.readUnsignedByte();
      41         4304 :     const std::string id = inputStorage.readString();
      42         4304 :     server.initWrapper(libsumo::RESPONSE_GET_POI_VARIABLE, variable, id);
      43              :     try {
      44         4304 :         if (!libsumo::POI::handleVariable(id, variable, &server, &inputStorage)) {
      45            6 :             return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      46              :         }
      47            2 :     } catch (libsumo::TraCIException& e) {
      48            2 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, e.what(), outputStorage);
      49            2 :     }
      50         4300 :     server.writeStatusCmd(libsumo::CMD_GET_POI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      51         4300 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      52              :     return true;
      53              : }
      54              : 
      55              : 
      56              : bool
      57          560 : TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      58              :                                tcpip::Storage& outputStorage) {
      59          560 :     std::string warning = ""; // additional description for response
      60              :     // variable & id
      61          560 :     int variable = inputStorage.readUnsignedByte();
      62          560 :     std::string id = inputStorage.readString();
      63              :     // check variable
      64          560 :     if (variable != libsumo::VAR_TYPE &&
      65          560 :             variable != libsumo::VAR_COLOR &&
      66          548 :             variable != libsumo::VAR_POSITION &&
      67          548 :             variable != libsumo::VAR_WIDTH &&
      68          486 :             variable != libsumo::VAR_HEIGHT &&
      69          486 :             variable != libsumo::VAR_ANGLE &&
      70          480 :             variable != libsumo::VAR_IMAGEFILE &&
      71          480 :             variable != libsumo::VAR_HIGHLIGHT &&
      72          456 :             variable != libsumo::ADD &&
      73          381 :             variable != libsumo::REMOVE &&
      74              :             variable != libsumo::VAR_PARAMETER) {
      75            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      76              :     }
      77              :     // process
      78              :     try {
      79          179 :         switch (variable) {
      80              :             case libsumo::VAR_TYPE: {
      81              :                 std::string type;
      82            6 :                 if (!server.readTypeCheckingString(inputStorage, type)) {
      83            3 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
      84              :                 }
      85            5 :                 libsumo::POI::setType(id, type);
      86              :             }
      87              :             break;
      88              :             case libsumo::VAR_COLOR: {
      89              :                 libsumo::TraCIColor col;
      90            6 :                 if (!server.readTypeCheckingColor(inputStorage, col)) {
      91            5 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
      92              :                 }
      93            5 :                 libsumo::POI::setColor(id, col);
      94              :             }
      95              :             break;
      96           59 :             case libsumo::VAR_POSITION: {
      97           59 :                 libsumo::TraCIPosition pos;
      98           59 :                 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
      99            5 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The position must be given using an according type.", outputStorage);
     100              :                 }
     101           58 :                 libsumo::POI::setPosition(id, pos.x, pos.y);
     102              :             }
     103              :             break;
     104            3 :             case libsumo::VAR_WIDTH: {
     105              :                 double width;
     106            3 :                 if (!server.readTypeCheckingDouble(inputStorage, width)) {
     107            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The width must be given using an according type.", outputStorage);
     108              :                 }
     109            3 :                 libsumo::POI::setWidth(id, width);
     110              :             }
     111            3 :             break;
     112            3 :             case libsumo::VAR_HEIGHT: {
     113              :                 double height;
     114            3 :                 if (!server.readTypeCheckingDouble(inputStorage, height)) {
     115            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The height must be given using an according type.", outputStorage);
     116              :                 }
     117            3 :                 libsumo::POI::setHeight(id, height);
     118              :             }
     119            3 :             break;
     120            3 :             case libsumo::VAR_ANGLE: {
     121              :                 double angle;
     122            3 :                 if (!server.readTypeCheckingDouble(inputStorage, angle)) {
     123            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The angle must be given using an according type.", outputStorage);
     124              :                 }
     125            3 :                 libsumo::POI::setAngle(id, angle);
     126              :             }
     127            3 :             break;
     128              :             case libsumo::VAR_IMAGEFILE: {
     129              :                 std::string imageFile;
     130            3 :                 if (!server.readTypeCheckingString(inputStorage, imageFile)) {
     131            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
     132              :                 }
     133            3 :                 libsumo::POI::setImageFile(id, imageFile);
     134              :             }
     135              :             break;
     136           21 :             case libsumo::VAR_HIGHLIGHT: {
     137              :                 // Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
     138           21 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     139            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for highlighting an object.", outputStorage);
     140              :                 }
     141           21 :                 const int itemNo = inputStorage.readInt();
     142           21 :                 if (itemNo > 5) {
     143            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
     144              :                 }
     145              :                 libsumo::TraCIColor col = libsumo::TraCIColor(255, 0, 0);
     146           21 :                 if (itemNo > 0) {
     147           21 :                     if (!server.readTypeCheckingColor(inputStorage, col)) {
     148            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first parameter for highlighting must be the highlight color.", outputStorage);
     149              :                     }
     150              :                 }
     151            0 :                 double size = -1;
     152           21 :                 if (itemNo > 1) {
     153           21 :                     if (!server.readTypeCheckingDouble(inputStorage, size)) {
     154            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second parameter for highlighting must be the highlight size.", outputStorage);
     155              :                     }
     156              :                 }
     157            0 :                 int alphaMax = -1;
     158           21 :                 if (itemNo > 2) {
     159           21 :                     if (!server.readTypeCheckingUnsignedByte(inputStorage, alphaMax)) {
     160            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third parameter for highlighting must be maximal alpha.", outputStorage);
     161              :                     }
     162              :                 }
     163            0 :                 double duration = -1;
     164           21 :                 if (itemNo > 3) {
     165           21 :                     if (!server.readTypeCheckingDouble(inputStorage, duration)) {
     166            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fourth parameter for highlighting must be the highlight duration.", outputStorage);
     167              :                     }
     168              :                 }
     169            0 :                 int type = 0;
     170           21 :                 if (itemNo > 4) {
     171           21 :                     if (!server.readTypeCheckingUnsignedByte(inputStorage, type)) {
     172            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fifth parameter for highlighting must be the highlight type id as ubyte.", outputStorage);
     173              :                     }
     174              :                 }
     175           21 :                 libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
     176              :             }
     177              :             break;
     178           66 :             case libsumo::ADD: {
     179           66 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     180            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
     181              :                 }
     182              :                 //read itemNo
     183           66 :                 const int parameterCount = inputStorage.readInt();
     184              :                 std::string type;
     185           66 :                 if (!server.readTypeCheckingString(inputStorage, type)) {
     186            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
     187              :                 }
     188              :                 libsumo::TraCIColor col;
     189           66 :                 if (!server.readTypeCheckingColor(inputStorage, col)) {
     190            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
     191              :                 }
     192           66 :                 int layer = 0;
     193           66 :                 if (!server.readTypeCheckingInt(inputStorage, layer)) {
     194            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
     195              :                 }
     196           66 :                 libsumo::TraCIPosition pos;
     197           66 :                 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
     198            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
     199              :                 }
     200           66 :                 if (parameterCount == 4) {
     201            2 :                     if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
     202            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
     203              :                     }
     204           65 :                 } else if (parameterCount >= 8) {
     205              :                     std::string imgFile;
     206           65 :                     if (!server.readTypeCheckingString(inputStorage, imgFile)) {
     207            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fifth PoI parameter must be the imgFile encoded as a string.", outputStorage);
     208              :                     }
     209              :                     double width;
     210           65 :                     if (!server.readTypeCheckingDouble(inputStorage, width)) {
     211            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The sixth PoI parameter must be the width encoded as a double.", outputStorage);
     212              :                     }
     213              :                     double height;
     214           65 :                     if (!server.readTypeCheckingDouble(inputStorage, height)) {
     215            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The seventh PoI parameter must be the height encoded as a double.", outputStorage);
     216              :                     }
     217              :                     double angle;
     218           65 :                     if (!server.readTypeCheckingDouble(inputStorage, angle)) {
     219            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The eigth PoI parameter must be the angle encoded as a double.", outputStorage);
     220              :                     }
     221              :                     std::string icon;
     222           65 :                     if (parameterCount == 9 && !server.readTypeCheckingString(inputStorage, icon)) {
     223            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The ninth PoI parameter must be the icon encoded as a string.", outputStorage);
     224              :                     }
     225              :                     //
     226           65 :                     if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle, icon)) {
     227            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
     228              :                     }
     229              :                 } else {
     230            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE,
     231              :                                                       "Adding a PoI requires either only type, color, layer and position parameters or these and icon, imageFile, width, height and angle parameters.",
     232              :                                                       outputStorage);
     233              :                 }
     234              :             }
     235              :             break;
     236            9 :             case libsumo::REMOVE: {
     237            9 :                 int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
     238            9 :                 if (!server.readTypeCheckingInt(inputStorage, layer)) {
     239            2 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
     240              :                 }
     241            8 :                 if (!libsumo::POI::remove(id, layer)) {
     242            3 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
     243              :                 }
     244              :             }
     245            7 :             break;
     246          381 :             case libsumo::VAR_PARAMETER: {
     247          381 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     248            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
     249              :                 }
     250              :                 //readt itemNo
     251          381 :                 inputStorage.readInt();
     252              :                 std::string name;
     253          381 :                 if (!server.readTypeCheckingString(inputStorage, name)) {
     254            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
     255              :                 }
     256              :                 std::string value;
     257          381 :                 if (!server.readTypeCheckingString(inputStorage, value)) {
     258            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
     259              :                 }
     260          381 :                 libsumo::POI::setParameter(id, name, value);
     261              :             }
     262              :             break;
     263              :             default:
     264              :                 break;
     265              :         }
     266            3 :     } catch (libsumo::TraCIException& e) {
     267            3 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
     268            3 :     }
     269          552 :     server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     270              :     return true;
     271              : }
     272              : 
     273              : 
     274              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1