SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-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 /****************************************************************************/
19 // APIs for getting/setting POI values via TraCI
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <microsim/MSNet.h>
31 #include "TraCIConstants.h"
32 #include <libsumo/POI.h>
33 #include "TraCIServerAPI_POI.h"
34 
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 bool
40  tcpip::Storage& outputStorage) {
41  const int variable = inputStorage.readUnsignedByte();
42  const std::string id = inputStorage.readString();
43  server.initWrapper(RESPONSE_GET_POI_VARIABLE, variable, id);
44  try {
45  if (!libsumo::POI::handleVariable(id, variable, &server)) {
46  switch (variable) {
47  case VAR_PARAMETER: {
48  std::string paramName = "";
49  if (!server.readTypeCheckingString(inputStorage, paramName)) {
50  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
51  }
54  break;
55  }
56  default:
57  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58  }
59  }
60  } catch (libsumo::TraCIException& e) {
61  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, e.what(), outputStorage);
62  }
63  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
64  server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
65  return true;
66 }
67 
68 
69 bool
71  tcpip::Storage& outputStorage) {
72  std::string warning = ""; // additional description for response
73  // variable & id
74  int variable = inputStorage.readUnsignedByte();
75  std::string id = inputStorage.readString();
76  // check variable
77  if (variable != VAR_TYPE &&
78  variable != VAR_COLOR &&
79  variable != VAR_POSITION &&
80  variable != ADD &&
81  variable != REMOVE &&
82  variable != VAR_PARAMETER) {
83  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
84  }
85  // process
86  try {
87  switch (variable) {
88  case VAR_TYPE: {
89  std::string type;
90  if (!server.readTypeCheckingString(inputStorage, type)) {
91  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
92  }
93  libsumo::POI::setType(id, type);
94  }
95  break;
96  case VAR_COLOR: {
98  if (!server.readTypeCheckingColor(inputStorage, col)) {
99  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
100  }
101  libsumo::POI::setColor(id, col);
102  }
103  break;
104  case VAR_POSITION: {
106  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
107  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
108  }
109  libsumo::POI::setPosition(id, pos.x, pos.y);
110  }
111  break;
112  case ADD: {
113  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
114  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
115  }
116  //read itemNo
117  inputStorage.readInt();
118  std::string type;
119  if (!server.readTypeCheckingString(inputStorage, type)) {
120  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
121  }
123  if (!server.readTypeCheckingColor(inputStorage, col)) {
124  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
125  }
126  int layer = 0;
127  if (!server.readTypeCheckingInt(inputStorage, layer)) {
128  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
129  }
131  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
132  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
133  }
134  //
135  if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
136  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
137  }
138  }
139  break;
140  case REMOVE: {
141  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
142  if (!server.readTypeCheckingInt(inputStorage, layer)) {
143  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
144  }
145  if (!libsumo::POI::remove(id, layer)) {
146  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
147  }
148  }
149  break;
150  case VAR_PARAMETER: {
151  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
152  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
153  }
154  //readt itemNo
155  inputStorage.readInt();
156  std::string name;
157  if (!server.readTypeCheckingString(inputStorage, name)) {
158  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
159  }
160  std::string value;
161  if (!server.readTypeCheckingString(inputStorage, value)) {
162  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
163  }
164  libsumo::POI::setParameter(id, name, value);
165  }
166  break;
167  default:
168  break;
169  }
170  } catch (libsumo::TraCIException& e) {
171  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, e.what(), outputStorage);
172  }
173  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
174  return true;
175 }
176 
177 
178 /****************************************************************************/
bool readTypeCheckingColor(tcpip::Storage &inputStorage, libsumo::TraCIColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_COMPOUND
static bool remove(const std::string &poiID, int layer=0)
Definition: POI.cpp:119
#define VAR_POSITION
static void setParameter(const std::string &poiID, const std::string &key, const std::string &value)
Definition: POI.cpp:126
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
static void setColor(const std::string &poiID, const TraCIColor &c)
Definition: POI.cpp:101
#define RTYPE_OK
#define VAR_TYPE
#define VAR_COLOR
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
static bool add(const std::string &poiID, double x, double y, const TraCIColor &color, const std::string &poiType="", int layer=0)
Definition: POI.cpp:107
virtual void writeUnsignedByte(int)
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()
virtual int readInt()
#define CMD_GET_POI_VARIABLE
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: POI.cpp:171
tcpip::Storage & getWrapperStorage()
virtual std::string readString()
#define CMD_SET_POI_VARIABLE
static void setType(const std::string &poiID, const std::string &setType)
Definition: POI.cpp:87
#define ADD
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:62
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
virtual void writeString(const std::string &s)
static std::string getParameter(const std::string &poiID, const std::string &key)
Definition: POI.cpp:81
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:59
#define RESPONSE_GET_POI_VARIABLE
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, libsumo::TraCIPosition &into)
Reads the value type and a 2D position, verifying the type.
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
void initWrapper(const int domainID, const int variable, const std::string &objID)
#define VAR_PARAMETER
A 3D-position.
Definition: TraCIDefs.h:107
static void setPosition(const std::string &poiID, double x, double y)
Definition: POI.cpp:93