diff --git a/Cargo.toml b/Cargo.toml index 1dc8373..9c12ca6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,9 +3,9 @@ name = "device" version = "0.1.0" authors = ["Romulus21 "] 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" - diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2198c4d --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..3a3ce95 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +to run, add this line to crontab: +``` +*/5 * * * * cd /path/to/folder && ./device +``` diff --git a/src/device_var.rs b/src/device_var.rs index ecd34d2..f475cb1 100644 --- a/src/device_var.rs +++ b/src/device_var.rs @@ -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

(filename: P) -> io::Result>> -where P: AsRef, { +where + P: AsRef, +{ let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) } diff --git a/src/main.rs b/src/main.rs index cca3567..84becee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); } - diff --git a/src/mqtt_pub.rs b/src/mqtt_pub.rs index 4bd67e3..78517b2 100644 --- a/src/mqtt_pub.rs +++ b/src/mqtt_pub.rs @@ -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; -} \ No newline at end of file + topic +} diff --git a/src/system_values.rs b/src/system_values.rs index afac5e0..7ae024b 100644 --- a/src/system_values.rs +++ b/src/system_values.rs @@ -25,7 +25,7 @@ pub fn get_temperature() -> f32 { temperature += temperature_string.parse::().unwrap(); } - return temperature; + temperature } pub fn get_storage() -> i8 { @@ -41,7 +41,7 @@ pub fn get_storage() -> i8 { value.pop(); let storage = value.parse::().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::().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 }