From 3960a2bb929150639d90d4d178f942976bd5a219 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Thu, 4 Jan 2024 22:49:55 -0500 Subject: Refactor + Document --- src/interface.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/interface.rs (limited to 'src/interface.rs') diff --git a/src/interface.rs b/src/interface.rs new file mode 100644 index 0000000..870162c --- /dev/null +++ b/src/interface.rs @@ -0,0 +1,55 @@ +use std::{sync::{Arc, Mutex}, time::Duration}; +use zbus::dbus_interface; + +use crate::pomd::Pomd; + +/// D-Bus interface for the program +pub struct PomdInterface { + pub state: Arc>, +} + +impl PomdInterface { + /// Create a new instance of the interface with a reference to the program state + pub fn new(state: Arc>) -> Self { + Self { + state + } + } +} + +#[dbus_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(); + } +} -- cgit v1.2.3