simconnect_sdk/simconnect/facilities.rs
1use crate::{bindings, success, FacilityType, SimConnect, SimConnectError};
2
3impl SimConnect {
4 /// Request a list of all the facilities of a given type currently held in the facilities cache.
5 ///
6 /// # Errors
7 /// - [`crate::SimConnectError::ObjectAlreadyRegistered`] -- Only one request per facility type is supported.
8 ///
9 /// # Remarks
10 /// The simulation keeps a facilities cache of all the airports, waypoints, NDB and VOR stations within a certain radius of the user aircraft.
11 /// 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.
12 /// 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.
13 #[tracing::instrument(
14 name = "SimConnect::request_facilities_list",
15 level = "debug",
16 skip(self)
17 )]
18 pub fn request_facilities_list(
19 &mut self,
20 facility_type: FacilityType,
21 ) -> Result<(), SimConnectError> {
22 let type_name = facility_type.to_type_name();
23 let request_id = self.new_request_id(type_name, true)?;
24
25 success!(unsafe {
26 bindings::SimConnect_RequestFacilitiesList(
27 self.handle.as_ptr(),
28 facility_type.into(),
29 request_id,
30 )
31 })
32 }
33
34 /// Request notifications when a facility of a certain type is added to the facilities cache.
35 ///
36 /// When this function is first called, a full list from the cache will be sent, thereafter just the additions will be transmitted.
37 /// No notification is given when a facility is removed from the cache.
38 /// To terminate these notifications use the [`crate::SimConnect::unsubscribe_to_facilities`] function.
39 ///
40 /// # Errors
41 /// - [`crate::SimConnectError::ObjectAlreadyRegistered`] -- Only one subscription per facility type is supported. If you wish to create a new one, unsubscribe first using [`crate::SimConnect::unsubscribe_to_facilities`].
42 ///
43 /// # Remarks
44 /// The simulation keeps a facilities cache of all the airports, waypoints, NDB and VOR stations within a certain radius of the user aircraft.
45 /// 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.
46 /// 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.
47 #[tracing::instrument(
48 name = "SimConnect::subscribe_to_facilities",
49 level = "debug",
50 skip(self)
51 )]
52 pub fn subscribe_to_facilities(
53 &mut self,
54 facility_type: FacilityType,
55 ) -> Result<(), SimConnectError> {
56 let type_name = facility_type.to_type_name();
57 let request_id = self.new_request_id(type_name, false)?;
58
59 success!(unsafe {
60 bindings::SimConnect_SubscribeToFacilities(
61 self.handle.as_ptr(),
62 facility_type.into(),
63 request_id,
64 )
65 })
66 }
67
68 /// Request that notifications of additions to the facilities cache are not longer sent.
69 ///
70 /// # Remarks
71 /// This is used to terminate notifications generated by the [`crate::SimConnect::subscribe_to_facilities`] function.
72 #[tracing::instrument(
73 name = "SimConnect::unsubscribe_to_facilities",
74 level = "debug",
75 skip(self)
76 )]
77 pub fn unsubscribe_to_facilities(
78 &mut self,
79 facility_type: FacilityType,
80 ) -> Result<(), SimConnectError> {
81 let type_name = facility_type.to_type_name();
82
83 success!(unsafe {
84 bindings::SimConnect_UnsubscribeToFacilities(self.handle.as_ptr(), facility_type.into())
85 })?;
86
87 self.unregister_request_id_by_type_name(&type_name);
88
89 Ok(())
90 }
91}