#[derive(Debug, Clone, Default)]
pub struct Device {
pub id: String,
pub name: String,
pub battery: u8,
pub update_time: u64,
}
const SEQ: &str = "----";
pub fn get_device() {}
use std::process::{Command, Stdio};
fn pwsh(cmd: &str) -> String {
let output = Command::new("pwsh")
.args(["-c", cmd])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::piped())
.output();
let s = output.unwrap().stdout;
String::from_utf8(s).unwrap()
}
use std::time::{SystemTime, UNIX_EPOCH};
pub fn get_bluetooth_battery() -> Vec<Device> {
let mut v: Vec<Device> = vec![];
let devices = pwsh(include_str!("../pwsh/blue.ps1"));
let now = SystemTime::now();
let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
let update_time = since_the_epoch.as_secs();
for i in devices.lines().flat_map(|i| {
if i.trim().is_empty() {
None
} else {
Some(i.trim())
}
}) {
let line: Vec<_> = i.split(SEQ).collect();
if line.len() == 3 {
let id = line[0].into();
let name = line[1].into();
let battery = line[2].parse().unwrap();
v.push(Device {
id,
name,
battery,
update_time,
});
}
}
v
}