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 // set the speed for all present and future (walking) stages and modify the vType so that stages added later are also affected
102 libsumo::Person::setSpeed(id, StoHelp::readTypedDouble(inputStorage, "Setting speed requires a double."));
103 }
104 break;
105 case libsumo::VAR_TYPE: {
106 libsumo::Person::setType(id, StoHelp::readTypedString(inputStorage, "The vehicle type id must be given as a string."));
107 break;
108 }
110 libsumo::Person::setSpeedFactor(id, StoHelp::readTypedDouble(inputStorage, "Setting SpeedFactor requires a double."));
111 }
112 break;
113 case libsumo::VAR_COLOR: {
114 libsumo::Person::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
115 break;
116 }
117 case libsumo::ADD: {
118 StoHelp::readCompound(inputStorage, 4, "Adding a person needs four parameters.");
119 const std::string vTypeID = StoHelp::readTypedString(inputStorage, "First parameter (type) requires a string.");
120 const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
121 const double depart = StoHelp::readTypedDouble(inputStorage, "Third parameter (depart) requires a double.");
122 const double pos = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (position) requires a double.");
123 libsumo::Person::add(id, edgeID, pos, depart, vTypeID);
124 }
125 break;
126 case libsumo::REMOVE: {
127 libsumo::Person::remove(id, (char)StoHelp::readTypedByte(inputStorage, "Removing a person requires a byte."));
128 }
129 break;
131 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Adding a person stage requires a compound object.");
132 if (parameterCount == 13) {
134 StoHelp::readStage(inputStorage, stage);
135 libsumo::Person::appendStage(id, stage);
136 } else {
137 const int stageType = StoHelp::readTypedInt(inputStorage, "The first parameter for adding a stage must be the stage type given as int.");
138 if (stageType == libsumo::STAGE_DRIVING) {
139 // append driving stage
140 if (parameterCount != 4) {
141 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a driving stage needs four parameters.", outputStorage);
142 }
143 const std::string edgeID = StoHelp::readTypedString(inputStorage, "Second parameter (edge) requires a string.");
144 const std::string lines = StoHelp::readTypedString(inputStorage, "Third parameter (lines) requires a string.");
145 const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
146 libsumo::Person::appendDrivingStage(id, edgeID, lines, stopID);
147 } else if (stageType == libsumo::STAGE_WAITING) {
148 // append waiting stage
149 if (parameterCount != 4) {
150 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a waiting stage needs four parameters.", outputStorage);
151 }
152 const double duration = StoHelp::readTypedDouble(inputStorage, "Second parameter (duration) requires a double.");
153 const std::string description = StoHelp::readTypedString(inputStorage, "Third parameter (description) requires a string.");
154 const std::string stopID = StoHelp::readTypedString(inputStorage, "Fourth parameter (stopID) requires a string.");
155 libsumo::Person::appendWaitingStage(id, duration, description, stopID);
156 } else if (stageType == libsumo::STAGE_WALKING) {
157 // append walking stage
158 if (parameterCount != 6) {
159 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Adding a walking stage needs six parameters.", outputStorage);
160 }
161 const std::vector<std::string> edgeIDs = StoHelp::readTypedStringList(inputStorage, "Second parameter (edges) route must be defined as a list of edge ids.");
162 const double arrivalPos = StoHelp::readTypedDouble(inputStorage, "Third parameter (arrivalPos) requires a double.");
163 const double duration = StoHelp::readTypedDouble(inputStorage, "Fourth parameter (duration) requires a double.");
164 const double speed = StoHelp::readTypedDouble(inputStorage, "Fifth parameter (speed) requires a double.");
165 const std::string stopID = StoHelp::readTypedString(inputStorage, "Sixth parameter (stopID) requires a string.");
166 libsumo::Person::appendWalkingStage(id, edgeIDs, arrivalPos, duration, speed, stopID);
167 } else {
168 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "Invalid stage type for person '" + id + "'", outputStorage);
169 }
170 }
171 }
172 break;
174 StoHelp::readCompound(inputStorage, 2, "Replacing a person stage requires a compound object of size 2.");
175 const int nextStageIndex = StoHelp::readTypedInt(inputStorage, "First parameter of replace stage should be an integer");
176 StoHelp::readCompound(inputStorage, 13, "Second parameter of replace stage should be a compound object of size 13");
178 StoHelp::readStage(inputStorage, stage);
179 libsumo::Person::replaceStage(id, nextStageIndex, stage);
180 }
181 break;
183 libsumo::Person::removeStage(id, StoHelp::readTypedInt(inputStorage, "The message must contain the stage index."));
184 }
185 break;
187 StoHelp::readCompound(inputStorage, 0, "Resuming requires an empty compound object.");
188 libsumo::Person::rerouteTraveltime(id);
189 }
190 break;
192 StoHelp::readCompound(inputStorage, 3, "Setting position should obtain the edge id, the position and the lateral position.");
193 const std::string laneID = StoHelp::readTypedString(inputStorage, "The first parameter for setting a position must be the laneID given as a string.");
194 const double position = StoHelp::readTypedDouble(inputStorage, "The second parameter for setting a position must be the position given as a double.");
195 const double posLat = StoHelp::readTypedDouble(inputStorage, "The third parameter for setting a position must be the lateral position given as a double.");
196 libsumo::Person::moveTo(id, laneID, position, posLat);
197 }
198 break;
199 case libsumo::MOVE_TO_XY: {
200 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "MoveToXY person requires a compound object.");
201 if (parameterCount != 5 && parameterCount != 6) {
202 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, "MoveToXY person should obtain: edgeID, x, y, angle, keepRouteFlag and optionally matchThreshold.", outputStorage);
203 }
204 const std::string edgeID = StoHelp::readTypedString(inputStorage, "The first parameter for moveToXY must be the edge ID given as a string.");
205 const double x = StoHelp::readTypedDouble(inputStorage, "The second parameter for moveToXY must be the x-position given as a double.");
206 const double y = StoHelp::readTypedDouble(inputStorage, "The third parameter for moveToXY must be the y-position given as a double.");
207 const double angle = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for moveToXY must be the angle given as a double.");
208 const int keepRouteFlag = StoHelp::readTypedByte(inputStorage, "The fifth parameter for moveToXY must be the keepRouteFlag given as a byte.");
209 double matchThreshold = 100.;
210 if (parameterCount == 6) {
211 matchThreshold = StoHelp::readTypedDouble(inputStorage, "The sixth parameter for moveToXY must be the matchThreshold given as a double.");
212 }
213 libsumo::Person::moveToXY(id, edgeID, x, y, angle, keepRouteFlag, matchThreshold);
214 }
215 break;
217 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
218 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
219 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
220 libsumo::Person::setParameter(id, name, value);
221 }
222 break;
223 default:
224 try {
225 if (!TraCIServerAPI_VehicleType::setVariable(libsumo::CMD_SET_PERSON_VARIABLE, variable, p->getSingularType().getID(), server, inputStorage, outputStorage)) {
226 return false;
227 }
228 } catch (ProcessError& e) {
229 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
230 }
231 break;
232 }
233 } catch (libsumo::TraCIException& e) {
234 return server.writeErrorStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, e.what(), outputStorage);
235 }
237 return true;
238}
239
240
241/****************************************************************************/
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 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.
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.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
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 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 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