use std::collections::HashSet;
use traci_rs::TraciClient;
const NUM_VEHICLES: usize = 5;
fn main() -> Result<(), traci_rs::TraciError> {
let mut client = TraciClient::connect("127.0.0.1", 8813)?;
println!("Connected to SUMO TraCI server.");
client.set_order(1)?;
let (api_version, sumo_version) = client.get_version()?;
println!("TraCI API version : {api_version}");
println!("SUMO version : {sumo_version}");
let routes = client.route_get_id_list()?;
let route_id = if let Some(r) = routes.first() {
r.clone()
} else {
let edges = client.edge_get_id_list()?;
let first_edge = edges.into_iter()
.find(|e| !e.starts_with(':'))
.expect("no drivable edge found in network");
client.route_add("route_traci", &[&first_edge])?;
"route_traci".to_string()
};
println!("Using route '{route_id}'");
for i in 0..NUM_VEHICLES {
let vid = format!("traci_veh_{i}");
client.vehicle_add(&vid, &route_id, "DEFAULT_VEHTYPE")?;
println!("Added vehicle '{vid}'");
}
let mut subscribed: HashSet<String> = HashSet::new();
let mut step = 0u32;
while client.simulation_step(0.0)? {
step += 1;
let ids = client.vehicle_get_id_list()?;
if ids.is_empty() {
println!("All vehicles finished. Stopping after {step} steps.");
break;
}
for id in &ids {
if subscribed.insert(id.clone()) {
client.vehicle_subscribe_kinematics(id, 0.0, 1e9)?;
println!("Step {step:>4} — subscribed '{id}'");
}
}
println!("--- Step {:>4} ({} vehicles) ---", step, ids.len());
for id in &ids {
if let Some(k) = client.vehicle_get_subscribed_kinematics(id) {
println!(
" Vehicle {:20} pos=({:8.2}, {:8.2}) \
speed={:6.2} m/s accel={:6.2} m/s\u{00b2} angle={:6.1}\u{00b0}",
id, k.position.x, k.position.y, k.speed, k.acceleration, k.angle
);
}
}
}
client.close()?;
Ok(())
}