Define the desired API
This commit is contained in:
102
examples/basic.rs
Normal file
102
examples/basic.rs
Normal file
@@ -0,0 +1,102 @@
|
||||
use logging::setup_logging;
|
||||
use simconnect_sdk::{
|
||||
ConditionEnum, DataType, Notification, NotificationData, PeriodEnum, SimConnect,
|
||||
SimConnectError,
|
||||
};
|
||||
use tracing::{error, info};
|
||||
|
||||
mod logging;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GpsData {
|
||||
pub lat: f64,
|
||||
pub lon: f64,
|
||||
pub alt: f64,
|
||||
pub gps_ground_magnetic_track: f64,
|
||||
pub gps_ground_speed: f64,
|
||||
}
|
||||
|
||||
impl simconnect_sdk::SimConnectObject for GpsData {
|
||||
fn register(client: &mut SimConnect, id: u32) -> Result<(), SimConnectError> {
|
||||
client.add_to_data_definition(id, "PLANE LATITUDE", "degrees", DataType::F64)?;
|
||||
client.add_to_data_definition(id, "PLANE LONGITUDE", "degrees", DataType::F64)?;
|
||||
client.add_to_data_definition(id, "PLANE ALTITUDE", "meters", DataType::F64)?;
|
||||
client.add_to_data_definition(id, "GPS GROUND MAGNETIC TRACK", "degrees", DataType::F64)?;
|
||||
client.add_to_data_definition(
|
||||
id,
|
||||
"GPS GROUND SPEED",
|
||||
"Meters per second",
|
||||
DataType::F64,
|
||||
)?;
|
||||
client.request_data_on_sim_object(id, PeriodEnum::Second, ConditionEnum::None)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&NotificationData> for GpsData {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &NotificationData) -> Result<Self, Self::Error> {
|
||||
value.try_into::<GpsData>().ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OnGround {
|
||||
pub sim_on_ground: bool,
|
||||
}
|
||||
|
||||
impl simconnect_sdk::SimConnectObject for OnGround {
|
||||
fn register(client: &mut SimConnect, id: u32) -> Result<(), SimConnectError> {
|
||||
client.add_to_data_definition(id, "SIM ON GROUND", "bool", DataType::Bool)?;
|
||||
client.request_data_on_sim_object(id, PeriodEnum::Second, ConditionEnum::None)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&NotificationData> for OnGround {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: &NotificationData) -> Result<Self, Self::Error> {
|
||||
value.try_into::<OnGround>().ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
setup_logging()?;
|
||||
|
||||
let client = SimConnect::new("Simple Program");
|
||||
|
||||
match client {
|
||||
Ok(mut client) => loop {
|
||||
let notification = client.get_next_dispatch()?;
|
||||
|
||||
match notification {
|
||||
Some(Notification::Open) => {
|
||||
info!("Open");
|
||||
|
||||
client.register_object::<GpsData>()?;
|
||||
client.register_object::<OnGround>()?;
|
||||
}
|
||||
Some(Notification::Data(data)) => {
|
||||
if let Ok(gps_data) = GpsData::try_from(&data) {
|
||||
info!("GPS Data: {:?}", gps_data);
|
||||
continue;
|
||||
}
|
||||
if let Ok(on_ground) = OnGround::try_from(&data) {
|
||||
info!("On Ground data: {:?}", on_ground);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
error!("{e:?}")
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
18
examples/logging.rs
Normal file
18
examples/logging.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
||||
|
||||
pub fn setup_logging() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let filter_layer = EnvFilter::try_from_default_env().or_else(|_| EnvFilter::try_new("info"))?;
|
||||
let fmt_layer = fmt::layer()
|
||||
.with_target(false)
|
||||
.with_span_events(fmt::format::FmtSpan::FULL);
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(filter_layer)
|
||||
.with(fmt_layer)
|
||||
.init();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn main() {}
|
Reference in New Issue
Block a user