Skip to content

Commit

Permalink
Merge branch 'master' into bluez-generated
Browse files Browse the repository at this point in the history
Cargo.lock changes may cause dbus to fail to build.
  • Loading branch information
alsuren committed Sep 16, 2020
2 parents d71d16e + 7ec3cb8 commit 2a75d1d
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 925 deletions.
84 changes: 74 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion homie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ futures = "0.3.5"
local_ipaddress = "0.1.3"
log = "0.4.11"
mac_address = "1.0.3"
rumqttc = "0.0.6"
rumqttc = { git = "https://github.com/bytebeamio/rumqtt" }
tokio = "0.2.22"

[dev-dependencies]
pretty_env_logger = "0.4.0"
rand = "0.7"
14 changes: 9 additions & 5 deletions homie/examples/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::time::Duration;
use tokio::task::{self, JoinHandle};
use tokio::{time, try_join};

#[tokio::main(core_threads = 2)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883);
mqttoptions.set_keep_alive(5);
pretty_env_logger::init();

let (mut homie, mqtt_handle) =
let mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883);

let (mut homie, homie_handle) =
HomieDevice::builder("homie/example", "Homie example", mqttoptions)
.spawn()
.await?;
Expand Down Expand Up @@ -39,12 +40,15 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
homie.ready().await?;
println!("ready");

time::delay_for(Duration::from_secs(5)).await;
homie.disconnect().await?;
println!("disconnected");
Ok(())
});

// Poll everything to completion, until the first one bombs out.
let res: Result<_, Box<dyn Error + Send + Sync>> = try_join! {
mqtt_handle,
homie_handle,
handle.map(|res| Ok(res??)),
};
res?;
Expand Down
34 changes: 34 additions & 0 deletions homie/examples/light.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use homie::{Datatype, HomieDevice, Node, Property};
use rumqttc::MqttOptions;
use std::error::Error;

#[tokio::main(core_threads = 2)]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
pretty_env_logger::init();

let mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883);

let mut builder = HomieDevice::builder("homie/example", "Homie light example", mqttoptions);
builder.set_update_callback(|node_id, property_id, value| async move {
println!("{}/{} is now {}", node_id, property_id, value);
Some(value)
});
let (mut homie, homie_handle) = builder.spawn().await?;

let node = Node::new(
"light",
"Light",
"light",
vec![
Property::new("power", "On", Datatype::Boolean, true, None, None),
Property::new("colour", "Colour", Datatype::Color, true, None, Some("rgb")),
],
);
homie.add_node(node).await?;

homie.ready().await?;
println!("Ready");

// This will only resolve (with an error) if we lose connection to the MQTT server.
homie_handle.await
}
71 changes: 71 additions & 0 deletions homie/examples/sensor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use futures::FutureExt;
use homie::{Datatype, HomieDevice, Node, Property};
use rand::random;
use rumqttc::MqttOptions;
use std::error::Error;
use std::time::Duration;
use tokio::task::{self, JoinHandle};
use tokio::{time, try_join};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
pretty_env_logger::init();

let mqttoptions = MqttOptions::new("homie_example", "test.mosquitto.org", 1883);

let (mut homie, homie_handle) =
HomieDevice::builder("homie/example", "Homie sensor example", mqttoptions)
.spawn()
.await?;

homie
.add_node(Node::new(
"sensor",
"Sensor",
"Environment sensor",
vec![
Property::new(
"temperature",
"Temperature",
Datatype::Float,
false,
Some("ºC"),
None,
),
Property::new(
"humidity",
"Humidity",
Datatype::Integer,
false,
Some("%"),
None,
),
],
))
.await?;

let handle: JoinHandle<Result<(), Box<dyn Error + Send + Sync>>> = task::spawn(async move {
homie.ready().await?;
println!("Ready");

loop {
let temperature: f32 = random::<f32>() * 40.0;
let humidity: u8 = (random::<f32>() * 100.0) as u8;
println!("Update: {}ºC {}%", temperature, humidity);
homie
.publish_value("sensor", "temperature", temperature)
.await?;
homie.publish_value("sensor", "humidity", humidity).await?;

time::delay_for(Duration::from_secs(10)).await;
}
});

// Poll everything to completion, until the first one bombs out.
let res: Result<_, Box<dyn Error + Send + Sync>> = try_join! {
homie_handle,
handle.map(|res| Ok(res??)),
};
res?;
Ok(())
}
Loading

0 comments on commit 2a75d1d

Please sign in to comment.