add battery level & load setting by device.env
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
/target
|
||||
.env
|
||||
.env
|
||||
device.env
|
||||
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -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"
|
||||
|
||||
@@ -8,4 +8,3 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
paho-mqtt = "0.9"
|
||||
dotenv = "0.15.0"
|
||||
|
||||
38
src/device_var.rs
Normal file
38
src/device_var.rs
Normal file
@@ -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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
11
src/main.rs
11
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);
|
||||
}
|
||||
|
||||
@@ -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('/');
|
||||
|
||||
@@ -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::<f32>().unwrap();
|
||||
let mem_use = vec_mem[8].parse::<f32>().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::<i8>().unwrap();
|
||||
}
|
||||
|
||||
return battery;
|
||||
}
|
||||
Reference in New Issue
Block a user