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

@ -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,
}
}

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()
}

View File

@ -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);
}
}