SUMO - Simulation of Urban MObility
TraCIServerAPI_Junction.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // APIs for getting/setting junction values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2009-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 "TraCIConstants.h"
38 #include <microsim/MSNet.h>
39 #include <microsim/MSJunction.h>
41 #include <microsim/MSNet.h>
43 #include "lib/TraCI_Junction.h"
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
49 bool
51  tcpip::Storage& outputStorage) {
52  // variable
53  int variable = inputStorage.readUnsignedByte();
54  std::string id = inputStorage.readString();
55  // check variable
56  if (variable != ID_LIST && variable != VAR_POSITION && variable != ID_COUNT && variable != VAR_SHAPE) {
57  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, "Get Junction Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58  }
59  // begin response building
60  tcpip::Storage tempMsg;
61  // response-code, variableID, objectID
63  tempMsg.writeUnsignedByte(variable);
64  tempMsg.writeString(id);
65 
66  try {
67  switch (variable) {
68  case ID_LIST:
71  break;
72  case ID_COUNT:
75  break;
76  case VAR_POSITION: {
79  tempMsg.writeDouble(p.x);
80  tempMsg.writeDouble(p.y);
81  break;
82  }
83  case VAR_SHAPE: {
86  tempMsg.writeUnsignedByte(MIN2(255, (int) shp.size()));
87  for (int iPoint = 0; iPoint < MIN2(255, (int) shp.size()); ++iPoint) {
88  tempMsg.writeDouble(shp[iPoint].x);
89  tempMsg.writeDouble(shp[iPoint].y);
90  }
91  break;
92  }
93  default:
94  break;
95 
96  }
97  } catch (TraCIException& e) {
98  return server.writeErrorStatusCmd(CMD_GET_JUNCTION_VARIABLE, e.what(), outputStorage);
99  }
100  server.writeStatusCmd(CMD_GET_JUNCTION_VARIABLE, RTYPE_OK, "", outputStorage);
101  server.writeResponseWithLength(outputStorage, tempMsg);
102 
103  return true;
104 }
105 
106 bool
107 TraCIServerAPI_Junction::getPosition(const std::string& id, Position& p) {
109  if (j == 0) {
110  return false;
111  }
112  p = j->getPosition();
113  return true;
114 }
115 
116 
117 NamedRTree*
119  NamedRTree* t = new NamedRTree();
120  const std::map<std::string, MSJunction*>& junctions = MSNet::getInstance()->getJunctionControl().getMyMap();
121  for (std::map<std::string, MSJunction*>::const_iterator i = junctions.begin(); i != junctions.end(); ++i) {
122  Boundary b = (*i).second->getShape().getBoxBoundary();
123  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
124  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
125  t->Insert(cmin, cmax, (*i).second);
126  }
127  return t;
128 }
129 
130 #endif
131 
132 
133 /****************************************************************************/
134 
double x
Definition: TraCIDefs.h:72
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
#define POSITION_2D
#define VAR_POSITION
static NamedRTree * getTree()
Returns a tree filled with junction instances.
The base class for an intersection.
Definition: MSJunction.h:64
#define RTYPE_OK
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa9: Get Junction Variable)
#define TYPE_POLYGON
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:72
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
#define TYPE_STRINGLIST
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 VAR_SHAPE
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
virtual void writeInt(int)
virtual int readUnsignedByte()
static int getIDCount()
A 3D-position.
Definition: TraCIDefs.h:71
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
const IDMap & getMyMap() const
static std::vector< std::string > getIDList()
virtual void writeStringList(const std::vector< std::string > &s)
T MIN2(T a, T b)
Definition: StdDefs.h:64
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
const Position & getPosition() const
Definition: MSJunction.cpp:63
virtual std::string readString()
#define CMD_GET_JUNCTION_VARIABLE
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
static bool getPosition(const std::string &id, Position &p)
Returns the named junction&#39;s position.
#define RESPONSE_GET_JUNCTION_VARIABLE
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
double y
Definition: TraCIDefs.h:72
MSJunctionControl & getJunctionControl()
Returns the junctions control.
Definition: MSNet.h:393
static TraCIPositionVector getShape(const std::string &junctionID)
#define ID_COUNT
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
#define TYPE_INTEGER
#define ID_LIST
static TraCIPosition getPosition(const std::string &junctionID)
A list of positions.
static MSJunction * getJunction(const std::string &id)