lifx-mqtt-bridge/src/main.rs

89 lines
2.2 KiB
Rust
Raw Permalink 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-02-09 17:32:23 +00:00
#[macro_use]
extern crate log;
extern crate env_logger;
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;
2019-02-09 17:32:23 +00:00
use env_logger::Builder;
use log::info;
use log::LevelFilter;
2019-01-16 21:46:12 +00:00
use std::thread;
2019-01-08 21:05:14 +00:00
pub const MQTT_ID: &str = "lifx-mqtt-bridge";
fn main() {
2019-02-09 17:32:23 +00:00
Builder::from_default_env()
.filter_level(LevelFilter::Info)
.init();
2019-01-20 09:02:44 +00:00
let matches = App::new(MQTT_ID)
2019-01-08 21:05:14 +00:00
.version("0.1")
.about("Lifx Mqtt Bridge")
.author("Buntpfotenkätzchen")
.arg(
Arg::with_name("host")
.short("h")
.long("host")
2019-02-09 17:47:58 +00:00
.env("HOST")
2019-01-08 21:05:14 +00:00
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("port")
.short("p")
.long("port")
2019-02-09 17:47:58 +00:00
.env("PORT")
2019-01-08 21:05:14 +00:00
.takes_value(true)
.default_value("1883"),
)
2019-01-15 20:41:43 +00:00
.arg(
2019-01-20 09:02:44 +00:00
Arg::with_name("lifx-secret")
2019-01-15 20:41:43 +00:00
.short("s")
2019-01-20 09:02:44 +00:00
.long("lifx-secret")
2019-02-09 17:47:58 +00:00
.env("LIFX_SECRET")
2019-01-15 20:41:43 +00:00
.required(true)
.takes_value(true),
)
2019-01-08 21:05:14 +00:00
.get_matches();
2019-01-20 12:02:39 +00:00
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");
2019-02-09 17:32:23 +00:00
info!("Connecting to {}:{}", host, port);
2019-01-08 21:05:14 +00:00
2019-01-16 21:46:12 +00:00
let (s_commands, r_commands) = unbounded();
let (s_updates, r_updates) = unbounded();
2019-01-20 09:02:44 +00:00
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),
};
2019-01-15 20:41:43 +00:00
2019-01-20 09:02:44 +00:00
let mut 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-20 09:02:44 +00:00
lifx_client.listen();
2019-01-08 21:05:14 +00:00
}