You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

extern crate clap;
#[macro_use]
extern crate lazy_static;
extern crate regex;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
extern crate env_logger;
mod lifx;
mod light;
mod mqtt;
mod mqtt_commands;
mod mqtt_updates;
use clap::App;
use clap::Arg;
use crossbeam_channel::unbounded;
use env_logger::Builder;
use log::info;
use log::LevelFilter;
use std::thread;
pub const MQTT_ID: &str = "lifx-mqtt-bridge";
fn main() {
Builder::from_default_env()
.filter_level(LevelFilter::Info)
.init();
let matches = App::new(MQTT_ID)
.version("0.1")
.about("Lifx Mqtt Bridge")
.author("Buntpfotenkätzchen")
.arg(
Arg::with_name("host")
.short("h")
.long("host")
.env("HOST")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("port")
.short("p")
.long("port")
.env("PORT")
.takes_value(true)
.default_value("1883"),
)
.arg(
Arg::with_name("lifx-secret")
.short("s")
.long("lifx-secret")
.env("LIFX_SECRET")
.required(true)
.takes_value(true),
)
.get_matches();
let host = matches.value_of("host").expect("Invalid host");
let port: u16 = matches
.value_of("port")
.expect("Invalid port")
.parse()
.expect("Invalid port");
let lifx_secret = matches
.value_of("lifx-secret")
.expect("Invalid lifx-secret");
info!("Connecting to {}:{}", host, port);
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) {
Ok(mqtt) => mqtt,
Err(err) => panic!("Error connecting: {}", err),
};
let mut lifx_client = lifx::Lifx::new(lifx_secret, s_updates, r_commands);
thread::spawn(move || mqtt_commands.listen());
thread::spawn(move || mqtt_updates.listen());
lifx_client.listen();
}