Add passive_refresh_state
This commit is contained in:
@@ -110,14 +110,11 @@ fn main() {
|
||||
|
||||
std::thread::sleep(Duration::from_secs_f64(0.5));
|
||||
|
||||
if let Err(error) = device.refresh_state() {
|
||||
if let Err(error) = device.active_refresh_state() {
|
||||
eprintln!("{error}");
|
||||
std::process::exit(1);
|
||||
};
|
||||
println!(
|
||||
"{}",
|
||||
device.get_device_state()
|
||||
);
|
||||
println!("{}", device.get_device_state());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -300,7 +300,8 @@ pub trait Device {
|
||||
self.get_event_from_device_response(&buf[0..res])
|
||||
}
|
||||
|
||||
fn refresh_state(&mut self) -> Result<(), DeviceError> {
|
||||
/// Refreshes the state by querying all available information
|
||||
fn active_refresh_state(&mut self) -> Result<(), DeviceError> {
|
||||
let packets = vec![
|
||||
self.get_wireless_connected_status_packet(),
|
||||
self.get_charging_packet(),
|
||||
@@ -333,4 +334,20 @@ pub trait Device {
|
||||
Err(DeviceError::NoResponse())
|
||||
}
|
||||
}
|
||||
|
||||
/// Refreshes the state by listening for events
|
||||
/// Only the battery level is actively queried because it is not communicated by the device on its own
|
||||
fn passive_refresh_state(&mut self) -> Result<(), DeviceError> {
|
||||
if let Some(event) = self.wait_for_updates(Duration::from_secs(1)) {
|
||||
self.get_device_state_mut().update_self_with_event(&event);
|
||||
}
|
||||
if let Some(batter_packet) = self.get_battery_packet() {
|
||||
self.get_device_state().hid_device.write(&batter_packet)?;
|
||||
if let Some(event) = self.wait_for_updates(Duration::from_secs(1)) {
|
||||
self.get_device_state_mut().update_self_with_event(&event);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -1,5 +1,5 @@
|
||||
use std::time::Duration;
|
||||
use clap::{Arg, Command};
|
||||
use std::time::Duration;
|
||||
|
||||
mod status_tray;
|
||||
use hyper_headset::devices::connect_compatible_device;
|
||||
@@ -14,11 +14,10 @@ fn main() {
|
||||
Arg::new("refresh_interval")
|
||||
.long("refresh_interval")
|
||||
.required(false)
|
||||
.help(
|
||||
"Set the refresh interval (in seconds)",
|
||||
)
|
||||
.help("Set the refresh interval (in seconds)")
|
||||
.value_parser(clap::value_parser!(u64)),
|
||||
).get_matches();
|
||||
)
|
||||
.get_matches();
|
||||
let refresh_interval = *matches.get_one::<u64>("refresh_interval").unwrap_or(&3);
|
||||
let refresh_interval = Duration::from_secs(refresh_interval);
|
||||
let tray_handler = TrayHandler::new(StatusTray::new());
|
||||
@@ -32,9 +31,16 @@ fn main() {
|
||||
};
|
||||
|
||||
// Run loop
|
||||
let mut run_counter = 0;
|
||||
loop {
|
||||
std::thread::sleep(refresh_interval);
|
||||
match device.refresh_state() {
|
||||
// with the default refresh_interval the state is only actively queried every 3min
|
||||
// quiting the device to frequently can lead to instability
|
||||
match if run_counter % 60 == 0 {
|
||||
device.active_refresh_state()
|
||||
} else {
|
||||
device.passive_refresh_state()
|
||||
} {
|
||||
Ok(()) => (),
|
||||
Err(error) => {
|
||||
eprintln!("{error}");
|
||||
@@ -43,7 +49,8 @@ fn main() {
|
||||
break; // try to reconnect
|
||||
}
|
||||
};
|
||||
tray_handler.update(device.get_device_state())
|
||||
tray_handler.update(device.get_device_state());
|
||||
run_counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user