Browse Source

start writing

master
Lara 3 years ago
commit
77da7cabd5
  1. 3
      .gitignore
  2. 1893
      Cargo.lock
  3. 10
      Cargo.toml
  4. 9
      src/lamp_lifx.rs
  5. 7
      src/lamp_mqtt.rs
  6. 1
      src/lifx_discover.rs
  7. 54
      src/main.rs
  8. 46
      src/mqtt_connection.rs

3
.gitignore

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

1893
Cargo.lock

File diff suppressed because it is too large

10
Cargo.toml

@ -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

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

7
src/lamp_mqtt.rs

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

1
src/lifx_discover.rs

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

54
src/main.rs

@ -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

@ -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()),
}
}
}
Loading…
Cancel
Save