1+ //! This module handles the raw interaction with The Bus Telemetry API.
2+
13use serde:: Deserialize ;
24use std:: string:: ToString ;
35use std:: time:: Duration ;
46
7+ /// Configuration for API requests.
58pub 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
1423impl 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 ) ]
5471pub 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 ) ]
6891pub 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 ) ]
98135pub 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 ) ]
126172pub 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
141193impl 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.
181240pub 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.
202262pub 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.
223285pub 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`.
262325pub 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).
285349pub 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.
305370pub fn get_button_by_name ( data : & serde_json:: Value , name : & str ) -> String {
306371 let ret = data
307372 . get ( "Buttons" )
0 commit comments