pub struct SimConnect { /* private fields */ }
Expand description

SimConnect SDK Client.

Example

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(())
}

Implementations

Create a new SimConnect SDK client.

Receive the next SimConnect message.

Remarks

This is a non-blocking function. If there are no messages to receive, it will return None immediately. When called in a loop, it is recommended to use a short sleep time.

Associates a client defined event with a Microsoft Flight Simulator event name.

WIP

Request that a specific system event is notified to the client.

Request that notifications are no longer received for the specified system event.

Request a list of all the facilities of a given type currently held in the facilities cache.

Errors
Remarks

The simulation keeps a facilities cache of all the airports, waypoints, NDB and VOR stations within a certain radius of the user aircraft. This radius varies depending on where the aircraft is in the world, but is at least large enough to encompass the whole of the reality bubble for airports and waypoints, and can be over 200 miles for VOR and NDB stations. As the user aircraft moves facilities will be added to, and removed from, the cache. However, in the interests pf performance, hysteresis is built into the system.

Request notifications when a facility of a certain type is added to the facilities cache.

When this function is first called, a full list from the cache will be sent, thereafter just the additions will be transmitted. No notification is given when a facility is removed from the cache. To terminate these notifications use the crate::SimConnect::unsubscribe_to_facilities function.

Errors
Remarks

The simulation keeps a facilities cache of all the airports, waypoints, NDB and VOR stations within a certain radius of the user aircraft. This radius varies depending on where the aircraft is in the world, but is at least large enough to encompass the whole of the reality bubble for airports and waypoints, and can be over 200 miles for VOR and NDB stations. As the user aircraft moves facilities will be added to, and removed from, the cache. However, in the interests pf performance, hysteresis is built into the system.

Request that notifications of additions to the facilities cache are not longer sent.

Remarks

This is used to terminate notifications generated by the crate::SimConnect::request_facilities_list or crate::SimConnect::subscribe_to_facilities functions.

Add a Microsoft Flight Simulator simulation variable name to a client defined object definition.

Remarks

The crate::SimConnectObject macro will automatically call this method for the struct.

Request when the SimConnect client is to receive data values for a specific object.

Current limitation

All objects are requested from the local user’s aircraft POV. This comes with the side-effect that currently there is no way to request data for other aircraft in multiplayer.

Arguments
  • request_id - The request ID of the object.
  • period - crate::Period
  • condition - crate::Condition
  • interval - The number of period events that should elapse between transmissions of the data. 0 means the data is transmitted every Period, 1 means that the data is transmitted every other Period, etc.
Remarks

The crate::SimConnectObject macro will automatically call this method for the struct.

Trait Implementations

Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more