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 std::string warning = ""; // additional description for response
45 // variable
46 int variable = inputStorage.readUnsignedByte();
47 if (variable != libsumo::VAR_PARAMETER
48 && variable != libsumo::ADD
49 && variable != libsumo::REMOVE
50 && variable != libsumo::APPEND_STAGE
51 && variable != libsumo::REPLACE_STAGE
52 && variable != libsumo::REMOVE_STAGE
54 && variable != libsumo::VAR_MOVE_TO
55 && variable != libsumo::MOVE_TO_XY
56 && variable != libsumo::VAR_SPEED
57 && variable != libsumo::VAR_TYPE
58 && variable != libsumo::VAR_SPEED_FACTOR
59 && variable != libsumo::VAR_LENGTH
60 && variable != libsumo::VAR_WIDTH
61 && variable != libsumo::VAR_HEIGHT
62 && variable != libsumo::VAR_MINGAP
63 && variable != libsumo::VAR_COLOR
64 ) {
65 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Change Person State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
66 }
67
68 try {
69 // TODO: remove declaration of c after completion
71 // id
72 std::string id = inputStorage.readString();
73 // TODO: remove declaration of p after completion
74 const bool shouldExist = variable != libsumo::ADD;
75 MSTransportable* p = c.get(id);
76 if (p == nullptr && shouldExist) {
77 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Person '" + id + "' is not known", outputStorage);
78 }
79 // process
80 switch (variable) {
81 case libsumo::VAR_SPEED: {
82 // set the speed for all present and future (walking) stages and modify the vType so that stages added later are also affected
83 libsumo::Person::setSpeed(id, StoHelp::readTypedDouble(inputStorage, "Setting speed requires a double."));
84 }
85 break;
86 case libsumo::VAR_TYPE: {
87 libsumo::Person::setType(id, StoHelp::readTypedString(inputStorage, "The vehicle type id must be given as a string."));
88 break;
89 }
91 libsumo::Person::setSpeedFactor(id, StoHelp::readTypedDouble(inputStorage, "Setting SpeedFactor requires a double."));
92 }
93 break;
94 case libsumo::VAR_COLOR: {
95 libsumo::Person::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
96 break;
97 }
98 case libsumo::ADD: {
99 StoHelp::readCompound(inputStorage, 4, "Adding a person needs four parameters.");
100 const std::string vTypeID = StoHelp::readTypedString(inputStorage, "First parameter (type) requires a string.");
101 const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
102 const double depart = StoHelp::readTypedDouble(inputStorage, "Third parameter (depart) requires a double.");
103 const double pos = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (position) requires a double.");
104 libsumo::Person::add(id, edgeID, pos, depart, vTypeID);
105 }
106 break;
107 case libsumo::REMOVE: {
108 libsumo::Person::remove(id, (char)StoHelp::readTypedByte(inputStorage, "Removing a person requires a byte."));
109 }
110 break;
112 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Adding a person stage requires a compound object.");
113 if (parameterCount == 13) {
115 StoHelp::readStage(inputStorage, stage);
116 libsumo::Person::appendStage(id, stage);
117 } else {
118 const int stageType = StoHelp::readTypedInt(inputStorage, "The first parameter for adding a stage must be the stage type given as int.");
119 if (stageType == libsumo::STAGE_DRIVING) {
120 // append driving stage
121 if (parameterCount != 4) {
122 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a driving stage needs four parameters.", outputStorage);
123 }
124 const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
125 const std::string lines = StoHelp::readTypedString(inputStorage, "Third parameter (lines) requires a string.");
126 const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
127 libsumo::Person::appendDrivingStage(id, edgeID, lines, stopID);
128 } else if (stageType == libsumo::STAGE_WAITING) {
129 // append waiting stage
130 if (parameterCount != 4) {
131 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a waiting stage needs four parameters.", outputStorage);
132 }
133 const double duration = StoHelp::readTypedDouble(inputStorage, "Second parameter (duration) requires a double.");
134 const std::string description = StoHelp::readTypedString(inputStorage, "Third parameter (description) requires a string.");
135 const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
136 libsumo::Person::appendWaitingStage(id, duration, description, stopID);
137 } else if (stageType == libsumo::STAGE_WALKING) {
138 // append walking stage
139 if (parameterCount != 6) {
140 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a walking stage needs six parameters.", outputStorage);
141 }
142 const std::vector<std::string> edgeIDs = StoHelp::readTypedStringList(inputStorage, "Second parameter (edges) route must be defined as a list of edge ids.");
143 const double arrivalPos = StoHelp::readTypedDouble(inputStorage, "Third parameter (arrivalPos) requires a double.");
144 const double duration = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (duration) requires a double.");
145 const double speed = StoHelp::readTypedDouble(inputStorage, "Fifth parameter (speed) requires a double.");
146 const std::string stopID = StoHelp::readTypedString(inputStorage, "Sixth parameter (stopID) requires a string.");
147 libsumo::Person::appendWalkingStage(id, edgeIDs, arrivalPos, duration, speed, stopID);
148 } else {
149 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Invalid stage type for person '" + id + "'", outputStorage);
150 }
151 }
152 }
153 break;
155 StoHelp::readCompound(inputStorage, 2, "Replacing a person stage requires a compound object of size 2.");
156 const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "First parameter of replace stage should be an integer");
157 StoHelp::readCompound(inputStorage, 13, "Second parameter of replace stage should be a compound object of size 13");
159 StoHelp::readStage(inputStorage, stage);
160 libsumo::Person::replaceStage(id, nextStageIndex, stage);
161 }
162 break;
164 libsumo::Person::removeStage(id, StoHelp::readTypedInt(inputStorage, "The message must contain the stage index."));
165 }
166 break;
168 StoHelp::readCompound(inputStorage, 0, "Resuming requires an empty compound object.");
169 libsumo::Person::rerouteTraveltime(id);
170 }
171 break;
173 StoHelp::readCompound(inputStorage, 3, "Setting position should obtain the edge id, the position and the lateral position.");
174 const std::string laneID = StoHelp::readTypedString(inputStorage, "The first parameter for setting a position must be the laneID given as a string.");
175 const double position = StoHelp::readTypedDouble(inputStorage, "The second parameter for setting a position must be the position given as a double.");
176 const double posLat = StoHelp::readTypedDouble(inputStorage, "The third parameter for setting a position must be the lateral position given as a double.");
177 libsumo::Person::moveTo(id, laneID, position, posLat);
178 }
179 break;
180 case libsumo::MOVE_TO_XY: {
181 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "MoveToXY person requires a compound object.");
182 if (parameterCount != 5 && parameterCount != 6) {
183 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person should obtain: edgeID, x, y, angle, keepRouteFlag and optionally matchThreshold.", outputStorage);
184 }
185 const std::string edgeID = StoHelp::readTypedString(inputStorage, "The first parameter for moveToXY must be the edge ID given as a string.");
186 const double x = StoHelp::readTypedDouble(inputStorage, "The second parameter for moveToXY must be the x-position given as a double.");
187 const double y = StoHelp::readTypedDouble(inputStorage, "The third parameter for moveToXY must be the y-position given as a double.");
188 const double angle = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for moveToXY must be the angle given as a double.");
189 const int keepRouteFlag = StoHelp::readTypedByte(inputStorage, "The fifth parameter for moveToXY must be the keepRouteFlag given as a byte.");
190 double matchThreshold = 100.;
191 if (parameterCount == 6) {
192 matchThreshold = StoHelp::readTypedDouble(inputStorage, "The sixth parameter for moveToXY must be the matchThreshold given as a double.");
193 }
194 libsumo::Person::moveToXY(id, edgeID, x, y, angle, keepRouteFlag, matchThreshold);
195 }
196 break;
198 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
199 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
200 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
201 libsumo::Person::setParameter(id, name, value);
202 }
203 break;
204 default:
205 try {
206 if (!TraCIServerAPI_VehicleType::setVariable(libsumo::CMD_SET_PERSON_VARIABLE, variable, p->getSingularType().getID(), server, inputStorage, outputStorage)) {
207 return false;
208 }
209 } catch (ProcessError& e) {
210 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
211 }
212 break;
213 }
214 } catch (libsumo::TraCIException& e) {
215 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
216 }
218 return true;
219}
220
221
222/****************************************************************************/
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:187
virtual MSTransportableControl & getPersonControl()
Returns the person control.
Definition MSNet.cpp:1257
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 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 writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static int readTypedByte(tcpip::Storage &ret, const std::string &error="")
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIColor readTypedColor(tcpip::Storage &ret, const std::string &error="")
static void readStage(tcpip::Storage &inputStorage, libsumo::TraCIStage &stage, const std::string &error="")
static double readTypedDouble(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
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 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 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 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