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
impl SimConnect
Sourcepub fn new(name: &str) -> Result<Self, SimConnectError>
pub fn new(name: &str) -> Result<Self, SimConnectError>
Create a new SimConnect SDK client.
Sourcepub fn get_next_dispatch(
&mut self,
) -> Result<Option<Notification>, SimConnectError>
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
impl SimConnect
Sourcepub fn register_event(
&self,
event: ClientEvent,
notification_group: NotificationGroup,
) -> Result<(), SimConnectError>
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
Sourcepub fn subscribe_to_system_event(
&mut self,
event: SystemEventRequest,
) -> Result<(), SimConnectError>
pub fn subscribe_to_system_event( &mut self, event: SystemEventRequest, ) -> Result<(), SimConnectError>
Request that a specific system event is notified to the client.
Sourcepub fn unsubscribe_from_system_event(
&mut self,
event: SystemEventRequest,
) -> Result<(), SimConnectError>
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
impl SimConnect
Sourcepub fn request_facilities_list(
&mut self,
facility_type: FacilityType,
) -> Result<(), SimConnectError>
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
crate::SimConnectError::ObjectAlreadyRegistered
– Only one request per facility type is supported.
§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.
Sourcepub fn subscribe_to_facilities(
&mut self,
facility_type: FacilityType,
) -> Result<(), SimConnectError>
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
crate::SimConnectError::ObjectAlreadyRegistered
– Only one subscription per facility type is supported. If you wish to create a new one, unsubscribe first usingcrate::SimConnect::unsubscribe_to_facilities
.
§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.
Sourcepub fn unsubscribe_to_facilities(
&mut self,
facility_type: FacilityType,
) -> Result<(), SimConnectError>
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
impl SimConnect
pub fn register_object<T: SimConnectObjectExt>( &mut self, ) -> Result<u32, SimConnectError>
pub fn unregister_object<T: SimConnectObjectExt>( &mut self, ) -> Result<u32, SimConnectError>
Sourcepub fn add_to_data_definition(
&self,
request_id: u32,
name: &str,
unit: &str,
data_type: DataType,
) -> Result<(), SimConnectError>
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.
Sourcepub fn request_data_on_sim_object(
&self,
request_id: u32,
period: Period,
condition: Condition,
interval: u32,
) -> Result<(), SimConnectError>
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.