aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f9b56539c9b13459ea19ed9fe39f1623437dae7a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::{error::Error, time::Duration};

use clap::{Parser, Subcommand};
use zbus::{Connection, fdo};

#[derive(Debug, Parser)]
#[clap(name = "pomc", version)]
struct Pomc {
    #[clap(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    Start,
    Pause,
    Stop,
    Skip,
    GetIteration,
    GetRemaining,
    IsRunning,
    IsOnBreak,
}

#[async_std::main]
async fn main() {
    let args = Pomc::parse();
    let connection = Connection::session().await.expect("Failed to open dbus connection, is dbus running?");
    match match args.command {
        Command::Start => start(&connection).await,
        Command::Pause => pause(&connection).await,
        Command::Stop => stop(&connection).await,
        Command::Skip => skip(&connection).await,
        Command::GetIteration => get_iteration(&connection).await,
        Command::GetRemaining => get_remaining(&connection).await,
        Command::IsRunning => is_running(&connection).await,
        Command::IsOnBreak => is_on_break(&connection).await,
    } {
        Ok(()) => std::process::exit(0),
        Err(e) =>  {
            match e { 
                fdo::Error::ServiceUnknown(_) => eprintln!("Error: Failed to find pomd dbus interface, is pomd running?"),
                _ => eprintln!("Error calling pomd command: {}", e)
            }
            std::process::exit(1)
        }
    }
}

async fn start(connection: &Connection) -> Result<(), fdo::Error> {
    connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "Start",
            &(),
        )
        .await?;
    Ok(())
}

async fn pause(connection: &Connection) -> Result<(), fdo::Error> {
    connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "Pause",
            &(),
        )
        .await?;
    Ok(())
}

async fn stop(connection: &Connection) -> Result<(), fdo::Error> {
    connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "Stop",
            &(),
        )
        .await?;
    Ok(())
}

async fn skip(connection: &Connection) -> Result<(), fdo::Error> {
    connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "Skip",
            &(),
        )
        .await?;
    Ok(())
}

async fn get_iteration(connection: &Connection) -> Result<(), fdo::Error> {
    let m = connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "GetIteration",
            &(),
        )
        .await?;
    let iteration: u8 = m.body().unwrap();
    println!("{}", iteration + 1);
    Ok(())
}

async fn get_remaining(connection: &Connection) -> Result<(), fdo::Error> {
    let m = connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "GetRemaining",
            &(),
        )
        .await?;
    let remaining: Duration = m.body().unwrap();
    let remaining_secs = remaining.as_secs();
    println!("{:02}:{:02}", remaining_secs / 60, remaining_secs % 60);
    Ok(())
}

async fn is_running(connection: &Connection) -> Result<(), fdo::Error> {
    let m = connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "IsRunning",
            &(),
        )
        .await?;
    let is_running: bool = m.body().unwrap();
    println!("{}", is_running as u8);
    Ok(())
}

async fn is_on_break(connection: &Connection) -> Result<(), fdo::Error> {
    let m = connection
        .call_method(
            Some("dev.exvacuum.pomd"),
            "/dev/exvacuum/pomd",
            Some("dev.exvacuum.pomd"),
            "IsOnBreak",
            &(),
        )
        .await?;
    let is_on_break: bool = m.body().unwrap();
    println!("{}", is_on_break as u8);
    Ok(())
}