1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use crate::lightstructs::*;
use dotenv;
use reqwest::Client;
use serde_json::value::Value;
use std::collections::BTreeMap;
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;
type Lights = BTreeMap<u8, Light>;
#[derive(Debug)]
pub struct Bridge {
ip: String,
key: String,
client: Client,
base_url: String,
pub light_ids: Vec<u8>,
pub n_lights: u8,
pub lights: Option<Lights>,
}
impl Bridge {
fn discover(filename: &str) -> Result<(String, String), Box<dyn Error>> {
dotenv::from_filename(filename)?;
let ip = env::var("HUE_IP")?;
let key = env::var("HUE_KEY")?;
Ok((ip, key))
}
pub fn register(configpath: &str) -> 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 `~/.huemanity` 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(configpath)?;
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 mut filename = dirs::home_dir().unwrap();
filename.push(".huemanity");
let path = filename.to_str().unwrap();
let client = Client::new();
let (ip, key) = match Self::discover(path) {
Ok(tupl) => tupl,
_ => {
println!("Unable to find required `HUE_KEY` and `HUE_IP` in environment!");
let result = match Self::register(path) {
Ok(tupl) => {
println!("Registration successful");
tupl
}
Err(e) => panic!("Could not register due to: {}", e),
};
result
}
};
let base_url = format!("http://{}/api/{}/", ip, key);
let mut bridge = Bridge {
ip,
key,
client,
base_url,
light_ids: Vec::new(),
n_lights: 0,
lights: None,
};
match bridge.collect_lights() {
Ok(_) => println!("Collected lights sucessfully!"),
Err(e) => println!("Could not collect lights: {}", e),
}
println!("Connected to:\n{}", bridge);
println!("Found {} lights", bridge.n_lights);
bridge
}
fn send(
&self,
endpoint: &str,
req_type: RequestType,
params: Option<&SendableState>,
) -> 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: &SendableState,
) -> Result<(), Box<dyn std::error::Error>> {
self.send(
&format!("lights/{}/state", light),
RequestType::Put,
Some(state),
)?;
Ok(())
}
pub fn state_all(&self, state: &SendableState) -> Result<(), Box<dyn std::error::Error>> {
for light in self.light_ids.iter() {
self.state(*light, state)?;
}
Ok(())
}
pub fn collect_lights(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let lights: Lights = self.send("lights", RequestType::Get, None)?.json()?;
self.light_ids = lights.keys().cloned().map(|integer| integer).collect();
self.lights = Some(lights);
self.n_lights = self.light_ids.len() as u8;
Ok(())
}
pub fn light_info(&self) {
println!("Lights available on your bridge:");
let lights = self.lights.as_ref().unwrap();
for (id, light) in lights.iter() {
println!("{}:\n{:?}", id, light);
}
}
}
impl fmt::Display for Bridge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "bridge: {}\nlights: {:?}", self.ip, self.light_ids)
}
}