Skip to content

Commit a4aab38

Browse files
committed
updated docs, bump to 4.4.0
1 parent 40d5aaf commit a4aab38

5 files changed

Lines changed: 92 additions & 5 deletions

File tree

.github/workflows/buildandpublish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
- name: Build binaries in "release" mode
2929
run: cargo build -r
3030
- name: "Publish to crates.io"
31-
run: cargo publish # publishes your crate as a library that can be added as a dependency
31+
run: cargo publish

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
[package]
22
name = "the-bus-telemetry"
3-
version = "4.3.0"
3+
version = "4.4.0"
44
edition = "2024"
5+
rust-version = "1.93.1"
56
description = "Library for handling data exchange with the api (called telemetry) of the simulation software The Bus"
6-
license = "MIT"
7+
license = "MIT OR Apache-2.0"
78
readme = "README.md"
89
repository = "https://github.com/thatzok/TheBusTelemetry"
910
[dependencies]
1011
serde = { version = "1.0", features = ["derive"] }
1112
serde_json = "1.0"
1213
reqwest = { version = "0.12", features = ["json","blocking"] }
13-
komsi = "1.1"
14+
komsi = "1.5.1"

src/api.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
//! This module handles the raw interaction with The Bus Telemetry API.
2+
13
use serde::Deserialize;
24
use std::string::ToString;
35
use std::time::Duration;
46

7+
/// Configuration for API requests.
58
pub struct RequestConfig {
9+
/// Host address of the telemetry server (default: "127.0.0.1").
610
pub host: String,
11+
/// Port of the telemetry server (default: "37337").
712
pub port: String,
13+
/// Name of the vehicle to query (default: "Current").
814
pub vehicle_name: String,
15+
/// Model of the vehicle to query (default: "Current").
916
pub vehicle_model: String,
17+
/// Request timeout (default: 300ms).
1018
pub timeout: Duration,
19+
/// Enable debug logging of URLs and data.
1120
pub debugging: bool,
1221
}
1322

1423
impl RequestConfig {
24+
/// Creates a new `RequestConfig` with default values.
1525
pub fn new() -> Self {
1626
Self {
1727
host: "127.0.0.1".to_string(),
@@ -22,126 +32,170 @@ impl RequestConfig {
2232
debugging: false,
2333
}
2434
}
35+
/// Sets the host address.
2536
pub fn host(mut self, host: String) -> Self {
2637
self.host = host;
2738
self
2839
}
2940

41+
/// Sets the port.
3042
pub fn port(mut self, port: String) -> Self {
3143
self.port = port;
3244
self
3345
}
46+
/// Sets the vehicle name.
3447
pub fn vehicle_name(mut self, vehicle_name: String) -> Self {
3548
self.vehicle_name = vehicle_name;
3649
self
3750
}
51+
/// Sets the vehicle model.
3852
pub fn vehicle_model(mut self, vehicle_model: String) -> Self {
3953
self.vehicle_model = vehicle_model;
4054
self
4155
}
4256

57+
/// Sets the request timeout.
4358
pub fn timeout(mut self, timeout: Duration) -> Self {
4459
self.timeout = timeout;
4560
self
4661
}
62+
/// Sets the debugging flag.
4763
pub fn debugging(mut self, debugging: bool) -> Self {
4864
self.debugging = debugging;
4965
self
5066
}
5167
}
5268

69+
/// World telemetry data.
5370
#[derive(Deserialize, Debug, PartialEq)]
5471
pub struct ApiWorldType {
72+
/// Name of the current level.
5573
#[serde(rename = "LevelName")]
5674
pub level_name: String,
75+
/// Current date and time in the game.
5776
#[serde(rename = "DateTime")]
5877
pub date_time: String,
78+
/// Time acceleration factor.
5979
#[serde(rename = "TimeFactor")]
6080
pub time_factor: f32,
81+
/// Latitude of the world origin.
6182
#[serde(rename = "BaseLatitude")]
6283
pub base_latitude: f64,
84+
/// Longitude of the world origin.
6385
#[serde(rename = "BaseLongitude")]
6486
pub base_longitude: f64,
6587
}
6688

89+
/// Vehicle telemetry data.
6790
#[derive(Deserialize, Debug, PartialEq)]
6891
pub struct ApiVehicleType {
92+
/// Internal actor name.
6993
#[serde(rename = "ActorName")]
7094
pub actor_name: String,
95+
/// Vehicle model name.
7196
#[serde(rename = "VehicleModel")]
7297
pub vehicle_model: String,
98+
/// Whether the ignition is enabled (string "true"/"false").
7399
#[serde(rename = "IgnitionEnabled")]
74100
pub ignition_enabled: String,
101+
/// Whether the engine is started (string "true"/"false").
75102
#[serde(rename = "EngineStarted")]
76103
pub engine_started: String,
104+
/// Whether warning lights are active (string "true"/"false").
77105
#[serde(rename = "WarningLights")]
78106
pub warning_lights: String,
107+
/// Whether any passenger door is open (string "true"/"false").
79108
#[serde(rename = "PassengerDoorsOpen")]
80109
pub passenger_doors_open: String,
110+
/// Whether the fixing (parking) brake is engaged (string "true"/"false").
81111
#[serde(rename = "FixingBrake")]
82112
pub fixing_brake: String,
113+
/// Current speed in km/h.
83114
#[serde(rename = "Speed")]
84115
pub speed: f32,
116+
/// Allowed speed limit.
85117
#[serde(rename = "AllowedSpeed")]
86118
pub allowed_speed: f32,
119+
/// Fuel level on display (0.0 to 1.0).
87120
#[serde(rename = "DisplayFuel")]
88121
pub display_fuel: f32,
122+
/// Indicator state (-1: left, 0: off, 1: right).
89123
#[serde(rename = "IndicatorState")]
90124
pub indicator_state: i8,
125+
/// Status of all external and internal lamps.
91126
#[serde(rename = "AllLamps")]
92127
pub all_lamps: ApiLamps,
128+
/// List of buttons and their states.
93129
#[serde(rename = "Buttons", default)]
94130
pub buttons: Vec<ApiButton>,
95131
}
96132

133+
/// Represents various lamp intensities or states.
97134
#[derive(Deserialize, Debug, PartialEq)]
98135
pub struct ApiLamps {
136+
/// Main headlight intensity (0.0 to 1.0).
99137
#[serde(
100138
rename = "LightHeadlight",
101139
alias = "LightHeadlight1",
102140
alias = "Light Headlight"
103141
)]
104142
pub light_main: f32,
143+
/// High beam / traveller light intensity (0.0 or 1.0).
105144
#[serde(
106145
rename = "LightTraveling",
107146
alias = "LightTraveling1",
108147
alias = "Light Travelling"
109148
)]
110149
pub traveller_light: f32,
150+
/// Front door light state.
111151
#[serde(rename = "Door Button 1", alias = "ButtonLight Door 1", default)]
112152
pub front_door_light: f32,
153+
/// Second door light state.
113154
#[serde(rename = "Door Button 2", alias = "ButtonLight Door 2", default)]
114155
pub second_door_light: f32,
156+
/// Third door light state.
115157
#[serde(rename = "Door Button 3", alias = "ButtonLight Door 3", default)]
116158
pub third_door_light: f32,
159+
/// Fourth door light state.
117160
#[serde(rename = "Door Button 4", alias = "ButtonLight Door 4", default)]
118161
pub fourth_door_light: f32,
162+
/// Stop request LED intensity.
119163
#[serde(rename = "LED StopRequest", default)]
120164
pub led_stop_request: f32,
165+
/// Bus stop brake light intensity.
121166
#[serde(rename = "ButtonLight BusStopBrake", default)]
122167
pub light_stopbrake: f32,
123168
}
124169

170+
/// Represents a button in the vehicle and its current state.
125171
#[derive(Deserialize, Debug, PartialEq, Default, Clone)]
126172
pub struct ApiButton {
173+
/// Button name.
127174
#[serde(rename = "Name")]
128175
pub name: String,
176+
/// Tooltip text for the button.
129177
#[serde(rename = "Tooltip", default)]
130178
pub tooltip: String,
179+
/// Current state of the button (e.g. "on", "off", "Drive", "Neutral").
131180
#[serde(rename = "State", default)]
132181
pub state: String,
182+
/// Numeric value as string, if applicable.
133183
#[serde(rename = "Value", default)]
134184
pub value: String,
185+
/// Possible actions for this button.
135186
#[serde(rename = "Actions", default)]
136187
pub actions: Vec<String>,
188+
/// Possible states for this button.
137189
#[serde(rename = "States", default)]
138190
pub states: Vec<String>,
139191
}
140192

141193
impl ApiVehicleType {
194+
/// Returns the button with the given name, if found.
142195
pub fn get_button(&self, name: &str) -> Option<ApiButton> {
143196
self.buttons.iter().find(|b| b.name == name).cloned()
144197
}
198+
/// Returns the state of the button with the given name, or an empty string if not found.
145199
pub fn get_button_state(&self, name: &str) -> String {
146200
self.buttons
147201
.iter()
@@ -150,6 +204,7 @@ impl ApiVehicleType {
150204
.unwrap_or_else(|| "".to_string())
151205
}
152206

207+
/// Returns the state of the first button whose name contains the given part.
153208
pub fn get_button_state_contains(&self, part: &str) -> String {
154209
self.buttons
155210
.iter()
@@ -158,6 +213,7 @@ impl ApiVehicleType {
158213
.unwrap_or_else(|| "".to_string())
159214
}
160215

216+
/// Returns all buttons with the exact given name.
161217
pub fn filtered_buttons(&self, name: &str) -> Vec<ApiButton> {
162218
self.buttons
163219
.iter()
@@ -166,10 +222,12 @@ impl ApiVehicleType {
166222
.collect()
167223
}
168224

225+
/// Keeps only the buttons with the exact given name in the vehicle.
169226
pub fn retain_buttons_by_name(&mut self, name: &str) {
170227
self.buttons.retain(|b| b.name == name);
171228
}
172229

230+
/// Returns a vector of tuples containing (name, state) for all buttons.
173231
pub fn buttons_name_state(&self) -> Vec<(String, String)> {
174232
self.buttons
175233
.iter()
@@ -178,6 +236,7 @@ impl ApiVehicleType {
178236
}
179237
}
180238

239+
/// Sends a command to the vehicle via the telemetry API.
181240
pub async fn send_telemetry_bus_cmd(
182241
config: &RequestConfig,
183242
cmd: &str,
@@ -199,6 +258,7 @@ pub async fn send_telemetry_bus_cmd(
199258
Ok(())
200259
}
201260

261+
/// Fetches raw JSON telemetry data from a specific API path.
202262
pub async fn get_telemetry_data(
203263
config: &RequestConfig,
204264
path: &str,
@@ -220,6 +280,8 @@ pub async fn get_telemetry_data(
220280
Ok(value)
221281
}
222282

283+
/// Returns the current vehicle name from the "player" telemetry endpoint.
284+
/// Returns an empty string if the player is not in a vehicle or if the request fails.
223285
pub async fn get_current_vehicle_name(config: &RequestConfig) -> String {
224286
let result = get_telemetry_data(&config, "player").await;
225287

@@ -259,6 +321,7 @@ pub async fn get_current_vehicle_name(config: &RequestConfig) -> String {
259321
bus
260322
}
261323

324+
/// Fetches telemetry data for the vehicle specified in `config`.
262325
pub async fn get_vehicle(
263326
config: &RequestConfig,
264327
) -> Result<ApiVehicleType, Box<dyn std::error::Error>> {
@@ -282,6 +345,7 @@ pub async fn get_vehicle(
282345
Ok(api_vehicle)
283346
}
284347

348+
/// Fetches world telemetry data (time, weather, etc).
285349
pub async fn get_world(config: &RequestConfig) -> Result<ApiWorldType, Box<dyn std::error::Error>> {
286350
let path = "world";
287351

@@ -302,6 +366,7 @@ pub async fn get_world(config: &RequestConfig) -> Result<ApiWorldType, Box<dyn s
302366
Ok(api_world)
303367
}
304368

369+
/// Extracts the state of a button by name from a raw JSON value containing a "Buttons" array.
305370
pub fn get_button_by_name(data: &serde_json::Value, name: &str) -> String {
306371
let ret = data
307372
.get("Buttons")

src/api2vehicle.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//! This module provides functions to map API-specific telemetry data to type-safe komsi vehicle states.
2+
13
use crate::api::ApiVehicleType;
24
use komsi::vehicle::VehicleState;
35

6+
/// Maps `ApiVehicleType` data to a `VehicleState` structure.
47
pub fn get_vehicle_state_from_api(av: ApiVehicleType) -> VehicleState {
58
let mut s = VehicleState::default();
69

@@ -29,7 +32,7 @@ pub fn get_vehicle_state_from_api(av: ApiVehicleType) -> VehicleState {
2932
_ => s.fixing_brake = false,
3033
}
3134

32-
// we only check if set, not in which direction (in api: -1,0,1 for left,off,right)
35+
// we only check if set and in which direction (in api: -1,0,1 for left,off,right)
3336
match av.indicator_state {
3437
0 => s.indicator = 0, // off
3538
-1 => s.indicator = 1, // on left

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
//! This crate provides a client for The Bus Telemetry API.
2+
//! It fetches telemetry data from the game "The Bus" and maps it to common vehicle states.
3+
14
// This file exposes the modules used by both binary targets and integration tests
25
pub mod api;
36
pub mod api2vehicle;
7+
8+
pub use api::ApiButton;
9+
pub use api::ApiLamps;
10+
pub use api::ApiVehicleType;
11+
pub use api::ApiWorldType;
12+
pub use api::RequestConfig;
13+
pub use api::get_current_vehicle_name;
14+
pub use api::get_vehicle;
15+
pub use api::get_world;
16+
pub use api::send_telemetry_bus_cmd;
17+
pub use api::get_telemetry_data;
18+
pub use api::get_button_by_name;
19+
20+
pub use api2vehicle::get_vehicle_state_from_api;
21+

0 commit comments

Comments
 (0)