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-2019 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 /****************************************************************************/
20 // APIs for getting/setting edge values via TraCI
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #include <config.h>
28 
29 #include <utils/common/StdDefs.h>
30 #include <microsim/MSNet.h>
31 #include <microsim/MSEdgeControl.h>
32 #include <microsim/MSEdge.h>
33 #include <microsim/MSLane.h>
34 #include <microsim/MSVehicle.h>
36 #include <libsumo/TraCIConstants.h>
37 #include "TraCIServerAPI_Edge.h"
40 #include <libsumo/Edge.h>
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 bool
48  tcpip::Storage& outputStorage) {
49  const int variable = inputStorage.readUnsignedByte();
50  const std::string id = inputStorage.readString();
52  try {
53  if (!libsumo::Edge::handleVariable(id, variable, &server)) {
54  switch (variable) {
56  double time = 0.;
57  if (!server.readTypeCheckingDouble(inputStorage, time)) {
59  "The message must contain the time definition.", outputStorage);
60  }
63  break;
64  }
66  double time = 0.;
67  if (!server.readTypeCheckingDouble(inputStorage, time)) {
69  "The message must contain the time definition.", outputStorage);
70  }
73  break;
74  }
76  std::string paramName;
77  if (!server.readTypeCheckingString(inputStorage, paramName)) {
79  "Retrieval of a parameter requires its name.",
80  outputStorage);
81  }
84  break;
85  }
86  default:
88  "Get Edge Variable: unsupported variable " + toHex(variable, 2)
89  + " specified", outputStorage);
90  }
91  }
92  } catch (libsumo::TraCIException& e) {
93  return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
94  }
96  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
97  return true;
98 }
99 
100 
101 bool
103  tcpip::Storage& outputStorage) {
104  std::string warning; // additional description for response
105  // variable
106  int variable = inputStorage.readUnsignedByte();
107  if (variable != libsumo::VAR_EDGE_TRAVELTIME && variable != libsumo::VAR_EDGE_EFFORT && variable != libsumo::VAR_MAXSPEED
108  && variable != libsumo::VAR_PARAMETER) {
110  "Change Edge State: unsupported variable " + toHex(variable, 2)
111  + " specified", outputStorage);
112  }
113  // id
114  std::string id = inputStorage.readString();
115  try {
116  // process
117  switch (variable) {
118  case libsumo::LANE_ALLOWED: {
119  // read and set allowed vehicle classes
120  std::vector<std::string> classes;
121  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
123  "Allowed vehicle classes must be given as a list of strings.",
124  outputStorage);
125  }
127  break;
128  }
130  // read and set disallowed vehicle classes
131  std::vector<std::string> classes;
132  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
134  "Not allowed vehicle classes must be given as a list of strings.",
135  outputStorage);
136  }
138  break;
139  }
141  // read and set travel time
142  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
144  "Setting travel time requires a compound object.", outputStorage);
145  }
146  const int parameterCount = inputStorage.readInt();
147  if (parameterCount == 3) {
148  // bound by time
149  double begTime = 0., endTime = 0., value = 0.;
150  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
152  "The first variable must be the begin time given as double.",
153  outputStorage);
154  }
155  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
157  "The second variable must be the end time given as double.",
158  outputStorage);
159  }
160  if (!server.readTypeCheckingDouble(inputStorage, value)) {
162  "The third variable must be the value given as double",
163  outputStorage);
164  }
165  libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
166  } else if (parameterCount == 1) {
167  // unbound
168  double value = 0;
169  if (!server.readTypeCheckingDouble(inputStorage, value)) {
171  "The variable must be the value given as double", outputStorage);
172  }
173  libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
174  } else {
176  "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
177  outputStorage);
178  }
179  break;
180  }
182  // read and set effort
183  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
185  "Setting effort requires a compound object.",
186  outputStorage);
187  }
188  const int parameterCount = inputStorage.readInt();
189  if (parameterCount == 3) {
190  // bound by time
191  double begTime = 0., endTime = 0., value = 0.;
192  if (!server.readTypeCheckingDouble(inputStorage, begTime)) {
194  "The first variable must be the begin time given as double.",
195  outputStorage);
196  }
197  if (!server.readTypeCheckingDouble(inputStorage, endTime)) {
199  "The second variable must be the end time given as double.",
200  outputStorage);
201  }
202  if (!server.readTypeCheckingDouble(inputStorage, value)) {
204  "The third variable must be the value given as double",
205  outputStorage);
206  }
207  libsumo::Edge::setEffort(id, value, begTime, endTime);
208  } else if (parameterCount == 1) {
209  // unbound
210  double value = 0.;
211  if (!server.readTypeCheckingDouble(inputStorage, value)) {
213  "The variable must be the value given as double", outputStorage);
214  }
215  libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
216  } else {
218  "Setting effort requires either begin time, end time, and value, or only value as parameter.",
219  outputStorage);
220  }
221  break;
222  }
223  case libsumo::VAR_MAXSPEED: {
224  // read and set max. speed
225  double value = 0.;
226  if (!server.readTypeCheckingDouble(inputStorage, value)) {
227  return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, "The speed must be given as a double.",
228  outputStorage);
229  }
230  libsumo::Edge::setMaxSpeed(id, value);
231  break;
232  }
233  case libsumo::VAR_PARAMETER: {
234  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
236  "A compound object is needed for setting a parameter.",
237  outputStorage);
238  }
239  //readt itemNo
240  inputStorage.readInt();
241  std::string name;
242  if (!server.readTypeCheckingString(inputStorage, name)) {
244  "The name of the parameter must be given as a string.",
245  outputStorage);
246  }
247  std::string value;
248  if (!server.readTypeCheckingString(inputStorage, value)) {
250  "The value of the parameter must be given as a string.",
251  outputStorage);
252  }
253  libsumo::Edge::setParameter(id, name, value);
254  break;
255  }
256  default:
257  break;
258  }
259  } catch (libsumo::TraCIException& e) {
260  return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
261  }
262  server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
263  return true;
264 }
265 
266 
267 /****************************************************************************/
libsumo::RTYPE_OK
TRACI_CONST int RTYPE_OK
Definition: TraCIConstants.h:352
libsumo::Edge::adaptTraveltime
static void adaptTraveltime(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:319
MSNet.h
libsumo::Edge::setParameter
static void setParameter(const std::string &id, const std::string &name, const std::string &value)
Definition: Edge.cpp:339
libsumo::VAR_MAXSPEED
TRACI_CONST int VAR_MAXSPEED
Definition: TraCIConstants.h:615
tcpip::Storage::writeUnsignedByte
virtual void writeUnsignedByte(int)
libsumo::Edge::setMaxSpeed
static void setMaxSpeed(const std::string &id, double value)
Definition: Edge.cpp:331
TraCIServer::readTypeCheckingString
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
Definition: TraCIServer.cpp:1414
TraCIServer::writeResponseWithLength
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Definition: TraCIServer.cpp:1366
libsumo::RESPONSE_GET_EDGE_VARIABLE
TRACI_CONST int RESPONSE_GET_EDGE_VARIABLE
Definition: TraCIConstants.h:241
libsumo::LANE_DISALLOWED
TRACI_CONST int LANE_DISALLOWED
Definition: TraCIConstants.h:600
libsumo::VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER
Definition: TraCIConstants.h:939
libsumo::VAR_EDGE_TRAVELTIME
TRACI_CONST int VAR_EDGE_TRAVELTIME
Definition: TraCIConstants.h:705
MSEdge.h
TraCIServer::getWrapperStorage
tcpip::Storage & getWrapperStorage()
Definition: TraCIServer.cpp:174
TraCIServer::readTypeCheckingDouble
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
Definition: TraCIServer.cpp:1404
MSVehicle.h
HelpersHarmonoise.h
TraCIServer::writeStatusCmd
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Definition: TraCIServer.cpp:968
TraCIServer::initWrapper
void initWrapper(const int domainID, const int variable, const std::string &objID)
Definition: TraCIServer.cpp:102
tcpip::Storage::readUnsignedByte
virtual int readUnsignedByte()
libsumo::TYPE_DOUBLE
TRACI_CONST int TYPE_DOUBLE
Definition: TraCIConstants.h:335
tcpip::Storage::readString
virtual std::string readString()
TraCIConstants.h
MSEdgeWeightsStorage.h
tcpip::Storage::readInt
virtual int readInt()
libsumo::Edge::getParameter
static std::string getParameter(const std::string &id, const std::string &paramName)
Definition: Edge.cpp:286
libsumo::Edge::setAllowedVehicleClasses
static void setAllowedVehicleClasses(const std::string &id, std::vector< std::string > vector)
Definition: Edge.cpp:292
libsumo::Edge::setDisallowedVehicleClasses
static void setDisallowedVehicleClasses(const std::string &id, std::vector< std::string > classes)
Definition: Edge.cpp:299
toHex
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:57
libsumo::Edge::setEffort
static void setEffort(const std::string &id, double value, double begTime=0., double endTime=std::numeric_limits< double >::max())
Definition: Edge.cpp:325
TraCIServer::readTypeCheckingStringList
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
Definition: TraCIServer.cpp:1424
libsumo::TraCIException
Definition: TraCIDefs.h:89
MSPerson.h
TraCIServerAPI_Edge::processSet
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
Definition: TraCIServerAPI_Edge.cpp:102
tcpip::Storage::writeString
virtual void writeString(const std::string &s)
MSEdgeControl.h
libsumo::LANE_ALLOWED
TRACI_CONST int LANE_ALLOWED
Definition: TraCIConstants.h:597
libsumo::TYPE_STRING
TRACI_CONST int TYPE_STRING
Definition: TraCIConstants.h:337
libsumo::VAR_EDGE_EFFORT
TRACI_CONST int VAR_EDGE_EFFORT
Definition: TraCIConstants.h:708
libsumo::Edge::getAdaptedTraveltime
static double getAdaptedTraveltime(const std::string &id, double time)
Definition: Edge.cpp:55
libsumo::Edge::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Edge.cpp:365
libsumo::CMD_SET_EDGE_VARIABLE
TRACI_CONST int CMD_SET_EDGE_VARIABLE
Definition: TraCIConstants.h:243
Edge.h
config.h
StdDefs.h
tcpip::Storage::writeDouble
virtual void writeDouble(double)
TraCIServer
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:61
MSLane.h
TraCIServerAPI_Edge.h
TraCIServerAPI_Edge::processGet
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
Definition: TraCIServerAPI_Edge.cpp:47
libsumo::CMD_GET_EDGE_VARIABLE
TRACI_CONST int CMD_GET_EDGE_VARIABLE
Definition: TraCIConstants.h:239
tcpip::Storage
Definition: storage.h:37
libsumo::Edge::getEffort
static double getEffort(const std::string &id, double time)
Definition: Edge.cpp:66
libsumo::TYPE_COMPOUND
TRACI_CONST int TYPE_COMPOUND
Definition: TraCIConstants.h:341
TraCIServer::writeErrorStatusCmd
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
Definition: TraCIServer.cpp:982