lifx-mqtt-bridge/src/main.rs

70 lines
1.8 KiB
Rust
Raw Normal View History

2019-01-08 21:05:14 +00:00
extern crate clap;
2019-01-16 21:46:12 +00:00
#[macro_use]
extern crate lazy_static;
extern crate regex;
#[macro_use]
extern crate serde_derive;
2019-01-08 21:05:14 +00:00
2019-01-15 20:41:43 +00:00
mod lifx;
mod light;
mod mqtt;
2019-01-16 21:46:12 +00:00
mod mqtt_commands;
mod mqtt_updates;
2019-01-08 21:05:14 +00:00
use clap::App;
use clap::Arg;
2019-01-16 21:46:12 +00:00
use crossbeam_channel::unbounded;
use std::thread;
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);
2019-01-16 21:46:12 +00:00
let (s_commands, r_commands) = unbounded();
let (s_updates, r_updates) = unbounded();
let (mqtt_commands, mut mqtt_updates) = match mqtt::mqtt_connect(host, port, s_commands, r_updates)
{
2019-01-08 21:05:14 +00:00
Ok(mqtt) => mqtt,
Err(err) => panic!("Error connecting: {}", err),
};
2019-01-15 20:41:43 +00:00
2019-01-16 21:46:12 +00:00
let lifx_client = lifx::Lifx::new(lifx_secret, s_updates, r_commands);
2019-01-15 20:41:43 +00:00
2019-01-16 21:46:12 +00:00
thread::spawn(move || mqtt_commands.listen());
thread::spawn(move || mqtt_updates.listen());
2019-01-15 20:41:43 +00:00
2019-01-16 21:46:12 +00:00
loop {}
2019-01-08 21:05:14 +00:00
}