aboutsummaryrefslogtreecommitdiff
path: root/src/interface.rs
blob: 205bb43c72ec805f0e94eabf246e13a414288c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::{sync::{Arc, Mutex}, time::Duration};
use zbus::interface;

use crate::pomd::Pomd;

/// D-Bus interface for the program
pub struct PomdInterface {
    pub state: Arc<Mutex<Pomd>>,
}

impl PomdInterface {
    /// Create a new instance of the interface with a reference to the program state
    pub fn new(state: Arc<Mutex<Pomd>>) -> Self {
        Self { 
            state
        }
    }
}

#[interface(name = "dev.exvacuum.pomd")]
impl PomdInterface {
    fn get_remaining(&self) -> Duration {
        let data = self.state.lock().unwrap();
        data.duration.checked_sub(data.start.elapsed(&data.clock)).unwrap_or_default()
    }

    fn get_iteration(&self) -> u8 {
        self.state.lock().unwrap().iteration
    }

    fn is_running(&self) -> bool {
        !self.state.lock().unwrap().clock.is_paused()
    }

    fn is_on_break(&self) -> bool {
        self.state.lock().unwrap().on_break
    }

    fn start(&self) {
        self.state.lock().unwrap().clock.resume();
    }

    fn pause(&self) {
        self.state.lock().unwrap().clock.pause();
    }

    fn stop(&self) {
        let mut data = self.state.lock().unwrap();
        *data = Pomd::new(data.config);
    }

    fn skip(&self) {
        self.state.lock().unwrap().setup_next_iteration();
    }
}