Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
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/****************************************************************************/
22// APIs for getting/setting POI values via TraCI
23/****************************************************************************/
24#include <config.h>
25
26#include <microsim/MSNet.h>
29#include <libsumo/POI.h>
32#include "TraCIServerAPI_POI.h"
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
38bool
40 tcpip::Storage& outputStorage) {
41 const int variable = inputStorage.readUnsignedByte();
42 const std::string id = inputStorage.readString();
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 }
52 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
53 return true;
54}
55
56
57bool
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: {
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;
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;
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;
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 }
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 }
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.");
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 {
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;
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/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:56
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition TraCIServer.h:59
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
tcpip::Storage & getWrapperStorage()
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
An error which allows to continue.
Definition TraCIDefs.h:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
virtual int readInt()
Definition storage.cpp:311
TRACI_CONST int VAR_IMAGEFILE
TRACI_CONST int CMD_GET_POI_VARIABLE
TRACI_CONST int VAR_TYPE
TRACI_CONST int VAR_ANGLE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_HIGHLIGHT
TRACI_CONST int CMD_SET_POI_VARIABLE
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_POSITION
TRACI_CONST int VAR_WIDTH
TRACI_CONST int RESPONSE_GET_POI_VARIABLE
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:179