Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/traci-server/TraCIServerAPI_POI.cpp
169665 views
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
TraCIServerAPI_POI::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
40
tcpip::Storage& outputStorage) {
41
const int variable = inputStorage.readUnsignedByte();
42
const std::string id = inputStorage.readString();
43
server.initWrapper(libsumo::RESPONSE_GET_POI_VARIABLE, variable, id);
44
try {
45
if (!libsumo::POI::handleVariable(id, variable, &server, &inputStorage)) {
46
return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
47
}
48
} catch (libsumo::TraCIException& e) {
49
return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, e.what(), outputStorage);
50
}
51
server.writeStatusCmd(libsumo::CMD_GET_POI_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
52
server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
53
return true;
54
}
55
56
57
bool
58
TraCIServerAPI_POI::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
59
tcpip::Storage& outputStorage) {
60
std::string warning = ""; // additional description for response
61
// variable & id
62
int variable = inputStorage.readUnsignedByte();
63
std::string id = inputStorage.readString();
64
// check variable
65
if (variable != libsumo::VAR_TYPE &&
66
variable != libsumo::VAR_COLOR &&
67
variable != libsumo::VAR_POSITION &&
68
variable != libsumo::VAR_WIDTH &&
69
variable != libsumo::VAR_HEIGHT &&
70
variable != libsumo::VAR_ANGLE &&
71
variable != libsumo::VAR_IMAGEFILE &&
72
variable != libsumo::VAR_HIGHLIGHT &&
73
variable != libsumo::ADD &&
74
variable != libsumo::REMOVE &&
75
variable != libsumo::VAR_PARAMETER) {
76
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
77
}
78
// process
79
try {
80
switch (variable) {
81
case libsumo::VAR_TYPE: {
82
std::string type;
83
if (!server.readTypeCheckingString(inputStorage, type)) {
84
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
85
}
86
libsumo::POI::setType(id, type);
87
}
88
break;
89
case libsumo::VAR_COLOR: {
90
libsumo::TraCIColor col;
91
if (!server.readTypeCheckingColor(inputStorage, col)) {
92
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
93
}
94
libsumo::POI::setColor(id, col);
95
}
96
break;
97
case libsumo::VAR_POSITION: {
98
libsumo::TraCIPosition pos;
99
if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
100
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The position must be given using an according type.", outputStorage);
101
}
102
libsumo::POI::setPosition(id, pos.x, pos.y);
103
}
104
break;
105
case libsumo::VAR_WIDTH: {
106
double width;
107
if (!server.readTypeCheckingDouble(inputStorage, width)) {
108
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The width must be given using an according type.", outputStorage);
109
}
110
libsumo::POI::setWidth(id, width);
111
}
112
break;
113
case libsumo::VAR_HEIGHT: {
114
double height;
115
if (!server.readTypeCheckingDouble(inputStorage, height)) {
116
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The height must be given using an according type.", outputStorage);
117
}
118
libsumo::POI::setHeight(id, height);
119
}
120
break;
121
case libsumo::VAR_ANGLE: {
122
double angle;
123
if (!server.readTypeCheckingDouble(inputStorage, angle)) {
124
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The angle must be given using an according type.", outputStorage);
125
}
126
libsumo::POI::setAngle(id, angle);
127
}
128
break;
129
case libsumo::VAR_IMAGEFILE: {
130
std::string imageFile;
131
if (!server.readTypeCheckingString(inputStorage, imageFile)) {
132
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
133
}
134
libsumo::POI::setImageFile(id, imageFile);
135
}
136
break;
137
case libsumo::VAR_HIGHLIGHT: {
138
// Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
139
if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
140
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for highlighting an object.", outputStorage);
141
}
142
const int itemNo = inputStorage.readInt();
143
if (itemNo > 5) {
144
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
if (itemNo > 0) {
148
if (!server.readTypeCheckingColor(inputStorage, col)) {
149
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first parameter for highlighting must be the highlight color.", outputStorage);
150
}
151
}
152
double size = -1;
153
if (itemNo > 1) {
154
if (!server.readTypeCheckingDouble(inputStorage, size)) {
155
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second parameter for highlighting must be the highlight size.", outputStorage);
156
}
157
}
158
int alphaMax = -1;
159
if (itemNo > 2) {
160
if (!server.readTypeCheckingUnsignedByte(inputStorage, alphaMax)) {
161
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third parameter for highlighting must be maximal alpha.", outputStorage);
162
}
163
}
164
double duration = -1;
165
if (itemNo > 3) {
166
if (!server.readTypeCheckingDouble(inputStorage, duration)) {
167
return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fourth parameter for highlighting must be the highlight duration.", outputStorage);
168
}
169
}
170
int type = 0;
171
if (itemNo > 4) {
172
if (!server.readTypeCheckingUnsignedByte(inputStorage, type)) {
173
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
libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
177
}
178
break;
179
case libsumo::ADD: {
180
if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
181
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
182
}
183
//read itemNo
184
const int parameterCount = inputStorage.readInt();
185
std::string type;
186
if (!server.readTypeCheckingString(inputStorage, type)) {
187
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
if (!server.readTypeCheckingColor(inputStorage, col)) {
191
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
192
}
193
int layer = StoHelp::readTypedInt(inputStorage, "The third PoI parameter must be the layer encoded as int.");
194
libsumo::TraCIPosition pos;
195
if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
196
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
197
}
198
if (parameterCount == 4) {
199
if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
200
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
201
}
202
} else if (parameterCount >= 8) {
203
std::string imgFile;
204
if (!server.readTypeCheckingString(inputStorage, imgFile)) {
205
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
if (!server.readTypeCheckingDouble(inputStorage, width)) {
209
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
if (!server.readTypeCheckingDouble(inputStorage, height)) {
213
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
if (!server.readTypeCheckingDouble(inputStorage, angle)) {
217
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
if (parameterCount == 9 && !server.readTypeCheckingString(inputStorage, icon)) {
221
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The ninth PoI parameter must be the icon encoded as a string.", outputStorage);
222
}
223
//
224
if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle, icon)) {
225
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
226
}
227
} else {
228
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
const int layer = StoHelp::readTypedInt(inputStorage, "The layer must be given using an int.");
237
if (!libsumo::POI::remove(id, layer)) {
238
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
239
}
240
}
241
break;
242
case libsumo::VAR_PARAMETER: {
243
if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
244
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
245
}
246
//readt itemNo
247
inputStorage.readInt();
248
std::string name;
249
if (!server.readTypeCheckingString(inputStorage, name)) {
250
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
if (!server.readTypeCheckingString(inputStorage, value)) {
254
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
255
}
256
libsumo::POI::setParameter(id, name, value);
257
}
258
break;
259
default:
260
break;
261
}
262
} catch (libsumo::TraCIException& e) {
263
return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
264
}
265
server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
266
return true;
267
}
268
269
270
/****************************************************************************/
271
272