bluetooth_battery/
lib.rs

1#[derive(Debug, Clone, Default)]
2pub struct Device {
3  pub id: String,
4  pub name: String,
5  pub battery: u8,
6  pub update_time: u64,
7}
8
9const SEQ: &str = "----";
10
11pub fn get_device() {}
12
13use std::process::{Command, Stdio};
14
15fn pwsh(cmd: &str) -> String {
16  let output = Command::new("pwsh")
17    .args(["-c", cmd])
18    .stdout(Stdio::piped())
19    .stderr(Stdio::piped())
20    .stdin(Stdio::piped())
21    .output();
22  let s = output.unwrap().stdout;
23
24  String::from_utf8(s).unwrap()
25}
26use std::time::{SystemTime, UNIX_EPOCH};
27
28pub fn get_bluetooth_battery() -> Vec<Device> {
29  let mut v: Vec<Device> = vec![];
30
31  let devices = pwsh(include_str!("../pwsh/blue.ps1"));
32  let now = SystemTime::now();
33  let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
34  let update_time = since_the_epoch.as_secs();
35
36  for i in devices.lines().flat_map(|i| {
37    if i.trim().is_empty() {
38      None
39    } else {
40      Some(i.trim())
41    }
42  }) {
43    let line: Vec<_> = i.split(SEQ).collect();
44    if line.len() == 3 {
45      let id = line[0].into();
46      let name = line[1].into();
47      let battery = line[2].parse().unwrap();
48      v.push(Device {
49        id,
50        name,
51        battery,
52        update_time,
53      });
54    }
55  }
56  v
57}