aboutsummaryrefslogtreecommitdiff
path: root/src/interface.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-01-04 22:49:55 -0500
committerLibravatar Silas Bartha <[email protected]>2024-01-04 22:49:55 -0500
commitad825d48308091927e57f35df0d7acfa1a7b7366 (patch)
tree8270270e4b791df802234c977bd0b34442e1c6e3 /src/interface.rs
parent37c7a239b81102d2d1a75b9542a91468676cbd98 (diff)
Refactor + Document
Diffstat (limited to 'src/interface.rs')
-rw-r--r--src/interface.rs55
1 files changed, 55 insertions, 0 deletions
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<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
+ }
+ }
+}
+
+#[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();
+ }
+}