From b4ca32cf0ce5520c18a7ad70da1d5943d7d37433 Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Fri, 3 Jan 2020 23:15:13 -0500 Subject: [PATCH] added power feature --- src/app.rs | 13 +++++++++++++ src/config.rs | 43 +++++++++++++++++++++++++++++-------------- src/main.rs | 15 +++++++++++---- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/app.rs b/src/app.rs index b76788b..5c6aa3f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,6 +13,7 @@ pub enum ActiveApp { } // enum used to select what kind of TV you are using +// not used atm pub enum TV { Roku, Android, @@ -20,6 +21,11 @@ pub enum TV { Amazon, } +pub enum TVPower { + On, + Off, +} + pub fn match_to_app(text: String) -> ActiveApp { let mut result = text.split_whitespace(); @@ -34,3 +40,10 @@ pub fn match_to_app(text: String) -> ActiveApp { _ => ActiveApp::Roku, } } + +pub fn match_to_power_status(text: String) -> TVPower { + match text.trim() { + "PowerOn" => TVPower::On, + _ => TVPower::Off, + } +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index a375057..13d25cb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,6 +29,35 @@ impl Configuration { pub fn change_active_app(&mut self) { // stuff happens here } + + pub fn get_tv_status(&self) -> String { + let request = format!( + "http://{ipaddr}:{port}/query/active-app", + ipaddr = self.ipaddr, + 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() + } + + pub fn get_power_status(&self) -> String { + let request = format!( + "http://{ipaddr}:{port}/query/device-info", + ipaddr = self.ipaddr, + 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() + } + } // grab the config @@ -41,17 +70,3 @@ pub fn init_config() -> Configuration { config } - -pub fn get_tv_status(configuration: &Configuration) -> String { - let request = format!( - "http://{ipaddr}:{port}/query/active-app", - ipaddr = configuration.ipaddr, - port = configuration.port - ); - let response = reqwest::get(&request).unwrap(); - //println!("{}", response.status()); - let document = Document::from_read(response).unwrap(); - let next = document.find(Name("app")).next().unwrap(); - - next.text().to_string() -} diff --git a/src/main.rs b/src/main.rs index 6bd67bf..0c0b490 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,9 +22,11 @@ fn main() { println!("{:?}", configuration); loop { - let text = config::get_tv_status(&configuration); + let app_text = configuration.get_tv_status(); + let power_text = configuration.get_power_status(); - let activeapp = app::match_to_app(text); + let activeapp = app::match_to_app(app_text); + let tvpower = app::match_to_power_status(power_text); match activeapp { app::ActiveApp::Roku => println!("The lights are light purple!"), @@ -35,8 +37,13 @@ fn main() { _ => println!("Oops!"), } - let sec = time::Duration::from_secs(1); + match tvpower { + app::TVPower::On => println!("TV is on!"), + app::TVPower::Off => println!("TV is off!"), + _ => println!("We don't know what the power status of the TV is..."), + } + + let sec = time::Duration::from_secs(3); thread::sleep(sec); - //println!("{:?}", text as str); } }