Struct SimConnect

Source
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§

Source§

impl SimConnect

Source

pub fn new(name: &str) -> Result<Self, SimConnectError>

Create a new SimConnect SDK client.

Source

pub fn get_next_dispatch( &mut self, ) -> Result<Option<Notification>, SimConnectError>

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.

Source§

impl SimConnect

Source

pub fn register_event( &self, event: ClientEvent, notification_group: NotificationGroup, ) -> Result<(), SimConnectError>

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

WIP

Source

pub fn subscribe_to_system_event( &mut self, event: SystemEventRequest, ) -> Result<(), SimConnectError>

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

Source

pub fn unsubscribe_from_system_event( &mut self, event: SystemEventRequest, ) -> Result<(), SimConnectError>

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

Source§

impl SimConnect

Source

pub fn request_facilities_list( &mut self, facility_type: FacilityType, ) -> Result<(), SimConnectError>

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.

Source

pub fn subscribe_to_facilities( &mut self, facility_type: FacilityType, ) -> Result<(), SimConnectError>

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.

Source

pub fn unsubscribe_to_facilities( &mut self, facility_type: FacilityType, ) -> Result<(), SimConnectError>

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::subscribe_to_facilities function.

Source§

impl SimConnect

Source

pub fn register_object<T: SimConnectObjectExt>( &mut self, ) -> Result<u32, SimConnectError>

Source

pub fn unregister_object<T: SimConnectObjectExt>( &mut self, ) -> Result<u32, SimConnectError>

Source

pub fn add_to_data_definition( &self, request_id: u32, name: &str, unit: &str, data_type: DataType, ) -> Result<(), SimConnectError>

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.

Source

pub fn request_data_on_sim_object( &self, request_id: u32, period: Period, condition: Condition, interval: u32, ) -> Result<(), SimConnectError>

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§

Source§

impl Debug for SimConnect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for SimConnect

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more