traci-rs 0.2.0

Pure Rust client library for the SUMO TraCI protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// SPDX-License-Identifier: EPL-2.0
//! TraCI Simulation domain scope.

use crate::{
    client::TraciClient,
    constants::*,
    error::TraciError,
    storage::Storage,
    types::{
        ContextSubscriptionResults, SubscriptionResults, TraciPosition, TraciRoadPosition,
        TraciStage,
    },
};

/// Scope for querying and controlling the SUMO simulation.
#[derive(Debug, Default)]
pub struct SimulationScope {
    pub subscription_results: SubscriptionResults,
    pub context_subscription_results: ContextSubscriptionResults,
}

impl SimulationScope {
    crate::impl_scope_accessors!();

    // -----------------------------------------------------------------------
    // Simple getters
    // -----------------------------------------------------------------------

    pub fn get_current_time(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TIME_STEP, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_time(&self, client: &mut TraciClient) -> Result<f64, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TIME, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_DOUBLE))?;
        client.read_double_from_input()
    }

    pub fn get_loaded_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_LOADED_VEHICLES_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_loaded_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_LOADED_VEHICLES_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_departed_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_DEPARTED_VEHICLES_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_departed_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_DEPARTED_VEHICLES_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_arrived_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_ARRIVED_VEHICLES_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_arrived_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_ARRIVED_VEHICLES_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_starting_teleport_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TELEPORT_STARTING_VEHICLES_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_starting_teleport_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TELEPORT_STARTING_VEHICLES_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_ending_teleport_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TELEPORT_ENDING_VEHICLES_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_ending_teleport_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_TELEPORT_ENDING_VEHICLES_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_departed_person_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_DEPARTED_PERSONS_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_departed_person_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_DEPARTED_PERSONS_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_arrived_person_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_ARRIVED_PERSONS_NUMBER, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_arrived_person_id_list(&self, client: &mut TraciClient) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_ARRIVED_PERSONS_IDS, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    pub fn get_delta_t(&self, client: &mut TraciClient) -> Result<f64, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_DELTA_T, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_DOUBLE))?;
        client.read_double_from_input()
    }

    pub fn get_net_boundary(&self, client: &mut TraciClient) -> Result<Vec<crate::types::TraciPosition>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_NET_BOUNDING_BOX, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_POLYGON))?;
        client.read_polygon_from_input()
    }

    pub fn get_min_expected_number(&self, client: &mut TraciClient) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_MIN_EXPECTED_VEHICLES, "", None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_option(&self, client: &mut TraciClient, option: &str) -> Result<String, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_OPTION, option, None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRING))?;
        client.read_string_from_input()
    }

    pub fn get_bus_stop_waiting(&self, client: &mut TraciClient, stop_id: &str) -> Result<i32, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_BUS_STOP_WAITING, stop_id, None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_INTEGER))?;
        client.read_int_from_input()
    }

    pub fn get_bus_stop_waiting_id_list(&self, client: &mut TraciClient, stop_id: &str) -> Result<Vec<String>, TraciError> {
        client.create_command(CMD_GET_SIM_VARIABLE, VAR_BUS_STOP_WAITING_IDS, stop_id, None);
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_STRINGLIST))?;
        client.read_string_list_from_input()
    }

    // -----------------------------------------------------------------------
    // Position conversion
    // -----------------------------------------------------------------------

    /// Convert a road position (edge + offset + lane) to a 2-D or geographic position.
    pub fn convert2d(
        &self,
        client: &mut TraciClient,
        edge_id: &str,
        pos: f64,
        lane_index: i32,
        to_geo: bool,
    ) -> Result<TraciPosition, TraciError> {
        let pos_type = if to_geo { POSITION_LON_LAT } else { POSITION_2D };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(2);
        add.write_u8(POSITION_ROADMAP);
        add.write_string(edge_id);
        add.write_f64(pos);
        add.write_u8(lane_index as u8);
        add.write_u8(TYPE_UBYTE);
        add.write_u8(pos_type);
        client.create_command(CMD_GET_SIM_VARIABLE, POSITION_CONVERSION, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(pos_type))?;
        let x = client.read_double_from_input()?;
        let y = client.read_double_from_input()?;
        Ok(TraciPosition::new_2d(x, y))
    }

    /// Convert a road position to a 3-D or geographic+alt position.
    pub fn convert3d(
        &self,
        client: &mut TraciClient,
        edge_id: &str,
        pos: f64,
        lane_index: i32,
        to_geo: bool,
    ) -> Result<TraciPosition, TraciError> {
        let pos_type = if to_geo { POSITION_LON_LAT_ALT } else { POSITION_3D };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(2);
        add.write_u8(POSITION_ROADMAP);
        add.write_string(edge_id);
        add.write_f64(pos);
        add.write_u8(lane_index as u8);
        add.write_u8(TYPE_UBYTE);
        add.write_u8(pos_type);
        client.create_command(CMD_GET_SIM_VARIABLE, POSITION_CONVERSION, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(pos_type))?;
        let x = client.read_double_from_input()?;
        let y = client.read_double_from_input()?;
        let z = client.read_double_from_input()?;
        Ok(TraciPosition::new_3d(x, y, z))
    }

    /// Convert a 2-D (or geo) position to a road position.
    pub fn convert_road(
        &self,
        client: &mut TraciClient,
        x: f64,
        y: f64,
        is_geo: bool,
        v_class: &str,
    ) -> Result<TraciRoadPosition, TraciError> {
        let src_pos_type = if is_geo { POSITION_LON_LAT } else { POSITION_2D };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(3);
        add.write_u8(src_pos_type);
        add.write_f64(x);
        add.write_f64(y);
        add.write_u8(TYPE_UBYTE);
        add.write_u8(POSITION_ROADMAP);
        add.write_u8(TYPE_STRING);
        add.write_string(v_class);
        client.create_command(CMD_GET_SIM_VARIABLE, POSITION_CONVERSION, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(POSITION_ROADMAP))?;
        let edge_id = client.read_string_from_input()?;
        let pos = client.read_double_from_input()?;
        let lane_index = client.read_ubyte_from_input()? as i32;
        Ok(TraciRoadPosition { edge_id, pos, lane_index })
    }

    /// Convert between geographic and Cartesian positions.
    pub fn convert_geo(
        &self,
        client: &mut TraciClient,
        x: f64,
        y: f64,
        from_geo: bool,
    ) -> Result<TraciPosition, TraciError> {
        let pos_type = if from_geo { POSITION_2D } else { POSITION_LON_LAT };
        let src_pos_type = if from_geo { POSITION_LON_LAT } else { POSITION_2D };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(2);
        add.write_u8(src_pos_type);
        add.write_f64(x);
        add.write_f64(y);
        add.write_u8(TYPE_UBYTE);
        add.write_u8(pos_type);
        client.create_command(CMD_GET_SIM_VARIABLE, POSITION_CONVERSION, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(pos_type))?;
        let rx = client.read_double_from_input()?;
        let ry = client.read_double_from_input()?;
        Ok(TraciPosition::new_2d(rx, ry))
    }

    // -----------------------------------------------------------------------
    // Distance queries
    // -----------------------------------------------------------------------

    /// Get the distance between two 2-D (or geo) positions.
    pub fn get_distance_2d(
        &self,
        client: &mut TraciClient,
        x1: f64,
        y1: f64,
        x2: f64,
        y2: f64,
        is_geo: bool,
        is_driving: bool,
    ) -> Result<f64, TraciError> {
        let pos_type = if is_geo { POSITION_LON_LAT } else { POSITION_2D };
        let dist_type: u8 = if is_driving { REQUEST_DRIVINGDIST } else { REQUEST_AIRDIST };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(3);
        add.write_u8(pos_type);
        add.write_f64(x1);
        add.write_f64(y1);
        add.write_u8(pos_type);
        add.write_f64(x2);
        add.write_f64(y2);
        add.write_u8(dist_type);
        client.create_command(CMD_GET_SIM_VARIABLE, DISTANCE_REQUEST, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_DOUBLE))?;
        client.read_double_from_input()
    }

    /// Get the distance between two road positions.
    pub fn get_distance_road(
        &self,
        client: &mut TraciClient,
        edge_id1: &str,
        pos1: f64,
        edge_id2: &str,
        pos2: f64,
        is_driving: bool,
    ) -> Result<f64, TraciError> {
        let dist_type: u8 = if is_driving { REQUEST_DRIVINGDIST } else { REQUEST_AIRDIST };
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(3);
        add.write_u8(POSITION_ROADMAP);
        add.write_string(edge_id1);
        add.write_f64(pos1);
        add.write_u8(0); // lane
        add.write_u8(POSITION_ROADMAP);
        add.write_string(edge_id2);
        add.write_f64(pos2);
        add.write_u8(0); // lane
        add.write_u8(dist_type);
        client.create_command(CMD_GET_SIM_VARIABLE, DISTANCE_REQUEST, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_DOUBLE))?;
        client.read_double_from_input()
    }

    // -----------------------------------------------------------------------
    // Route finding
    // -----------------------------------------------------------------------

    /// Find a route between two edges, returning a [`TraciStage`].
    pub fn find_route(
        &self,
        client: &mut TraciClient,
        from_edge: &str,
        to_edge: &str,
        v_type: &str,
        pos: f64,
        routing_mode: i32,
    ) -> Result<TraciStage, TraciError> {
        let mut add = Storage::new();
        add.write_u8(TYPE_COMPOUND);
        add.write_i32(5);
        add.write_u8(TYPE_STRING);
        add.write_string(from_edge);
        add.write_u8(TYPE_STRING);
        add.write_string(to_edge);
        add.write_u8(TYPE_STRING);
        add.write_string(v_type);
        add.write_u8(TYPE_DOUBLE);
        add.write_f64(pos);
        add.write_u8(TYPE_INTEGER);
        add.write_i32(routing_mode);
        client.create_command(CMD_GET_SIM_VARIABLE, FIND_ROUTE, "", Some(&add));
        client.process_get(CMD_GET_SIM_VARIABLE, Some(TYPE_COMPOUND))?;
        read_traci_stage(client)
    }

    // -----------------------------------------------------------------------
    // State management
    // -----------------------------------------------------------------------

    pub fn load_state(&self, client: &mut TraciClient, path: &str) -> Result<(), TraciError> {
        let mut add = Storage::new();
        add.write_u8(TYPE_STRING);
        add.write_string(path);
        client.create_command(CMD_SET_SIM_VARIABLE, CMD_LOAD_SIMSTATE, "", Some(&add));
        client.process_set(CMD_SET_SIM_VARIABLE)?;
        Ok(())
    }

    pub fn save_state(&self, client: &mut TraciClient, destination: &str) -> Result<(), TraciError> {
        let mut add = Storage::new();
        add.write_u8(TYPE_STRING);
        add.write_string(destination);
        client.create_command(CMD_SET_SIM_VARIABLE, CMD_SAVE_SIMSTATE, "", Some(&add));
        client.process_set(CMD_SET_SIM_VARIABLE)?;
        Ok(())
    }

    pub fn write_message(&self, client: &mut TraciClient, msg: &str) -> Result<(), TraciError> {
        let mut add = Storage::new();
        add.write_u8(TYPE_STRING);
        add.write_string(msg);
        client.create_command(CMD_SET_SIM_VARIABLE, CMD_MESSAGE, "", Some(&add));
        client.process_set(CMD_SET_SIM_VARIABLE)?;
        Ok(())
    }

    pub fn subscribe(&self, client: &mut TraciClient, vars: &[u8], begin: f64, end: f64) -> Result<(), TraciError> {
        client.subscribe_object_variable(CMD_SUBSCRIBE_SIM_VARIABLE, "", begin, end, vars)
    }
}

// ============================================================================
// Shared helper: read a TraciStage from client.input (TYPE_COMPOUND already consumed)
// ============================================================================

/// Read a `TraciStage` from the input buffer. The TYPE_COMPOUND tag and expected-type
/// check have already been performed by `process_get`; the next bytes are the payload.
pub(crate) fn read_traci_stage(client: &mut TraciClient) -> Result<TraciStage, TraciError> {
    client.read_int_from_input()?; // components count
    client.read_ubyte_from_input()?; // TYPE_INTEGER tag
    let type_ = client.read_int_from_input()?;

    client.read_ubyte_from_input()?; // TYPE_STRING tag
    let v_type = client.read_string_from_input()?;

    client.read_ubyte_from_input()?;
    let line = client.read_string_from_input()?;

    client.read_ubyte_from_input()?;
    let dest_stop = client.read_string_from_input()?;

    client.read_ubyte_from_input()?;
    let edges = client.read_string_list_from_input()?;

    client.read_ubyte_from_input()?;
    let travel_time = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let cost = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let length = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let intended = client.read_string_from_input()?;

    client.read_ubyte_from_input()?;
    let depart = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let depart_pos = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let arrival_pos = client.read_double_from_input()?;

    client.read_ubyte_from_input()?;
    let description = client.read_string_from_input()?;

    Ok(TraciStage {
        type_,
        v_type,
        line,
        dest_stop,
        edges,
        travel_time,
        cost,
        length,
        intended,
        depart,
        depart_pos,
        arrival_pos,
        description,
    })
}