Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_Edge.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2020 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 /****************************************************************************/
24 // APIs for getting/setting edge values via TraCI
25 /****************************************************************************/
26 #include <config.h>
27 
28 #include <utils/common/StdDefs.h>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSEdgeControl.h>
31 #include <microsim/MSEdge.h>
32 #include <microsim/MSLane.h>
33 #include <microsim/MSVehicle.h>
35 #include <libsumo/TraCIConstants.h>
36 #include "TraCIServerAPI_Edge.h"
39 #include <libsumo/Edge.h>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 bool
47  tcpip::Storage& outputStorage) {
48  const int variable = inputStorage.readUnsignedByte();
49  const std::string id = inputStorage.readString();
51  try {
52  if (!libsumo::Edge::handleVariable(id, variable, &server)) {
53  switch (variable) {
55  double time = 0.;
56  if (!server.readTypeCheckingDouble(inputStorage, time)) {
58  "The message must contain the time definition.", outputStorage);
59  }
61  server.getWrapperStorage().writeDouble(libsumo::Edge::getAdaptedTraveltime(id, time));
62  break;
63  }
65  double time = 0.;
66  if (!server.readTypeCheckingDouble(inputStorage, time)) {
68  "The message must contain the time definition.", outputStorage);
69  }
71  server.getWrapperStorage().writeDouble(libsumo::Edge::getEffort(id, time));
72  break;
73  }
75  std::string paramName;
76  if (!server.readTypeCheckingString(inputStorage, paramName)) {
78  "Retrieval of a parameter requires its name.",
79  outputStorage);
80  }
82  server.getWrapperStorage().writeString(libsumo::Edge::getParameter(id, paramName));
83  break;
84  }
86  std::string paramName;
87  if (!server.readTypeCheckingString(inputStorage, paramName)) {
89  "Retrieval of a parameter requires its name.",
90  outputStorage);
91  }
93  server.getWrapperStorage().writeInt(2);
95  server.getWrapperStorage().writeString(paramName);
97  server.getWrapperStorage().writeString(libsumo::Edge::getParameter(id, paramName));
98  break;
99  }
100  default:
102  "Get Edge Variable: unsupported variable " + toHex(variable, 2)
103  + " specified", outputStorage);
104  }
105  }
106  } catch (libsumo::TraCIException& e) {
107  return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
108  }
110  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
111  return true;
112 }
113 
114 
115 bool
117  tcpip::Storage& outputStorage) {
118  std::string warning; // additional description for response
119  // variable
120  int variable = inputStorage.readUnsignedByte();
121  if (variable != libsumo::VAR_EDGE_TRAVELTIME && variable != libsumo::VAR_EDGE_EFFORT && variable != libsumo::VAR_MAXSPEED
122  && variable != libsumo::VAR_PARAMETER) {
124  "Change Edge State: unsupported variable " + toHex(variable, 2)
125  + " specified", outputStorage);
126  }
127  // id
128  std::string id = inputStorage.readString();
129  try {
130  // process
131  switch (variable) {
132  case libsumo::LANE_ALLOWED: {
133  // read and set allowed vehicle classes
134  std::vector<std::string> classes;
135  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
137  "Allowed vehicle classes must be given as a list of strings.",
138  outputStorage);
139  }
140  libsumo::Edge::setAllowedVehicleClasses(id, classes);
141  break;
142  }
144  // read and set disallowed vehicle classes
145  std::vector<std::string> classes;
146  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
148  "Not allowed vehicle classes must be given as a list of strings.",
149  outputStorage);
150  }
151  libsumo::Edge::setDisallowedVehicleClasses(id, classes);
152  break;
153  }
155  // read and set travel time
156  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
158  "Setting travel time requires a compound object.", outputStorage);
159  }
160  const int parameterCount = inputStorage.readInt();
161  if (parameterCount == 3) {
162  // bound by time
163  double begTime = 0., endTime = 0., value = 0.;
164  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
166  "The first variable must be the begin time given as double.",
167  outputStorage);
168  }
169  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
171  "The second variable must be the end time given as double.",
172  outputStorage);
173  }
174  if (!server.readTypeCheckingDouble(inputStorage, value)) {
176  "The third variable must be the value given as double",
177  outputStorage);
178  }
179  libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
180  } else if (parameterCount == 1) {
181  // unbound
182  double value = 0;
183  if (!server.readTypeCheckingDouble(inputStorage, value)) {
185  "The variable must be the value given as double", outputStorage);
186  }
187  libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
188  } else {
190  "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
191  outputStorage);
192  }
193  break;
194  }
196  // read and set effort
197  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
199  "Setting effort requires a compound object.",
200  outputStorage);
201  }
202  const int parameterCount = inputStorage.readInt();
203  if (parameterCount == 3) {
204  // bound by time
205  double begTime = 0., endTime = 0., value = 0.;
206  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
208  "The first variable must be the begin time given as double.",
209  outputStorage);
210  }
211  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
213  "The second variable must be the end time given as double.",
214  outputStorage);
215  }
216  if (!server.readTypeCheckingDouble(inputStorage, value)) {
218  "The third variable must be the value given as double",
219  outputStorage);
220  }
221  libsumo::Edge::setEffort(id, value, begTime, endTime);
222  } else if (parameterCount == 1) {
223  // unbound
224  double value = 0.;
225  if (!server.readTypeCheckingDouble(inputStorage, value)) {
227  "The variable must be the value given as double", outputStorage);
228  }
229  libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
230  } else {
232  "Setting effort requires either begin time, end time, and value, or only value as parameter.",
233  outputStorage);
234  }
235  break;
236  }
237  case libsumo::VAR_MAXSPEED: {
238  // read and set max. speed
239  double value = 0.;
240  if (!server.readTypeCheckingDouble(inputStorage, value)) {
241  return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, "The speed must be given as a double.",
242  outputStorage);
243  }
244  libsumo::Edge::setMaxSpeed(id, value);
245  break;
246  }
247  case libsumo::VAR_PARAMETER: {
248  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
250  "A compound object is needed for setting a parameter.",
251  outputStorage);
252  }
253  //readt itemNo
254  inputStorage.readInt();
255  std::string name;
256  if (!server.readTypeCheckingString(inputStorage, name)) {
258  "The name of the parameter must be given as a string.",
259  outputStorage);
260  }
261  std::string value;
262  if (!server.readTypeCheckingString(inputStorage, value)) {
264  "The value of the parameter must be given as a string.",
265  outputStorage);
266  }
267  libsumo::Edge::setParameter(id, name, value);
268  break;
269  }
270  default:
271  break;
272  }
273  } catch (libsumo::TraCIException& e) {
274  return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
275  }
276  server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
277  return true;
278 }
279 
280 
281 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:54
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge 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()
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)
virtual std::string readString()
Definition: storage.cpp:175
virtual void writeString(const std::string &s)
Definition: storage.cpp:192
virtual void writeInt(int)
Definition: storage.cpp:316
virtual void writeDouble(double)
Definition: storage.cpp:349
virtual int readUnsignedByte()
Definition: storage.cpp:150
virtual void writeUnsignedByte(int)
Definition: storage.cpp:160
virtual int readInt()
Definition: storage.cpp:306
TRACI_CONST int CMD_SET_EDGE_VARIABLE
TRACI_CONST int VAR_EDGE_TRAVELTIME
TRACI_CONST int CMD_GET_EDGE_VARIABLE
TRACI_CONST int RESPONSE_GET_EDGE_VARIABLE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_EDGE_EFFORT
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int LANE_DISALLOWED
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int RTYPE_OK
TRACI_CONST int LANE_ALLOWED
TRACI_CONST int TYPE_STRING