web request changes light color

don't have the led light strip in just yet. however, whenever a
web request is sent to a IP address of a TV, the fictional led
light strip will change color

I also got to flex my reference borrowing skills, which is always
a fun time
This commit is contained in:
Wyatt J. Miller
2019-12-29 20:58:49 -05:00
parent fb02ec9d99
commit 591c554c3e
3 changed files with 71 additions and 11 deletions

View File

@ -6,6 +6,9 @@ use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use reqwest::Client;
use select::document::Document;
use select::predicate::Name;
use serde::Deserialize;
use serde_json::Result;
@ -18,13 +21,37 @@ pub struct Configuration {
pub active_app: String,
}
impl Configuration {
pub fn is_change_app(&self) -> bool {
true
}
pub fn change_active_app(&mut self) {
// stuff happens here
}
}
// grab the config
pub fn init_config() -> Configuration {
let mut file = File::open(&Path::new("config.json")).expect("Couldn't grab JSON!");
let mut data = String::new();
file.read_to_string(&mut data).expect("Poop");
file.read_to_string(&mut data).expect("Couldn't read file!");
let config: Configuration = serde_json::from_str(&data).expect("Couldn't parse JSON!");
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()
}