Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/traci-server/TraCIServerAPI_Person.cpp
193674 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2026 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
/****************************************************************************/
14
/// @file TraCIServerAPI_Person.cpp
15
/// @author Daniel Krajzewicz
16
/// @date 26.05.2014
17
///
18
// APIs for getting/setting person values via TraCI
19
/****************************************************************************/
20
#include <config.h>
21
22
#include <utils/common/StringTokenizer.h>
23
#include <microsim/transportables/MSTransportableControl.h>
24
#include <microsim/MSVehicleControl.h>
25
#include <microsim/transportables/MSPerson.h>
26
#include <microsim/MSNet.h>
27
#include <microsim/MSEdge.h>
28
#include <libsumo/Person.h>
29
#include <libsumo/StorageHelper.h>
30
#include <libsumo/TraCIConstants.h>
31
#include <libsumo/VehicleType.h>
32
#include "TraCIServer.h"
33
#include "TraCIServerAPI_VehicleType.h"
34
#include "TraCIServerAPI_Person.h"
35
#include "TraCIServerAPI_Simulation.h"
36
37
38
// ===========================================================================
39
// method definitions
40
// ===========================================================================
41
bool
42
TraCIServerAPI_Person::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
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
53
&& variable != libsumo::CMD_REROUTE_TRAVELTIME
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
70
MSTransportableControl& c = MSNet::getInstance()->getPersonControl();
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
}
90
case libsumo::VAR_SPEED_FACTOR: {
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;
111
case libsumo::APPEND_STAGE: {
112
const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Adding a person stage requires a compound object.");
113
if (parameterCount == 13) {
114
libsumo::TraCIStage stage;
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;
154
case libsumo::REPLACE_STAGE: {
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");
158
libsumo::TraCIStage stage;
159
StoHelp::readStage(inputStorage, stage);
160
libsumo::Person::replaceStage(id, nextStageIndex, stage);
161
}
162
break;
163
case libsumo::REMOVE_STAGE: {
164
libsumo::Person::removeStage(id, StoHelp::readTypedInt(inputStorage, "The message must contain the stage index."));
165
}
166
break;
167
case libsumo::CMD_REROUTE_TRAVELTIME: {
168
StoHelp::readCompound(inputStorage, 0, "Resuming requires an empty compound object.");
169
libsumo::Person::rerouteTraveltime(id);
170
}
171
break;
172
case libsumo::VAR_MOVE_TO: {
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;
197
case libsumo::VAR_PARAMETER: {
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
}
217
server.writeStatusCmd(libsumo::CMD_SET_PERSON_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
218
return true;
219
}
220
221
222
/****************************************************************************/
223
224