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 497 : TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
40 : tcpip::Storage& outputStorage) {
41 497 : std::string warning = ""; // additional description for response
42 : // variable & id
43 497 : int variable = inputStorage.readUnsignedByte();
44 497 : std::string id = inputStorage.readString();
45 : // check variable
46 497 : if (variable != libsumo::VAR_TYPE &&
47 497 : variable != libsumo::VAR_COLOR &&
48 485 : variable != libsumo::VAR_POSITION &&
49 485 : variable != libsumo::VAR_WIDTH &&
50 432 : variable != libsumo::VAR_HEIGHT &&
51 432 : variable != libsumo::VAR_ANGLE &&
52 426 : variable != libsumo::VAR_IMAGEFILE &&
53 426 : variable != libsumo::VAR_HIGHLIGHT &&
54 402 : variable != libsumo::ADD &&
55 327 : variable != libsumo::REMOVE &&
56 : variable != libsumo::VAR_PARAMETER) {
57 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58 : }
59 : // process
60 : try {
61 170 : switch (variable) {
62 : case libsumo::VAR_TYPE: {
63 6 : libsumo::POI::setType(id, StoHelp::readTypedString(inputStorage, "The type must be given as a string."));
64 : }
65 4 : break;
66 : case libsumo::VAR_COLOR: {
67 6 : libsumo::POI::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
68 : }
69 4 : break;
70 : case libsumo::VAR_POSITION: {
71 57 : const libsumo::TraCIPosition pos = StoHelp::readTypedPosition2D(inputStorage, "The position must be given using an according type.");
72 49 : libsumo::POI::setPosition(id, pos.x, pos.y);
73 : }
74 48 : break;
75 : case libsumo::VAR_WIDTH: {
76 3 : libsumo::POI::setWidth(id, StoHelp::readTypedDouble(inputStorage, "The width must be given as a double."));
77 : }
78 3 : break;
79 : case libsumo::VAR_HEIGHT: {
80 3 : libsumo::POI::setHeight(id, StoHelp::readTypedDouble(inputStorage, "The height must be given as a double."));
81 : }
82 3 : break;
83 : case libsumo::VAR_ANGLE: {
84 3 : libsumo::POI::setAngle(id, StoHelp::readTypedDouble(inputStorage, "The angle must be given as a double."));
85 : }
86 3 : break;
87 : case libsumo::VAR_IMAGEFILE: {
88 3 : libsumo::POI::setImageFile(id, StoHelp::readTypedString(inputStorage, "The image file must be given as a string."));
89 : }
90 3 : break;
91 : case libsumo::VAR_HIGHLIGHT: {
92 21 : const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for highlighting an object.");
93 21 : if (parameterCount > 5) {
94 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
95 : }
96 : libsumo::TraCIColor col = libsumo::TraCIColor(255, 0, 0);
97 21 : if (parameterCount > 0) {
98 21 : col = StoHelp::readTypedColor(inputStorage, "The first parameter for highlighting must be the highlight color.");
99 : }
100 : double size = -1;
101 21 : if (parameterCount > 1) {
102 21 : size = StoHelp::readTypedDouble(inputStorage, "The second parameter for highlighting must be the highlight size.");
103 : }
104 : int alphaMax = -1;
105 21 : if (parameterCount > 2) {
106 21 : alphaMax = StoHelp::readTypedUnsignedByte(inputStorage, "The third parameter for highlighting must be maximal alpha.");
107 : }
108 : double duration = -1;
109 21 : if (parameterCount > 3) {
110 21 : duration = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for highlighting must be the highlight duration.");
111 : }
112 : int type = 0;
113 21 : if (parameterCount > 4) {
114 49 : type = StoHelp::readTypedUnsignedByte(inputStorage, "The fifth parameter for highlighting must be the highlight type id as ubyte.");
115 : }
116 21 : libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
117 : }
118 21 : break;
119 : case libsumo::ADD: {
120 66 : const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for adding a new PoI.");
121 66 : const std::string type = StoHelp::readTypedString(inputStorage, "The first PoI parameter must be the type encoded as a string.");
122 66 : libsumo::TraCIColor col = StoHelp::readTypedColor(inputStorage, "The second PoI parameter must be the color.");
123 66 : const int layer = StoHelp::readTypedInt(inputStorage, "The third PoI parameter must be the layer encoded as int.");
124 66 : const libsumo::TraCIPosition pos = StoHelp::readTypedPosition2D(inputStorage, "The fourth PoI parameter must be the position.");
125 66 : if (parameterCount == 4) {
126 2 : if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
127 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
128 : }
129 65 : } else if (parameterCount >= 8) {
130 65 : const std::string imgFile = StoHelp::readTypedString(inputStorage, "The fifth PoI parameter must be the imgFile encoded as a string.");
131 65 : const double width = StoHelp::readTypedDouble(inputStorage, "The sixth PoI parameter must be the width encoded as a double.");
132 65 : const double height = StoHelp::readTypedDouble(inputStorage, "The seventh PoI parameter must be the height encoded as a double.");
133 130 : const double angle = StoHelp::readTypedDouble(inputStorage, "The eighth PoI parameter must be the angle encoded as a double.");
134 : std::string icon;
135 65 : if (parameterCount == 9) {
136 130 : icon = StoHelp::readTypedString(inputStorage, "The ninth PoI parameter must be the icon encoded as a string.");
137 : }
138 65 : if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle, icon)) {
139 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
140 : }
141 : } else {
142 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE,
143 : "Adding a PoI requires either only type, color, layer and position parameters or these and icon, imageFile, width, height and angle parameters.",
144 : outputStorage);
145 : }
146 : }
147 : break;
148 : case libsumo::REMOVE: {
149 9 : const int layer = StoHelp::readTypedInt(inputStorage, "The layer must be given using an int.");
150 8 : if (!libsumo::POI::remove(id, layer)) {
151 3 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
152 : }
153 : }
154 : break;
155 : case libsumo::VAR_PARAMETER: {
156 327 : StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
157 327 : const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
158 327 : const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
159 327 : libsumo::POI::setParameter(id, name, value);
160 : }
161 327 : break;
162 : default:
163 : break;
164 : }
165 7 : } catch (libsumo::TraCIException& e) {
166 7 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
167 7 : }
168 489 : server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
169 : return true;
170 : }
171 :
172 :
173 : /****************************************************************************/
|