LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_POI.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 75.6 % 127 96
Test Date: 2025-06-03 15:29:59 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-2025 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/StorageHelper.h>
      31              : #include <libsumo/TraCIConstants.h>
      32              : #include "TraCIServerAPI_POI.h"
      33              : 
      34              : 
      35              : // ===========================================================================
      36              : // method definitions
      37              : // ===========================================================================
      38              : bool
      39         4397 : TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      40              :                                tcpip::Storage& outputStorage) {
      41         4397 :     const int variable = inputStorage.readUnsignedByte();
      42         4397 :     const std::string id = inputStorage.readString();
      43         4397 :     server.initWrapper(libsumo::RESPONSE_GET_POI_VARIABLE, variable, id);
      44              :     try {
      45         4397 :         if (!libsumo::POI::handleVariable(id, variable, &server, &inputStorage)) {
      46           15 :             return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      47              :         }
      48            2 :     } catch (libsumo::TraCIException& e) {
      49            2 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, e.what(), outputStorage);
      50            2 :     }
      51         4390 :     server.writeStatusCmd(libsumo::CMD_GET_POI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
      52         4390 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
      53              :     return true;
      54              : }
      55              : 
      56              : 
      57              : bool
      58          497 : TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
      59              :                                tcpip::Storage& outputStorage) {
      60          497 :     std::string warning = ""; // additional description for response
      61              :     // variable & id
      62          497 :     int variable = inputStorage.readUnsignedByte();
      63          497 :     std::string id = inputStorage.readString();
      64              :     // check variable
      65          497 :     if (variable != libsumo::VAR_TYPE &&
      66          497 :             variable != libsumo::VAR_COLOR &&
      67          485 :             variable != libsumo::VAR_POSITION &&
      68          485 :             variable != libsumo::VAR_WIDTH &&
      69          432 :             variable != libsumo::VAR_HEIGHT &&
      70          432 :             variable != libsumo::VAR_ANGLE &&
      71          426 :             variable != libsumo::VAR_IMAGEFILE &&
      72          426 :             variable != libsumo::VAR_HIGHLIGHT &&
      73          402 :             variable != libsumo::ADD &&
      74          327 :             variable != libsumo::REMOVE &&
      75              :             variable != libsumo::VAR_PARAMETER) {
      76            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
      77              :     }
      78              :     // process
      79              :     try {
      80          170 :         switch (variable) {
      81              :             case libsumo::VAR_TYPE: {
      82              :                 std::string type;
      83            6 :                 if (!server.readTypeCheckingString(inputStorage, type)) {
      84            3 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
      85              :                 }
      86            5 :                 libsumo::POI::setType(id, type);
      87              :             }
      88              :             break;
      89              :             case libsumo::VAR_COLOR: {
      90              :                 libsumo::TraCIColor col;
      91            6 :                 if (!server.readTypeCheckingColor(inputStorage, col)) {
      92            6 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
      93              :                 }
      94            5 :                 libsumo::POI::setColor(id, col);
      95              :             }
      96              :             break;
      97           50 :             case libsumo::VAR_POSITION: {
      98           50 :                 libsumo::TraCIPosition pos;
      99           50 :                 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
     100            6 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The position must be given using an according type.", outputStorage);
     101              :                 }
     102           49 :                 libsumo::POI::setPosition(id, pos.x, pos.y);
     103              :             }
     104              :             break;
     105            3 :             case libsumo::VAR_WIDTH: {
     106              :                 double width;
     107            3 :                 if (!server.readTypeCheckingDouble(inputStorage, width)) {
     108            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The width must be given using an according type.", outputStorage);
     109              :                 }
     110            3 :                 libsumo::POI::setWidth(id, width);
     111              :             }
     112            3 :             break;
     113            3 :             case libsumo::VAR_HEIGHT: {
     114              :                 double height;
     115            3 :                 if (!server.readTypeCheckingDouble(inputStorage, height)) {
     116            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The height must be given using an according type.", outputStorage);
     117              :                 }
     118            3 :                 libsumo::POI::setHeight(id, height);
     119              :             }
     120            3 :             break;
     121            3 :             case libsumo::VAR_ANGLE: {
     122              :                 double angle;
     123            3 :                 if (!server.readTypeCheckingDouble(inputStorage, angle)) {
     124            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The angle must be given using an according type.", outputStorage);
     125              :                 }
     126            3 :                 libsumo::POI::setAngle(id, angle);
     127              :             }
     128            3 :             break;
     129              :             case libsumo::VAR_IMAGEFILE: {
     130              :                 std::string imageFile;
     131            3 :                 if (!server.readTypeCheckingString(inputStorage, imageFile)) {
     132            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
     133              :                 }
     134            3 :                 libsumo::POI::setImageFile(id, imageFile);
     135              :             }
     136              :             break;
     137           21 :             case libsumo::VAR_HIGHLIGHT: {
     138              :                 // Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
     139           21 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     140            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for highlighting an object.", outputStorage);
     141              :                 }
     142           21 :                 const int itemNo = inputStorage.readInt();
     143           21 :                 if (itemNo > 5) {
     144            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
     145              :                 }
     146              :                 libsumo::TraCIColor col = libsumo::TraCIColor(255, 0, 0);
     147           21 :                 if (itemNo > 0) {
     148           21 :                     if (!server.readTypeCheckingColor(inputStorage, col)) {
     149            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first parameter for highlighting must be the highlight color.", outputStorage);
     150              :                     }
     151              :                 }
     152            0 :                 double size = -1;
     153           21 :                 if (itemNo > 1) {
     154           21 :                     if (!server.readTypeCheckingDouble(inputStorage, size)) {
     155            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second parameter for highlighting must be the highlight size.", outputStorage);
     156              :                     }
     157              :                 }
     158            0 :                 int alphaMax = -1;
     159           21 :                 if (itemNo > 2) {
     160           21 :                     if (!server.readTypeCheckingUnsignedByte(inputStorage, alphaMax)) {
     161            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third parameter for highlighting must be maximal alpha.", outputStorage);
     162              :                     }
     163              :                 }
     164            0 :                 double duration = -1;
     165           21 :                 if (itemNo > 3) {
     166           21 :                     if (!server.readTypeCheckingDouble(inputStorage, duration)) {
     167            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fourth parameter for highlighting must be the highlight duration.", outputStorage);
     168              :                     }
     169              :                 }
     170            0 :                 int type = 0;
     171           21 :                 if (itemNo > 4) {
     172           21 :                     if (!server.readTypeCheckingUnsignedByte(inputStorage, type)) {
     173            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fifth parameter for highlighting must be the highlight type id as ubyte.", outputStorage);
     174              :                     }
     175              :                 }
     176           21 :                 libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
     177              :             }
     178              :             break;
     179           66 :             case libsumo::ADD: {
     180           66 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     181            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
     182              :                 }
     183              :                 //read itemNo
     184           66 :                 const int parameterCount = inputStorage.readInt();
     185              :                 std::string type;
     186           66 :                 if (!server.readTypeCheckingString(inputStorage, type)) {
     187            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
     188              :                 }
     189              :                 libsumo::TraCIColor col;
     190           66 :                 if (!server.readTypeCheckingColor(inputStorage, col)) {
     191            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
     192              :                 }
     193           66 :                 int layer = StoHelp::readTypedInt(inputStorage, "The third PoI parameter must be the layer encoded as int.");
     194           66 :                 libsumo::TraCIPosition pos;
     195           66 :                 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
     196            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
     197              :                 }
     198           66 :                 if (parameterCount == 4) {
     199            2 :                     if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
     200            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
     201              :                     }
     202           65 :                 } else if (parameterCount >= 8) {
     203              :                     std::string imgFile;
     204           65 :                     if (!server.readTypeCheckingString(inputStorage, imgFile)) {
     205            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fifth PoI parameter must be the imgFile encoded as a string.", outputStorage);
     206              :                     }
     207              :                     double width;
     208           65 :                     if (!server.readTypeCheckingDouble(inputStorage, width)) {
     209            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The sixth PoI parameter must be the width encoded as a double.", outputStorage);
     210              :                     }
     211              :                     double height;
     212           65 :                     if (!server.readTypeCheckingDouble(inputStorage, height)) {
     213            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The seventh PoI parameter must be the height encoded as a double.", outputStorage);
     214              :                     }
     215              :                     double angle;
     216           65 :                     if (!server.readTypeCheckingDouble(inputStorage, angle)) {
     217            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The eigth PoI parameter must be the angle encoded as a double.", outputStorage);
     218              :                     }
     219              :                     std::string icon;
     220           65 :                     if (parameterCount == 9 && !server.readTypeCheckingString(inputStorage, icon)) {
     221            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The ninth PoI parameter must be the icon encoded as a string.", outputStorage);
     222              :                     }
     223              :                     //
     224           65 :                     if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle, icon)) {
     225            0 :                         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
     226              :                     }
     227              :                 } else {
     228            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE,
     229              :                                                       "Adding a PoI requires either only type, color, layer and position parameters or these and icon, imageFile, width, height and angle parameters.",
     230              :                                                       outputStorage);
     231              :                 }
     232              :             }
     233              :             break;
     234              :             case libsumo::REMOVE: {
     235              :                 // !!! layer not used yet (shouldn't the id be enough?)
     236            9 :                 const int layer = StoHelp::readTypedInt(inputStorage, "The layer must be given using an int.");
     237            8 :                 if (!libsumo::POI::remove(id, layer)) {
     238            3 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
     239              :                 }
     240              :             }
     241              :             break;
     242          327 :             case libsumo::VAR_PARAMETER: {
     243          327 :                 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
     244            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
     245              :                 }
     246              :                 //readt itemNo
     247          327 :                 inputStorage.readInt();
     248              :                 std::string name;
     249          327 :                 if (!server.readTypeCheckingString(inputStorage, name)) {
     250            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
     251              :                 }
     252              :                 std::string value;
     253          327 :                 if (!server.readTypeCheckingString(inputStorage, value)) {
     254            0 :                     return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
     255              :                 }
     256          327 :                 libsumo::POI::setParameter(id, name, value);
     257              :             }
     258              :             break;
     259              :             default:
     260              :                 break;
     261              :         }
     262            4 :     } catch (libsumo::TraCIException& e) {
     263            4 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
     264            4 :     }
     265          489 :     server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     266              :     return true;
     267              : }
     268              : 
     269              : 
     270              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1