2019-12-28 21:43:13 -06:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
|
|
|
|
2020-04-03 17:39:59 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2019-12-28 21:43:13 -06:00
|
|
|
use std::path::Path;
|
|
|
|
|
2019-12-29 19:58:49 -06:00
|
|
|
use select::document::Document;
|
|
|
|
use select::predicate::Name;
|
2020-01-24 16:08:10 -06:00
|
|
|
use serde::{Serialize, Deserialize};
|
2019-12-28 21:43:13 -06:00
|
|
|
|
|
|
|
// config structure
|
2020-01-24 16:08:10 -06:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2019-12-28 21:43:13 -06:00
|
|
|
pub struct Configuration {
|
|
|
|
pub ipaddr: String,
|
|
|
|
pub port: i32,
|
|
|
|
pub tv: String,
|
|
|
|
pub active_app: String,
|
2020-01-24 16:08:10 -06:00
|
|
|
pub power_status: String,
|
2019-12-28 21:43:13 -06:00
|
|
|
}
|
|
|
|
|
2019-12-29 19:58:49 -06:00
|
|
|
impl Configuration {
|
|
|
|
pub fn is_change_app(&self) -> bool {
|
2020-01-24 16:08:10 -06:00
|
|
|
self.active_app == self.get_app_status()
|
2019-12-29 19:58:49 -06:00
|
|
|
}
|
|
|
|
|
2020-01-24 16:08:10 -06:00
|
|
|
pub fn is_change_power(&self) -> bool {
|
|
|
|
self.power_status == self.get_power_status()
|
2019-12-29 19:58:49 -06:00
|
|
|
}
|
2020-01-03 22:15:13 -06:00
|
|
|
|
2020-01-24 16:08:10 -06:00
|
|
|
pub fn change_active_app(&mut self, app_text: &String) {
|
|
|
|
self.active_app = app_text.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_power(&mut self, power_text: &String) {
|
|
|
|
self.power_status = power_text.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_app_status(&self) -> String {
|
2020-01-03 22:15:13 -06:00
|
|
|
let request = format!(
|
|
|
|
"http://{ipaddr}:{port}/query/active-app",
|
|
|
|
ipaddr = self.ipaddr,
|
|
|
|
port = self.port
|
|
|
|
);
|
|
|
|
|
2020-04-03 21:34:52 -05:00
|
|
|
let response = get_request(&request);
|
|
|
|
match response {
|
|
|
|
Ok(res) => {
|
|
|
|
let document = Document::from_read(res).unwrap();
|
|
|
|
let next = document.find(Name("app")).next().unwrap();
|
|
|
|
next.text().to_string()
|
|
|
|
}
|
|
|
|
Err(_) => "_".to_string()
|
|
|
|
}
|
2020-01-03 22:15:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_power_status(&self) -> String {
|
|
|
|
let request = format!(
|
|
|
|
"http://{ipaddr}:{port}/query/device-info",
|
|
|
|
ipaddr = self.ipaddr,
|
|
|
|
port = self.port
|
|
|
|
);
|
2020-01-04 14:16:54 -06:00
|
|
|
|
2020-04-03 21:34:52 -05:00
|
|
|
let response = get_request(&request);
|
|
|
|
match response {
|
|
|
|
Ok(res) => {
|
|
|
|
let document = Document::from_read(res).unwrap();
|
|
|
|
let next = document.find(Name("power-mode")).next().unwrap();
|
|
|
|
next.text().to_string()
|
|
|
|
}
|
|
|
|
Err(_) => "_".to_string()
|
|
|
|
}
|
2020-01-03 22:15:13 -06:00
|
|
|
}
|
2019-12-29 19:58:49 -06:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:43:13 -06:00
|
|
|
// 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();
|
2019-12-29 19:58:49 -06:00
|
|
|
file.read_to_string(&mut data).expect("Couldn't read file!");
|
2019-12-28 21:43:13 -06:00
|
|
|
|
|
|
|
let config: Configuration = serde_json::from_str(&data).expect("Couldn't parse JSON!");
|
|
|
|
|
|
|
|
config
|
|
|
|
}
|
2020-04-03 21:34:52 -05:00
|
|
|
|
|
|
|
pub fn get_request(request: &String) -> Result<reqwest::Response, reqwest::Error> {
|
|
|
|
let response = reqwest::get(request);
|
|
|
|
response
|
|
|
|
}
|