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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
21 // APIs for getting/setting edge values via TraCI
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #include <config.h>
29 
30 #include <utils/common/StdDefs.h>
31 #include <microsim/MSNet.h>
32 #include <microsim/MSEdgeControl.h>
33 #include <microsim/MSEdge.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSVehicle.h>
37 #include "TraCIConstants.h"
38 #include "TraCIServerAPI_Edge.h"
41 #include <libsumo/Edge.h>
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 bool
49  tcpip::Storage& outputStorage) {
50  const int variable = inputStorage.readUnsignedByte();
51  const std::string id = inputStorage.readString();
52  server.initWrapper(RESPONSE_GET_EDGE_VARIABLE, variable, id);
53  try {
54  if (!libsumo::Edge::handleVariable(id, variable, &server)) {
55  switch (variable) {
56  case VAR_EDGE_TRAVELTIME: {
57  double time = 0.;
58  if (!server.readTypeCheckingDouble(inputStorage, time)) {
60  "The message must contain the time definition.", outputStorage);
61  }
64  break;
65  }
66  case VAR_EDGE_EFFORT: {
67  double time = 0.;
68  if (!server.readTypeCheckingDouble(inputStorage, time)) {
70  "The message must contain the time definition.", outputStorage);
71  }
74  break;
75  }
76  case VAR_PARAMETER: {
77  std::string paramName;
78  if (!server.readTypeCheckingString(inputStorage, paramName)) {
80  "Retrieval of a parameter requires its name.",
81  outputStorage);
82  }
85  break;
86  }
87  default:
89  "Get Edge Variable: unsupported variable " + toHex(variable, 2)
90  + " specified", outputStorage);
91  }
92  }
93  } catch (libsumo::TraCIException& e) {
94  return server.writeErrorStatusCmd(CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
95  }
96  server.writeStatusCmd(CMD_GET_EDGE_VARIABLE, RTYPE_OK, "", outputStorage);
97  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
98  return true;
99 }
100 
101 
102 bool
104  tcpip::Storage& outputStorage) {
105  std::string warning; // additional description for response
106  // variable
107  int variable = inputStorage.readUnsignedByte();
108  if (variable != VAR_EDGE_TRAVELTIME && variable != VAR_EDGE_EFFORT && variable != VAR_MAXSPEED
109  && variable != VAR_PARAMETER) {
111  "Change Edge State: unsupported variable " + toHex(variable, 2)
112  + " specified", outputStorage);
113  }
114  // id
115  std::string id = inputStorage.readString();
116  try {
117  // process
118  switch (variable) {
119  case LANE_ALLOWED: {
120  // read and set allowed vehicle classes
121  std::vector<std::string> classes;
122  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
124  "Allowed vehicle classes must be given as a list of strings.",
125  outputStorage);
126  }
128  break;
129  }
130  case LANE_DISALLOWED: {
131  // read and set disallowed vehicle classes
132  std::vector<std::string> classes;
133  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
135  "Not allowed vehicle classes must be given as a list of strings.",
136  outputStorage);
137  }
139  break;
140  }
141  case VAR_EDGE_TRAVELTIME: {
142  // read and set travel time
143  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
145  "Setting travel time requires a compound object.", outputStorage);
146  }
147  const int parameterCount = inputStorage.readInt();
148  if (parameterCount == 3) {
149  // bound by time
150  double begTime = 0., endTime = 0., value = 0.;
151  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
153  "The first variable must be the begin time given as double.",
154  outputStorage);
155  }
156  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
158  "The second variable must be the end time given as double.",
159  outputStorage);
160  }
161  if (!server.readTypeCheckingDouble(inputStorage, value)) {
163  "The third variable must be the value given as double",
164  outputStorage);
165  }
166  libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
167  } else if (parameterCount == 1) {
168  // unbound
169  double value = 0;
170  if (!server.readTypeCheckingDouble(inputStorage, value)) {
172  "The variable must be the value given as double", outputStorage);
173  }
174  libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
175  } else {
177  "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
178  outputStorage);
179  }
180  break;
181  }
182  case VAR_EDGE_EFFORT: {
183  // read and set effort
184  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
186  "Setting effort requires a compound object.",
187  outputStorage);
188  }
189  const int parameterCount = inputStorage.readInt();
190  if (parameterCount == 3) {
191  // bound by time
192  double begTime = 0., endTime = 0., value = 0.;
193  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
195  "The first variable must be the begin time given as double.",
196  outputStorage);
197  }
198  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
200  "The second variable must be the end time given as double.",
201  outputStorage);
202  }
203  if (!server.readTypeCheckingDouble(inputStorage, value)) {
205  "The third variable must be the value given as double",
206  outputStorage);
207  }
208  libsumo::Edge::setEffort(id, value, begTime, endTime);
209  } else if (parameterCount == 1) {
210  // unbound
211  double value = 0.;
212  if (!server.readTypeCheckingDouble(inputStorage, value)) {
214  "The variable must be the value given as double", outputStorage);
215  }
216  libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
217  } else {
219  "Setting effort requires either begin time, end time, and value, or only value as parameter.",
220  outputStorage);
221  }
222  break;
223  }
224  case VAR_MAXSPEED: {
225  // read and set max. speed
226  double value = 0.;
227  if (!server.readTypeCheckingDouble(inputStorage, value)) {
228  return server.writeErrorStatusCmd(CMD_SET_EDGE_VARIABLE, "The speed must be given as a double.",
229  outputStorage);
230  }
231  libsumo::Edge::setMaxSpeed(id, value);
232  break;
233  }
234  case VAR_PARAMETER: {
235  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
237  "A compound object is needed for setting a parameter.",
238  outputStorage);
239  }
240  //readt itemNo
241  inputStorage.readInt();
242  std::string name;
243  if (!server.readTypeCheckingString(inputStorage, name)) {
245  "The name of the parameter must be given as a string.",
246  outputStorage);
247  }
248  std::string value;
249  if (!server.readTypeCheckingString(inputStorage, value)) {
251  "The value of the parameter must be given as a string.",
252  outputStorage);
253  }
254  libsumo::Edge::setParameter(id, name, value);
255  break;
256  }
257  default:
258  break;
259  }
260  } catch (libsumo::TraCIException& e) {
261  return server.writeErrorStatusCmd(CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
262  }
263  server.writeStatusCmd(CMD_SET_EDGE_VARIABLE, RTYPE_OK, warning, outputStorage);
264  return true;
265 }
266 
267 
268 /****************************************************************************/
static void setEffort(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:326
#define TYPE_COMPOUND
#define RTYPE_OK
static double getAdaptedTraveltime(const std::string &id, double time)
Definition: Edge.cpp:56
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
static void setDisallowedVehicleClasses(const std::string &id, std::vector< std::string > classes)
Definition: Edge.cpp:300
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
virtual void writeUnsignedByte(int)
#define CMD_SET_EDGE_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define TYPE_STRING
virtual int readUnsignedByte()
static std::string getParameter(const std::string &id, const std::string &paramName)
Definition: Edge.cpp:287
#define LANE_ALLOWED
static void setAllowedVehicleClasses(const std::string &id, std::vector< std::string > vector)
Definition: Edge.cpp:293
virtual int readInt()
static double getEffort(const std::string &id, double time)
Definition: Edge.cpp:67
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
#define VAR_EDGE_EFFORT
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static void setMaxSpeed(const std::string &id, double value)
Definition: Edge.cpp:332
static void adaptTraveltime(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:320
#define VAR_EDGE_TRAVELTIME
virtual void writeString(const std::string &s)
#define LANE_DISALLOWED
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Edge.cpp:366
#define VAR_MAXSPEED
void initWrapper(const int domainID, const int variable, const std::string &objID)
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
Definition: Edge.cpp:340
#define VAR_PARAMETER
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
#define RESPONSE_GET_EDGE_VARIABLE