start writing

This commit is contained in:
Lara 2019-01-08 22:05:14 +01:00
commit 77da7cabd5
8 changed files with 2023 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
**/*.rs.bk
/.vscode

1893
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "lifx-mqtt-bridge"
version = "0.1.0"
authors = ["Lara <git@lara.uber.space>"]
edition = "2018"
[dependencies]
lifxi = "0.1"
rumqtt = { git = "https://github.com/AtherEnergy/rumqtt" }
clap = "2.32"

9
src/lamp_lifx.rs Normal file
View File

@ -0,0 +1,9 @@
use lifxi;
pub struct LampLifx {}
impl LampLifx {
fn new() -> Self {
unimplemented!()
}
}

7
src/lamp_mqtt.rs Normal file
View File

@ -0,0 +1,7 @@
pub struct LampMqtt {}
impl LampMqtt {
fn new() -> Self {
LampMqtt {}
}
}

1
src/lifx_discover.rs Normal file
View File

@ -0,0 +1 @@
use lifxi;

54
src/main.rs Normal file
View File

@ -0,0 +1,54 @@
extern crate clap;
mod lamp_lifx;
mod lamp_mqtt;
mod lifx_discover;
mod mqtt_connection;
use crate::mqtt_connection::Mqtt;
use clap::App;
use clap::Arg;
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"),
)
.get_matches();
let host = matches.value_of("host").unwrap();
let port: u16 = matches.value_of("port").unwrap().parse().unwrap();
println!("Connecting to {}:{}", host, port);
let mut mqtt = match Mqtt::connect(host, port) {
Ok(mqtt) => mqtt,
Err(err) => panic!("Error connecting: {}", err),
};
loop {
match mqtt.notifications.recv() {
Ok(notification) => {
println!("MQTT notification received: {:#?}", notification);
}
Err(recv_error) => {
println!("MQTT channel closed: {}", recv_error);
return;
}
}
}
}

46
src/mqtt_connection.rs Normal file
View File

@ -0,0 +1,46 @@
use rumqtt;
use rumqtt::LastWill;
use rumqtt::MqttClient;
use rumqtt::Notification;
use rumqtt::Receiver;
use rumqtt::{ConnectionMethod, MqttOptions, QoS, ReconnectOptions};
pub struct Mqtt {
pub client: MqttClient,
pub notifications: Receiver<Notification>,
}
impl Mqtt {
pub fn connect(host: &str, port: u16) -> Result<Self, String> {
let last_will = LastWill {
topic: format!("{}/status", crate::MQTT_ID),
message: "disconnected".to_string(),
qos: QoS::AtLeastOnce,
retain: true,
};
let options = MqttOptions::new(crate::MQTT_ID, host, port);
let options = options
.set_connection_method(ConnectionMethod::Tcp)
.set_keep_alive(10)
.set_last_will(last_will)
.set_reconnect_opts(ReconnectOptions::Always(20));
match MqttClient::start(options) {
Ok((mut client, notifications)) => {
match client.publish(
format!("{}/status", crate::MQTT_ID),
QoS::AtLeastOnce,
true,
"connected",
) {
Ok(()) => Ok(Mqtt {
client,
notifications,
}),
Err(conn_err) => Err(conn_err.to_string()),
}
}
Err(conn_err) => Err(conn_err.to_string()),
}
}
}