Skip to content

Commit 40d5aaf

Browse files
committed
added ApiWorldType, get_world, 4.3.0
1 parent 77116bc commit 40d5aaf

4 files changed

Lines changed: 90 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "the-bus-telemetry"
3-
version = "4.1.0"
3+
version = "4.3.0"
44
edition = "2024"
55
description = "Library for handling data exchange with the api (called telemetry) of the simulation software The Bus"
66
license = "MIT"
@@ -10,4 +10,4 @@ repository = "https://github.com/thatzok/TheBusTelemetry"
1010
serde = { version = "1.0", features = ["derive"] }
1111
serde_json = "1.0"
1212
reqwest = { version = "0.12", features = ["json","blocking"] }
13-
komsi = "1.0"
13+
komsi = "1.1"

src/api.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ impl RequestConfig {
5050
}
5151
}
5252

53+
#[derive(Deserialize, Debug, PartialEq)]
54+
pub struct ApiWorldType {
55+
#[serde(rename = "LevelName")]
56+
pub level_name: String,
57+
#[serde(rename = "DateTime")]
58+
pub date_time: String,
59+
#[serde(rename = "TimeFactor")]
60+
pub time_factor: f32,
61+
#[serde(rename = "BaseLatitude")]
62+
pub base_latitude: f64,
63+
#[serde(rename = "BaseLongitude")]
64+
pub base_longitude: f64,
65+
}
66+
5367
#[derive(Deserialize, Debug, PartialEq)]
5468
pub struct ApiVehicleType {
5569
#[serde(rename = "ActorName")]
@@ -82,9 +96,17 @@ pub struct ApiVehicleType {
8296

8397
#[derive(Deserialize, Debug, PartialEq)]
8498
pub struct ApiLamps {
85-
#[serde(rename = "LightHeadlight", alias = "LightHeadlight1", alias="Light Headlight")]
99+
#[serde(
100+
rename = "LightHeadlight",
101+
alias = "LightHeadlight1",
102+
alias = "Light Headlight"
103+
)]
86104
pub light_main: f32,
87-
#[serde(rename = "LightTraveling", alias = "LightTraveling1", alias="Light Travelling")]
105+
#[serde(
106+
rename = "LightTraveling",
107+
alias = "LightTraveling1",
108+
alias = "Light Travelling"
109+
)]
88110
pub traveller_light: f32,
89111
#[serde(rename = "Door Button 1", alias = "ButtonLight Door 1", default)]
90112
pub front_door_light: f32,
@@ -260,6 +282,26 @@ pub async fn get_vehicle(
260282
Ok(api_vehicle)
261283
}
262284

285+
pub async fn get_world(config: &RequestConfig) -> Result<ApiWorldType, Box<dyn std::error::Error>> {
286+
let path = "world";
287+
288+
if config.debugging {
289+
println!("get_world path: {}", path);
290+
}
291+
292+
let body = get_telemetry_data(&config, &path).await?;
293+
294+
let api_world: ApiWorldType = serde_json::from_value(body).map_err(|e| {
295+
eprintln!("Failed to parse API response as World JSON: {}", e);
296+
Box::new(e) as Box<dyn std::error::Error>
297+
})?;
298+
299+
if config.debugging {
300+
println!("{:?}", &api_world);
301+
}
302+
Ok(api_world)
303+
}
304+
263305
pub fn get_button_by_name(data: &serde_json::Value, name: &str) -> String {
264306
let ret = data
265307
.get("Buttons")

tests/json/world.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"LevelName": "Castrop",
3+
"IsInMainMenu": "false",
4+
"DateTime": "2026-01-01T09:43:48",
5+
"NightLightEnabled": "false",
6+
"TimeFactor": 1.0,
7+
"BaseLatitude": 51.549339,
8+
"BaseLongitude": 7.308765,
9+
"Temperature": -7.083445,
10+
"Wetness": 0.0,
11+
"RainIntensity": 0.0,
12+
"SnowIntensity": 0.0,
13+
"SnowPack": 1.0,
14+
"CloudIntensity": 0.0,
15+
"NebularIntensity": 0.5,
16+
"WindIntensity": 0.4
17+
}

tests/json_deserialization_test.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::fs;
22
use std::path::Path;
3-
use the_bus_telemetry::api::ApiVehicleType;
3+
use the_bus_telemetry::api::{ApiVehicleType, ApiWorldType};
44
use serde_json;
5+
use komsi::komsi::KomsiDateTime;
6+
57

68
#[test]
79
fn test_json_deserialization_from_files() {
@@ -14,6 +16,30 @@ fn test_json_deserialization_from_files() {
1416
test_vehicle_deserialization("tests/json/Solaris_Urbino.txt", "BP_Solaris_Urbino_18m_3D_C_2146245266");
1517
test_vehicle_deserialization("tests/json/vdl_citea.json", "BP_VDL_Citea_LLE_120_2D_C_2147124848");
1618

19+
test_world_deserialization("tests/json/world.json");
20+
}
21+
22+
fn test_world_deserialization(file_path: &str) {
23+
let file = Path::new(file_path);
24+
let json = fs::read_to_string(file)
25+
.expect(&format!("Failed to read {}", file_path));
26+
27+
let world: ApiWorldType = serde_json::from_str(&json)
28+
.expect(&format!("Failed to deserialize {}", file_path));
29+
30+
assert_eq!(world.level_name, "Castrop");
31+
assert_eq!(world.date_time, "2026-01-01T09:43:48");
32+
assert!(world.time_factor > 0.0);
33+
34+
let date_time = KomsiDateTime::from_iso(&world.date_time).unwrap();
35+
assert_eq!(date_time.year, 2026);
36+
assert_eq!(date_time.month, 1);
37+
assert_eq!(date_time.day, 1);
38+
assert_eq!(date_time.hour, 9);
39+
assert_eq!(date_time.min, 43);
40+
assert_eq!(date_time.sec, 48);
41+
42+
println!("Successfully deserialized world: {}", world.level_name);
1743
}
1844

1945
fn test_vehicle_deserialization(file_path: &str, actor_name: &str) {

0 commit comments

Comments
 (0)