diff --git a/.gitignore b/.gitignore
index 0b745e2..4566d63 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
/target
-.env
\ No newline at end of file
+.env
+device.env
\ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
index a02980d..4015be8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -31,16 +31,9 @@ dependencies = [
name = "device"
version = "0.1.0"
dependencies = [
- "dotenv",
"paho-mqtt",
]
-[[package]]
-name = "dotenv"
-version = "0.15.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
-
[[package]]
name = "futures"
version = "0.3.15"
diff --git a/Cargo.toml b/Cargo.toml
index 7835f47..41526bc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,4 +8,3 @@ edition = "2018"
[dependencies]
paho-mqtt = "0.9"
-dotenv = "0.15.0"
diff --git a/src/device_var.rs b/src/device_var.rs
new file mode 100644
index 0000000..b9a3451
--- /dev/null
+++ b/src/device_var.rs
@@ -0,0 +1,38 @@
+use std::env;
+use std::fs;
+use std::fs::File;
+use std::io::{self, BufRead};
+use std::path::Path;
+
+pub fn read_env_var() {
+ let filename = "device.env";
+ // println!("In file {}", filename);
+
+ fs::read_to_string(filename)
+ .expect("Something went wrong reading the file : device.env");
+
+
+ if let Ok(lines) = read_lines(filename) {
+ // Consumes the iterator, returns an (Optional) String
+ for line in lines {
+ if let Ok(var) = line {
+ let new_var: Vec<&str> = var.split('=').collect();
+ env::set_var(new_var[0], new_var[1]);
+ // println!("Ma ligne : {:?}", new_var);
+ }
+ }
+ }
+
+ // println!("\n");
+
+ // for (key, value) in env::vars() {
+ // println!("{}: {}", key, value);
+ // }
+ // println!("\n");
+}
+
+fn read_lines
(filename: P) -> io::Result>>
+where P: AsRef, {
+ let file = File::open(filename)?;
+ Ok(io::BufReader::new(file).lines())
+}
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 3f8be38..9bb7cc3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,15 @@
mod mqtt_pub;
mod system_values;
+mod device_var;
+
+use std::env;
extern crate paho_mqtt as mqtt;
-extern crate dotenv;
fn main() {
+ device_var::read_env_var();
+
println!("Resp Temperature : {}", system_values::get_temperature());
let mut cli = mqtt_pub::init_cli();
@@ -14,5 +18,10 @@ fn main() {
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());
+ let battery = env::var("BATTERY").unwrap().to_string();
+ if battery == "true" {
+ cli = mqtt_pub::publish_message(cli, "battery", &system_values::get_battery().to_string());
+ }
+
mqtt_pub::disconnect(cli);
}
diff --git a/src/mqtt_pub.rs b/src/mqtt_pub.rs
index f7d5e94..dbf0d39 100644
--- a/src/mqtt_pub.rs
+++ b/src/mqtt_pub.rs
@@ -3,13 +3,11 @@ use std::{
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()
@@ -44,8 +42,8 @@ pub fn init_cli() -> mqtt::Client {
}
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 topic = get_topic_name(topic);
+ let msg = mqtt::Message::new(get_topic_name(topic), message, QOS);
let tok = cli.publish(msg);
if let Err(e) = tok {
@@ -65,7 +63,6 @@ pub fn disconnect(cli: mqtt::Client) {
}
pub fn get_topic_name(types: &str) -> String {
- dotenv().ok();
let mut topic = env::var("SERVICE").unwrap().to_string();
topic.push('/');
diff --git a/src/system_values.rs b/src/system_values.rs
index be42b49..000bd07 100644
--- a/src/system_values.rs
+++ b/src/system_values.rs
@@ -2,10 +2,8 @@ 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();
@@ -53,7 +51,7 @@ pub fn get_storage() -> i8 {
return storage;
}
-pub fn get_mem() -> f32 {
+pub fn get_mem() -> i8 {
let mem_command = Command::new("free")
.arg("-m")
.output().unwrap_or_else(|e| {
@@ -64,7 +62,25 @@ pub fn get_mem() -> f32 {
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 ;
+ let result = (mem_use / total * 100.0) as i8;
return result;
+}
+
+pub fn get_battery() -> i8 {
+ let system = env::var("SYSTEM").unwrap().to_string();
+ let mut battery = 0;
+
+ if system == "ubuntu" {
+ let battery_command = Command::new("cat")
+ .arg("/sys/class/power_supply/BAT0/capacity")
+ .output().unwrap_or_else(|e| {
+ panic!("failed to execute process: {}", e)
+ });
+
+ let battery_string = &String::from_utf8_lossy(&battery_command.stdout);
+ battery = (&battery_string[..battery_string.len() - 1]).parse::().unwrap();
+ }
+
+ return battery;
}
\ No newline at end of file