Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2009-2024 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 : /****************************************************************************/
14 : /// @file TraCIServerAPI_TrafficLight.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date 07.05.2009
20 : ///
21 : // APIs for getting/setting traffic light values via TraCI
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/MSLane.h>
26 : #include <microsim/MSEdge.h>
27 : #include <microsim/traffic_lights/MSTLLogicControl.h>
28 : #include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>
29 : #include <libsumo/TraCIConstants.h>
30 : #include <libsumo/StorageHelper.h>
31 : #include <libsumo/TrafficLight.h>
32 : #include "TraCIServerAPI_TrafficLight.h"
33 :
34 :
35 : // ===========================================================================
36 : // method definitions
37 : // ===========================================================================
38 : bool
39 73271 : TraCIServerAPI_TrafficLight::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
40 : tcpip::Storage& outputStorage) {
41 73271 : const int variable = inputStorage.readUnsignedByte();
42 73271 : const std::string id = inputStorage.readString();
43 73271 : server.initWrapper(libsumo::RESPONSE_GET_TL_VARIABLE, variable, id);
44 : try {
45 73271 : if (!libsumo::TrafficLight::handleVariable(id, variable, &server, &inputStorage)) {
46 16107 : switch (variable) {
47 53 : case libsumo::TL_COMPLETE_DEFINITION_RYG: {
48 53 : std::vector<libsumo::TraCILogic> logics = libsumo::TrafficLight::getAllProgramLogics(id);
49 53 : tcpip::Storage& storage = server.getWrapperStorage();
50 53 : StoHelp::writeCompound(storage, (int)logics.size());
51 145 : for (const libsumo::TraCILogic& logic : logics) {
52 : StoHelp::writeCompound(storage, 5);
53 92 : StoHelp::writeTypedString(storage, logic.programID);
54 92 : StoHelp::writeTypedInt(storage, logic.type);
55 92 : StoHelp::writeTypedInt(storage, logic.currentPhaseIndex);
56 92 : StoHelp::writeCompound(storage, (int)logic.phases.size());
57 707 : for (const std::shared_ptr<libsumo::TraCIPhase>& phase : logic.phases) {
58 : StoHelp::writeCompound(storage, 6);
59 615 : StoHelp::writeTypedDouble(storage, phase->duration);
60 615 : StoHelp::writeTypedString(storage, phase->state);
61 615 : StoHelp::writeTypedDouble(storage, phase->minDur);
62 615 : StoHelp::writeTypedDouble(storage, phase->maxDur);
63 615 : StoHelp::writeCompound(storage, (int)phase->next.size());
64 731 : for (int n : phase->next) {
65 : StoHelp::writeTypedInt(storage, n);
66 : }
67 615 : StoHelp::writeTypedString(storage, phase->name);
68 : }
69 92 : StoHelp::writeCompound(storage, (int)logic.subParameter.size());
70 98 : for (const auto& item : logic.subParameter) {
71 24 : StoHelp::writeTypedStringList(storage, std::vector<std::string> {item.first, item.second});
72 : }
73 : }
74 : break;
75 53 : }
76 26 : case libsumo::TL_CONTROLLED_LINKS: {
77 26 : const std::vector<std::vector<libsumo::TraCILink> > links = libsumo::TrafficLight::getControlledLinks(id);
78 26 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
79 26 : tcpip::Storage tempContent;
80 26 : tempContent.writeUnsignedByte(libsumo::TYPE_INTEGER);
81 26 : tempContent.writeInt((int)links.size());
82 : int cnt = 1;
83 449 : for (const std::vector<libsumo::TraCILink>& sublinks : links) {
84 423 : tempContent.writeUnsignedByte(libsumo::TYPE_INTEGER);
85 423 : tempContent.writeInt((int)sublinks.size());
86 423 : ++cnt;
87 846 : for (const libsumo::TraCILink& link : sublinks) {
88 423 : tempContent.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
89 1692 : tempContent.writeStringList(std::vector<std::string>({ link.fromLane, link.toLane, link.viaLane }));
90 423 : ++cnt;
91 : }
92 : }
93 26 : server.getWrapperStorage().writeInt(cnt);
94 26 : server.getWrapperStorage().writeStorage(tempContent);
95 : break;
96 26 : }
97 1671 : case libsumo::VAR_PERSON_NUMBER: {
98 1671 : int index = 0;
99 1671 : if (!server.readTypeCheckingInt(inputStorage, index)) {
100 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The phase index must be given as an integer.", outputStorage);
101 : }
102 1671 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_INTEGER);
103 1671 : server.getWrapperStorage().writeInt(libsumo::TrafficLight::getServedPersonCount(id, index));
104 1671 : break;
105 : }
106 4522 : case libsumo::TL_BLOCKING_VEHICLES: {
107 4522 : int index = 0;
108 4522 : if (!server.readTypeCheckingInt(inputStorage, index)) {
109 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The link index must be given as an integer.", outputStorage);
110 : }
111 4522 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_STRINGLIST);
112 4522 : server.getWrapperStorage().writeStringList(libsumo::TrafficLight::getBlockingVehicles(id, index));
113 4522 : break;
114 : }
115 4516 : case libsumo::TL_RIVAL_VEHICLES: {
116 4516 : int index = 0;
117 4516 : if (!server.readTypeCheckingInt(inputStorage, index)) {
118 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The link index must be given as an integer.", outputStorage);
119 : }
120 4516 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_STRINGLIST);
121 4516 : server.getWrapperStorage().writeStringList(libsumo::TrafficLight::getRivalVehicles(id, index));
122 4516 : break;
123 : }
124 4516 : case libsumo::TL_PRIORITY_VEHICLES: {
125 4516 : int index = 0;
126 4516 : if (!server.readTypeCheckingInt(inputStorage, index)) {
127 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The link index must be given as an integer.", outputStorage);
128 : }
129 4516 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_STRINGLIST);
130 4516 : server.getWrapperStorage().writeStringList(libsumo::TrafficLight::getPriorityVehicles(id, index));
131 4516 : break;
132 : }
133 : case libsumo::TL_CONSTRAINT: {
134 : std::string tripId;
135 729 : if (!server.readTypeCheckingString(inputStorage, tripId)) {
136 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The tripId must be given as a string.", outputStorage);
137 : }
138 729 : std::vector<libsumo::TraCISignalConstraint> constraints = libsumo::TrafficLight::getConstraints(id, tripId);
139 729 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
140 729 : const int cnt = 1 + (int)constraints.size() * 5;
141 729 : server.getWrapperStorage().writeInt(cnt);
142 729 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_INTEGER);
143 729 : server.getWrapperStorage().writeInt((int)constraints.size());
144 1314 : for (const auto& c : constraints) {
145 585 : writeConstraint(server, c);
146 : }
147 : break;
148 729 : }
149 : case libsumo::TL_CONSTRAINT_BYFOE: {
150 : std::string foeId;
151 6 : if (!server.readTypeCheckingString(inputStorage, foeId)) {
152 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foeId must be given as a string.", outputStorage);
153 : }
154 6 : std::vector<libsumo::TraCISignalConstraint> constraints = libsumo::TrafficLight::getConstraintsByFoe(id, foeId);
155 6 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
156 6 : const int cnt = 1 + (int)constraints.size() * 5;
157 6 : server.getWrapperStorage().writeInt(cnt);
158 6 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_INTEGER);
159 6 : server.getWrapperStorage().writeInt((int)constraints.size());
160 27 : for (const auto& c : constraints) {
161 21 : writeConstraint(server, c);
162 : }
163 : break;
164 6 : }
165 66 : case libsumo::TL_CONSTRAINT_SWAP: {
166 66 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
167 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for swapping constraints.", outputStorage);
168 : }
169 : //read itemNo
170 66 : inputStorage.readInt();
171 : std::string tripId;
172 66 : if (!server.readTypeCheckingString(inputStorage, tripId)) {
173 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The tripId must be given as a string.", outputStorage);
174 : }
175 : std::string foeSignal;
176 66 : if (!server.readTypeCheckingString(inputStorage, foeSignal)) {
177 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foeSignal id must be given as a string.", outputStorage);
178 : }
179 : std::string foeId;
180 66 : if (!server.readTypeCheckingString(inputStorage, foeId)) {
181 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foe tripId must be given as a string.", outputStorage);
182 : }
183 66 : std::vector<libsumo::TraCISignalConstraint> constraints = libsumo::TrafficLight::swapConstraints(id, tripId, foeSignal, foeId);
184 66 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
185 66 : const int cnt = 1 + (int)constraints.size() * 5;
186 66 : server.getWrapperStorage().writeInt(cnt);
187 66 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_INTEGER);
188 66 : server.getWrapperStorage().writeInt((int)constraints.size());
189 126 : for (const auto& c : constraints) {
190 60 : writeConstraint(server, c);
191 : }
192 : break;
193 66 : }
194 0 : case libsumo::TL_EXTERNAL_STATE: {
195 0 : if (!MSNet::getInstance()->getTLSControl().knows(id)) {
196 0 : throw libsumo::TraCIException("Traffic light '" + id + "' is not known");
197 : }
198 0 : MSTrafficLightLogic* tls = MSNet::getInstance()->getTLSControl().get(id).getActive();
199 0 : const std::string& state = tls->getCurrentPhaseDef().getState();
200 0 : const Parameterised::Map& params = tls->getParametersMap();
201 : int num = 0;
202 0 : for (Parameterised::Map::const_iterator i = params.begin(); i != params.end(); ++i) {
203 0 : if ("connection:" == (*i).first.substr(0, 11)) {
204 0 : ++num;
205 : }
206 : }
207 :
208 0 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
209 0 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_INTEGER);
210 0 : server.getWrapperStorage().writeInt(num * 2);
211 0 : for (Parameterised::Map::const_iterator i = params.begin(); i != params.end(); ++i) {
212 0 : if ("connection:" != (*i).first.substr(0, 11)) {
213 0 : continue;
214 : }
215 0 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_STRING);
216 0 : server.getWrapperStorage().writeString((*i).second); // foreign id
217 0 : std::string connection = (*i).first.substr(11);
218 : std::string from, to;
219 : const std::string::size_type b = connection.find("->");
220 0 : if (b == std::string::npos) {
221 : from = connection;
222 : } else {
223 0 : from = connection.substr(0, b);
224 0 : to = connection.substr(b + 2);
225 : }
226 : bool denotesEdge = from.find("_") == std::string::npos;
227 : MSLane* fromLane = nullptr;
228 : const MSTrafficLightLogic::LaneVectorVector& lanes = tls->getLaneVectors();
229 : MSTrafficLightLogic::LaneVectorVector::const_iterator j = lanes.begin();
230 0 : for (; j != lanes.end() && fromLane == nullptr;) {
231 0 : for (MSTrafficLightLogic::LaneVector::const_iterator k = (*j).begin(); k != (*j).end() && fromLane == nullptr;) {
232 0 : if (denotesEdge && (*k)->getEdge().getID() == from) {
233 : fromLane = *k;
234 0 : } else if (!denotesEdge && (*k)->getID() == from) {
235 : fromLane = *k;
236 : }
237 0 : if (fromLane == nullptr) {
238 : ++k;
239 : }
240 : }
241 0 : if (fromLane == nullptr) {
242 : ++j;
243 : }
244 : }
245 0 : if (fromLane == nullptr) {
246 0 : return server.writeErrorStatusCmd(libsumo::CMD_GET_TL_VARIABLE, "Could not find edge or lane '" + from + "' in traffic light '" + id + "'.", outputStorage);
247 : }
248 0 : int pos = (int)std::distance(lanes.begin(), j);
249 0 : server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_UBYTE);
250 0 : server.getWrapperStorage().writeUnsignedByte(state[pos]); // state
251 : }
252 : break;
253 : }
254 2 : default:
255 6 : return server.writeErrorStatusCmd(libsumo::CMD_GET_TL_VARIABLE, "Get TLS Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
256 : }
257 : }
258 3 : } catch (libsumo::TraCIException& e) {
259 3 : return server.writeErrorStatusCmd(libsumo::CMD_GET_TL_VARIABLE, e.what(), outputStorage);
260 3 : }
261 73266 : server.writeStatusCmd(libsumo::CMD_GET_TL_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
262 73266 : server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
263 : return true;
264 0 : }
265 :
266 :
267 : bool
268 1860 : TraCIServerAPI_TrafficLight::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
269 : tcpip::Storage& outputStorage) {
270 1860 : std::string warning = ""; // additional description for response
271 : // variable
272 1860 : const int variable = inputStorage.readUnsignedByte();
273 1860 : if (variable != libsumo::TL_PHASE_INDEX && variable != libsumo::TL_PROGRAM && variable != libsumo::TL_PHASE_DURATION
274 1860 : && variable != libsumo::TL_RED_YELLOW_GREEN_STATE && variable != libsumo::TL_COMPLETE_PROGRAM_RYG
275 149 : && variable != libsumo::VAR_NAME
276 : && variable != libsumo::TL_CONSTRAINT_REMOVE
277 98 : && variable != libsumo::TL_CONSTRAINT_UPDATE
278 81 : && variable != libsumo::TL_CONSTRAINT_ADD
279 81 : && variable != libsumo::VAR_PARAMETER) {
280 3 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "Change TLS State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
281 : }
282 1859 : const std::string id = inputStorage.readString();
283 : try {
284 1859 : switch (variable) {
285 1646 : case libsumo::TL_PHASE_INDEX: {
286 1646 : int index = 0;
287 1646 : if (!server.readTypeCheckingInt(inputStorage, index)) {
288 2 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The phase index must be given as an integer.", outputStorage);
289 : }
290 1645 : libsumo::TrafficLight::setPhase(id, index);
291 : }
292 1642 : break;
293 : case libsumo::VAR_NAME: {
294 : std::string name;
295 8 : if (!server.readTypeCheckingString(inputStorage, name)) {
296 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The phase name must be given as a string.", outputStorage);
297 : }
298 8 : libsumo::TrafficLight::setPhaseName(id, name);
299 : }
300 : break;
301 : case libsumo::TL_PROGRAM: {
302 : std::string subID;
303 20 : if (!server.readTypeCheckingString(inputStorage, subID)) {
304 1 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The program must be given as a string.", outputStorage);
305 : }
306 20 : libsumo::TrafficLight::setProgram(id, subID);
307 : }
308 : break;
309 20 : case libsumo::TL_PHASE_DURATION: {
310 20 : double duration = 0.;
311 20 : if (!server.readTypeCheckingDouble(inputStorage, duration)) {
312 2 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The phase duration must be given as a double.", outputStorage);
313 : }
314 19 : libsumo::TrafficLight::setPhaseDuration(id, duration);
315 : }
316 19 : break;
317 : case libsumo::TL_RED_YELLOW_GREEN_STATE: {
318 : std::string state;
319 25 : if (!server.readTypeCheckingString(inputStorage, state)) {
320 2 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The phase must be given as a string.", outputStorage);
321 : }
322 24 : libsumo::TrafficLight::setRedYellowGreenState(id, state);
323 : }
324 : break;
325 43 : case libsumo::TL_COMPLETE_PROGRAM_RYG: {
326 43 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
327 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for setting a new program.", outputStorage);
328 : }
329 : //read itemNo
330 43 : inputStorage.readInt();
331 : libsumo::TraCILogic logic;
332 43 : if (!server.readTypeCheckingString(inputStorage, logic.programID)) {
333 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 1. parameter (programID) must be a string.", outputStorage);
334 : }
335 43 : if (!server.readTypeCheckingInt(inputStorage, logic.type)) {
336 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 2. parameter (type) must be an int.", outputStorage);
337 : }
338 43 : if (!server.readTypeCheckingInt(inputStorage, logic.currentPhaseIndex)) {
339 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 3. parameter (index) must be an int.", outputStorage);
340 : }
341 43 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
342 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for the phases.", outputStorage);
343 : }
344 43 : const int numPhases = inputStorage.readInt();
345 211 : for (int j = 0; j < numPhases; ++j) {
346 168 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
347 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for every phase.", outputStorage);
348 : }
349 168 : const int items = inputStorage.readInt();
350 168 : if (items != 6 && items != 5) {
351 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A phase compound object requires 5 or 6 items.", outputStorage);
352 : }
353 168 : double duration = 0., minDuration = 0., maxDuration = 0.;
354 : std::vector<int> next;
355 : std::string name;
356 168 : if (!server.readTypeCheckingDouble(inputStorage, duration)) {
357 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.1. parameter (duration) must be a double.", outputStorage);
358 : }
359 : std::string state;
360 168 : if (!server.readTypeCheckingString(inputStorage, state)) {
361 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.2. parameter (phase) must be a string.", outputStorage);
362 : }
363 168 : if (!server.readTypeCheckingDouble(inputStorage, minDuration)) {
364 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.3. parameter (min duration) must be a double.", outputStorage);
365 : }
366 168 : if (!server.readTypeCheckingDouble(inputStorage, maxDuration)) {
367 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.4. parameter (max duration) must be a double.", outputStorage);
368 : }
369 168 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
370 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program 4.5 parameter (next) must be a compound (list of ints).", outputStorage);
371 : }
372 168 : const int numNext = inputStorage.readInt();
373 177 : for (int k = 0; k < numNext; k++) {
374 : int nextEntry;
375 9 : if (!server.readTypeCheckingInt(inputStorage, nextEntry)) {
376 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.5. parameter (next) must be a list of int.", outputStorage);
377 : }
378 9 : next.push_back(nextEntry);
379 : }
380 168 : if (items == 6) {
381 150 : if (!server.readTypeCheckingString(inputStorage, name)) {
382 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 4.6. parameter (name) must be a string.", outputStorage);
383 : }
384 : }
385 168 : logic.phases.emplace_back(new libsumo::TraCIPhase(duration, state, minDuration, maxDuration, next, name));
386 168 : }
387 43 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
388 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "set program: 5. parameter (subparams) must be a compound object.", outputStorage);
389 : }
390 43 : const int numParams = inputStorage.readInt();
391 43 : for (int j = 0; j < numParams; j++) {
392 : std::vector<std::string> par;
393 0 : server.readTypeCheckingStringList(inputStorage, par);
394 0 : logic.subParameter[par[0]] = par[1];
395 0 : }
396 : libsumo::TrafficLight::setCompleteRedYellowGreenDefinition(id, logic);
397 43 : }
398 : break;
399 15 : case libsumo::TL_CONSTRAINT_REMOVE: {
400 15 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
401 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for removing constraints.", outputStorage);
402 : }
403 : //read itemNo
404 15 : inputStorage.readInt();
405 : std::string tripId;
406 15 : if (!server.readTypeCheckingString(inputStorage, tripId)) {
407 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The tripId must be given as a string.", outputStorage);
408 : }
409 : std::string foeSignal;
410 15 : if (!server.readTypeCheckingString(inputStorage, foeSignal)) {
411 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foeSignal id must be given as a string.", outputStorage);
412 : }
413 : std::string foeId;
414 15 : if (!server.readTypeCheckingString(inputStorage, foeId)) {
415 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foe tripId must be given as a string.", outputStorage);
416 : }
417 15 : libsumo::TrafficLight::removeConstraints(id, tripId, foeSignal, foeId);
418 : }
419 : break;
420 : case libsumo::TL_CONSTRAINT_UPDATE: {
421 : std::string tripId;
422 2 : if (!server.readTypeCheckingString(inputStorage, tripId)) {
423 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The tripId index must be given as a string.", outputStorage);
424 : }
425 4 : libsumo::TrafficLight::updateConstraints(id, tripId);
426 : }
427 : break;
428 3 : case libsumo::TL_CONSTRAINT_ADD: {
429 3 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
430 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for adding constraints.", outputStorage);
431 : }
432 : //read itemNo
433 3 : inputStorage.readInt();
434 : std::string tripId;
435 3 : if (!server.readTypeCheckingString(inputStorage, tripId)) {
436 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The tripId must be given as a string.", outputStorage);
437 : }
438 : std::string foeSignal;
439 3 : if (!server.readTypeCheckingString(inputStorage, foeSignal)) {
440 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foe signal must be given as a string.", outputStorage);
441 : }
442 : std::string foeId;
443 3 : if (!server.readTypeCheckingString(inputStorage, foeId)) {
444 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The foe tripId must be given as a string.", outputStorage);
445 : }
446 : int type;
447 3 : if (!server.readTypeCheckingInt(inputStorage, type)) {
448 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The type must be an int.", outputStorage);
449 : }
450 : int limit;
451 3 : if (!server.readTypeCheckingInt(inputStorage, limit)) {
452 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The limit must be an int.", outputStorage);
453 : }
454 3 : libsumo::TrafficLight::addConstraint(id, tripId, foeSignal, foeId, type, limit);
455 : }
456 : break;
457 77 : case libsumo::VAR_PARAMETER: {
458 77 : if (inputStorage.readUnsignedByte() != libsumo::TYPE_COMPOUND) {
459 0 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
460 : }
461 : //read itemNo
462 77 : inputStorage.readInt();
463 : std::string name;
464 77 : if (!server.readTypeCheckingString(inputStorage, name)) {
465 4 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
466 : }
467 : std::string value;
468 77 : if (!server.readTypeCheckingString(inputStorage, value)) {
469 4 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
470 : }
471 77 : libsumo::TrafficLight::setParameter(id, name, value);
472 : }
473 : break;
474 : default:
475 : break;
476 : }
477 9 : } catch (libsumo::TraCIException& e) {
478 9 : return server.writeErrorStatusCmd(libsumo::CMD_SET_TL_VARIABLE, e.what(), outputStorage);
479 9 : }
480 1847 : server.writeStatusCmd(libsumo::CMD_SET_TL_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
481 : return true;
482 : }
483 :
484 :
485 : void
486 666 : TraCIServerAPI_TrafficLight::writeConstraint(TraCIServer& server, const libsumo::TraCISignalConstraint& c) {
487 666 : StoHelp::writeTypedString(server.getWrapperStorage(), c.signalId);
488 666 : StoHelp::writeTypedString(server.getWrapperStorage(), c.tripId);
489 666 : StoHelp::writeTypedString(server.getWrapperStorage(), c.foeId);
490 666 : StoHelp::writeTypedString(server.getWrapperStorage(), c.foeSignal);
491 666 : StoHelp::writeTypedInt(server.getWrapperStorage(), c.limit);
492 666 : StoHelp::writeTypedInt(server.getWrapperStorage(), c.type);
493 666 : StoHelp::writeTypedByte(server.getWrapperStorage(), c.mustWait);
494 666 : StoHelp::writeTypedByte(server.getWrapperStorage(), c.active);
495 : std::vector<std::string> paramItems;
496 1002 : for (auto item : c.param) {
497 336 : paramItems.push_back(item.first);
498 336 : paramItems.push_back(item.second);
499 : }
500 666 : StoHelp::writeTypedStringList(server.getWrapperStorage(), paramItems);
501 666 : }
502 :
503 :
504 : /****************************************************************************/
|