From 9b0709b278ef39ab9049fb2fea08bd2d072f72b9 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Sun, 7 Oct 2018 08:33:15 -0400 Subject: [PATCH 1/5] try a loop in led status helper --- led_status_helper/src/main.rs | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/led_status_helper/src/main.rs b/led_status_helper/src/main.rs index d30ddbc4..f488c8cb 100644 --- a/led_status_helper/src/main.rs +++ b/led_status_helper/src/main.rs @@ -141,13 +141,7 @@ fn main() { }; let redis_conn = redis_client.get_connection().unwrap(); - let status = generate_status( - &redis_conn, - &config.temp_unit.unwrap_or('F'), - &config.redis_namespace.unwrap_or("".to_string()), - ); - - let mut mq_request_handler = { + let mut mq_cli = { // Specify client connection options let opts: MqttOptions = MqttOptions::new() .set_keep_alive(5) @@ -163,15 +157,21 @@ fn main() { MqttClient::start(opts, None).expect("MQTT client couldn't start") }; - mq_request_handler - .publish( - &config.mqtt_topic, - QoS::Level0, - status.unwrap().clone().into_bytes(), - ) - .unwrap(); - - // let the other thread do its thing before we exit - std::thread::sleep(std::time::Duration::from_millis(1000)); - let _ = mq_request_handler.disconnect(); + loop { + let status = generate_status( + &redis_conn, + &config.temp_unit.unwrap_or('F'), + &config.redis_namespace.clone().unwrap_or("".to_string()), + ); + mq_cli + .publish( + &config.mqtt_topic, + QoS::Level0, + status.unwrap().clone().into_bytes(), + ) + .unwrap(); + + // let the other thread do its thing before we exit + std::thread::sleep(std::time::Duration::from_secs(13)); + } } From 18015e9a01a821f86731528acfbd2b644cf85728 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Sun, 7 Oct 2018 08:37:53 -0400 Subject: [PATCH 2/5] try looping --- led_status_helper/src/main.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/led_status_helper/src/main.rs b/led_status_helper/src/main.rs index f488c8cb..822a91c1 100644 --- a/led_status_helper/src/main.rs +++ b/led_status_helper/src/main.rs @@ -157,21 +157,22 @@ fn main() { MqttClient::start(opts, None).expect("MQTT client couldn't start") }; + let mut last_publish = std::time::SystemTime::now(); loop { - let status = generate_status( - &redis_conn, - &config.temp_unit.unwrap_or('F'), - &config.redis_namespace.clone().unwrap_or("".to_string()), - ); - mq_cli - .publish( - &config.mqtt_topic, - QoS::Level0, - status.unwrap().clone().into_bytes(), - ) - .unwrap(); - - // let the other thread do its thing before we exit - std::thread::sleep(std::time::Duration::from_secs(13)); + if last_publish.elapsed().unwrap().as_secs() > 10 { + let status = generate_status( + &redis_conn, + &config.temp_unit.unwrap_or('F'), + &config.redis_namespace.clone().unwrap_or("".to_string()), + ); + mq_cli + .publish( + &config.mqtt_topic, + QoS::Level0, + status.unwrap().clone().into_bytes(), + ) + .unwrap(); + last_publish = std::time::SystemTime::now(); + } } } From 67cb521880f1e0edf78574e1b7a792e2829ae3c9 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Sun, 7 Oct 2018 12:40:47 +0000 Subject: [PATCH 3/5] sleepy --- led_status_helper/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/led_status_helper/src/main.rs b/led_status_helper/src/main.rs index 822a91c1..b773d74f 100644 --- a/led_status_helper/src/main.rs +++ b/led_status_helper/src/main.rs @@ -157,9 +157,7 @@ fn main() { MqttClient::start(opts, None).expect("MQTT client couldn't start") }; - let mut last_publish = std::time::SystemTime::now(); loop { - if last_publish.elapsed().unwrap().as_secs() > 10 { let status = generate_status( &redis_conn, &config.temp_unit.unwrap_or('F'), @@ -172,7 +170,6 @@ fn main() { status.unwrap().clone().into_bytes(), ) .unwrap(); - last_publish = std::time::SystemTime::now(); - } + std::thread::sleep(std::time::Duration::from_millis(13000)); } } From 2ba6fca69cda06e3073e3219ffeb16e6f4e5d49f Mon Sep 17 00:00:00 2001 From: Terkwood Date: Sun, 7 Oct 2018 08:41:12 -0400 Subject: [PATCH 4/5] fmt --- led_status_helper/src/main.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/led_status_helper/src/main.rs b/led_status_helper/src/main.rs index b773d74f..ed49e670 100644 --- a/led_status_helper/src/main.rs +++ b/led_status_helper/src/main.rs @@ -158,18 +158,18 @@ fn main() { }; loop { - let status = generate_status( - &redis_conn, - &config.temp_unit.unwrap_or('F'), - &config.redis_namespace.clone().unwrap_or("".to_string()), - ); - mq_cli - .publish( - &config.mqtt_topic, - QoS::Level0, - status.unwrap().clone().into_bytes(), - ) - .unwrap(); - std::thread::sleep(std::time::Duration::from_millis(13000)); + let status = generate_status( + &redis_conn, + &config.temp_unit.unwrap_or('F'), + &config.redis_namespace.clone().unwrap_or("".to_string()), + ); + mq_cli + .publish( + &config.mqtt_topic, + QoS::Level0, + status.unwrap().clone().into_bytes(), + ) + .unwrap(); + std::thread::sleep(std::time::Duration::from_millis(13000)); } } From 851602a46b3964b23a441ef57dc9a2f3e09f5a31 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Sun, 7 Oct 2018 09:50:47 -0400 Subject: [PATCH 5/5] configurable wait --- led_status_helper/src/main.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/led_status_helper/src/main.rs b/led_status_helper/src/main.rs index ed49e670..c434f03f 100644 --- a/led_status_helper/src/main.rs +++ b/led_status_helper/src/main.rs @@ -22,6 +22,7 @@ struct Config { mqtt_port: Option, mqtt_topic: String, temp_unit: Option, + wait_secs: Option, } fn generate_mq_client_id() -> String { @@ -157,6 +158,8 @@ fn main() { MqttClient::start(opts, None).expect("MQTT client couldn't start") }; + let wait_secs = config.wait_secs.unwrap_or(10); + loop { let status = generate_status( &redis_conn, @@ -170,6 +173,6 @@ fn main() { status.unwrap().clone().into_bytes(), ) .unwrap(); - std::thread::sleep(std::time::Duration::from_millis(13000)); + std::thread::sleep(std::time::Duration::from_secs(wait_secs)); } }