|
1 | | -use std::rc::Rc; |
2 | | - |
3 | | -use crate::download::downloader::NavdataDownloader; |
4 | | -use msfs::{commbus::*, sys::sGaugeDrawData, MSFSEvent}; |
5 | | - |
6 | | -pub struct Dispatcher<'a> { |
7 | | - commbus: CommBus<'a>, |
8 | | - downloader: Rc<NavdataDownloader>, |
9 | | - delta_time: std::time::Duration, |
10 | | -} |
11 | | - |
12 | | -impl<'a> Dispatcher<'a> { |
13 | | - pub fn new() -> Self { |
14 | | - Dispatcher { |
15 | | - commbus: CommBus::default(), |
16 | | - downloader: Rc::new(NavdataDownloader::new()), |
17 | | - delta_time: std::time::Duration::from_secs(0), |
18 | | - } |
19 | | - } |
20 | | - |
21 | | - pub fn on_msfs_event(&mut self, event: MSFSEvent) { |
22 | | - match event { |
23 | | - MSFSEvent::PostInitialize => { |
24 | | - self.handle_initialized(); |
25 | | - } |
26 | | - MSFSEvent::PreDraw(data) => { |
27 | | - self.handle_update(data); |
28 | | - } |
29 | | - MSFSEvent::PreKill => { |
30 | | - self.commbus.unregister_all(); |
31 | | - } |
32 | | - |
33 | | - _ => {} |
34 | | - } |
35 | | - } |
36 | | - |
37 | | - fn handle_initialized(&mut self) { |
38 | | - { |
39 | | - let captured_downloader = self.downloader.clone(); |
40 | | - self.commbus |
41 | | - .register("NAVIGRAPH_DownloadNavdata", move |args| { |
42 | | - captured_downloader.download(Dispatcher::trim_null_terminator(args)) |
43 | | - }) |
44 | | - .expect("Failed to register NAVIGRAPH_DownloadNavdata"); |
45 | | - } |
46 | | - { |
47 | | - let captured_downloader = self.downloader.clone(); |
48 | | - self.commbus |
49 | | - .register("NAVIGRAPH_SetDownloadOptions", move |args| { |
50 | | - captured_downloader.set_download_options(Dispatcher::trim_null_terminator(args)) |
51 | | - }) |
52 | | - .expect("Failed to register NAVIGRAPH_SetDownloadOptions"); |
53 | | - } |
54 | | - { |
55 | | - let captured_downloader = self.downloader.clone(); |
56 | | - self.commbus |
57 | | - .register("NAVIGRAPH_DeleteAllNavdata", move |_| { |
58 | | - captured_downloader.delete_all_navdata() |
59 | | - }) |
60 | | - .expect("Failed to register NAVIGRAPH_DeleteAllNavdata"); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - fn handle_update(&mut self, data: &sGaugeDrawData) { |
65 | | - // Accumulate delta time for heartbeat |
66 | | - self.delta_time += data.delta_time(); |
67 | | - |
68 | | - // Send heartbeat every 5 seconds |
69 | | - if self.delta_time >= std::time::Duration::from_secs(5) { |
70 | | - CommBus::call("NAVIGRAPH_Heartbeat", "", CommBusBroadcastFlags::All); |
71 | | - self.delta_time = std::time::Duration::from_secs(0); |
72 | | - } |
73 | | - |
74 | | - self.downloader.on_update(); |
75 | | - } |
76 | | - |
77 | | - fn trim_null_terminator(s: &str) -> &str { |
78 | | - s.trim_end_matches(char::from(0)) |
79 | | - } |
80 | | -} |
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use crate::download::downloader::NavdataDownloader; |
| 4 | +use msfs::{commbus::*, sys::sGaugeDrawData, MSFSEvent}; |
| 5 | + |
| 6 | +pub struct Dispatcher<'a> { |
| 7 | + commbus: CommBus<'a>, |
| 8 | + downloader: Rc<NavdataDownloader>, |
| 9 | + delta_time: std::time::Duration, |
| 10 | +} |
| 11 | + |
| 12 | +impl<'a> Dispatcher<'a> { |
| 13 | + pub fn new() -> Self { |
| 14 | + Dispatcher { |
| 15 | + commbus: CommBus::default(), |
| 16 | + downloader: Rc::new(NavdataDownloader::new()), |
| 17 | + delta_time: std::time::Duration::from_secs(0), |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + pub fn on_msfs_event(&mut self, event: MSFSEvent) { |
| 22 | + match event { |
| 23 | + MSFSEvent::PostInitialize => { |
| 24 | + self.handle_initialized(); |
| 25 | + } |
| 26 | + MSFSEvent::PreDraw(data) => { |
| 27 | + self.handle_update(data); |
| 28 | + } |
| 29 | + MSFSEvent::PreKill => { |
| 30 | + self.commbus.unregister_all(); |
| 31 | + } |
| 32 | + |
| 33 | + _ => {} |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + fn handle_initialized(&mut self) { |
| 38 | + { |
| 39 | + let captured_downloader = self.downloader.clone(); |
| 40 | + self.commbus |
| 41 | + .register("NAVIGRAPH_DownloadNavdata", move |args| { |
| 42 | + captured_downloader.download(Dispatcher::trim_null_terminator(args)) |
| 43 | + }) |
| 44 | + .expect("Failed to register NAVIGRAPH_DownloadNavdata"); |
| 45 | + } |
| 46 | + { |
| 47 | + let captured_downloader = self.downloader.clone(); |
| 48 | + self.commbus |
| 49 | + .register("NAVIGRAPH_SetDownloadOptions", move |args| { |
| 50 | + captured_downloader.set_download_options(Dispatcher::trim_null_terminator(args)) |
| 51 | + }) |
| 52 | + .expect("Failed to register NAVIGRAPH_SetDownloadOptions"); |
| 53 | + } |
| 54 | + { |
| 55 | + let captured_downloader = self.downloader.clone(); |
| 56 | + self.commbus |
| 57 | + .register("NAVIGRAPH_DeleteAllNavdata", move |_| { |
| 58 | + captured_downloader.delete_all_navdata() |
| 59 | + }) |
| 60 | + .expect("Failed to register NAVIGRAPH_DeleteAllNavdata"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + fn handle_update(&mut self, data: &sGaugeDrawData) { |
| 65 | + // Accumulate delta time for heartbeat |
| 66 | + self.delta_time += data.delta_time(); |
| 67 | + |
| 68 | + // Send heartbeat every 5 seconds |
| 69 | + if self.delta_time >= std::time::Duration::from_secs(5) { |
| 70 | + CommBus::call("NAVIGRAPH_Heartbeat", "", CommBusBroadcastFlags::All); |
| 71 | + self.delta_time = std::time::Duration::from_secs(0); |
| 72 | + } |
| 73 | + |
| 74 | + self.downloader.on_update(); |
| 75 | + } |
| 76 | + |
| 77 | + fn trim_null_terminator(s: &str) -> &str { |
| 78 | + s.trim_end_matches(char::from(0)) |
| 79 | + } |
| 80 | +} |
0 commit comments