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
//! Battery info
use std::collections::HashMap;
use std::marker::PhantomData;
use std::ops::Deref;
use async_trait::async_trait;
use fantoccini::error::CmdError;
use serde_json::Value;
use crate::{AndroidClient, AppiumClientTrait, IOSClient};
use crate::capabilities::android::AndroidCapabilities;
use crate::capabilities::AppiumCapability;
use crate::capabilities::ios::IOSCapabilities;

/// Device battery level and state
#[async_trait]
pub trait HasBattery<Caps>: AppiumClientTrait
    where Caps: AppiumCapability
{
    async fn battery_info(&self) -> Result<BatteryInfo<Caps>, CmdError> {
        let value = self.execute("mobile: batteryInfo", vec![]).await?;
        Ok(BatteryInfo {
            inner: serde_json::from_value(value)?,
            caps: PhantomData,
        })
    }
}


#[async_trait]
impl HasBattery<AndroidCapabilities> for AndroidClient {}

#[async_trait]
impl HasBattery<IOSCapabilities> for IOSClient {}

pub struct BatteryInfo<Caps>
    where Caps: AppiumCapability {
    inner: HashMap<String, Value>,
    caps: PhantomData<Caps>,
}

impl<Caps> BatteryInfo<Caps>
    where Caps: AppiumCapability {

    pub fn level(&self) -> f64 {
        self.get("level")
            .cloned()
            .and_then(|v| serde_json::from_value(v).ok())
            .unwrap_or(0f64)
    }
}

impl<Caps> Deref for BatteryInfo<Caps>
    where Caps: AppiumCapability {
    type Target = HashMap<String, Value>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

pub trait CanBeCharged {
    fn is_full(&self) -> bool;
    fn is_charging(&self) -> bool;
    fn is_plugged(&self) -> bool;
    fn is_invalid(&self) -> bool;
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AndroidBatteryState {
    Unknown,
    Charging,
    Discharging,
    NotCharging,
    Full,
}

impl BatteryInfo<AndroidCapabilities> {
    pub fn state(&self) -> AndroidBatteryState {
        self.get("state")
            .cloned()
            .and_then(|v| serde_json::from_value::<u32>(v).ok())
            .map(|state| match state {
                2 => AndroidBatteryState::Charging,
                3 => AndroidBatteryState::Discharging,
                4 => AndroidBatteryState::NotCharging,
                5 => AndroidBatteryState::Full,
                _ => AndroidBatteryState::Unknown
            })
            .unwrap_or(AndroidBatteryState::Unknown)
    }
}

impl CanBeCharged for BatteryInfo<AndroidCapabilities> {
    fn is_full(&self) -> bool {
        self.state() == AndroidBatteryState::Full
    }

    fn is_charging(&self) -> bool {
        self.state() == AndroidBatteryState::Charging
    }

    fn is_plugged(&self) -> bool {
        let state = self.state();
        state == AndroidBatteryState::NotCharging || state == AndroidBatteryState::Discharging
    }

    fn is_invalid(&self) -> bool {
        self.state() == AndroidBatteryState::Unknown
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IOSBatteryState {
    Unknown,
    Unplugged,
    Charging,
    Full,
}

impl BatteryInfo<IOSCapabilities> {
    pub fn state(&self) -> IOSBatteryState {
        self.get("state")
            .cloned()
            .and_then(|v| serde_json::from_value::<u32>(v).ok())
            .map(|state| match state {
                1 => IOSBatteryState::Unplugged,
                2 => IOSBatteryState::Charging,
                3 => IOSBatteryState::Full,
                _ => IOSBatteryState::Unknown
            })
            .unwrap_or(IOSBatteryState::Unknown)
    }
}

impl CanBeCharged for BatteryInfo<IOSCapabilities> {
    fn is_full(&self) -> bool {
        self.state() == IOSBatteryState::Full
    }

    fn is_charging(&self) -> bool {
        self.state() == IOSBatteryState::Charging
    }

    fn is_plugged(&self) -> bool {
        self.state() != IOSBatteryState::Unplugged
    }

    fn is_invalid(&self) -> bool {
        self.state() == IOSBatteryState::Unknown
    }
}