Eclipse SUMO - Simulation of Urban MObility
TraCIServerAPI_Calibrator.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
18 // APIs for getting/setting Calibrator values via TraCI
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <microsim/MSNet.h>
23 #include <microsim/MSEdge.h>
25 #include <libsumo/Calibrator.h>
26 #include <libsumo/TraCIConstants.h>
28 
29 
30 // ===========================================================================
31 // method definitions
32 // ===========================================================================
33 bool
35  tcpip::Storage& outputStorage) {
36  const int variable = inputStorage.readUnsignedByte();
37  const std::string id = inputStorage.readString();
39  try {
40  if (!libsumo::Calibrator::handleVariable(id, variable, &server)) {
41  switch (variable) {
43  std::string paramName = "";
44  if (!server.readTypeCheckingString(inputStorage, paramName)) {
45  return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
46  }
48  server.getWrapperStorage().writeString(libsumo::Calibrator::getParameter(id, paramName));
49  break;
50  }
52  std::string paramName = "";
53  if (!server.readTypeCheckingString(inputStorage, paramName)) {
54  return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
55  }
57  server.getWrapperStorage().writeInt(2);
59  server.getWrapperStorage().writeString(paramName);
61  server.getWrapperStorage().writeString(libsumo::Calibrator::getParameter(id, paramName));
62  break;
63  }
64  default:
65  return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, "Get Calibrator Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
66  }
67  }
68  } catch (libsumo::TraCIException& e) {
69  return server.writeErrorStatusCmd(libsumo::CMD_GET_CALIBRATOR_VARIABLE, e.what(), outputStorage);
70  }
72  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
73  return true;
74 }
75 
76 
77 bool
79  tcpip::Storage& outputStorage) {
80  std::string warning = ""; // additional description for response
81  // variable
82  int variable = inputStorage.readUnsignedByte();
83  if (variable != libsumo::CMD_SET_FLOW && variable != libsumo::VAR_PARAMETER) {
84  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Change Calibrator State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
85  }
86  // id
87  std::string id = inputStorage.readString();
88 
89  try {
90  // process
91  switch (variable) {
92  case libsumo::CMD_SET_FLOW: {
93  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
94  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "A compound object is needed for setting calibrator flow.", outputStorage);
95  }
96  int parameterCount = inputStorage.readInt();
97  double begin;
98  double end;
99  double vehsPerHour;
100  double speed;
101  std::string typeID;
102  std::string routeID;
103  std::string departLane;
104  std::string departSpeed;
105  if (parameterCount == 8) {
106  if (!server.readTypeCheckingDouble(inputStorage, begin)) {
107  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the begin time as the first value.", outputStorage);
108  }
109  if (!server.readTypeCheckingDouble(inputStorage, end)) {
110  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the end time as the second value.", outputStorage);
111  }
112  if (!server.readTypeCheckingDouble(inputStorage, vehsPerHour)) {
113  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the number of vehicles per hour as the third value.", outputStorage);
114  }
115  if (!server.readTypeCheckingDouble(inputStorage, speed)) {
116  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the speed as the fourth value.", outputStorage);
117  }
118  if (!server.readTypeCheckingString(inputStorage, typeID)) {
119  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the type id as the fifth value.", outputStorage);
120  }
121  if (!server.readTypeCheckingString(inputStorage, routeID)) {
122  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the route id as the sixth value.", outputStorage);
123  }
124  if (!server.readTypeCheckingString(inputStorage, departLane)) {
125  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the departLane as the seventh value.", outputStorage);
126  }
127  if (!server.readTypeCheckingString(inputStorage, departSpeed)) {
128  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "Setting flow requires the departSpeed as the eigth value.", outputStorage);
129  }
130  } else {
131  return server.writeErrorStatusCmd(libsumo::CMD_GET_VEHICLE_VARIABLE, "Setting calibrator flow requires 8 parameters.", outputStorage);
132  }
133  libsumo::Calibrator::setFlow(id, begin, end, vehsPerHour, speed, typeID, routeID, departLane, departSpeed);
134  }
135  break;
136  case libsumo::VAR_PARAMETER: {
137  if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
138  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
139  }
140  //read itemNo
141  inputStorage.readInt();
142  std::string name;
143  if (!server.readTypeCheckingString(inputStorage, name)) {
144  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
145  }
146  std::string value;
147  if (!server.readTypeCheckingString(inputStorage, value)) {
148  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
149  }
150  libsumo::Calibrator::setParameter(id, name, value);
151  }
152  break;
153  default:
154  break;
155  }
156  } catch (libsumo::TraCIException& e) {
157  return server.writeErrorStatusCmd(libsumo::CMD_SET_CALIBRATOR_VARIABLE, e.what(), outputStorage);
158  }
160  return true;
161 }
162 
163 
164 /****************************************************************************/
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 0xc6: Change Calibrator State)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Calibrator 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 readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: Calibrator.cpp:203
LIBSUMO_ID_PARAMETER_API static LIBSUMO_SUBSCRIPTION_API void setFlow(const std::string &calibratorID, double begin, double end, double vehsPerHour, double speed, const std::string &typeID, const std::string &routeID, const std::string &departLane="first", const std::string &departSpeed="max")
Definition: Calibrator.cpp:152
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 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_FLOW
TRACI_CONST int CMD_GET_VEHICLE_VARIABLE
TRACI_CONST int CMD_SET_CALIBRATOR_VARIABLE
TRACI_CONST int CMD_GET_CALIBRATOR_VARIABLE
TRACI_CONST int TYPE_COMPOUND
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int RESPONSE_GET_CALIBRATOR_VARIABLE
TRACI_CONST int RTYPE_OK
TRACI_CONST int TYPE_STRING