Crate simconnect_sdk

Source
Expand description

§SimConnect SDK

SimConnect SDK in Rust.

§Usage

[dependencies]
simconnect-sdk = { version = "0.2", features = ["derive"] }
use simconnect_sdk::{Notification, SimConnect, SimConnectObject};

/// A data structure that will be used to receive data from SimConnect.
/// See the documentation of `SimConnectObject` for more information on the arguments of the `simconnect` attribute.
#[derive(Debug, Clone, SimConnectObject)]
#[simconnect(period = "second")]
#[allow(dead_code)]
struct AirplaneData {
    #[simconnect(name = "TITLE")]
    title: String,
    #[simconnect(name = "CATEGORY")]
    category: String,
    #[simconnect(name = "PLANE LATITUDE", unit = "degrees")]
    lat: f64,
    #[simconnect(name = "PLANE LONGITUDE", unit = "degrees")]
    lon: f64,
    #[simconnect(name = "PLANE ALTITUDE", unit = "feet")]
    alt: f64,
    #[simconnect(name = "SIM ON GROUND")]
    sim_on_ground: bool,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = SimConnect::new("Receiving data example");

    match client {
        Ok(mut client) => {
            let mut notifications_received = 0;

            loop {
                let notification = client.get_next_dispatch()?;

                match notification {
                    Some(Notification::Open) => {
                        println!("Connection opened.");

                        // After the connection is successfully open, we register the struct
                        client.register_object::<AirplaneData>()?;
                    }
                    Some(Notification::Object(data)) => {
                        if let Ok(airplane_data) = AirplaneData::try_from(&data) {
                            println!("{airplane_data:?}");

                            notifications_received += 1;

                            // After we have received 10 notifications, we unregister the struct
                            if notifications_received > 10 {
                                client.unregister_object::<AirplaneData>()?;
                                println!("Subscription stopped.");
                                break;
                            }
                        }
                    }
                    _ => (),
                }

                // sleep for about a frame to reduce CPU usage
                std::thread::sleep(std::time::Duration::from_millis(16));
            }
        }
        Err(e) => {
            println!("Error: {e:?}")
        }
    }

    Ok(())
}

See more examples.

Structs§

Airport
Information on a single airport in the facilities cache.
NDB
Information on a single NDB station in the facilities cache.
Object
Notification data object.
SimConnect
SimConnect SDK Client.
VOR
Information on a single VOR station in the facilities cache.
Waypoint
Information on a single waypoint in the facilities cache.

Enums§

ClientEvent
SimConnect Client Event.
Condition
Specifies under which conditions the data is to be sent by the server and received by the client.
DataType
crate::SimConnectObject object property data type.
FacilityType
Facility Type. The simulation keeps a facilities cache of all the airports, waypoints, NDB and VOR stations within a certain radius of the user aircraft. They can be requested using crate::SimConnect::subscribe_to_facilities or crate::SimConnect::request_facilities_list.
Notification
Notification received from SimConnect.
NotificationGroup
SimConnect event notification group.
Period
Specifies how often the data is to be sent by the server and received by the client. 0 - every interval. 1 - every other interval. 2 - every third interval. etc.
SimConnectError
SimConnect SDK error.
SystemEvent
SimConnect System Event Notification.
SystemEventRequest
SimConnect System Event Request.
ViewType
Cockpit view type.

Traits§

SimConnectObjectExt
Trait to be implemented by objects that can be registered with SimConnect.

Functions§

fixed_c_str_to_string

Derive Macros§

SimConnectObject
SimConnectObject derive macro.