SUMO - Simulation of Urban MObility
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // APIs for getting/setting POI values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2017-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifndef NO_TRACI
36 
37 #include <microsim/MSNet.h>
40 #include "TraCIConstants.h"
41 #include "lib/TraCI_POI.h"
42 #include "TraCIServerAPI_POI.h"
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 bool
49  tcpip::Storage& outputStorage) {
50 
51  // variable & id
52  int variable = inputStorage.readUnsignedByte();
53  std::string id = inputStorage.readString();
54  // check variable
55  if (variable != ID_LIST &&
56  variable != VAR_TYPE &&
57  variable != VAR_COLOR &&
58  variable != VAR_POSITION &&
59  variable != VAR_POSITION3D &&
60  variable != ID_COUNT &&
61  variable != VAR_PARAMETER) {
62  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Get PoI Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
63  }
64  // begin response building
65  tcpip::Storage tempMsg;
66  // response-code, variableID, objectID
68  tempMsg.writeUnsignedByte(variable);
69  tempMsg.writeString(id);
70  // process request
71  try {
72  switch (variable) {
73  case ID_LIST:
76  break;
77  case ID_COUNT:
79  tempMsg.writeInt((int) TraCI_POI::getIDCount());
80  break;
81  case VAR_TYPE:
83  tempMsg.writeString(TraCI_POI::getType(id));
84  break;
85  case VAR_COLOR:
91  break;
92  case VAR_POSITION:
94  tempMsg.writeDouble(TraCI_POI::getPosition(id).x);
95  tempMsg.writeDouble(TraCI_POI::getPosition(id).y);
96  break;
97  case VAR_POSITION3D:
99  tempMsg.writeDouble(TraCI_POI::getPosition(id).x);
100  tempMsg.writeDouble(TraCI_POI::getPosition(id).y);
101  tempMsg.writeDouble(TraCI_POI::getPosition(id).z);
102  break;
103  case VAR_PARAMETER: {
104  std::string paramName = "";
105  if (!server.readTypeCheckingString(inputStorage, paramName)) {
106  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
107  }
109  tempMsg.writeString(TraCI_POI::getParameter(id, paramName));
110  break;
111  }
112  default:
113  break;
114  }
115  } catch (TraCIException& e) {
116  return server.writeErrorStatusCmd(CMD_GET_POI_VARIABLE, e.what(), outputStorage);
117  }
118 
119  server.writeStatusCmd(CMD_GET_POI_VARIABLE, RTYPE_OK, "", outputStorage);
120  server.writeResponseWithLength(outputStorage, tempMsg);
121 
122  return true;
123 }
124 
125 
126 bool
128  tcpip::Storage& outputStorage) {
129  std::string warning = ""; // additional description for response
130  // variable & id
131  int variable = inputStorage.readUnsignedByte();
132  std::string id = inputStorage.readString();
133  // check variable
134  if (variable != VAR_TYPE &&
135  variable != VAR_COLOR &&
136  variable != VAR_POSITION &&
137  variable != ADD &&
138  variable != REMOVE &&
139  variable != VAR_PARAMETER) {
140  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
141  }
142  // process
143  try {
144  switch (variable) {
145  case VAR_TYPE: {
146  std::string type;
147  if (!server.readTypeCheckingString(inputStorage, type)) {
148  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The type must be given as a string.", outputStorage);
149  }
150  TraCI_POI::setType(id, type);
151  }
152  break;
153  case VAR_COLOR: {
154  TraCIColor col;
155  if (!server.readTypeCheckingColor(inputStorage, col)) {
156  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The color must be given using an according type.", outputStorage);
157  }
158  TraCI_POI::setColor(id, col);
159  }
160  break;
161  case VAR_POSITION: {
162  TraCIPosition pos;
163  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
164  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The position must be given using an accoring type.", outputStorage);
165  }
166  TraCI_POI::setPosition(id, pos);
167  }
168  break;
169  case ADD: {
170  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
171  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a new PoI.", outputStorage);
172  }
173  //read itemNo
174  inputStorage.readInt();
175  std::string type;
176  if (!server.readTypeCheckingString(inputStorage, type)) {
177  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The first PoI parameter must be the type encoded as a string.", outputStorage);
178  }
179  TraCIColor col;
180  if (!server.readTypeCheckingColor(inputStorage, col)) {
181  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The second PoI parameter must be the color.", outputStorage);
182  }
183  int layer = 0;
184  if (!server.readTypeCheckingInt(inputStorage, layer)) {
185  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The third PoI parameter must be the layer encoded as int.", outputStorage);
186  }
187  TraCIPosition pos;
188  if (!server.readTypeCheckingPosition2D(inputStorage, pos)) {
189  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The fourth PoI parameter must be the position.", outputStorage);
190  }
191  //
192  if (!TraCI_POI::add(id, pos, col, type, layer)) {
193  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
194  }
195  }
196  break;
197  case REMOVE: {
198  int layer = 0; // !!! layer not used yet (shouldn't the id be enough?)
199  if (!server.readTypeCheckingInt(inputStorage, layer)) {
200  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The layer must be given using an int.", outputStorage);
201  }
202  if (!TraCI_POI::remove(id, layer)) {
203  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
204  }
205  }
206  break;
207  case VAR_PARAMETER: {
208  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
209  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
210  }
211  //readt itemNo
212  inputStorage.readInt();
213  std::string name;
214  if (!server.readTypeCheckingString(inputStorage, name)) {
215  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
216  }
217  std::string value;
218  if (!server.readTypeCheckingString(inputStorage, value)) {
219  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
220  }
221  TraCI_POI::addParameter(id, name, value);
222  }
223  break;
224  default:
225  break;
226  }
227  } catch (TraCIException& e) {
228  return server.writeErrorStatusCmd(CMD_SET_POI_VARIABLE, e.what(), outputStorage);
229  }
230  server.writeStatusCmd(CMD_SET_POI_VARIABLE, RTYPE_OK, warning, outputStorage);
231  return true;
232 }
233 
234 
235 bool
236 TraCIServerAPI_POI::getPosition(const std::string& id, Position& p) {
237  PointOfInterest* poi = getPoI(id);
238  if (poi == 0) {
239  return false;
240  }
241  p = *poi;
242  return true;
243 }
244 
245 
247 TraCIServerAPI_POI::getPoI(const std::string& id) {
249 }
250 
251 
252 NamedRTree*
254  NamedRTree* t = new NamedRTree();
256  const std::map<std::string, PointOfInterest*>& pois = shapeCont.getPOIs().getMyMap();
257  for (std::map<std::string, PointOfInterest*>::const_iterator i = pois.begin(); i != pois.end(); ++i) {
258  const float cmin[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
259  const float cmax[2] = {(float)(*i).second->x(), (float)(*i).second->y()};
260  t->Insert(cmin, cmax, (*i).second);
261  }
262  return t;
263 }
264 
265 
266 #endif
267 
268 
269 /****************************************************************************/
270 
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
bool readTypeCheckingColor(tcpip::Storage &inputStorage, TraCIColor &into)
Reads the value type and a color, verifying the type.
#define TYPE_COMPOUND
static void setColor(const std::string &poiID, const TraCIColor &c)
Definition: TraCI_POI.cpp:101
#define POSITION_2D
#define VAR_POSITION
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
#define VAR_TYPE
static void setType(const std::string &poiID, const std::string &setType)
Definition: TraCI_POI.cpp:89
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:72
#define VAR_COLOR
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
static void addParameter(const std::string &poiID, const std::string &param, const std::string &value)
Definition: TraCI_POI.cpp:124
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_COLOR
#define TYPE_STRINGLIST
#define POSITION_3D
static std::vector< std::string > getIDList()
Definition: TraCI_POI.cpp:48
Storage for geometrical objects.
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.
virtual void writeInt(int)
#define VAR_POSITION3D
#define TYPE_STRING
virtual int readUnsignedByte()
static NamedRTree * getTree()
Returns a tree filled with PoI instances.
virtual int readInt()
A 3D-position.
Definition: TraCIDefs.h:71
ShapeContainer & getShapeContainer()
Returns the shapes container.
Definition: MSNet.h:433
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
#define CMD_GET_POI_VARIABLE
static std::string getParameter(const std::string &poiID, const std::string &param)
Definition: TraCI_POI.cpp:83
const IDMap & getMyMap() const
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
#define CMD_SET_POI_VARIABLE
static bool getPosition(const std::string &id, Position &p)
Returns the named PoI&#39;s position.
#define ADD
#define REMOVE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
static TraCIPosition getPosition(const std::string &poiID)
Definition: TraCI_POI.cpp:73
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)
static bool add(const std::string &poiID, const TraCIPosition &pos, const TraCIColor &c, const std::string &type, int layer)
Definition: TraCI_POI.cpp:107
virtual void writeString(const std::string &s)
static TraCIColor getColor(const std::string &poiID)
Definition: TraCI_POI.cpp:66
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static void setPosition(const std::string &poiID, const TraCIPosition &pos)
Definition: TraCI_POI.cpp:95
#define RESPONSE_GET_POI_VARIABLE
static std::string getType(const std::string &poiID)
Definition: TraCI_POI.cpp:61
virtual void writeDouble(double)
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, 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.
A point-of-interest.
static PointOfInterest * getPoI(const std::string &id)
Returns the named PoI.
static int getIDCount()
Definition: TraCI_POI.cpp:56
#define VAR_PARAMETER
#define ID_COUNT
static bool remove(const std::string &poiID, int layer=0)
Definition: TraCI_POI.cpp:118
#define TYPE_INTEGER
#define ID_LIST
const POIs & getPOIs() const
Returns all pois.