added power feature

This commit is contained in:
Wyatt J. Miller 2020-01-03 23:15:13 -05:00
parent 2e6c297a06
commit b4ca32cf0c
3 changed files with 53 additions and 18 deletions

View File

@ -13,6 +13,7 @@ pub enum ActiveApp {
} }
// enum used to select what kind of TV you are using // enum used to select what kind of TV you are using
// not used atm
pub enum TV { pub enum TV {
Roku, Roku,
Android, Android,
@ -20,6 +21,11 @@ pub enum TV {
Amazon, Amazon,
} }
pub enum TVPower {
On,
Off,
}
pub fn match_to_app(text: String) -> ActiveApp { pub fn match_to_app(text: String) -> ActiveApp {
let mut result = text.split_whitespace(); let mut result = text.split_whitespace();
@ -34,3 +40,10 @@ pub fn match_to_app(text: String) -> ActiveApp {
_ => ActiveApp::Roku, _ => ActiveApp::Roku,
} }
} }
pub fn match_to_power_status(text: String) -> TVPower {
match text.trim() {
"PowerOn" => TVPower::On,
_ => TVPower::Off,
}
}

View File

@ -29,6 +29,35 @@ impl Configuration {
pub fn change_active_app(&mut self) { pub fn change_active_app(&mut self) {
// stuff happens here // 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 // grab the config
@ -41,17 +70,3 @@ pub fn init_config() -> Configuration {
config 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()
}

View File

@ -22,9 +22,11 @@ fn main() {
println!("{:?}", configuration); println!("{:?}", configuration);
loop { 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 { match activeapp {
app::ActiveApp::Roku => println!("The lights are light purple!"), app::ActiveApp::Roku => println!("The lights are light purple!"),
@ -35,8 +37,13 @@ fn main() {
_ => println!("Oops!"), _ => 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); thread::sleep(sec);
//println!("{:?}", text as str);
} }
} }