connect to lifx
This commit is contained in:
parent
77da7cabd5
commit
ecd17044ae
10 changed files with 229 additions and 240 deletions
|
@ -1,9 +0,0 @@
|
|||
use lifxi;
|
||||
|
||||
pub struct LampLifx {}
|
||||
|
||||
impl LampLifx {
|
||||
fn new() -> Self {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
pub struct LampMqtt {}
|
||||
|
||||
impl LampMqtt {
|
||||
fn new() -> Self {
|
||||
LampMqtt {}
|
||||
}
|
||||
}
|
42
src/lifx.rs
Normal file
42
src/lifx.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use crate::light::Light;
|
||||
use lifxi::http::prelude::*;
|
||||
|
||||
pub struct Lifx {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl Lifx {
|
||||
pub fn new<S: ToString>(secret: S) -> Self {
|
||||
Lifx {
|
||||
client: Client::new(secret),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_lights(&self) -> Vec<Light> {
|
||||
self.client
|
||||
.select(Selector::All)
|
||||
.list()
|
||||
.send()
|
||||
.unwrap()
|
||||
.json()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub 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(()))
|
||||
}
|
||||
|
||||
pub 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(()))
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
use lifxi;
|
18
src/light.rs
Normal file
18
src/light.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Color {
|
||||
pub hue: f32,
|
||||
pub saturation: f32,
|
||||
pub kelvin: f32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Light {
|
||||
pub id: String,
|
||||
pub label: String,
|
||||
pub connected: bool,
|
||||
pub power: String,
|
||||
pub color: Color,
|
||||
pub brightness: f32,
|
||||
}
|
||||
|
28
src/main.rs
28
src/main.rs
|
@ -1,13 +1,14 @@
|
|||
extern crate clap;
|
||||
|
||||
mod lamp_lifx;
|
||||
mod lamp_mqtt;
|
||||
mod lifx_discover;
|
||||
mod mqtt_connection;
|
||||
mod lifx;
|
||||
mod light;
|
||||
mod mqtt;
|
||||
|
||||
use crate::mqtt_connection::Mqtt;
|
||||
use crate::mqtt::Mqtt;
|
||||
use clap::App;
|
||||
use clap::Arg;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
pub const MQTT_ID: &str = "lifx-mqtt-bridge";
|
||||
|
||||
|
@ -30,16 +31,33 @@ fn main() {
|
|||
.takes_value(true)
|
||||
.default_value("1883"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("lifx_secret")
|
||||
.short("s")
|
||||
.long("lifx_secret")
|
||||
.required(true)
|
||||
.takes_value(true),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let host = matches.value_of("host").unwrap();
|
||||
let port: u16 = matches.value_of("port").unwrap().parse().unwrap();
|
||||
let lifx_secret = matches.value_of("lifx_secret").unwrap();
|
||||
println!("Connecting to {}:{}", host, port);
|
||||
|
||||
let mut mqtt = match Mqtt::connect(host, port) {
|
||||
Ok(mqtt) => mqtt,
|
||||
Err(err) => panic!("Error connecting: {}", err),
|
||||
};
|
||||
|
||||
let lifx_client = lifx::Lifx::new(lifx_secret);
|
||||
let lights = lifx_client.find_lights();
|
||||
println!("lights: {:#?}", lights);
|
||||
|
||||
for light in lights {
|
||||
mqtt.add_light(&light.id, &light.label);
|
||||
}
|
||||
|
||||
loop {
|
||||
match mqtt.notifications.recv() {
|
||||
Ok(notification) => {
|
||||
|
|
|
@ -43,4 +43,28 @@ impl Mqtt {
|
|||
Err(conn_err) => Err(conn_err.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_light(&mut self, id: &str, label: &str) -> Result<(), rumqtt::ClientError> {
|
||||
self.client.publish(
|
||||
format!("{}/lights", crate::MQTT_ID),
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
format!("{}:{}", id, label),
|
||||
)?;
|
||||
self.client.publish(
|
||||
format!("{}/{}/status/connected", crate::MQTT_ID, label),
|
||||
QoS::AtLeastOnce,
|
||||
false,
|
||||
"true",
|
||||
)?;
|
||||
self.client.subscribe(
|
||||
format!("{}/{}/command/power", crate::MQTT_ID, label),
|
||||
QoS::AtLeastOnce,
|
||||
)?;
|
||||
self.client.subscribe(
|
||||
format!("{}/{}/command/brightness", crate::MQTT_ID, label),
|
||||
QoS::AtLeastOnce,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue