diff --git a/src/app.rs b/src/app.rs index ab3d754..b76788b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,10 @@ // enum used when a certain TV app is being used -enum ActiveApp { +pub enum ActiveApp { + Roku, Netflix, Hulu, AmazonPrime, + Plex, Pandora, Spotify, Crunchyroll, @@ -11,9 +13,24 @@ enum ActiveApp { } // enum used to select what kind of TV you are using -enum TV { +pub enum TV { Roku, Android, Samsung, Amazon, } + +pub fn match_to_app(text: String) -> ActiveApp { + let mut result = text.split_whitespace(); + + match result.next() { + Some("Roku") => ActiveApp::Roku, + Some("Netflix") => ActiveApp::Netflix, + Some("Hulu") => ActiveApp::Hulu, + Some("Prime") => ActiveApp::AmazonPrime, + Some("Plex") => ActiveApp::Plex, + Some("Pandora") => ActiveApp::Pandora, + Some("Spotify") => ActiveApp::Spotify, + _ => ActiveApp::Roku, + } +} diff --git a/src/config.rs b/src/config.rs index a219f54..a375057 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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() +} diff --git a/src/main.rs b/src/main.rs index 35a50fc..883d1a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,16 @@ // external crates getting imported -extern crate ureq; +extern crate chrono; +extern crate reqwest; +extern crate select; extern crate serde; extern crate serde_derive; extern crate serde_json; -extern crate xml; - -// standard library stuff -use std::fs::File; -use std::io::{BufReader, Read}; +extern crate time; // actual third party library being imported use serde::{Deserialize, Serialize}; use serde_json::Result; -use xml::reader::{EventReader, XmlEvent}; +use time::Duration; // local files that need to be imported mod app; @@ -21,5 +19,23 @@ mod generate; fn main() { let configuration = config::init_config(); - println!("{:?}", configuration.ipaddr); + println!("{:?}", configuration); + + loop { + let text = config::get_tv_status(&configuration); + + let activeapp = app::match_to_app(text); + + match activeapp { + app::ActiveApp::Roku => println!("The lights are light purple!"), + app::ActiveApp::Netflix => println!("The lights are red!"), + app::ActiveApp::Hulu => println!("The lights are green!"), + app::ActiveApp::AmazonPrime => println!("The light are light blue!"), + app::ActiveApp::Spotify => println!("The lights are light green!"), + _ => println!("Oops!"), + } + + time::Duration::seconds(1); + //println!("{:?}", text as str); + } }