51 lines
1.2 KiB
Rust
51 lines
1.2 KiB
Rust
use crate::light::{Command, Light, Status};
|
|
use lifxi::http::prelude::*;
|
|
|
|
pub struct Lifx {
|
|
client: Client,
|
|
updates: crossbeam_channel::Sender<Status>,
|
|
commands: crossbeam_channel::Receiver<Command>,
|
|
}
|
|
|
|
impl Lifx {
|
|
pub fn new<S: ToString>(
|
|
secret: S,
|
|
updates: crossbeam_channel::Sender<Status>,
|
|
commands: crossbeam_channel::Receiver<Command>,
|
|
) -> Self {
|
|
Lifx {
|
|
client: Client::new(secret),
|
|
updates,
|
|
commands,
|
|
}
|
|
}
|
|
|
|
pub fn find_lights(&self) -> Vec<Light> {
|
|
self.client
|
|
.select(Selector::All)
|
|
.list()
|
|
.send()
|
|
.unwrap()
|
|
.json()
|
|
.unwrap()
|
|
}
|
|
|
|
fn set_power(&self, id: String, state: bool) -> Result<(), lifxi::http::Error> {
|
|
self.client
|
|
.select(Selector::Id(id))
|
|
.change_state()
|
|
.power(state)
|
|
.send()
|
|
.and(Ok(()))
|
|
}
|
|
|
|
fn set_brightness(&self, id: String, brightness: f32) -> Result<(), lifxi::http::Error> {
|
|
self.client
|
|
.select(Selector::Id(id))
|
|
.change_state()
|
|
.brightness(brightness)
|
|
.send()
|
|
.and(Ok(()))
|
|
}
|
|
}
|