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 6338 : TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
39 : tcpip::Storage& outputStorage) {
40 6338 : const int variable = inputStorage.readUnsignedByte();
41 6338 : const std::string id = inputStorage.readString();
42 6338 : server.initWrapper(libsumo::RESPONSE_GET_POI_VARIABLE, variable, id);
43 : try {
44 6338 : if (!libsumo::POI::handleVariable(id, variable, &server, &inputStorage)) {
45 4 : 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 6334 : server.writeStatusCmd(libsumo::CMD_GET_POI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
51 6334 : server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
52 : return true;
53 : }
54 :
55 :
56 : bool
57 1042 : TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
58 : tcpip::Storage& outputStorage) {
59 1042 : std::string warning = ""; // additional description for response
60 : // variable & id
61 1042 : int variable = inputStorage.readUnsignedByte();
62 1042 : std::string id = inputStorage.readString();
63 : // check variable
64 1042 : if (variable != libsumo::VAR_TYPE &&
65 1042 : variable != libsumo::VAR_COLOR &&
66 1028 : variable != libsumo::VAR_POSITION &&
67 1028 : variable != libsumo::VAR_WIDTH &&
68 911 : variable != libsumo::VAR_HEIGHT &&
69 911 : variable != libsumo::VAR_ANGLE &&
70 903 : variable != libsumo::VAR_IMAGEFILE &&
71 903 : variable != libsumo::VAR_HIGHLIGHT &&
72 871 : variable != libsumo::ADD &&
73 761 : 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 1042 : switch (variable) {
80 : case libsumo::VAR_TYPE: {
81 : std::string type;
82 7 : 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 6 : libsumo::POI::setType(id, type);
86 : }
87 : break;
88 : case libsumo::VAR_COLOR: {
89 : libsumo::TraCIColor col;
90 7 : 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 6 : libsumo::POI::setColor(id, col);
94 : }
95 : break;
96 113 : case libsumo::VAR_POSITION: {
97 113 : libsumo::TraCIPosition pos;
98 113 : 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 112 : libsumo::POI::setPosition(id, pos.x, pos.y);
102 : }
103 : break;
104 4 : case libsumo::VAR_WIDTH: {
105 : double width;
106 4 : 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 4 : libsumo::POI::setWidth(id, width);
110 : }
111 4 : break;
112 4 : case libsumo::VAR_HEIGHT: {
113 : double height;
114 4 : 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 4 : libsumo::POI::setHeight(id, height);
118 : }
119 4 : break;
120 4 : case libsumo::VAR_ANGLE: {
121 : double angle;
122 4 : 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 4 : libsumo::POI::setAngle(id, angle);
126 : }
127 4 : break;
128 : case libsumo::VAR_IMAGEFILE: {
129 : std::string imageFile;
130 4 : 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 4 : libsumo::POI::setImageFile(id, imageFile);
134 : }
135 : break;
136 28 : case libsumo::VAR_HIGHLIGHT: {
137 : // Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
138 28 : 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 28 : const int itemNo = inputStorage.readInt();
142 28 : 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 28 : if (itemNo > 0) {
147 28 : 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 28 : double size = -1;
152 28 : if (itemNo > 1) {
153 28 : 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 28 : int alphaMax = -1;
158 28 : if (itemNo > 2) {
159 28 : 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 28 : double duration = -1;
164 28 : if (itemNo > 3) {
165 28 : 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 28 : int type = 0;
170 28 : if (itemNo > 4) {
171 28 : 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 28 : libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
176 : }
177 : break;
178 99 : case libsumo::ADD: {
179 99 : 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 99 : const int parameterCount = inputStorage.readInt();
184 : std::string type;
185 99 : 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 99 : 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 99 : int layer = 0;
193 99 : 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 99 : libsumo::TraCIPosition pos;
197 99 : 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 99 : 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 98 : } else if (parameterCount >= 8) {
205 : std::string imgFile;
206 98 : 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 98 : 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 98 : 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 98 : 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 98 : 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 98 : 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 11 : case libsumo::REMOVE: {
237 11 : int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
238 11 : 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 10 : if (!libsumo::POI::remove(id, layer)) {
242 2 : return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
243 : }
244 : }
245 9 : break;
246 761 : case libsumo::VAR_PARAMETER: {
247 761 : 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 761 : inputStorage.readInt();
252 : std::string name;
253 761 : 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 761 : 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 761 : 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 1034 : server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
270 : return true;
271 : }
272 :
273 :
274 : /****************************************************************************/
|