Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServerAPI_POI.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2017-2026 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/****************************************************************************/
22// APIs for getting/setting POI values via TraCI
23/****************************************************************************/
24#include <config.h>
25
26#include <microsim/MSNet.h>
29#include <libsumo/POI.h>
32#include "TraCIServerAPI_POI.h"
33
34
35// ===========================================================================
36// method definitions
37// ===========================================================================
38bool
40 tcpip::Storage& outputStorage) {
41 std::string warning = ""; // additional description for response
42 // variable & id
43 int variable = inputStorage.readUnsignedByte();
44 std::string id = inputStorage.readString();
45 // check variable
46 if (variable != libsumo::VAR_TYPE &&
47 variable != libsumo::VAR_COLOR &&
48 variable != libsumo::VAR_POSITION &&
49 variable != libsumo::VAR_WIDTH &&
50 variable != libsumo::VAR_HEIGHT &&
51 variable != libsumo::VAR_ANGLE &&
52 variable != libsumo::VAR_IMAGEFILE &&
53 variable != libsumo::VAR_HIGHLIGHT &&
54 variable != libsumo::ADD &&
55 variable != libsumo::REMOVE &&
56 variable != libsumo::VAR_PARAMETER) {
57 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Change PoI State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
58 }
59 // process
60 try {
61 switch (variable) {
62 case libsumo::VAR_TYPE: {
63 libsumo::POI::setType(id, StoHelp::readTypedString(inputStorage, "The type must be given as a string."));
64 }
65 break;
66 case libsumo::VAR_COLOR: {
67 libsumo::POI::setColor(id, StoHelp::readTypedColor(inputStorage, "The color must be given using the according type."));
68 }
69 break;
71 const libsumo::TraCIPosition pos = StoHelp::readTypedPosition2D(inputStorage, "The position must be given using an according type.");
72 libsumo::POI::setPosition(id, pos.x, pos.y);
73 }
74 break;
75 case libsumo::VAR_WIDTH: {
76 libsumo::POI::setWidth(id, StoHelp::readTypedDouble(inputStorage, "The width must be given as a double."));
77 }
78 break;
80 libsumo::POI::setHeight(id, StoHelp::readTypedDouble(inputStorage, "The height must be given as a double."));
81 }
82 break;
83 case libsumo::VAR_ANGLE: {
84 libsumo::POI::setAngle(id, StoHelp::readTypedDouble(inputStorage, "The angle must be given as a double."));
85 }
86 break;
88 libsumo::POI::setImageFile(id, StoHelp::readTypedString(inputStorage, "The image file must be given as a string."));
89 }
90 break;
92 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for highlighting an object.");
93 if (parameterCount > 5) {
94 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Highlighting an object needs zero to five parameters.", outputStorage);
95 }
97 if (parameterCount > 0) {
98 col = StoHelp::readTypedColor(inputStorage, "The first parameter for highlighting must be the highlight color.");
99 }
100 double size = -1;
101 if (parameterCount > 1) {
102 size = StoHelp::readTypedDouble(inputStorage, "The second parameter for highlighting must be the highlight size.");
103 }
104 int alphaMax = -1;
105 if (parameterCount > 2) {
106 alphaMax = StoHelp::readTypedUnsignedByte(inputStorage, "The third parameter for highlighting must be maximal alpha.");
107 }
108 double duration = -1;
109 if (parameterCount > 3) {
110 duration = StoHelp::readTypedDouble(inputStorage, "The fourth parameter for highlighting must be the highlight duration.");
111 }
112 int type = 0;
113 if (parameterCount > 4) {
114 type = StoHelp::readTypedUnsignedByte(inputStorage, "The fifth parameter for highlighting must be the highlight type id as ubyte.");
115 }
116 libsumo::POI::highlight(id, col, size, alphaMax, duration, type);
117 }
118 break;
119 case libsumo::ADD: {
120 const int parameterCount = StoHelp::readCompound(inputStorage, -1, "A compound object is needed for adding a new PoI.");
121 const std::string type = StoHelp::readTypedString(inputStorage, "The first PoI parameter must be the type encoded as a string.");
122 libsumo::TraCIColor col = StoHelp::readTypedColor(inputStorage, "The second PoI parameter must be the color.");
123 const int layer = StoHelp::readTypedInt(inputStorage, "The third PoI parameter must be the layer encoded as int.");
124 const libsumo::TraCIPosition pos = StoHelp::readTypedPosition2D(inputStorage, "The fourth PoI parameter must be the position.");
125 if (parameterCount == 4) {
126 if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer)) {
127 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
128 }
129 } else if (parameterCount >= 8) {
130 const std::string imgFile = StoHelp::readTypedString(inputStorage, "The fifth PoI parameter must be the imgFile encoded as a string.");
131 const double width = StoHelp::readTypedDouble(inputStorage, "The sixth PoI parameter must be the width encoded as a double.");
132 const double height = StoHelp::readTypedDouble(inputStorage, "The seventh PoI parameter must be the height encoded as a double.");
133 const double angle = StoHelp::readTypedDouble(inputStorage, "The eighth PoI parameter must be the angle encoded as a double.");
134 std::string icon;
135 if (parameterCount == 9) {
136 icon = StoHelp::readTypedString(inputStorage, "The ninth PoI parameter must be the icon encoded as a string.");
137 }
138 if (!libsumo::POI::add(id, pos.x, pos.y, col, type, layer, imgFile, width, height, angle, icon)) {
139 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not add PoI.", outputStorage);
140 }
141 } else {
143 "Adding a PoI requires either only type, color, layer and position parameters or these and icon, imageFile, width, height and angle parameters.",
144 outputStorage);
145 }
146 }
147 break;
148 case libsumo::REMOVE: {
149 const int layer = StoHelp::readTypedInt(inputStorage, "The layer must be given using an int.");
150 if (!libsumo::POI::remove(id, layer)) {
151 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, "Could not remove PoI '" + id + "'", outputStorage);
152 }
153 }
154 break;
156 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
157 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
158 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
159 libsumo::POI::setParameter(id, name, value);
160 }
161 break;
162 default:
163 break;
164 }
165 } catch (libsumo::TraCIException& e) {
166 return server.writeErrorStatusCmd(libsumo::CMD_SET_POI_VARIABLE, e.what(), outputStorage);
167 }
168 server.writeStatusCmd(libsumo::CMD_SET_POI_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
169 return true;
170}
171
172
173/****************************************************************************/
const std::string invalid_return< std::string >::value
std::string toHex(const T i, std::streamsize numDigits=0)
Definition ToString.h:81
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State).
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 writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static int readTypedUnsignedByte(tcpip::Storage &ret, const std::string &error="")
static int readCompound(tcpip::Storage &ret, int expectedSize=-1, const std::string &error="")
static int readTypedInt(tcpip::Storage &ret, const std::string &error="")
static std::string readTypedString(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIPosition readTypedPosition2D(tcpip::Storage &ret, const std::string &error="")
static const libsumo::TraCIColor readTypedColor(tcpip::Storage &ret, const std::string &error="")
static double readTypedDouble(tcpip::Storage &ret, const std::string &error="")
An error which allows to continue.
Definition TraCIDefs.h:145
virtual std::string readString()
Definition storage.cpp:180
virtual int readUnsignedByte()
Definition storage.cpp:155
TRACI_CONST int VAR_IMAGEFILE
TRACI_CONST int VAR_TYPE
TRACI_CONST int VAR_ANGLE
TRACI_CONST int VAR_HIGHLIGHT
TRACI_CONST int CMD_SET_POI_VARIABLE
TRACI_CONST int VAR_COLOR
TRACI_CONST int VAR_POSITION
TRACI_CONST int VAR_WIDTH
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int REMOVE
TRACI_CONST int VAR_HEIGHT
TRACI_CONST int RTYPE_OK
TRACI_CONST int ADD
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:179