lifx-mqtt-bridge/src/lifx.rs

51 lines
1.2 KiB
Rust
Raw Normal View History

2019-01-16 21:46:12 +00:00
use crate::light::{Command, Light, Status};
2019-01-15 20:41:43 +00:00
use lifxi::http::prelude::*;
pub struct Lifx {
client: Client,
2019-01-16 21:46:12 +00:00
updates: crossbeam_channel::Sender<Status>,
commands: crossbeam_channel::Receiver<Command>,
2019-01-15 20:41:43 +00:00
}
impl Lifx {
2019-01-16 21:46:12 +00:00
pub fn new<S: ToString>(
secret: S,
updates: crossbeam_channel::Sender<Status>,
commands: crossbeam_channel::Receiver<Command>,
) -> Self {
2019-01-15 20:41:43 +00:00
Lifx {
client: Client::new(secret),
2019-01-16 21:46:12 +00:00
updates,
commands,
2019-01-15 20:41:43 +00:00
}
}
pub fn find_lights(&self) -> Vec<Light> {
self.client
.select(Selector::All)
.list()
.send()
.unwrap()
.json()
.unwrap()
}
2019-01-16 21:46:12 +00:00
fn set_power(&self, id: String, state: bool) -> Result<(), lifxi::http::Error> {
2019-01-15 20:41:43 +00:00
self.client
.select(Selector::Id(id))
.change_state()
.power(state)
.send()
.and(Ok(()))
}
2019-01-16 21:46:12 +00:00
fn set_brightness(&self, id: String, brightness: f32) -> Result<(), lifxi::http::Error> {
2019-01-15 20:41:43 +00:00
self.client
.select(Selector::Id(id))
.change_state()
.brightness(brightness)
.send()
.and(Ok(()))
}
}