Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_Person.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-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/****************************************************************************/
18// APIs for getting/setting person values via TraCI
19/****************************************************************************/
20#include <config.h>
21
26#include <microsim/MSNet.h>
27#include <microsim/MSEdge.h>
28#include <libsumo/Person.h>
31#include <libsumo/VehicleType.h>
32#include "TraCIServer.h"
36
37
38// ===========================================================================
39// method definitions
40// ===========================================================================
41bool
43 tcpip::Storage& outputStorage) {
44 const int variable = inputStorage.readUnsignedByte();
45 const std::string id = inputStorage.readString();
47 try {
48 if (!libsumo::Person::handleVariable(id, variable, &server, &inputStorage)) {
49 return server.writeErrorStatusCmd(libsumo::CMD_GET_PERSON_VARIABLE, "Get Person Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
50 }
51 } catch (libsumo::TraCIException& e) {
52 return server.writeErrorStatusCmd(libsumo::CMD_GET_PERSON_VARIABLE, e.what(), outputStorage);
53 }
55 server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
56 return true;
57}
58
59
60bool
62 tcpip::Storage& outputStorage) {
63 std::string warning = ""; // additional description for response
64 // variable
65 int variable = inputStorage.readUnsignedByte();
66 if (variable != libsumo::VAR_PARAMETER
67 && variable != libsumo::ADD
68 && variable != libsumo::REMOVE
69 && variable != libsumo::APPEND_STAGE
70 && variable != libsumo::REPLACE_STAGE
71 && variable != libsumo::REMOVE_STAGE
73 && variable != libsumo::VAR_MOVE_TO
74 && variable != libsumo::MOVE_TO_XY
75 && variable != libsumo::VAR_SPEED
76 && variable != libsumo::VAR_TYPE
77 && variable != libsumo::VAR_SPEED_FACTOR
78 && variable != libsumo::VAR_LENGTH
79 && variable != libsumo::VAR_WIDTH
80 && variable != libsumo::VAR_HEIGHT
81 && variable != libsumo::VAR_MINGAP
82 && variable != libsumo::VAR_COLOR
83 ) {
84 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
85 }
86
87 try {
88 // TODO: remove declaration of c after completion
90 // id
91 std::string id = inputStorage.readString();
92 // TODO: remove declaration of p after completion
93 const bool shouldExist = variable != libsumo::ADD;
94 MSTransportable* p = c.get(id);
95 if (p == nullptr && shouldExist) {
96 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
97 }
98 // process
99 switch (variable) {
100 case libsumo::VAR_SPEED: {
101 double speed = 0;
102 if (!server.readTypeCheckingDouble(inputStorage, speed)) {
103 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Setting speed requires a double.", outputStorage);
104 }
105 // set the speed for all present and future (walking) stages and modify the vType so that stages added later are also affected
106 libsumo::Person::setSpeed(id, speed);
107 }
108 break;
109 case libsumo::VAR_TYPE: {
110 std::string vTypeID;
111 if (!server.readTypeCheckingString(inputStorage, vTypeID)) {
112 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The vehicle type id must be given as a string.", outputStorage);
113 }
114 libsumo::Person::setType(id, vTypeID);
115 break;
116 }
118 double speedfactor = 0;
119 if (!server.readTypeCheckingDouble(inputStorage, speedfactor)) {
120 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Setting SpeedFactor requires a double.", outputStorage);
121 }
122 libsumo::Person::setSpeedFactor(id, speedfactor);
123 }
124 break;
125 case libsumo::VAR_COLOR: {
127 if (!server.readTypeCheckingColor(inputStorage, col)) {
128 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The color must be given using the according type.", outputStorage);
129 }
130 libsumo::Person::setColor(id, col);
131 break;
132 }
133 case libsumo::ADD: {
134 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
135 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a person requires a compound object.", outputStorage);
136 }
137 if (inputStorage.readInt() != 4) {
138 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a person needs four parameters.", outputStorage);
139 }
140 std::string vTypeID;
141 if (!server.readTypeCheckingString(inputStorage, vTypeID)) {
142 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "First parameter (type) requires a string.", outputStorage);
143 }
144 std::string edgeID;
145 if (!server.readTypeCheckingString(inputStorage, edgeID)) {
146 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Second parameter (edge) requires a string.", outputStorage);
147 }
148 double depart;
149 if (!server.readTypeCheckingDouble(inputStorage, depart)) {
150 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Third parameter (depart) requires a double.", outputStorage);
151 }
152 double pos;
153 if (!server.readTypeCheckingDouble(inputStorage, pos)) {
154 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fourth parameter (position) requires a double.", outputStorage);
155 }
156 libsumo::Person::add(id, edgeID, pos, depart, vTypeID);
157 }
158 break;
159 case libsumo::REMOVE: {
160 int why = 0;
161 if (!server.readTypeCheckingByte(inputStorage, why)) {
162 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Removing a person requires a byte.", outputStorage);
163 }
164 libsumo::Person::remove(id, (char)why);
165 }
166 break;
168 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
169 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a person stage requires a compound object.", outputStorage);
170 }
171 int numParameters = inputStorage.readInt();
172 if (numParameters == 13) {
174 libsumo::StorageHelper::readStage(inputStorage, stage);
175 libsumo::Person::appendStage(id, stage);
176 } else {
177 const int stageType = StoHelp::readTypedInt(inputStorage, "The first parameter for adding a stage must be the stage type given as int.");
178 if (stageType == libsumo::STAGE_DRIVING) {
179 // append driving stage
180 if (numParameters != 4) {
181 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a driving stage needs four parameters.", outputStorage);
182 }
183 std::string edgeID;
184 if (!server.readTypeCheckingString(inputStorage, edgeID)) {
185 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Second parameter (edge) requires a string.", outputStorage);
186 }
187 std::string lines;
188 if (!server.readTypeCheckingString(inputStorage, lines)) {
189 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Third parameter (lines) requires a string.", outputStorage);
190 }
191 std::string stopID;
192 if (!server.readTypeCheckingString(inputStorage, stopID)) {
193 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fourth parameter (stopID) requires a string.", outputStorage);
194 }
195 libsumo::Person::appendDrivingStage(id, edgeID, lines, stopID);
196 } else if (stageType == libsumo::STAGE_WAITING) {
197 // append waiting stage
198 if (numParameters != 4) {
199 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a waiting stage needs four parameters.", outputStorage);
200 }
201 double duration;
202 if (!server.readTypeCheckingDouble(inputStorage, duration)) {
203 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Second parameter (duration) requires a double.", outputStorage);
204 }
205 std::string description;
206 if (!server.readTypeCheckingString(inputStorage, description)) {
207 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Third parameter (description) requires a string.", outputStorage);
208 }
209 std::string stopID;
210 if (!server.readTypeCheckingString(inputStorage, stopID)) {
211 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fourth parameter (stopID) requires a string.", outputStorage);
212 }
213 libsumo::Person::appendWaitingStage(id, duration, description, stopID);
214 } else if (stageType == libsumo::STAGE_WALKING) {
215 // append walking stage
216 if (numParameters != 6) {
217 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a walking stage needs six parameters.", outputStorage);
218 }
219 std::vector<std::string> edgeIDs;
220 if (!server.readTypeCheckingStringList(inputStorage, edgeIDs)) {
221 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "Second parameter (edges) route must be defined as a list of edge ids.", outputStorage);
222 }
223 double arrivalPos;
224 if (!server.readTypeCheckingDouble(inputStorage, arrivalPos)) {
225 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Third parameter (arrivalPos) requires a double.", outputStorage);
226 }
227 double duration;
228 if (!server.readTypeCheckingDouble(inputStorage, duration)) {
229 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fourth parameter (duration) requires a double.", outputStorage);
230 }
231 double speed;
232 if (!server.readTypeCheckingDouble(inputStorage, speed)) {
233 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fifth parameter (speed) requires a double.", outputStorage);
234 }
235 std::string stopID;
236 if (!server.readTypeCheckingString(inputStorage, stopID)) {
237 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Fourth parameter (stopID) requires a string.", outputStorage);
238 }
239 libsumo::Person::appendWalkingStage(id, edgeIDs, arrivalPos, duration, speed, stopID);
240 } else {
241 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Invalid stage type for person '" + id + "'", outputStorage);
242 }
243 }
244
245 }
246 break;
247
249 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
250 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Replacing a person stage requires a compound object.", outputStorage);
251 }
252 if (inputStorage.readInt() != 2) {
253 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Replacing a person stage requires a compound object of size 2.", outputStorage);
254 }
255 const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "First parameter of replace stage should be an integer");
256 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
257 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Second parameter of replace stage should be a compound object", outputStorage);
258 }
259 if (inputStorage.readInt() != 13) {
260 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Second parameter of replace stage should be a compound object of size 13", outputStorage);
261 }
263 libsumo::StorageHelper::readStage(inputStorage, stage);
264 libsumo::Person::replaceStage(id, nextStageIndex, stage);
265 }
266 break;
267
269 const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "The message must contain the stage index.");
270 libsumo::Person::removeStage(id, nextStageIndex);
271 }
272 break;
274 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
275 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Rerouting requires a compound object.", outputStorage);
276 }
277 if (inputStorage.readInt() != 0) {
278 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Rerouting should obtain an empty compound object.", outputStorage);
279 }
280 libsumo::Person::rerouteTraveltime(id);
281 }
282 break;
284 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
285 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Setting position requires a compound object.", outputStorage);
286 }
287 const int numArgs = inputStorage.readInt();
288 if (numArgs != 3) {
289 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Setting position should obtain the edge id, the position and the lateral position.", outputStorage);
290 }
291 std::string laneID;
292 if (!server.readTypeCheckingString(inputStorage, laneID)) {
293 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The first parameter for setting a position must be the laneID given as a string.", outputStorage);
294 }
295 double position = 0;
296 if (!server.readTypeCheckingDouble(inputStorage, position)) {
297 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The second parameter for setting a position must be the position given as a double.", outputStorage);
298 }
299 double posLat = 0;
300 if (!server.readTypeCheckingDouble(inputStorage, posLat)) {
301 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The third parameter for setting a position must be the lateral position given as a double.", outputStorage);
302 }
303 // process
304 libsumo::Person::moveTo(id, laneID, position, posLat);
305 }
306 break;
307 case libsumo::MOVE_TO_XY: {
308 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
309 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person requires a compound object.", outputStorage);
310 }
311 const int numArgs = inputStorage.readInt();
312 if (numArgs != 5 && numArgs != 6) {
313 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person should obtain: edgeID, x, y, angle, keepRouteFlag and optionally matchThreshold.", outputStorage);
314 }
315 // edge ID
316 std::string edgeID;
317 if (!server.readTypeCheckingString(inputStorage, edgeID)) {
318 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The first parameter for moveToXY must be the edge ID given as a string.", outputStorage);
319 }
320 // x
321 double x = 0;
322 if (!server.readTypeCheckingDouble(inputStorage, x)) {
323 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The second parameter for moveToXY must be the x-position given as a double.", outputStorage);
324 }
325 // y
326 double y = 0;
327 if (!server.readTypeCheckingDouble(inputStorage, y)) {
328 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The third parameter for moveToXY must be the y-position given as a double.", outputStorage);
329 }
330 // angle
331 double angle = 0;
332 if (!server.readTypeCheckingDouble(inputStorage, angle)) {
333 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The fourth parameter for moveToXY must be the angle given as a double.", outputStorage);
334 }
335 int keepRouteFlag = 1;
336 if (!server.readTypeCheckingByte(inputStorage, keepRouteFlag)) {
337 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The fifth parameter for moveToXY must be the keepRouteFlag given as a byte.", outputStorage);
338 }
339 double matchThreshold = 100;
340 if (numArgs == 6) {
341 if (!server.readTypeCheckingDouble(inputStorage, matchThreshold)) {
342 return server.writeErrorStatusCmd(libsumo::CMD_SET_VEHICLE_VARIABLE, "The sixth parameter for moveToXY must be the matchThreshold given as a double.", outputStorage);
343 }
344 }
345 libsumo::Person::moveToXY(id, edgeID, x, y, angle, keepRouteFlag, matchThreshold);
346 }
347 break;
349 if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
350 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
351 }
352 //read itemNo
353 inputStorage.readInt();
354 std::string name;
355 if (!server.readTypeCheckingString(inputStorage, name)) {
356 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
357 }
358 std::string value;
359 if (!server.readTypeCheckingString(inputStorage, value)) {
360 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
361 }
362 libsumo::Person::setParameter(id, name, value);
363 }
364 break;
365 default:
366 try {
367 if (!TraCIServerAPI_VehicleType::setVariable(libsumo::CMD_SET_PERSON_VARIABLE, variable, p->getSingularType().getID(), server, inputStorage, outputStorage)) {
368 return false;
369 }
370 } catch (ProcessError& e) {
371 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
372 }
373 break;
374 }
375 } catch (libsumo::TraCIException& e) {
376 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
377 }
379 return true;
380}
381
382
383/****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:56
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition MSNet.cpp:1250
MSTransportable * get(const std::string &id) const
Returns the named transportable, if existing.
MSVehicleType & getSingularType()
Replaces the current vehicle type with a new one used by this vehicle only.
const std::string & getID() const
Returns the name of the vehicle type.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xae: Get Person Variable)
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xce: Change Person State)
static bool setVariable(const int cmd, const int variable, const std::string &id, TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value for the given type.
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.
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
tcpip::Storage & getWrapperStorage()
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 readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string 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 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="")
static void readStage(tcpip::Storage &inputStorage, libsumo::TraCIStage &stage, 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_TYPE
TRACI_CONST int VAR_MINGAP
TRACI_CONST int REPLACE_STAGE
TRACI_CONST int VAR_SPEED_FACTOR
TRACI_CONST int MOVE_TO_XY
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int CMD_GET_PERSON_VARIABLE
TRACI_CONST int VAR_MOVE_TO
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_WIDTH
TRACI_CONST int STAGE_WAITING
TRACI_CONST int CMD_REROUTE_TRAVELTIME
TRACI_CONST int APPEND_STAGE
TRACI_CONST int CMD_SET_VEHICLE_VARIABLE
TRACI_CONST int VAR_LENGTH
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int CMD_SET_PERSON_VARIABLE
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int RESPONSE_GET_PERSON_VARIABLE
TRACI_CONST int STAGE_WALKING
TRACI_CONST int REMOVE_STAGE
TRACI_CONST int VAR_SPEED
TRACI_CONST int STAGE_DRIVING
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD