use fritzapi::{AVMDevice, FritzDect2XX, FritzError};
use std::{
env::args,
time::{Duration, Instant},
};
fn main() -> Result<(), FritzError> {
let start_time = Instant::now();
let mut args = args().skip(1);
let user = args
.next()
.expect("Expected username to be provided on the command line");
let password = args
.next()
.expect("Expected password to be provided on the command line");
let hrr = args.next() == Some("HRR".to_string());
let mut client = fritzapi::FritzClient::new(user, password);
if hrr {
let mut client = client.clone();
std::thread::spawn(move || loop {
match client.trigger_high_refresh_rate() {
Ok(()) => println!("Successfully triggered high refresh rate."),
Err(e) => println!("Error triggering high refresh rate: {e}"),
}
std::thread::sleep(Duration::from_secs(30));
});
}
let mut current_devices = vec![];
loop {
let devices = client.list_devices()?;
let dect_2xx_devices = devices
.into_iter()
.filter_map(fritz_dect_2xx_filter)
.collect::<Vec<_>>();
if dect_2xx_devices != current_devices {
current_devices = dect_2xx_devices;
println!(
"[{}] {:?}",
format_elapsed_time(&start_time),
¤t_devices
);
}
std::thread::sleep(Duration::from_secs(1));
}
}
fn format_elapsed_time(start_time: &Instant) -> String {
let secs = start_time.elapsed().as_secs();
format!("{:3}m{:02}s", secs / 60, secs % 60)
}
fn fritz_dect_2xx_filter(device: AVMDevice) -> Option<FritzDect2XX> {
if let AVMDevice::FritzDect2XX(x) = device {
Some(x)
} else {
None
}
}