melvin_ob/imaging/
cycle_state.rs

1use chrono::{DateTime, TimeDelta, Utc};
2use fixed::types::I32F32;
3
4pub struct CycleState {
5    last_mark: (isize, DateTime<Utc>),
6    last_pic: Option<DateTime<Utc>>,
7    done_ranges: Vec<(isize, isize)>,
8    overlap: TimeDelta,
9}
10
11impl CycleState {
12    #[allow(clippy::cast_possible_truncation)]
13    pub fn init_cycle(img_max_dt: I32F32, start_i: isize) -> Self {
14        let overlap = {
15            let overlap_dt = (img_max_dt.floor() / I32F32::lit("2.0")).to_num::<isize>();
16            TimeDelta::seconds(overlap_dt as i64)
17        };
18        Self {
19            last_mark: (
20                start_i - overlap.num_seconds() as isize,
21                Utc::now() - overlap,
22            ),
23            last_pic: None,
24            done_ranges: Vec::new(),
25            overlap,
26        }
27    }
28
29    fn get_p_secs(&self) -> i64 {
30        if let Some(last_pic_val) = self.last_pic {
31            (last_pic_val - self.last_mark.1 + self.overlap).num_seconds()
32        } else {
33            0
34        }
35    }
36
37    #[allow(clippy::cast_possible_truncation)]
38    pub fn update_failed(&mut self, img_t: DateTime<Utc>) {
39        let p_secs = self.get_p_secs();
40        self.done_ranges.push((self.last_mark.0, self.last_mark.0 + p_secs as isize));
41        let tot_passed_secs = (img_t - self.last_mark.1 - self.overlap).num_seconds();
42        self.last_mark = (tot_passed_secs as isize, Utc::now());
43        self.last_pic = None;
44    }
45
46    pub fn update_success(&mut self, img_t: DateTime<Utc>) { self.last_pic = Some(img_t); }
47
48    #[allow(clippy::cast_possible_truncation)]
49    pub fn finish(mut self) -> Vec<(isize, isize)> {
50        let p_secs = self.get_p_secs();
51        self.done_ranges.push((self.last_mark.0, self.last_mark.0 + p_secs as isize));
52        self.done_ranges
53    }
54}