From 44ba86a985a271505928177c02c8322210f2e7f9 Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Fri, 4 Jun 2021 23:45:38 +0200 Subject: [PATCH] finish for ubuntu --- src/main.rs | 62 +++++------------------------------ src/mqtt_pub.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++ src/system_values.rs | 70 +++++++++++++++++++++++++++++++++++++++ src/topic.rs | 21 ------------ 4 files changed, 156 insertions(+), 75 deletions(-) create mode 100644 src/mqtt_pub.rs create mode 100644 src/system_values.rs delete mode 100644 src/topic.rs diff --git a/src/main.rs b/src/main.rs index a7304b7..3f8be38 100644 --- a/src/main.rs +++ b/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); } diff --git a/src/mqtt_pub.rs b/src/mqtt_pub.rs new file mode 100644 index 0000000..f7d5e94 --- /dev/null +++ b/src/mqtt_pub.rs @@ -0,0 +1,78 @@ +use std::{ + env, + process, + time::Duration +}; +use dotenv::dotenv; + +// Define the qos. +const QOS:i32 = 1; + +pub fn init_cli() -> mqtt::Client { + dotenv().ok(); + + let host = env::args().nth(1).unwrap_or_else(|| + env::var("BROKER_ADDRESS").unwrap().to_string() + ); + + // 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(); + + // Create a client. + let cli = mqtt::Client::new(create_opts).unwrap_or_else(|err| { + println!("Error creating the client: {:?}", err); + process::exit(1); + }); + + // 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); + } + + return cli; +} + +pub fn publish_message(cli: mqtt::Client, topic: &str, message: &str) -> mqtt::Client { + let topic = get_topic_name(topic); + let msg = mqtt::Message::new(topic.clone(), message.clone(), QOS); + let tok = cli.publish(msg); + + if let Err(e) = tok { + println!("Error sending message: {:?}", e); + } + + println!("{} : {}", topic, message); + + return cli; +} + +pub fn disconnect(cli: mqtt::Client) { + // Disconnect from the broker. + let tok = cli.disconnect(None); + println!("Disconnect from the broker"); + tok.unwrap(); +} + +pub fn get_topic_name(types: &str) -> String { + dotenv().ok(); + + let mut topic = env::var("SERVICE").unwrap().to_string(); + topic.push('/'); + let capteur = env::var("CAPTEUR").unwrap().to_string(); + topic.push_str(&capteur); + topic.push('/'); + topic.push_str(&types); + + return topic; +} \ No newline at end of file diff --git a/src/system_values.rs b/src/system_values.rs new file mode 100644 index 0000000..be42b49 --- /dev/null +++ b/src/system_values.rs @@ -0,0 +1,70 @@ +use std::{ + env, + process::Command, +}; +use dotenv::dotenv; + +pub fn get_temperature() -> f32 { + dotenv().ok(); + + let mut temperature = 0.0; + let system = env::var("SYSTEM").unwrap().to_string(); + + if system == "ubuntu" { + let temperature_command = Command::new("cat") + .arg("/sys/class/thermal/thermal_zone1/temp") + .output().unwrap_or_else(|e| { + panic!("failed to execute process: {}", e) + }); + + let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..5]; + temperature = temperature_string.parse::().unwrap(); + temperature = temperature / 1000.0; + } else { + let temperature_command = Command::new("cat") + .arg("/sys/class/thermal/thermal_zone1/temp") + .output().unwrap_or_else(|e| { + panic!("failed to execute process: {}", e) + }); + + let temperature_string = &String::from_utf8_lossy(&temperature_command.stdout)[..9]; + println!("Raspi temp {:?}", temperature_string); + + temperature += 1.0; + } + + println!("Temperature {}", temperature); + + return temperature; +} + +pub fn get_storage() -> i8 { + let storage_command = Command::new("df") + .arg("-h") + .arg("/") + .output().unwrap_or_else(|e| { + panic!("failed to execute process: {}", e) + }); + + let storage_string = &String::from_utf8_lossy(&storage_command.stdout); + let vec_storage : Vec<&str> = storage_string.split_whitespace().collect(); + let storage = (vec_storage[vec_storage.len() - 2][..2]).parse::().unwrap(); + + return storage; +} + +pub fn get_mem() -> f32 { + let mem_command = Command::new("free") + .arg("-m") + .output().unwrap_or_else(|e| { + panic!("failed to execute process: {}", e) + }); + + let mem_string = &String::from_utf8_lossy(&mem_command.stdout); + let vec_mem : Vec<&str> = mem_string.split_whitespace().collect(); + let total = vec_mem[7].parse::().unwrap(); + let mem_use = vec_mem[8].parse::().unwrap(); + let result = mem_use / total * 100.0 ; + + return result; +} \ No newline at end of file diff --git a/src/topic.rs b/src/topic.rs deleted file mode 100644 index 9ccbb1f..0000000 --- a/src/topic.rs +++ /dev/null @@ -1,21 +0,0 @@ -use std::{ - env, -}; -use dotenv::dotenv; - -pub fn get_topic_name(types: &str) -> String { - dotenv().ok(); - println!("In Topic"); - - let mut topic = env::var("SERVICE").unwrap().to_string(); - topic.push('/'); - let capteur = env::var("CAPTEUR").unwrap().to_string(); - topic.push_str(&capteur); - topic.push('/'); - topic.push_str(&types); - - println!("Topic in func : {}", topic); - - return topic; - -} \ No newline at end of file