Skip to content

Commit e3f387d

Browse files
committed
add publish flag to SensorData and conditionally process MQTT messages
1 parent 9ed1dee commit e3f387d

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
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

+23-8
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 {
@@ -179,14 +193,12 @@ async fn handle_sensor_data(
179193
println!("Discovery messages already sent");
180194
}
181195

182-
// act on sensor data - turn pump on/off
196+
// Act on sensor data - turn pump on/off
183197
sensor_data.data.iter().for_each(|entry| {
184198
if let Sensor::WaterLevel(WaterLevel::Full) = entry {
185-
// Water level is full, stop the pump in any case if it's running
186199
println!("Water level is full, stopping pump");
187-
update_pump_state(false);
200+
ENABLE_PUMP.signal(false);
188201
} else if let Sensor::PumpTrigger(enabled) = entry {
189-
// Pump trigger is enabled, start the pump
190202
if *enabled {
191203
println!("Soil moisture is low, starting pump");
192204
update_pump_state(true);
@@ -216,8 +228,6 @@ async fn handle_sensor_data(
216228
.await?;
217229
}
218230

219-
display.write_multiline(&format!("{}", sensor_data))?;
220-
221231
let pump_topic = format!("{}/pump/state", DEVICE_ID);
222232
let message = "OFF";
223233
println!(
@@ -234,11 +244,16 @@ async fn handle_sensor_data(
234244
false,
235245
)
236246
.await?;
247+
Ok(())
248+
}
237249

250+
async fn process_display(
251+
display: &mut Display<'static, Delay>,
252+
sensor_data: &SensorData,
253+
) -> Result<(), Error> {
254+
display.write_multiline(&format!("{}", sensor_data))?;
238255
Timer::after(Duration::from_secs(AWAKE_DURATION_SECONDS)).await;
239-
240256
display.enable_powersave()?;
241-
242257
Ok(())
243258
}
244259

0 commit comments

Comments
 (0)