extern crate serde;
extern crate serde_json;
use reqwest::Client;
use serde_json::value::Value;
use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::fmt;
use std::fs::File;
use std::io::prelude::*;
use std::thread::sleep;
use std::time::Duration;
#[derive(Debug)]
pub struct Bridge {
pub ip: String,
pub key: String,
pub client: Client,
pub base_url: String,
pub light_ids: Vec<u8>,
pub n_lights: u8,
}
impl Bridge {
fn discover() -> Result<(String, String), Box<dyn Error>> {
let ip = env::var("HUE_IP")?;
let key = env::var("HUE_KEY")?;
Ok((ip, key))
}
pub fn register() -> Result<(String, String), Box<dyn Error>> {
let _client = Client::new();
let mut ip = String::new();
let mut name = String::new();
println!("NOTE! Registration will create the `.env` containing IP and KEY info");
println!("Enter the IP of your HUE bridge:");
std::io::stdin().read_line(&mut ip)?;
ip = ip.trim().to_string();
println!("Enter the desired app name:");
std::io::stdin().read_line(&mut name)?;
name = name.trim().to_string();
let body = serde_json::json!({ "devicetype": format!("{}", name) });
let ping_it = || -> Value {
_client
.post(&format!("http://{}/api", ip))
.json(&body)
.send()
.unwrap()
.json()
.unwrap()
};
let mut response = ping_it();
loop {
if response[0]["error"]["type"] == 101 {
println!("Please press the hub button!");
sleep(Duration::from_secs(5));
response = ping_it();
} else {
break;
}
}
let key = &response[0]["success"]["username"];
let mut file = File::create(".env")?;
file.write_all(format!("HUE_IP=\"{}\"\nHUE_KEY={}", ip, key).as_ref())?;
println!(".env File successfully saved!");
Ok((ip, key.to_string().replace("\"", "")))
}
pub fn link() -> Self {
let client = Client::new();
let (ip, key) = match Self::discover() {
Ok((ip, key)) => (ip, key),
_ => {
println!("Unable to find required `HUE_KEY` and `HUE_IP` in environment!");
Self::register().unwrap()
}
};
let base_url = format!("http://{}/api/{}/", ip, key);
let mut bridge = Bridge {
ip,
key,
client,
base_url,
light_ids: Vec::new(),
n_lights: 0,
};
bridge.collect_ids();
bridge.n_lights = bridge.light_ids.len() as u8;
println!("Connected to:\n{}", bridge);
println!("Found {} lights", bridge.n_lights);
bridge
}
pub fn send(
&self,
endpoint: &str,
req_type: RequestType,
params: Option<&serde_json::value::Value>,
) -> Result<reqwest::Response, Box<dyn std::error::Error>> {
let target = format!("{}{}", self.base_url, endpoint);
let response = match req_type {
RequestType::Post => self.client.post(&target).json(¶ms).send()?,
RequestType::Get => self.client.get(&target).send()?,
RequestType::Put => self.client.put(&target).json(¶ms).send()?,
};
Ok(response)
}
pub fn state(&self, light: u8, state: &Value) -> Result<(), Box<dyn std::error::Error>> {
self.send(
&format!("lights/{}/state", light),
RequestType::Put,
Some(state),
)?;
Ok(())
}
pub fn state_all(&self, state: &Value) -> Result<(), Box<dyn std::error::Error>> {
for light in self.light_ids.iter() {
self.state(*light, state)?;
}
Ok(())
}
pub fn collect_ids(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let endpoint = "lights";
let map: HashMap<u8, Value> = self.send(&endpoint, RequestType::Get, None)?.json()?;
let ids: Vec<u8> = map.keys().cloned().map(|integer| integer).collect();
self.light_ids = ids;
Ok(())
}
pub fn light_info(&self) {
println!("Lights available on your bridge");
for id in self.light_ids.iter() {
println!("{}", id)
}
}
}
impl fmt::Display for Bridge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "bridge: {}\nlights: {:?}", self.ip, self.light_ids)
}
}
#[derive(PartialEq)]
pub enum RequestType {
Get,
Post,
Put,
}