From c32454792f40e57e02cb395fc04b654c62e7d61e Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Sat, 4 Apr 2020 03:34:52 +0100 Subject: [PATCH] added handling of request timeouts --- src/app.rs | 3 ++- src/config.rs | 33 +++++++++++++++++++++++---------- src/main.rs | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7eb2f59..1ca2ba3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,6 +11,7 @@ pub enum ActiveApp { Crunchyroll, Funimation, VRV, + Nothing } // enum used to select what kind of TV you are using @@ -40,7 +41,7 @@ pub fn match_to_app(text: String) -> ActiveApp { Some("Pandora") => ActiveApp::Pandora, Some("Spotify") => ActiveApp::Spotify, Some("Plex") => ActiveApp::Plex, - _ => ActiveApp::Roku, + _ => ActiveApp::Nothing, } } diff --git a/src/config.rs b/src/config.rs index 5336fe1..f8cc368 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,11 +44,15 @@ impl Configuration { port = self.port ); - let response = reqwest::get(&request).unwrap(); - let document = Document::from_read(response).unwrap(); - let next = document.find(Name("app")).next().unwrap(); - - next.text().to_string() + let response = get_request(&request); + match response { + Ok(res) => { + let document = Document::from_read(res).unwrap(); + let next = document.find(Name("app")).next().unwrap(); + next.text().to_string() + } + Err(_) => "_".to_string() + } } pub fn get_power_status(&self) -> String { @@ -58,11 +62,15 @@ impl Configuration { port = self.port ); - let response = reqwest::get(&request).unwrap(); - let document = Document::from_read(response).unwrap(); - let next = document.find(Name("power-mode")).next().unwrap(); - - next.text().to_string() + let response = get_request(&request); + match response { + Ok(res) => { + let document = Document::from_read(res).unwrap(); + let next = document.find(Name("power-mode")).next().unwrap(); + next.text().to_string() + } + Err(_) => "_".to_string() + } } } @@ -76,3 +84,8 @@ pub fn init_config() -> Configuration { config } + +pub fn get_request(request: &String) -> Result { + let response = reqwest::get(request); + response +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 785a0c1..51848ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,7 @@ fn main() { } ws.write(brightness(data.iter().cloned(), 32)).unwrap(); }, - _ => println!("Oops!"), + _ => println!("We don't know what app is running right now..."), } } },