conditions.rs
1 use std::time::Duration; 2 3 use tokio::time::{Interval, interval}; 4 use tracing::{info, instrument, warn}; 5 6 use crate::ambient_weather::Latest; 7 8 pub struct Conditions { 9 interval: Interval, 10 latest: Latest, 11 } 12 13 impl Conditions { 14 pub fn new(conditions_interval: Duration, latest: Latest) -> Self { 15 let mut interval = interval(conditions_interval); 16 interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); 17 18 Self { interval, latest } 19 } 20 21 #[instrument(name = "conditions", skip_all)] 22 pub async fn run(mut self) { 23 let mut last_report_time = None; 24 25 loop { 26 self.interval.tick().await; 27 28 let latest = { 29 let guard = self.latest.read().unwrap(); 30 31 guard.clone() 32 }; 33 34 let Some(report) = latest else { 35 warn!("none reported"); 36 continue; 37 }; 38 39 if let Some(last_report_time) = last_report_time { 40 if last_report_time == report.time() { 41 warn!(?last_report_time, interval = ?self.interval.period(), "no update"); 42 continue; 43 } 44 } 45 46 info!( 47 station = %report.passkey, 48 report_time = %report.time(), 49 temp_out_f = report.tempf, 50 humid_out = report.humidity, 51 temp_in_f = report.tempinf, 52 humid_in = report.humidityin, 53 wind_mph = report.windspdmph_avg10m, 54 wind_dir = report.winddir_avg10m, 55 barom_abs_in = report.baromabsin, 56 rain_event_in = report.eventrainin, 57 "current" 58 ); 59 60 last_report_time = Some(report.time()); 61 } 62 } 63 64 pub fn start(self) { 65 tokio::spawn(self.run()); 66 } 67 }