lifx-mqtt-bridge/src/main.rs

73 lines
1.8 KiB
Rust
Raw Normal View History

2019-01-08 21:05:14 +00:00
extern crate clap;
2019-01-15 20:41:43 +00:00
mod lifx;
mod light;
mod mqtt;
2019-01-08 21:05:14 +00:00
2019-01-15 20:41:43 +00:00
use crate::mqtt::Mqtt;
2019-01-08 21:05:14 +00:00
use clap::App;
use clap::Arg;
2019-01-15 20:41:43 +00:00
#[macro_use]
extern crate serde_derive;
2019-01-08 21:05:14 +00:00
pub const MQTT_ID: &str = "lifx-mqtt-bridge";
fn main() {
let matches = App::new("lifx-mqtt-bridge")
.version("0.1")
.about("Lifx Mqtt Bridge")
.author("Buntpfotenkätzchen")
.arg(
Arg::with_name("host")
.short("h")
.long("host")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("port")
.short("p")
.long("port")
.takes_value(true)
.default_value("1883"),
)
2019-01-15 20:41:43 +00:00
.arg(
Arg::with_name("lifx_secret")
.short("s")
.long("lifx_secret")
.required(true)
.takes_value(true),
)
2019-01-08 21:05:14 +00:00
.get_matches();
let host = matches.value_of("host").unwrap();
let port: u16 = matches.value_of("port").unwrap().parse().unwrap();
2019-01-15 20:41:43 +00:00
let lifx_secret = matches.value_of("lifx_secret").unwrap();
2019-01-08 21:05:14 +00:00
println!("Connecting to {}:{}", host, port);
let mut mqtt = match Mqtt::connect(host, port) {
Ok(mqtt) => mqtt,
Err(err) => panic!("Error connecting: {}", err),
};
2019-01-15 20:41:43 +00:00
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);
}
2019-01-08 21:05:14 +00:00
loop {
match mqtt.notifications.recv() {
Ok(notification) => {
println!("MQTT notification received: {:#?}", notification);
}
Err(recv_error) => {
println!("MQTT channel closed: {}", recv_error);
return;
}
}
}
}