Skip to content

Commit 6372426

Browse files
committed
tplink: Implement battery level support
First draft
1 parent 69260d2 commit 6372426

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

daemon/src/battery/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313

1414
pub mod orbic;
1515
pub mod tmobile;
16+
pub mod tplink;
1617
pub mod wingtech;
1718

1819
const LOW_BATTERY_LEVEL: u8 = 10;
@@ -50,6 +51,7 @@ pub async fn get_battery_status(device: &Device) -> Result<BatteryState, Rayhunt
5051
Device::Orbic => orbic::get_battery_state().await?,
5152
Device::Wingtech => wingtech::get_battery_state().await?,
5253
Device::Tmobile => tmobile::get_battery_state().await?,
54+
Device::Tplink => tplink::get_battery_state().await?,
5355
_ => return Err(RayhunterError::FunctionNotSupportedForDeviceError),
5456
})
5557
}

daemon/src/battery/tplink.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{battery::BatteryState, error::RayhunterError};
2+
3+
pub async fn get_battery_state() -> Result<BatteryState, RayhunterError> {
4+
let uci_battery = tokio::process::Command::new("uci")
5+
.arg("get")
6+
.arg("battery.battery_mgr.power_level")
7+
.output()
8+
.await?;
9+
10+
let uci_plugged_in = tokio::process::Command::new("uci")
11+
.arg("get")
12+
.arg("battery.battery_mgr.is_charging")
13+
.output()
14+
.await?;
15+
16+
if !uci_battery.status.success() {
17+
return Err(RayhunterError::BatteryLevelParseError);
18+
}
19+
20+
if !uci_plugged_in.status.success() {
21+
return Err(RayhunterError::BatteryPluggedInStatusParseError);
22+
}
23+
24+
let uci_battery = String::from_utf8_lossy(&uci_battery.stdout)
25+
.to_string()
26+
.trim_end()
27+
.parse()
28+
.or(Err(RayhunterError::BatteryLevelParseError));
29+
30+
let uci_plugged_in = match String::from_utf8_lossy(&uci_plugged_in.stdout)
31+
.to_string()
32+
.chars()
33+
.next()
34+
{
35+
Some('0') => Ok(false),
36+
Some('1') => Ok(true),
37+
_ => Err(RayhunterError::BatteryPluggedInStatusParseError),
38+
};
39+
40+
Ok(BatteryState {
41+
level: uci_battery?,
42+
is_plugged_in: uci_plugged_in?,
43+
})
44+
}

0 commit comments

Comments
 (0)