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-2022 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/StorageHelper.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, &inputStorage)) {
54  switch (variable) {
56  const double time = StoHelp::readTypedDouble(inputStorage, "The message must contain the time definition.");
57  StoHelp::writeTypedDouble(server.getWrapperStorage(), libsumo::Edge::getAdaptedTraveltime(id, time));
58  break;
59  }
61  const double time = StoHelp::readTypedDouble(inputStorage, "The message must contain the time definition.");
62  StoHelp::writeTypedDouble(server.getWrapperStorage(), libsumo::Edge::getEffort(id, time));
63  break;
64  }
65  default:
67  "Get Edge Variable: unsupported variable " + toHex(variable, 2)
68  + " specified", outputStorage);
69  }
70  }
71  } catch (libsumo::TraCIException& e) {
72  return server.writeErrorStatusCmd(libsumo::CMD_GET_EDGE_VARIABLE, e.what(), outputStorage);
73  }
75  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
76  return true;
77 }
78 
79 
80 bool
82  tcpip::Storage& outputStorage) {
83  std::string warning; // additional description for response
84  // variable
85  int variable = inputStorage.readUnsignedByte();
86  if (variable != libsumo::VAR_EDGE_TRAVELTIME && variable != libsumo::VAR_EDGE_EFFORT && variable != libsumo::VAR_MAXSPEED
87  && variable != libsumo::VAR_PARAMETER) {
89  "Change Edge State: unsupported variable " + toHex(variable, 2)
90  + " specified", outputStorage);
91  }
92  // id
93  std::string id = inputStorage.readString();
94  try {
95  // process
96  switch (variable) {
97  case libsumo::LANE_ALLOWED: {
98  // read and set allowed vehicle classes
99  const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");
100  libsumo::Edge::setAllowedVehicleClasses(id, classes);
101  break;
102  }
104  // read and set disallowed vehicle classes
105  const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");
106  libsumo::Edge::setDisallowedVehicleClasses(id, classes);
107  break;
108  }
110  // read and set travel time
111  const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting travel time requires a compound object.");
112  if (parameterCount == 3) {
113  // bound by time
114  const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
115  const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
116  const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
117  libsumo::Edge::adaptTraveltime(id, value, begTime, endTime);
118  } else if (parameterCount == 1) {
119  // unbound
120  const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
121  libsumo::Edge::adaptTraveltime(id, value, 0., std::numeric_limits<double>::max());
122  } else {
124  "Setting travel time requires either begin time, end time, and value, or only value as parameter.",
125  outputStorage);
126  }
127  break;
128  }
130  // read and set effort
131  const int parameterCount = StoHelp::readCompound(inputStorage, -1, "Setting effort requires a compound object.");
132  if (parameterCount == 3) {
133  // bound by time
134  const double begTime = StoHelp::readTypedDouble(inputStorage, "The first variable must be the begin time given as double.");
135  const double endTime = StoHelp::readTypedDouble(inputStorage, "The second variable must be the end time given as double.");
136  const double value = StoHelp::readTypedDouble(inputStorage, "The third variable must be the value given as double.");
137  libsumo::Edge::setEffort(id, value, begTime, endTime);
138  } else if (parameterCount == 1) {
139  // unbound
140  const double value = StoHelp::readTypedDouble(inputStorage, "The variable must be the value given as double.");
141  libsumo::Edge::setEffort(id, value, 0., std::numeric_limits<double>::max());
142  } else {
144  "Setting effort requires either begin time, end time, and value, or only value as parameter.",
145  outputStorage);
146  }
147  break;
148  }
149  case libsumo::VAR_MAXSPEED: {
150  // read and set max. speed
151  const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");
152  libsumo::Edge::setMaxSpeed(id, value);
153  break;
154  }
155  case libsumo::VAR_PARAMETER: {
156  // read and check item number
157  StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
158  const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
159  const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
160  libsumo::Edge::setParameter(id, name, value);
161  break;
162  }
163  default:
164  break;
165  }
166  } catch (libsumo::TraCIException& e) {
167  return server.writeErrorStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, e.what(), outputStorage);
168  }
169  server.writeStatusCmd(libsumo::CMD_SET_EDGE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
170  return true;
171 }
172 
173 
174 /****************************************************************************/
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:56
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.
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 void writeTypedDouble(tcpip::Storage &content, double value)
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
Definition: StorageHelper.h:85
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:71
static std::vector< std::string > readTypedStringList(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:78
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
Definition: StorageHelper.h:64
An error which allows to continue.
Definition: TraCIDefs.h:130
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
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 VAR_EDGE_EFFORT
TRACI_CONST int VAR_MAXSPEED
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int LANE_DISALLOWED
TRACI_CONST int RTYPE_OK
TRACI_CONST int LANE_ALLOWED