Skip to content

Commit 3d76575

Browse files
committed
add publish flag to SensorData and update MQTT publishing logic
1 parent 9ed1dee commit 3d76575

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

src/domain.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const MOISTURE_DRY_THRESHOLD: f32 = 0.15;
1717
#[derive(Default, Debug)]
1818
pub struct SensorData {
1919
pub data: Vec<Sensor, 7>,
20+
pub publish: bool,
2021
}
2122

2223
impl Display for SensorData {

src/sensors_task.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,11 @@ pub async fn sensor_task(
199199
.data
200200
.push(Sensor::BatteryVoltage(avg_battery_voltage))
201201
.expect("Too many samples");
202-
} else {
203-
println!("Error measuring battery voltage");
204202
}
205203

204+
// no battery samples - no publish!
205+
sensor_data.publish = !battery_voltage_samples.is_empty();
206+
206207
if battery_voltage_samples.is_empty() {
207208
println!(
208209
"No battery voltage samples collected - skipping this cycle {:?}",

src/update_task.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,20 @@ async fn handle_sensor_data(
146146
client: &mut MqttClientImpl<'_>,
147147
display: &mut Display<'static, Delay>,
148148
sensor_data: SensorData,
149+
) -> Result<(), Error> {
150+
if sensor_data.publish {
151+
process_mqtt(client, &sensor_data).await?;
152+
} else {
153+
println!("skipping publishing to MQTT");
154+
}
155+
156+
process_display(display, &sensor_data).await?;
157+
Ok(())
158+
}
159+
160+
async fn process_mqtt(
161+
client: &mut MqttClientImpl<'_>,
162+
sensor_data: &SensorData,
149163
) -> Result<(), Error> {
150164
let discovery_messages_sent = unsafe { DISCOVERY_MESSAGES_SENT };
151165
if !discovery_messages_sent {
@@ -216,8 +230,6 @@ async fn handle_sensor_data(
216230
.await?;
217231
}
218232

219-
display.write_multiline(&format!("{}", sensor_data))?;
220-
221233
let pump_topic = format!("{}/pump/state", DEVICE_ID);
222234
let message = "OFF";
223235
println!(
@@ -234,11 +246,16 @@ async fn handle_sensor_data(
234246
false,
235247
)
236248
.await?;
249+
Ok(())
250+
}
237251

252+
async fn process_display(
253+
display: &mut Display<'static, Delay>,
254+
sensor_data: &SensorData,
255+
) -> Result<(), Error> {
256+
display.write_multiline(&format!("{}", sensor_data))?;
238257
Timer::after(Duration::from_secs(AWAKE_DURATION_SECONDS)).await;
239-
240258
display.enable_powersave()?;
241-
242259
Ok(())
243260
}
244261

0 commit comments

Comments
 (0)