Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2017-2022 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>
31#include "TraCIServerAPI_POI.h"
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
37bool
39 tcpip::Storage& outputStorage) {
40 const int variable = inputStorage.readUnsignedByte();
41 const std::string id = inputStorage.readString();
43 try {
44 if (!libsumo::POI::handleVariable(id, variable, &server, &inputStorage)) {
45 return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
46 }
47 } catch (libsumo::TraCIException& e) {
48 return server.writeErrorStatusCmd(libsumo::CMD_GET_POI_VARIABLE, e.what(), outputStorage);
49 }
51 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
52 return true;
53}
54
55
56bool
58 tcpip::Storage& outputStorage) {
59 std::string warning = ""; // additional description for response
60 // variable & id
61 int variable = inputStorage.readUnsignedByte();
62 std::string id = inputStorage.readString();
63 // check variable
64 if (variable != libsumo::VAR_TYPE &&
65 variable != libsumo::VAR_COLOR &&
66 variable != libsumo::VAR_POSITION &&
67 variable != libsumo::VAR_WIDTH &&
68 variable != libsumo::VAR_HEIGHT &&
69 variable != libsumo::VAR_ANGLE &&
70 variable != libsumo::VAR_IMAGEFILE &&
71 variable != libsumo::VAR_HIGHLIGHT &&
72 variable != libsumo::ADD &&
73 variable != libsumo::REMOVE &&
74 variable != libsumo::VAR_PARAMETER) {
75 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
76 }
77 // process
78 try {
79 switch (variable) {
80 case libsumo::VAR_TYPE: {
81 std::string type;
82 if (!server.readTypeCheckingString(inputStorage, type)) {
83 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
84 }
85 libsumo::POI::setType(id, type);
86 }
87 break;
88 case libsumo::VAR_COLOR: {
90 if (!server.readTypeCheckingColor(inputStorage, col)) {
91 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
92 }
93 libsumo::POI::setColor(id, col);
94 }
95 break;
98 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
99 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The position must be given using an according type.", outputStorage);
100 }
101 libsumo::POI::setPosition(id, pos.x, pos.y);
102 }
103 break;
104 case libsumo::VAR_WIDTH: {
105 double width;
106 if (!server.readTypeCheckingDouble(inputStorage, width)) {
107 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The width must be given using an according type.", outputStorage);
108 }
109 libsumo::POI::setWidth(id, width);
110 }
111 break;
112 case libsumo::VAR_HEIGHT: {
113 double height;
114 if (!server.readTypeCheckingDouble(inputStorage, height)) {
115 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The height must be given using an according type.", outputStorage);
116 }
117 libsumo::POI::setHeight(id, height);
118 }
119 break;
120 case libsumo::VAR_ANGLE: {
121 double angle;
122 if (!server.readTypeCheckingDouble(inputStorage, angle)) {
123 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The angle must be given using an according type.", outputStorage);
124 }
125 libsumo::POI::setAngle(id, angle);
126 }
127 break;
129 std::string imageFile;
130 if (!server.readTypeCheckingString(inputStorage, imageFile)) {
131 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
132 }
133 libsumo::POI::setImageFile(id, imageFile);
134 }
135 break;
137 // Highlight the POI by adding a polygon (NOTE: duplicated code exists for vehicle domain)
138 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
139 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for highlighting an object.", outputStorage);
140 }
141 const int itemNo = inputStorage.readInt();
142 if (itemNo > 5) {
143 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
144 }
146 if (itemNo > 0) {
147 if (!server.readTypeCheckingColor(inputStorage, col)) {
148 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first parameter for highlighting must be the highlight color.", outputStorage);
149 }
150 }
151 double size = -1;
152 if (itemNo > 1) {
153 if (!server.readTypeCheckingDouble(inputStorage, size)) {
154 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second parameter for highlighting must be the highlight size.", outputStorage);
155 }
156 }
157 int alphaMax = -1;
158 if (itemNo > 2) {
159 if (!server.readTypeCheckingUnsignedByte(inputStorage, alphaMax)) {
160 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third parameter for highlighting must be maximal alpha.", outputStorage);
161 }
162 }
163 double duration = -1;
164 if (itemNo > 3) {
165 if (!server.readTypeCheckingDouble(inputStorage, duration)) {
166 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The fourth parameter for highlighting must be the highlight duration.", outputStorage);
167 }
168 }
169 int type = 0;
170 if (itemNo > 4) {
171 if (!server.readTypeCheckingUnsignedByte(inputStorage, type)) {
172 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 libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
176 }
177 break;
178 case libsumo::ADD: {
179 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
180 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
181 }
182 //read itemNo
183 const int parameterCount = inputStorage.readInt();
184 std::string type;
185 if (!server.readTypeCheckingString(inputStorage, type)) {
186 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
187 }
189 if (!server.readTypeCheckingColor(inputStorage, col)) {
190 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
191 }
192 int layer = 0;
193 if (!server.readTypeCheckingInt(inputStorage, layer)) {
194 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
195 }
197 if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
198 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
199 }
200 if (parameterCount == 4) {
201 if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
202 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
203 }
204 } else if (parameterCount == 8) {
205 std::string imgFile;
206 if (!server.readTypeCheckingString(inputStorage, imgFile)) {
207 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 if (!server.readTypeCheckingDouble(inputStorage, width)) {
211 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 if (!server.readTypeCheckingDouble(inputStorage, height)) {
215 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 if (!server.readTypeCheckingDouble(inputStorage, angle)) {
219 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The eighth PoI parameter must be the angle encoded as a double.", outputStorage);
220 }
221 //
222 if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle)) {
223 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
224 }
225 } else {
227 "Adding a PoI requires either only type, color, layer and position parameters or these and imageFile, width, height and angle parameters.",
228 outputStorage);
229 }
230 }
231 break;
232 case libsumo::REMOVE: {
233 int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
234 if (!server.readTypeCheckingInt(inputStorage, layer)) {
235 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
236 }
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 readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, 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.
An error which allows to continue.
Definition: TraCIDefs.h:138
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 3D-position.
Definition: TraCIDefs.h:172