Files
simconnect-sdk-rs/examples/src/data_multiple_objects.rs
2022-10-15 22:51:57 +01:00

65 lines
2.2 KiB
Rust

#![allow(dead_code)]
use simconnect_sdk::{Notification, SimConnect, SimConnectObject};
/// A data structure that will be used to receive data from SimConnect.
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second")]
struct GpsData {
#[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
lat: f64,
#[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
lon: f64,
#[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
alt: f64,
}
/// A second data structure that will be used to receive data from SimConnect.
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second", condition = "changed")]
pub struct OnGround {
#[simconnect(name = "SIM ON GROUND", unit = "bool")]
sim_on_ground: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = SimConnect::new("Simple Program");
match client {
Ok(mut client) => loop {
let notification = client.get_next_dispatch()?;
match notification {
Some(Notification::Open) => {
println!("Open");
// After the connection is successfully open, we register the structs
client.register_object::<GpsData>()?;
client.register_object::<OnGround>()?;
}
Some(Notification::Object(data)) => {
if let Ok(gps_data) = GpsData::try_from(&data) {
println!("{gps_data:?}");
// We've already got our data, there's no point in trying another in this iteration
continue;
}
if let Ok(on_ground) = OnGround::try_from(&data) {
println!("{on_ground:?}");
// We've already got our data, there's no point in trying another in this iteration
continue;
}
}
_ => (),
}
// sleep for about a frame to reduce CPU usage
std::thread::sleep(std::time::Duration::from_millis(16));
},
Err(e) => {
println!("Error: {e:?}")
}
}
Ok(())
}