|
1 | | -use std::{ |
2 | | - cell::RefCell, |
3 | | - path::{Path, PathBuf}, |
4 | | - rc::Rc, |
5 | | -}; |
6 | | - |
7 | | -use msfs::network::NetworkRequestState; |
8 | | - |
9 | | -use crate::{ |
10 | | - consts, |
11 | | - dispatcher::{Task, TaskStatus}, |
12 | | - network_helper::{Method, NetworkHelper}, |
13 | | - util::path_exists, |
14 | | -}; |
15 | | - |
16 | | -#[derive(serde::Serialize, Debug)] |
17 | | -pub enum InstallStatus { |
18 | | - Bundled, |
19 | | - Manual, |
20 | | - None, |
21 | | -} |
22 | | - |
23 | | -#[derive(serde::Serialize, Debug)] |
24 | | -pub struct NavigationDataStatus { |
25 | | - pub status: InstallStatus, |
26 | | - #[serde(rename = "installedFormat")] |
27 | | - pub installed_format: Option<String>, |
28 | | - #[serde(rename = "installedRevision")] |
29 | | - pub installed_revision: Option<String>, |
30 | | - #[serde(rename = "installedCycle")] |
31 | | - pub installed_cycle: Option<String>, |
32 | | - #[serde(rename = "validityPeriod")] |
33 | | - pub validity_period: Option<String>, |
34 | | - #[serde(rename = "latestCycle")] |
35 | | - pub latest_cycle: String, |
36 | | -} |
37 | | - |
38 | | -#[derive(serde::Deserialize)] |
39 | | -pub struct CurrentCycleResponse { |
40 | | - pub name: String, |
41 | | - pub version: String, |
42 | | - pub configuration: String, |
43 | | - pub cycle: String, |
44 | | -} |
45 | | - |
46 | | -#[derive(serde::Deserialize)] |
47 | | -pub struct InstalledNavigationDataCycleInfo { |
48 | | - pub cycle: String, |
49 | | - pub revision: String, |
50 | | - pub name: String, |
51 | | - pub format: String, |
52 | | - #[serde(rename = "validityPeriod")] |
53 | | - pub validity_period: String, |
54 | | -} |
55 | | - |
56 | | -pub fn start_network_request(task: Rc<RefCell<Task>>) { |
57 | | - let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None); |
58 | | - let request = match request { |
59 | | - Ok(request) => request, |
60 | | - Err(e) => { |
61 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
62 | | - return; |
63 | | - }, |
64 | | - }; |
65 | | - task.borrow_mut().associated_network_request = Some(request); |
66 | | -} |
67 | | - |
68 | | -pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) { |
69 | | - let response_bytes = match task.borrow().associated_network_request.as_ref() { |
70 | | - Some(request) => { |
71 | | - if request.response_state() == NetworkRequestState::DataReady { |
72 | | - let response = request.get_response(); |
73 | | - match response { |
74 | | - Ok(response) => response, |
75 | | - Err(e) => { |
76 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
77 | | - return; |
78 | | - }, |
79 | | - } |
80 | | - } else { |
81 | | - return; |
82 | | - } |
83 | | - }, |
84 | | - None => { |
85 | | - task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string()); |
86 | | - return; |
87 | | - }, |
88 | | - }; |
89 | | - |
90 | | - let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) { |
91 | | - Ok(response_struct) => response_struct, |
92 | | - Err(e) => { |
93 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
94 | | - return; |
95 | | - }, |
96 | | - }; |
97 | | - |
98 | | - // figure out install status |
99 | | - let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION)); |
100 | | - |
101 | | - let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION)); |
102 | | - |
103 | | - let status = if found_downloaded { |
104 | | - InstallStatus::Manual |
105 | | - } else if found_bundled { |
106 | | - InstallStatus::Bundled |
107 | | - } else { |
108 | | - InstallStatus::None |
109 | | - }; |
110 | | - |
111 | | - // Open JSON |
112 | | - let json_path = match status { |
113 | | - InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")), |
114 | | - InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")), |
115 | | - InstallStatus::None => None, |
116 | | - }; |
117 | | - |
118 | | - let installed_cycle_info = match json_path { |
119 | | - Some(json_path) => { |
120 | | - let json_file = match std::fs::File::open(json_path) { |
121 | | - Ok(json_file) => json_file, |
122 | | - Err(e) => { |
123 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
124 | | - return; |
125 | | - }, |
126 | | - }; |
127 | | - |
128 | | - let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) { |
129 | | - Ok(installed_cycle_info) => installed_cycle_info, |
130 | | - Err(e) => { |
131 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
132 | | - return; |
133 | | - }, |
134 | | - }; |
135 | | - |
136 | | - Some(installed_cycle_info) |
137 | | - }, |
138 | | - None => None, |
139 | | - }; |
140 | | - |
141 | | - let status = NavigationDataStatus { |
142 | | - status, |
143 | | - installed_format: match &installed_cycle_info { |
144 | | - Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()), |
145 | | - None => None, |
146 | | - }, |
147 | | - installed_revision: match &installed_cycle_info { |
148 | | - Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()), |
149 | | - None => None, |
150 | | - }, |
151 | | - installed_cycle: match &installed_cycle_info { |
152 | | - Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()), |
153 | | - None => None, |
154 | | - }, |
155 | | - validity_period: match &installed_cycle_info { |
156 | | - Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()), |
157 | | - None => None, |
158 | | - }, |
159 | | - latest_cycle: response_struct.cycle, |
160 | | - }; |
161 | | - |
162 | | - let status_as_value = match serde_json::to_value(&status) { |
163 | | - Ok(status_as_value) => status_as_value, |
164 | | - Err(e) => { |
165 | | - task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
166 | | - return; |
167 | | - }, |
168 | | - }; |
169 | | - |
170 | | - println!("Status: {:#?}", status); |
171 | | - |
172 | | - task.borrow_mut().status = TaskStatus::Success(Some(status_as_value)); |
173 | | -} |
| 1 | +use std::{ |
| 2 | + cell::RefCell, |
| 3 | + path::{Path, PathBuf}, |
| 4 | + rc::Rc, |
| 5 | +}; |
| 6 | + |
| 7 | +use msfs::network::NetworkRequestState; |
| 8 | + |
| 9 | +use crate::{ |
| 10 | + consts, |
| 11 | + dispatcher::{Task, TaskStatus}, |
| 12 | + network_helper::{Method, NetworkHelper}, |
| 13 | + util::path_exists, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(serde::Serialize, Debug)] |
| 17 | +pub enum InstallStatus { |
| 18 | + Bundled, |
| 19 | + Manual, |
| 20 | + None, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(serde::Serialize, Debug)] |
| 24 | +pub struct NavigationDataStatus { |
| 25 | + pub status: InstallStatus, |
| 26 | + #[serde(rename = "installedFormat")] |
| 27 | + pub installed_format: Option<String>, |
| 28 | + #[serde(rename = "installedRevision")] |
| 29 | + pub installed_revision: Option<String>, |
| 30 | + #[serde(rename = "installedCycle")] |
| 31 | + pub installed_cycle: Option<String>, |
| 32 | + #[serde(rename = "validityPeriod")] |
| 33 | + pub validity_period: Option<String>, |
| 34 | + #[serde(rename = "latestCycle")] |
| 35 | + pub latest_cycle: String, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(serde::Deserialize)] |
| 39 | +pub struct CurrentCycleResponse { |
| 40 | + pub name: String, |
| 41 | + pub version: String, |
| 42 | + pub configuration: String, |
| 43 | + pub cycle: String, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(serde::Deserialize)] |
| 47 | +pub struct InstalledNavigationDataCycleInfo { |
| 48 | + pub cycle: String, |
| 49 | + pub revision: String, |
| 50 | + pub name: String, |
| 51 | + pub format: String, |
| 52 | + #[serde(rename = "validityPeriod")] |
| 53 | + pub validity_period: String, |
| 54 | +} |
| 55 | + |
| 56 | +pub fn start_network_request(task: Rc<RefCell<Task>>) { |
| 57 | + let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None); |
| 58 | + let request = match request { |
| 59 | + Ok(request) => request, |
| 60 | + Err(e) => { |
| 61 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 62 | + return; |
| 63 | + }, |
| 64 | + }; |
| 65 | + task.borrow_mut().associated_network_request = Some(request); |
| 66 | +} |
| 67 | + |
| 68 | +pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) { |
| 69 | + let response_bytes = match task.borrow().associated_network_request.as_ref() { |
| 70 | + Some(request) => { |
| 71 | + if request.response_state() == NetworkRequestState::DataReady { |
| 72 | + let response = request.get_response(); |
| 73 | + match response { |
| 74 | + Ok(response) => response, |
| 75 | + Err(e) => { |
| 76 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 77 | + return; |
| 78 | + }, |
| 79 | + } |
| 80 | + } else { |
| 81 | + return; |
| 82 | + } |
| 83 | + }, |
| 84 | + None => { |
| 85 | + task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string()); |
| 86 | + return; |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) { |
| 91 | + Ok(response_struct) => response_struct, |
| 92 | + Err(e) => { |
| 93 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 94 | + return; |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + // figure out install status |
| 99 | + let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION)); |
| 100 | + |
| 101 | + let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION)); |
| 102 | + |
| 103 | + let status = if found_downloaded { |
| 104 | + InstallStatus::Manual |
| 105 | + } else if found_bundled { |
| 106 | + InstallStatus::Bundled |
| 107 | + } else { |
| 108 | + InstallStatus::None |
| 109 | + }; |
| 110 | + |
| 111 | + // Open JSON |
| 112 | + let json_path = match status { |
| 113 | + InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")), |
| 114 | + InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")), |
| 115 | + InstallStatus::None => None, |
| 116 | + }; |
| 117 | + |
| 118 | + let installed_cycle_info = match json_path { |
| 119 | + Some(json_path) => { |
| 120 | + let json_file = match std::fs::File::open(json_path) { |
| 121 | + Ok(json_file) => json_file, |
| 122 | + Err(e) => { |
| 123 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 124 | + return; |
| 125 | + }, |
| 126 | + }; |
| 127 | + |
| 128 | + let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) { |
| 129 | + Ok(installed_cycle_info) => installed_cycle_info, |
| 130 | + Err(e) => { |
| 131 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 132 | + return; |
| 133 | + }, |
| 134 | + }; |
| 135 | + |
| 136 | + Some(installed_cycle_info) |
| 137 | + }, |
| 138 | + None => None, |
| 139 | + }; |
| 140 | + |
| 141 | + let status = NavigationDataStatus { |
| 142 | + status, |
| 143 | + installed_format: match &installed_cycle_info { |
| 144 | + Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()), |
| 145 | + None => None, |
| 146 | + }, |
| 147 | + installed_revision: match &installed_cycle_info { |
| 148 | + Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()), |
| 149 | + None => None, |
| 150 | + }, |
| 151 | + installed_cycle: match &installed_cycle_info { |
| 152 | + Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()), |
| 153 | + None => None, |
| 154 | + }, |
| 155 | + validity_period: match &installed_cycle_info { |
| 156 | + Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()), |
| 157 | + None => None, |
| 158 | + }, |
| 159 | + latest_cycle: response_struct.cycle, |
| 160 | + }; |
| 161 | + |
| 162 | + let status_as_value = match serde_json::to_value(&status) { |
| 163 | + Ok(status_as_value) => status_as_value, |
| 164 | + Err(e) => { |
| 165 | + task.borrow_mut().status = TaskStatus::Failure(e.to_string()); |
| 166 | + return; |
| 167 | + }, |
| 168 | + }; |
| 169 | + |
| 170 | + println!("Status: {:#?}", status); |
| 171 | + |
| 172 | + task.borrow_mut().status = TaskStatus::Success(Some(status_as_value)); |
| 173 | +} |
0 commit comments