simconnect_sdk/simconnect/
objects.rs

1use crate::{
2    as_c_string, bindings, success, Condition, DataType, Period, SimConnect, SimConnectError,
3    SimConnectObjectExt,
4};
5
6impl SimConnect {
7    // Register an object with SimConnect by assigning it an unique interval `request_id` and then calling the [`crate::SimConnectObjectExt::register`] method on the struct.
8    #[tracing::instrument(name = "SimConnect::register_object", level = "debug", skip(self))]
9    pub fn register_object<T: SimConnectObjectExt>(&mut self) -> Result<u32, SimConnectError> {
10        let type_name: String = std::any::type_name::<T>().into();
11
12        let id = self.new_request_id(type_name, false)?;
13
14        T::register(self, id)?;
15
16        Ok(id)
17    }
18
19    // Unregister an object with SimConnect.
20    #[tracing::instrument(name = "SimConnect::unregister_object", level = "debug", skip(self))]
21    pub fn unregister_object<T: SimConnectObjectExt>(&mut self) -> Result<u32, SimConnectError> {
22        let type_name: String = std::any::type_name::<T>().into();
23
24        let request_id = self
25            .registered_objects
26            .get(&type_name)
27            .ok_or_else(|| SimConnectError::ObjectNotRegistered(type_name.clone()))?;
28
29        success!(unsafe {
30            bindings::SimConnect_ClearDataDefinition(self.handle.as_ptr(), request_id.id)
31        })?;
32
33        self.unregister_request_id_by_type_name(&type_name)
34            .ok_or(SimConnectError::ObjectNotRegistered(type_name))
35    }
36
37    /// Add a Microsoft Flight Simulator simulation variable name to a client defined object definition.
38    ///
39    /// # Remarks
40    /// The [`crate::SimConnectObject`] macro will automatically call this method for the struct.
41    #[tracing::instrument(
42        name = "SimConnect::add_to_data_definition",
43        level = "debug",
44        skip(self)
45    )]
46    pub fn add_to_data_definition(
47        &self,
48        request_id: u32,
49        name: &str,
50        unit: &str,
51        data_type: DataType,
52    ) -> Result<(), SimConnectError> {
53        let c_type = match data_type {
54            DataType::Float64 => bindings::SIMCONNECT_DATATYPE_SIMCONNECT_DATATYPE_FLOAT64,
55            DataType::Bool => bindings::SIMCONNECT_DATATYPE_SIMCONNECT_DATATYPE_INT32,
56            DataType::String => bindings::SIMCONNECT_DATATYPE_SIMCONNECT_DATATYPE_STRING256,
57        };
58
59        success!(unsafe {
60            bindings::SimConnect_AddToDataDefinition(
61                self.handle.as_ptr(),
62                request_id,
63                as_c_string!(name),
64                as_c_string!(unit),
65                c_type,
66                0.0,
67                u32::MAX,
68            )
69        })
70    }
71
72    /// Request when the SimConnect client is to receive data values for a specific object.
73    ///
74    /// # Current limitation
75    /// All objects are requested from the local user's aircraft POV.
76    /// This comes with the side-effect that currently there is no way to request data for other aircraft in multiplayer.
77    ///
78    /// # Arguments
79    /// * `request_id` - The request ID of the object.
80    /// * `period` - [`crate::Period`]
81    /// * `condition` - [`crate::Condition`]
82    /// * `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.
83    ///
84    /// # Remarks
85    /// The [`crate::SimConnectObject`] macro will automatically call this method for the struct.
86    #[tracing::instrument(
87        name = "SimConnect::request_data_on_sim_object",
88        level = "debug",
89        skip(self)
90    )]
91    pub fn request_data_on_sim_object(
92        &self,
93        request_id: u32,
94        period: Period,
95        condition: Condition,
96        interval: u32,
97    ) -> Result<(), SimConnectError> {
98        success!(unsafe {
99            bindings::SimConnect_RequestDataOnSimObject(
100                self.handle.as_ptr(),
101                request_id,
102                request_id,
103                bindings::SIMCONNECT_OBJECT_ID_USER,
104                period.into(),
105                condition.into(),
106                0,
107                interval,
108                0,
109            )
110        })
111    }
112}