finish for ubuntu
This commit is contained in:
62
src/main.rs
62
src/main.rs
@@ -1,64 +1,18 @@
|
||||
mod topic;
|
||||
|
||||
use std::{
|
||||
env,
|
||||
process,
|
||||
time::Duration
|
||||
};
|
||||
use dotenv::dotenv;
|
||||
mod mqtt_pub;
|
||||
mod system_values;
|
||||
|
||||
extern crate paho_mqtt as mqtt;
|
||||
extern crate dotenv;
|
||||
|
||||
// Define the qos.
|
||||
const QOS:i32 = 1;
|
||||
|
||||
fn main() {
|
||||
dotenv().ok();
|
||||
|
||||
let host = env::args().nth(1).unwrap_or_else(||
|
||||
env::var("BROKER_ADDRESS").unwrap().to_string()
|
||||
);
|
||||
println!("Resp Temperature : {}", system_values::get_temperature());
|
||||
|
||||
// Define the set of options for the create.
|
||||
// Use an ID for a persistent session.
|
||||
let create_opts = mqtt::CreateOptionsBuilder::new()
|
||||
.server_uri(host)
|
||||
.client_id(env::var("BROKER_CLIENT").unwrap().to_string())
|
||||
.finalize();
|
||||
let mut cli = mqtt_pub::init_cli();
|
||||
|
||||
// Create a client.
|
||||
let cli = mqtt::Client::new(create_opts).unwrap_or_else(|err| {
|
||||
println!("Error creating the client: {:?}", err);
|
||||
process::exit(1);
|
||||
});
|
||||
cli = mqtt_pub::publish_message(cli, "temperature", &system_values::get_temperature().to_string());
|
||||
cli = mqtt_pub::publish_message(cli, "storage", &system_values::get_storage().to_string());
|
||||
cli = mqtt_pub::publish_message(cli, "mem", &system_values::get_mem().to_string());
|
||||
|
||||
// Define the set of options for the connection.
|
||||
let conn_opts = mqtt::ConnectOptionsBuilder::new()
|
||||
.keep_alive_interval(Duration::from_secs(20))
|
||||
.clean_session(true)
|
||||
.finalize();
|
||||
|
||||
// Connect and wait for it to complete or fail.
|
||||
if let Err(e) = cli.connect(conn_opts) {
|
||||
println!("Unable to connect:\n\t{:?}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
// Create a message and publish it.
|
||||
// Publish message to 'test' and 'hello' topics.
|
||||
let topic = topic::get_topic_name("temperature");
|
||||
let content = "Hello world! ".to_string();
|
||||
let msg = mqtt::Message::new(topic.clone(), content.clone(), QOS);
|
||||
let tok = cli.publish(msg);
|
||||
|
||||
if let Err(e) = tok {
|
||||
println!("Error sending message: {:?}", e);
|
||||
}
|
||||
|
||||
|
||||
// Disconnect from the broker.
|
||||
let tok = cli.disconnect(None);
|
||||
println!("Disconnect from the broker");
|
||||
tok.unwrap();
|
||||
mqtt_pub::disconnect(cli);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user