Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traas/subscription/data/Subscription.java
169689 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 German Aerospace Center (DLR) and others.
4
// TraaS module
5
// Copyright (C) 2013-2017 Dresden University of Technology
6
// This program and the accompanying materials are made available under the
7
// terms of the Eclipse Public License 2.0 which is available at
8
// https://www.eclipse.org/legal/epl-2.0/
9
// This Source Code may also be made available under the following Secondary
10
// Licenses when the conditions for such availability set forth in the Eclipse
11
// Public License 2.0 are satisfied: GNU General Public License, version 2
12
// or later which is available at
13
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15
/****************************************************************************/
16
/// @file Subscription.java
17
/// @author Jakob Erdmann
18
/// @date 2019
19
///
20
//
21
/****************************************************************************/
22
import it.polito.appeal.traci.SumoTraciConnection;
23
import it.polito.appeal.traci.TraCIException;
24
import de.tudresden.sumo.cmd.Simulation;
25
import de.tudresden.sumo.cmd.Vehicle;
26
import de.tudresden.sumo.cmd.Inductionloop;
27
import de.tudresden.sumo.cmd.Trafficlight;
28
import de.tudresden.sumo.config.Constants;
29
import de.tudresden.sumo.util.Observer;
30
import de.tudresden.sumo.util.Observable;
31
import de.tudresden.sumo.subscription.VariableSubscription;
32
import de.tudresden.sumo.subscription.SubscribtionVariable;
33
import de.tudresden.sumo.subscription.SubscriptionObject;
34
import de.tudresden.sumo.subscription.ResponseType;
35
import de.tudresden.sumo.objects.SumoVehicleData;
36
import de.tudresden.sumo.objects.SumoStringList;
37
import de.tudresden.sumo.objects.SumoPrimitive;
38
import de.tudresden.sumo.objects.SumoPosition2D;
39
40
public class Subscription implements Observer {
41
42
static SumoTraciConnection conn = null;
43
44
public static void main(String[] args) {
45
String sumo_bin = "sumo";
46
String config_file = "data/config.sumocfg";
47
double step_length = 1.0;
48
49
if (args.length > 0) {
50
sumo_bin = args[0];
51
}
52
if (args.length > 1) {
53
config_file = args[1];
54
}
55
56
try {
57
conn = new SumoTraciConnection(sumo_bin, config_file);
58
conn.addOption("step-length", step_length + "");
59
conn.addOption("start", "true"); //start sumo immediately
60
61
//start Traci Server
62
conn.runServer();
63
conn.setOrder(1);
64
conn.addObserver(new Subscription());
65
66
VariableSubscription vs = new VariableSubscription(SubscribtionVariable.simulation, 0, 100000 * 60, "");
67
vs.addCommand(Constants.VAR_DEPARTED_VEHICLES_IDS);
68
conn.do_subscription(vs);
69
70
71
for (int i = 0; i < 10; i++) {
72
73
conn.do_timestep();
74
conn.do_job_set(Vehicle.addFull("v" + i, "r1", "car", "now", "0", "0", "max", "current", "max", "current", "", "", "", 0, 0));
75
double timeSeconds = (double)conn.do_job_get(Simulation.getTime());
76
System.out.println("Step: " + i);
77
78
}
79
80
conn.close();
81
82
} catch (Exception ex) {
83
ex.printStackTrace();
84
}
85
86
}
87
88
public void update(Observable arg0, SubscriptionObject so) {
89
//System.out.println("Subscription id=" + so.id + " domain=" + so.domain + " name=" + so.name + " var=" + so.variable + " status=" + so.status + " ret=" + so.return_type + " resp=" + so.response.getID());
90
91
if (so.response == ResponseType.SIM_VARIABLE) {
92
assert(so.variable == Constants.VAR_DEPARTED_VEHICLES_IDS);
93
SumoStringList ssl = (SumoStringList) so.object;
94
if (ssl.size() > 0) {
95
for (String vehID : ssl) {
96
System.out.println("Subscription Departed vehicles: " + vehID);
97
VariableSubscription vs = new VariableSubscription(SubscribtionVariable.vehicle, 0, 100000 * 60, vehID);
98
vs.addCommand(Constants.VAR_POSITION);
99
vs.addCommand(Constants.VAR_SPEED);
100
try {
101
conn.do_subscription(vs);
102
} catch (Exception ex) {
103
System.err.println("subscription to " + vehID + " failed");
104
}
105
}
106
}
107
} else if (so.response == ResponseType.VEHICLE_VARIABLE) {
108
if (so.variable == Constants.VAR_SPEED) {
109
SumoPrimitive sp = (SumoPrimitive) so.object;
110
System.out.println("Speed of vehicle " + so.id + ": " + sp.val);
111
} else if (so.variable == Constants.VAR_POSITION) {
112
SumoPosition2D sc = (SumoPosition2D) so.object;
113
System.out.println("Position of vehicle " + so.id + ": x = " + sc.x + " y = " + sc.y);
114
}
115
}
116
117
}
118
119
120
121
}
122
123