fix env file call

This commit is contained in:
Romulus21
2024-08-24 14:15:14 +02:00
parent 03f888c4a7
commit 244c6f6ca6
7 changed files with 41 additions and 32 deletions

View File

@@ -3,9 +3,9 @@ name = "device"
version = "0.1.0"
authors = ["Romulus21 <romain@delanoe.pro>"]
edition = "2018"
description = "Send metrics to MQTT server"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rumqttc = "0.23.0"

6
Makefile Normal file
View File

@@ -0,0 +1,6 @@
deploy:
ssh raspiwork 'cd /home/pi/Scripts/rust_device && git pull && make build'
build:
cargo build --release
scp target/release/device raspigate::/home/pi/Scripts/device

4
README.md Normal file
View File

@@ -0,0 +1,4 @@
to run, add this line to crontab:
```
*/5 * * * * cd /path/to/folder && ./device
```

View File

@@ -5,14 +5,8 @@ use std::io::{self, BufRead};
use std::path::Path;
pub fn read_env_var() {
let filename = "device.env";
let mut path = env::var("HOME").unwrap().to_string();
path.push_str("/Scripts/");
path.push_str(filename);
println!("{}", path);
fs::read_to_string(path.clone())
.expect("Something went wrong reading the file : device.env");
let path = "device.env";
fs::read_to_string(path).expect("Something went wrong reading the file : device.env");
if let Ok(lines) = read_lines(path) {
for line in lines {
@@ -23,15 +17,19 @@ pub fn read_env_var() {
}
}
// println!("\n");
// for (key, value) in env::vars() {
// println!("{}: {}", key, value);
// }
// println!("\n");
/*
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>, {
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@@ -1,17 +1,20 @@
use std::env;
mod device_var;
mod mqtt_pub;
mod system_values;
mod device_var;
fn main() {
device_var::read_env_var();
let mut count = 3;
let (mut cli, connection) = mqtt_pub::init_cli();
cli = mqtt_pub::publish_message(cli, "temperature", system_values::get_temperature().to_string());
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());
@@ -23,4 +26,3 @@ fn main() {
mqtt_pub::disconnect(connection, count);
}

View File

@@ -1,11 +1,9 @@
use std::{
env,
time::Duration
};
use rumqttc::{Client, Connection, MqttOptions, QoS};
use std::{env, time::Duration};
pub fn init_cli() -> (Client, Connection) {
let mut mqttoptions = MqttOptions::new("test-1", env::var("BROKER_ADDRESS").unwrap().to_string(), 1883);
let mut mqttoptions =
MqttOptions::new("test-1", env::var("MQTT_HOST").unwrap().to_string(), 1883);
mqttoptions.set_keep_alive(Duration::from_secs(1));
let (client, connection) = Client::new(mqttoptions, 10);
@@ -13,7 +11,9 @@ pub fn init_cli() -> (Client, Connection) {
}
pub fn publish_message(mut client: Client, topic: &str, payload: String) -> Client {
client.publish(get_topic_name(topic), QoS::AtLeastOnce, true, payload).unwrap();
client
.publish(get_topic_name(topic), QoS::AtLeastOnce, true, payload)
.unwrap();
client
}
@@ -36,7 +36,6 @@ pub fn disconnect(mut connection: Connection, count: usize) {
}
pub fn get_topic_name(types: &str) -> String {
let mut topic = env::var("SERVICE").unwrap().to_string();
topic.push('/');
let capteur = env::var("CAPTEUR").unwrap().to_string();
@@ -44,5 +43,5 @@ pub fn get_topic_name(types: &str) -> String {
topic.push('/');
topic.push_str(&types);
return topic;
}
topic
}

View File

@@ -25,7 +25,7 @@ pub fn get_temperature() -> f32 {
temperature += temperature_string.parse::<f32>().unwrap();
}
return temperature;
temperature
}
pub fn get_storage() -> i8 {
@@ -41,7 +41,7 @@ pub fn get_storage() -> i8 {
value.pop();
let storage = value.parse::<i8>().unwrap();
return storage;
storage
}
pub fn get_mem() -> i8 {
@@ -56,7 +56,7 @@ pub fn get_mem() -> i8 {
let mem_use = vec_mem[8].parse::<f32>().unwrap();
let result = (mem_use / total * 100.0) as i8;
return result;
result
}
pub fn get_battery() -> i8 {
@@ -75,5 +75,5 @@ pub fn get_battery() -> i8 {
.unwrap();
}
return battery;
battery
}