Skip to content

Commit

Permalink
style: format
Browse files Browse the repository at this point in the history
  • Loading branch information
pepperoni505 committed Apr 26, 2024
1 parent 9e08507 commit 875a250
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 230 deletions.
14 changes: 11 additions & 3 deletions src/wasm/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use msfs::{commbus::*, network::NetworkRequestState, sys::sGaugeDrawData, MSFSEv
use navigation_database::database::Database;

use crate::{
consts, download::downloader::{DownloadStatus, NavigationDataDownloader}, json_structs::{
consts,
download::downloader::{DownloadStatus, NavigationDataDownloader},
json_structs::{
events,
functions::{CallFunction, FunctionResult, FunctionStatus, FunctionType},
params,
}, meta, network_helper::NetworkHelper, util::{self, path_exists}
},
meta,
network_helper::NetworkHelper,
util::{self, path_exists},
};

#[derive(PartialEq, Eq)]
Expand Down Expand Up @@ -405,7 +410,10 @@ impl<'a> Dispatcher<'a> {
}

// Network request tasks
for task in queue.iter().filter(|task| task.borrow().status == TaskStatus::InProgress) {
for task in queue
.iter()
.filter(|task| task.borrow().status == TaskStatus::InProgress)
{
let response_state = match task.borrow().associated_network_request {
Some(ref request) => request.response_state(),
None => continue,
Expand Down
346 changes: 173 additions & 173 deletions src/wasm/src/meta.rs
Original file line number Diff line number Diff line change
@@ -1,173 +1,173 @@
use std::{
cell::RefCell,
path::{Path, PathBuf},
rc::Rc,
};

use msfs::network::NetworkRequestState;

use crate::{
consts,
dispatcher::{Task, TaskStatus},
network_helper::{Method, NetworkHelper},
util::path_exists,
};

#[derive(serde::Serialize, Debug)]
pub enum InstallStatus {
Bundled,
Manual,
None,
}

#[derive(serde::Serialize, Debug)]
pub struct NavigationDataStatus {
pub status: InstallStatus,
#[serde(rename = "installedFormat")]
pub installed_format: Option<String>,
#[serde(rename = "installedRevision")]
pub installed_revision: Option<String>,
#[serde(rename = "installedCycle")]
pub installed_cycle: Option<String>,
#[serde(rename = "validityPeriod")]
pub validity_period: Option<String>,
#[serde(rename = "latestCycle")]
pub latest_cycle: String,
}

#[derive(serde::Deserialize)]
pub struct CurrentCycleResponse {
pub name: String,
pub version: String,
pub configuration: String,
pub cycle: String,
}

#[derive(serde::Deserialize)]
pub struct InstalledNavigationDataCycleInfo {
pub cycle: String,
pub revision: String,
pub name: String,
pub format: String,
#[serde(rename = "validityPeriod")]
pub validity_period: String,
}

pub fn start_network_request(task: Rc<RefCell<Task>>) {
let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None);
let request = match request {
Ok(request) => request,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};
task.borrow_mut().associated_network_request = Some(request);
}

pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) {
let response_bytes = match task.borrow().associated_network_request.as_ref() {
Some(request) => {
if request.response_state() == NetworkRequestState::DataReady {
let response = request.get_response();
match response {
Ok(response) => response,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
}
} else {
return;
}
},
None => {
task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string());
return;
},
};

let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) {
Ok(response_struct) => response_struct,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

// figure out install status
let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION));

let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION));

let status = if found_downloaded {
InstallStatus::Manual
} else if found_bundled {
InstallStatus::Bundled
} else {
InstallStatus::None
};

// Open JSON
let json_path = match status {
InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")),
InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")),
InstallStatus::None => None,
};

let installed_cycle_info = match json_path {
Some(json_path) => {
let json_file = match std::fs::File::open(json_path) {
Ok(json_file) => json_file,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) {
Ok(installed_cycle_info) => installed_cycle_info,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

Some(installed_cycle_info)
},
None => None,
};

let status = NavigationDataStatus {
status,
installed_format: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()),
None => None,
},
installed_revision: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()),
None => None,
},
installed_cycle: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()),
None => None,
},
validity_period: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()),
None => None,
},
latest_cycle: response_struct.cycle,
};

let status_as_value = match serde_json::to_value(&status) {
Ok(status_as_value) => status_as_value,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

println!("Status: {:#?}", status);

task.borrow_mut().status = TaskStatus::Success(Some(status_as_value));
}
use std::{
cell::RefCell,
path::{Path, PathBuf},
rc::Rc,
};

use msfs::network::NetworkRequestState;

use crate::{
consts,
dispatcher::{Task, TaskStatus},
network_helper::{Method, NetworkHelper},
util::path_exists,
};

#[derive(serde::Serialize, Debug)]
pub enum InstallStatus {
Bundled,
Manual,
None,
}

#[derive(serde::Serialize, Debug)]
pub struct NavigationDataStatus {
pub status: InstallStatus,
#[serde(rename = "installedFormat")]
pub installed_format: Option<String>,
#[serde(rename = "installedRevision")]
pub installed_revision: Option<String>,
#[serde(rename = "installedCycle")]
pub installed_cycle: Option<String>,
#[serde(rename = "validityPeriod")]
pub validity_period: Option<String>,
#[serde(rename = "latestCycle")]
pub latest_cycle: String,
}

#[derive(serde::Deserialize)]
pub struct CurrentCycleResponse {
pub name: String,
pub version: String,
pub configuration: String,
pub cycle: String,
}

#[derive(serde::Deserialize)]
pub struct InstalledNavigationDataCycleInfo {
pub cycle: String,
pub revision: String,
pub name: String,
pub format: String,
#[serde(rename = "validityPeriod")]
pub validity_period: String,
}

pub fn start_network_request(task: Rc<RefCell<Task>>) {
let request = NetworkHelper::make_request("https://navdata.api.navigraph.com/info", Method::Get, None, None);
let request = match request {
Ok(request) => request,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};
task.borrow_mut().associated_network_request = Some(request);
}

pub fn get_navigation_data_install_status(task: Rc<RefCell<Task>>) {
let response_bytes = match task.borrow().associated_network_request.as_ref() {
Some(request) => {
if request.response_state() == NetworkRequestState::DataReady {
let response = request.get_response();
match response {
Ok(response) => response,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
}
} else {
return;
}
},
None => {
task.borrow_mut().status = TaskStatus::Failure("No associated network request".to_string());
return;
},
};

let response_struct: CurrentCycleResponse = match serde_json::from_slice(&response_bytes) {
Ok(response_struct) => response_struct,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

// figure out install status
let found_downloaded = path_exists(Path::new(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION));

let found_bundled = path_exists(Path::new(consts::NAVIGATION_DATA_DEFAULT_LOCATION));

let status = if found_downloaded {
InstallStatus::Manual
} else if found_bundled {
InstallStatus::Bundled
} else {
InstallStatus::None
};

// Open JSON
let json_path = match status {
InstallStatus::Manual => Some(PathBuf::from(consts::NAVIGATION_DATA_DOWNLOADED_LOCATION).join("cycle.json")),
InstallStatus::Bundled => Some(PathBuf::from(consts::NAVIGATION_DATA_DEFAULT_LOCATION).join("cycle.json")),
InstallStatus::None => None,
};

let installed_cycle_info = match json_path {
Some(json_path) => {
let json_file = match std::fs::File::open(json_path) {
Ok(json_file) => json_file,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

let installed_cycle_info: InstalledNavigationDataCycleInfo = match serde_json::from_reader(json_file) {
Ok(installed_cycle_info) => installed_cycle_info,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

Some(installed_cycle_info)
},
None => None,
};

let status = NavigationDataStatus {
status,
installed_format: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.format.clone()),
None => None,
},
installed_revision: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.revision.clone()),
None => None,
},
installed_cycle: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.cycle.clone()),
None => None,
},
validity_period: match &installed_cycle_info {
Some(installed_cycle_info) => Some(installed_cycle_info.validity_period.clone()),
None => None,
},
latest_cycle: response_struct.cycle,
};

let status_as_value = match serde_json::to_value(&status) {
Ok(status_as_value) => status_as_value,
Err(e) => {
task.borrow_mut().status = TaskStatus::Failure(e.to_string());
return;
},
};

println!("Status: {:#?}", status);

task.borrow_mut().status = TaskStatus::Success(Some(status_as_value));
}
Loading

0 comments on commit 875a250

Please sign in to comment.