Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_Polygon.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2002-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/****************************************************************************/
23// APIs for getting/setting polygon values via TraCI
24/****************************************************************************/
25#include <config.h>
26
28#include <microsim/MSNet.h>
30#include <libsumo/Polygon.h>
31#include <libsumo/Helper.h>
34
35
36// ===========================================================================
37// method definitions
38// ===========================================================================
39bool
41 tcpip::Storage& outputStorage) {
42 const int variable = inputStorage.readUnsignedByte();
43 const std::string id = inputStorage.readString();
45 try {
46 if (!libsumo::Polygon::handleVariable(id, variable, &server, &inputStorage)) {
47 return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, "Get Polygon Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
48 }
49 } catch (libsumo::TraCIException& e) {
50 return server.writeErrorStatusCmd(libsumo::CMD_GET_POLYGON_VARIABLE, e.what(), outputStorage);
51 }
53 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
54 return true;
55}
56
57bool
59 tcpip::Storage& outputStorage) {
60 std::string warning = ""; // additional description for response
61 // variable
62 int variable = inputStorage.readUnsignedByte();
63 if (variable != libsumo::VAR_TYPE && variable != libsumo::VAR_COLOR && variable != libsumo::VAR_SHAPE && variable != libsumo::VAR_FILL
64 && variable != libsumo::VAR_WIDTH && variable != libsumo::VAR_MOVE_TO
65 && variable != libsumo::ADD && variable != libsumo::REMOVE && variable != libsumo::VAR_PARAMETER) {
67 "Change Polygon State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
68 }
69 // id
70 std::string id = inputStorage.readString();
71 try {
72 // process
73 switch (variable) {
74 case libsumo::VAR_TYPE: {
75 std::string type;
76 if (!server.readTypeCheckingString(inputStorage, type)) {
77 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
78 }
79 libsumo::Polygon::setType(id, type);
80 }
81 break;
82 case libsumo::VAR_COLOR: {
84 if (!server.readTypeCheckingColor(inputStorage, col)) {
85 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The color must be given using an according type.", outputStorage);
86 }
87 libsumo::Polygon::setColor(id, col);
88 }
89 break;
90 case libsumo::VAR_SHAPE: {
91 PositionVector shape;
92 if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
93 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The shape must be given using an according type.", outputStorage);
94 }
95 libsumo::Polygon::setShape(id, libsumo::Helper::makeTraCIPositionVector(shape));
96 }
97 break;
98 case libsumo::VAR_FILL: {
99 int value = 0;
100 if (!server.readTypeCheckingInt(inputStorage, value)) {
101 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'fill' must be defined using an integer.", outputStorage);
102 }
103 libsumo::Polygon::setFilled(id, value != 0);
104 }
105 break;
106 case libsumo::VAR_WIDTH: {
107 double value = 0;
108 if (!server.readTypeCheckingDouble(inputStorage, value)) {
109 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "'lineWidth' must be defined using an double.", outputStorage);
110 }
111 libsumo::Polygon::setLineWidth(id, value);
112 }
113 break;
114 case libsumo::ADD: {
115 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
116 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a new polygon.", outputStorage);
117 }
118 int itemNo = inputStorage.readInt();
119 if (itemNo != 5 && itemNo != 6) {
120 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding a polygon needs five to six parameters.", outputStorage);
121 }
122 std::string type;
123 if (!server.readTypeCheckingString(inputStorage, type)) {
124 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The type must be given as a string.", outputStorage);
125 }
127 if (!server.readTypeCheckingColor(inputStorage, col)) {
128 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second polygon parameter must be the color.", outputStorage);
129 }
130 int value = 0;
131 if (!server.readTypeCheckingUnsignedByte(inputStorage, value)) {
132 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third polygon parameter must be 'fill' encoded as ubyte.", outputStorage);
133 }
134 bool fill = value != 0;
135 int layer = 0;
136 if (!server.readTypeCheckingInt(inputStorage, layer)) {
137 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth polygon parameter must be the layer encoded as int.", outputStorage);
138 }
139 PositionVector shape;
140 if (!server.readTypeCheckingPolygon(inputStorage, shape)) {
141 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth polygon parameter must be the shape.", outputStorage);
142 }
143 double lineWidth = 1;
144 if (itemNo == 6) {
145 if (!server.readTypeCheckingDouble(inputStorage, lineWidth)) {
146 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The sixth polygon parameter must be the lineWidth encoded as double.", outputStorage);
147 }
148 }
150 libsumo::Polygon::add(id, tp, col, fill, type, layer, lineWidth);
151 }
152 break;
154 // Add dynamics to polygon.
155 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
156 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for adding dynamics to a polygon.", outputStorage);
157 }
158 int itemNo = inputStorage.readInt();
159 if (itemNo != 5) {
160 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Adding polygon dynamics needs four parameters.", outputStorage);
161 }
162
163 std::string trackedID;
164 if (!server.readTypeCheckingString(inputStorage, trackedID)) {
165 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The first parameter for adding polygon dynamics must be ID of the tracked object as a string ('' to disregard tracking).", outputStorage);
166 }
167
168 std::vector<double> timeSpan;
169 if (!server.readTypeCheckingDoubleList(inputStorage, timeSpan)) {
170 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The second parameter for adding polygon dynamics must be the timespan of the animation (length=0 to disregard animation).", outputStorage);
171 }
172
173 std::vector<double> alphaSpan;
174 if (!server.readTypeCheckingDoubleList(inputStorage, alphaSpan)) {
175 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The third parameter for adding polygon dynamics must be the alphaSpanStr of the animation (length=0 to disregard alpha animation).", outputStorage);
176 }
177
178 int looped;
179 if (!server.readTypeCheckingUnsignedByte(inputStorage, looped)) {
180 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fourth parameter for adding polygon dynamics must be boolean indicating whether the animation should be looped.", outputStorage);
181 }
182
183 int rotate;
184 if (!server.readTypeCheckingUnsignedByte(inputStorage, rotate)) {
185 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The fifth parameter for adding polygon dynamics must be boolean indicating whether the tracking polygon should be rotated.", outputStorage);
186 }
187
188 libsumo::Polygon::addDynamics(id, trackedID, timeSpan, alphaSpan, looped != 0, rotate != 0);
189 }
190 break;
191 case libsumo::REMOVE: {
192 int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
193 if (!server.readTypeCheckingInt(inputStorage, layer)) {
194 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The layer must be given using an int.", outputStorage);
195 }
196
197 libsumo::Polygon::remove(id, layer);
198
199 }
200 break;
202 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
203 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
204 }
205 //readt itemNo
206 inputStorage.readInt();
207 std::string name;
208 if (!server.readTypeCheckingString(inputStorage, name)) {
209 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
210 }
211 std::string value;
212 if (!server.readTypeCheckingString(inputStorage, value)) {
213 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
214 }
215 libsumo::Polygon::setParameter(id, name, value);
216
217 }
218 break;
219 default:
220 break;
221 }
222 } catch (libsumo::TraCIException& e) {
223 return server.writeErrorStatusCmd(libsumo::CMD_SET_POLYGON_VARIABLE, e.what(), outputStorage);
224 }
226 return true;
227}
228
229
230/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:56
A list of positions.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon 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 readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool readTypeCheckingDoubleList(tcpip::Storage &inputStorage, std::vector< double > &into)
Reads the value type and a double list, 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 readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
static TraCIPositionVector makeTraCIPositionVector(const PositionVector &positionVector)
helper functions
Definition Helper.cpp:337
An error which allows to continue.
Definition TraCIDefs.h:144
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 RESPONSE_GET_POLYGON_VARIABLE
TRACI_CONST int VAR_TYPE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_MOVE_TO
TRACI_CONST int CMD_SET_POLYGON_VARIABLE
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_SHAPE
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int VAR_ADD_DYNAMICS
TRACI_CONST int CMD_GET_POLYGON_VARIABLE
TRACI_CONST int VAR_FILL
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
A list of positions.
Definition TraCIDefs.h:234